From 26a14dd987b2021f7652bf14da83046eeca2dd6c Mon Sep 17 00:00:00 2001 From: Hugo Caloto Date: Mon, 9 Jul 2018 09:27:17 +0200 Subject: [PATCH 1/3] Add missing catch blocks for migration from 1.1.1 to 1.2.0 Signed-off-by: Hugo Caloto --- lib/migrations/20150702001020-update-to-0_3_1.js | 6 ++++++ .../20160112220142-note-add-lastchange.js | 6 ++++++ lib/migrations/20160420180355-note-add-alias.js | 6 ++++++ lib/migrations/20160515114000-user-add-tokens.js | 6 ++++++ lib/migrations/20160607060246-support-revision.js | 6 ++++++ .../20160703062241-support-authorship.js | 6 ++++++ .../20161009040430-support-delete-note.js | 8 +++++++- .../20161201050312-support-email-signin.js | 14 +++++++++++++- 8 files changed, 56 insertions(+), 2 deletions(-) diff --git a/lib/migrations/20150702001020-update-to-0_3_1.js b/lib/migrations/20150702001020-update-to-0_3_1.js index 40d9c97..e661a34 100644 --- a/lib/migrations/20150702001020-update-to-0_3_1.js +++ b/lib/migrations/20150702001020-update-to-0_3_1.js @@ -20,6 +20,12 @@ module.exports = { type: Sequelize.INTEGER, defaultValue: 0 }) + }).catch(function (error) { + if (error.message === "ER_DUP_FIELDNAME: Duplicate column name 'shortid'") { + console.log('Migration has already run… ignoring.') + } else { + throw error + } }) }, diff --git a/lib/migrations/20160112220142-note-add-lastchange.js b/lib/migrations/20160112220142-note-add-lastchange.js index b4e111b..d0030d6 100644 --- a/lib/migrations/20160112220142-note-add-lastchange.js +++ b/lib/migrations/20160112220142-note-add-lastchange.js @@ -7,6 +7,12 @@ module.exports = { return queryInterface.addColumn('Notes', 'lastchangeAt', { type: Sequelize.DATE }) + }).catch(function (error) { + if (error.message === "ER_DUP_FIELDNAME: Duplicate column name 'lastchangeuserId'") { + console.log('Migration has already run… ignoring.') + } else { + throw error + } }) }, diff --git a/lib/migrations/20160420180355-note-add-alias.js b/lib/migrations/20160420180355-note-add-alias.js index a043cd5..4bad29c 100644 --- a/lib/migrations/20160420180355-note-add-alias.js +++ b/lib/migrations/20160420180355-note-add-alias.js @@ -7,6 +7,12 @@ module.exports = { return queryInterface.addIndex('Notes', ['alias'], { indicesType: 'UNIQUE' }) + }).catch(function (error) { + if (error.message === "ER_DUP_FIELDNAME: Duplicate column name 'alias'") { + console.log('Migration has already run… ignoring.') + } else { + throw error + } }) }, diff --git a/lib/migrations/20160515114000-user-add-tokens.js b/lib/migrations/20160515114000-user-add-tokens.js index 4d5818b..4245f1a 100644 --- a/lib/migrations/20160515114000-user-add-tokens.js +++ b/lib/migrations/20160515114000-user-add-tokens.js @@ -3,6 +3,12 @@ module.exports = { up: function (queryInterface, Sequelize) { return queryInterface.addColumn('Users', 'accessToken', Sequelize.STRING).then(function () { return queryInterface.addColumn('Users', 'refreshToken', Sequelize.STRING) + }).catch(function (error) { + if (error.message === "ER_DUP_FIELDNAME: Duplicate column name 'accessToken'") { + console.log('Migration has already run… ignoring.') + } else { + throw error + } }) }, diff --git a/lib/migrations/20160607060246-support-revision.js b/lib/migrations/20160607060246-support-revision.js index bcab97e..10f288b 100644 --- a/lib/migrations/20160607060246-support-revision.js +++ b/lib/migrations/20160607060246-support-revision.js @@ -15,6 +15,12 @@ module.exports = { createdAt: Sequelize.DATE, updatedAt: Sequelize.DATE }) + }).catch(function (error) { + if (error.message === "ER_DUP_FIELDNAME: Duplicate column name 'savedAt'") { + console.log('Migration has already run… ignoring.') + } else { + throw error + } }) }, diff --git a/lib/migrations/20160703062241-support-authorship.js b/lib/migrations/20160703062241-support-authorship.js index d73923b..b3ced8c 100644 --- a/lib/migrations/20160703062241-support-authorship.js +++ b/lib/migrations/20160703062241-support-authorship.js @@ -16,6 +16,12 @@ module.exports = { createdAt: Sequelize.DATE, updatedAt: Sequelize.DATE }) + }).catch(function (error) { + if (error.message === "ER_DUP_FIELDNAME: Duplicate column name 'authorship'") { + console.log('Migration has already run… ignoring.') + } else { + throw error + } }) }, diff --git a/lib/migrations/20161009040430-support-delete-note.js b/lib/migrations/20161009040430-support-delete-note.js index a39d108..4df7a81 100644 --- a/lib/migrations/20161009040430-support-delete-note.js +++ b/lib/migrations/20161009040430-support-delete-note.js @@ -1,7 +1,13 @@ 'use strict' module.exports = { up: function (queryInterface, Sequelize) { - return queryInterface.addColumn('Notes', 'deletedAt', Sequelize.DATE) + return queryInterface.addColumn('Notes', 'deletedAt', Sequelize.DATE).catch(function (error) { + if (error.message === "ER_DUP_FIELDNAME: Duplicate column name 'deletedAt'") { + console.log('Migration has already run… ignoring.') + } else { + throw error + } + }) }, down: function (queryInterface, Sequelize) { diff --git a/lib/migrations/20161201050312-support-email-signin.js b/lib/migrations/20161201050312-support-email-signin.js index d33225f..4653e67 100644 --- a/lib/migrations/20161201050312-support-email-signin.js +++ b/lib/migrations/20161201050312-support-email-signin.js @@ -2,7 +2,19 @@ module.exports = { up: function (queryInterface, Sequelize) { return queryInterface.addColumn('Users', 'email', Sequelize.TEXT).then(function () { - return queryInterface.addColumn('Users', 'password', Sequelize.TEXT) + return queryInterface.addColumn('Users', 'password', Sequelize.TEXT).catch(function (error) { + if (error.message === "ER_DUP_FIELDNAME: Duplicate column name 'password'") { + console.log('Migration has already run… ignoring.') + } else { + throw error + } + }) + }).catch(function (error) { + if (error.message === "ER_DUP_FIELDNAME: Duplicate column name 'email'") { + console.log('Migration has already run… ignoring.') + } else { + throw error + } }) }, From 66d374b1289e6dd33008dc931e9fb642051724d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Couralet?= Date: Mon, 30 Jul 2018 13:47:09 +0000 Subject: [PATCH 2/3] Add possibility to choose between version v3 or v4 for the gitlab api. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Apart from the uri versioning, one big change is the snippet visibility post data (visibility_level -> visibility) Default gitlab api version to v4 Signed-off-by: Cédric Couralet --- README.md | 1 + config.json.example | 3 ++- lib/config/index.js | 6 ++++++ lib/response.js | 4 ++-- public/js/index.js | 17 +++++++++++------ public/views/codimd/body.ejs | 2 ++ 6 files changed, 24 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index a9f0d4b..e0f7aca 100644 --- a/README.md +++ b/README.md @@ -194,6 +194,7 @@ There are some config settings you need to change in the files below. | `CMD_GITLAB_BASEURL` | no example | GitLab authentication endpoint, set to use other endpoint than GitLab.com (optional) | | `CMD_GITLAB_CLIENTID` | no example | GitLab API client id | | `CMD_GITLAB_CLIENTSECRET` | no example | GitLab API client secret | +| `CMD_GITLAB_VERSION` | no example | GitLab API version (v3 or v4) | | `CMD_MATTERMOST_BASEURL` | no example | Mattermost authentication endpoint | | `CMD_MATTERMOST_CLIENTID` | no example | Mattermost API client id | | `CMD_MATTERMOST_CLIENTSECRET` | no example | Mattermost API client secret | diff --git a/config.json.example b/config.json.example index 1f2ec3d..16c9550 100644 --- a/config.json.example +++ b/config.json.example @@ -55,7 +55,8 @@ "baseURL": "change this", "clientID": "change this", "clientSecret": "change this", - "scope": "use 'read_user' scope for auth user only or remove this property if you need gitlab snippet import/export support (will result to be default scope 'api')" + "scope": "use 'read_user' scope for auth user only or remove this property if you need gitlab snippet import/export support (will result to be default scope 'api')", + "version": "use 'v4' if gitlab version > 11, 'v3' otherwise. Default to 'v4'" }, "mattermost": { "baseURL": "change this", diff --git a/lib/config/index.js b/lib/config/index.js index ac03fcd..e66c513 100644 --- a/lib/config/index.js +++ b/lib/config/index.js @@ -103,6 +103,12 @@ config.isSAMLEnable = config.saml.idpSsoUrl config.isOAuth2Enable = config.oauth2.clientID && config.oauth2.clientSecret config.isPDFExportEnable = config.allowPDFExport +// Check gitlab api version +if (config.gitlab.version !== 'v4' || config.gitlab.version !== 'v3') { + logger.warn('config.js contains wrong version (' + config.gitlab.version + ') for gitlab api; it should be \'v3\' or \'v4\'. Defaulting to v3') + config.gitlab.version = 'v4' +} + // Only update i18n files in development setups config.updateI18nFiles = (env === Environment.development) diff --git a/lib/response.js b/lib/response.js index 3a31c51..3721199 100644 --- a/lib/response.js +++ b/lib/response.js @@ -573,11 +573,11 @@ function gitlabActionProjects (req, res, note) { } }).then(function (user) { if (!user) { return response.errorNotFound(res) } - var ret = { baseURL: config.gitlab.baseURL } + var ret = { baseURL: config.gitlab.baseURL, version: config.gitlab.version } ret.accesstoken = user.accessToken ret.profileid = user.profileid request( - config.gitlab.baseURL + '/api/v3/projects?access_token=' + user.accessToken, + config.gitlab.baseURL + '/api/' + config.gitlab.version + '/projects?access_token=' + user.accessToken, function (error, httpResponse, body) { if (!error && httpResponse.statusCode === 200) { ret.projects = JSON.parse(body) diff --git a/public/js/index.js b/public/js/index.js index 6e13fe9..1330dea 100644 --- a/public/js/index.js +++ b/public/js/index.js @@ -970,6 +970,7 @@ ui.toolbar.export.snippet.click(function () { .done(function (data) { $('#snippetExportModalAccessToken').val(data.accesstoken) $('#snippetExportModalBaseURL').val(data.baseURL) + $('#snippetExportModalVersion').val(data.version) $('#snippetExportModalLoading').hide() $('#snippetExportModal').modal('toggle') $('#snippetExportModalProjects').find('option').remove().end().append('') @@ -1021,6 +1022,7 @@ ui.toolbar.import.snippet.click(function () { .done(function (data) { $('#snippetImportModalAccessToken').val(data.accesstoken) $('#snippetImportModalBaseURL').val(data.baseURL) + $('#snippetImportModalVersion').val(data.version) $('#snippetImportModalContent').prop('disabled', false) $('#snippetImportModalConfirm').prop('disabled', false) $('#snippetImportModalLoading').hide() @@ -1243,10 +1245,10 @@ ui.modal.snippetImportProjects.change(function () { var accesstoken = $('#snippetImportModalAccessToken').val() var baseURL = $('#snippetImportModalBaseURL').val() var project = $('#snippetImportModalProjects').val() - + var version = $('#snippetImportModalVersion').val() $('#snippetImportModalLoading').show() $('#snippetImportModalContent').val('/projects/' + project) - $.get(baseURL + '/api/v3/projects/' + project + '/snippets?access_token=' + accesstoken) + $.get(baseURL + '/api/' + version + '/projects/' + project + '/snippets?access_token=' + accesstoken) .done(function (data) { $('#snippetImportModalSnippets').find('option').remove().end().append('') data.forEach(function (snippet) { @@ -1433,7 +1435,7 @@ $('#snippetImportModalConfirm').click(function () { } else { ui.spinner.show() var accessToken = '?access_token=' + $('#snippetImportModalAccessToken').val() - var fullURL = $('#snippetImportModalBaseURL').val() + '/api/v3' + snippeturl + var fullURL = $('#snippetImportModalBaseURL').val() + '/api/' + $('#snippetImportModalVersion').val() + snippeturl $.get(fullURL + accessToken) .done(function (data) { var content = '# ' + (data.title || 'Snippet Import') @@ -1470,15 +1472,19 @@ $('#snippetImportModalConfirm').click(function () { $('#snippetExportModalConfirm').click(function () { var accesstoken = $('#snippetExportModalAccessToken').val() var baseURL = $('#snippetExportModalBaseURL').val() + var version = $('#snippetExportModalVersion').val() + var data = { title: $('#snippetExportModalTitle').val(), file_name: $('#snippetExportModalFileName').val(), code: editor.getValue(), - visibility_level: $('#snippetExportModalVisibility').val() + visibility_level: $('#snippetExportModalVisibility').val(), + visibility: $('#snippetExportModalVisibility').val() === 0 ? 'private' : ($('#snippetExportModalVisibility').val() === 10 ? 'internal' : '') } + if (!data.title || !data.file_name || !data.code || !data.visibility_level || !$('#snippetExportModalProjects').val()) return $('#snippetExportModalLoading').show() - var fullURL = baseURL + '/api/v3/projects/' + $('#snippetExportModalProjects').val() + '/snippets?access_token=' + accesstoken + var fullURL = baseURL + '/api/' + version + '/projects/' + $('#snippetExportModalProjects').val() + '/snippets?access_token=' + accesstoken $.post(fullURL , data , function (ret) { @@ -1487,7 +1493,6 @@ $('#snippetExportModalConfirm').click(function () { var redirect = baseURL + '/' + $("#snippetExportModalProjects option[value='" + $('#snippetExportModalProjects').val() + "']").text() + '/snippets/' + ret.id showMessageModal(' Export to Snippet', 'Export Successful!', redirect, 'View Snippet Here', true) } - , 'json' ) }) diff --git a/public/views/codimd/body.ejs b/public/views/codimd/body.ejs index b5932a6..d4f27a9 100644 --- a/public/views/codimd/body.ejs +++ b/public/views/codimd/body.ejs @@ -153,6 +153,7 @@