Skip to content

Commit 881ca88

Browse files
authored
Merge pull request #908 from micedre/gitlabV4
Add possibility to choose between version v3 or v4 for the gitlab api.
2 parents 48ddcef + 66d374b commit 881ca88

File tree

6 files changed

+24
-9
lines changed

6 files changed

+24
-9
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ There are some config settings you need to change in the files below.
194194
| `CMD_GITLAB_BASEURL` | no example | GitLab authentication endpoint, set to use other endpoint than GitLab.com (optional) |
195195
| `CMD_GITLAB_CLIENTID` | no example | GitLab API client id |
196196
| `CMD_GITLAB_CLIENTSECRET` | no example | GitLab API client secret |
197+
| `CMD_GITLAB_VERSION` | no example | GitLab API version (v3 or v4) |
197198
| `CMD_MATTERMOST_BASEURL` | no example | Mattermost authentication endpoint |
198199
| `CMD_MATTERMOST_CLIENTID` | no example | Mattermost API client id |
199200
| `CMD_MATTERMOST_CLIENTSECRET` | no example | Mattermost API client secret |

config.json.example

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@
5555
"baseURL": "change this",
5656
"clientID": "change this",
5757
"clientSecret": "change this",
58-
"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')"
58+
"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')",
59+
"version": "use 'v4' if gitlab version > 11, 'v3' otherwise. Default to 'v4'"
5960
},
6061
"mattermost": {
6162
"baseURL": "change this",

lib/config/index.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,12 @@ config.isSAMLEnable = config.saml.idpSsoUrl
103103
config.isOAuth2Enable = config.oauth2.clientID && config.oauth2.clientSecret
104104
config.isPDFExportEnable = config.allowPDFExport
105105

106+
// Check gitlab api version
107+
if (config.gitlab.version !== 'v4' || config.gitlab.version !== 'v3') {
108+
logger.warn('config.js contains wrong version (' + config.gitlab.version + ') for gitlab api; it should be \'v3\' or \'v4\'. Defaulting to v3')
109+
config.gitlab.version = 'v4'
110+
}
111+
106112
// Only update i18n files in development setups
107113
config.updateI18nFiles = (env === Environment.development)
108114

lib/response.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -573,11 +573,11 @@ function gitlabActionProjects (req, res, note) {
573573
}
574574
}).then(function (user) {
575575
if (!user) { return response.errorNotFound(res) }
576-
var ret = { baseURL: config.gitlab.baseURL }
576+
var ret = { baseURL: config.gitlab.baseURL, version: config.gitlab.version }
577577
ret.accesstoken = user.accessToken
578578
ret.profileid = user.profileid
579579
request(
580-
config.gitlab.baseURL + '/api/v3/projects?access_token=' + user.accessToken,
580+
config.gitlab.baseURL + '/api/' + config.gitlab.version + '/projects?access_token=' + user.accessToken,
581581
function (error, httpResponse, body) {
582582
if (!error && httpResponse.statusCode === 200) {
583583
ret.projects = JSON.parse(body)

public/js/index.js

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -970,6 +970,7 @@ ui.toolbar.export.snippet.click(function () {
970970
.done(function (data) {
971971
$('#snippetExportModalAccessToken').val(data.accesstoken)
972972
$('#snippetExportModalBaseURL').val(data.baseURL)
973+
$('#snippetExportModalVersion').val(data.version)
973974
$('#snippetExportModalLoading').hide()
974975
$('#snippetExportModal').modal('toggle')
975976
$('#snippetExportModalProjects').find('option').remove().end().append('<option value="init" selected="selected" disabled="disabled">Select From Available Projects</option>')
@@ -1021,6 +1022,7 @@ ui.toolbar.import.snippet.click(function () {
10211022
.done(function (data) {
10221023
$('#snippetImportModalAccessToken').val(data.accesstoken)
10231024
$('#snippetImportModalBaseURL').val(data.baseURL)
1025+
$('#snippetImportModalVersion').val(data.version)
10241026
$('#snippetImportModalContent').prop('disabled', false)
10251027
$('#snippetImportModalConfirm').prop('disabled', false)
10261028
$('#snippetImportModalLoading').hide()
@@ -1243,10 +1245,10 @@ ui.modal.snippetImportProjects.change(function () {
12431245
var accesstoken = $('#snippetImportModalAccessToken').val()
12441246
var baseURL = $('#snippetImportModalBaseURL').val()
12451247
var project = $('#snippetImportModalProjects').val()
1246-
1248+
var version = $('#snippetImportModalVersion').val()
12471249
$('#snippetImportModalLoading').show()
12481250
$('#snippetImportModalContent').val('/projects/' + project)
1249-
$.get(baseURL + '/api/v3/projects/' + project + '/snippets?access_token=' + accesstoken)
1251+
$.get(baseURL + '/api/' + version + '/projects/' + project + '/snippets?access_token=' + accesstoken)
12501252
.done(function (data) {
12511253
$('#snippetImportModalSnippets').find('option').remove().end().append('<option value="init" selected="selected" disabled="disabled">Select From Available Snippets</option>')
12521254
data.forEach(function (snippet) {
@@ -1433,7 +1435,7 @@ $('#snippetImportModalConfirm').click(function () {
14331435
} else {
14341436
ui.spinner.show()
14351437
var accessToken = '?access_token=' + $('#snippetImportModalAccessToken').val()
1436-
var fullURL = $('#snippetImportModalBaseURL').val() + '/api/v3' + snippeturl
1438+
var fullURL = $('#snippetImportModalBaseURL').val() + '/api/' + $('#snippetImportModalVersion').val() + snippeturl
14371439
$.get(fullURL + accessToken)
14381440
.done(function (data) {
14391441
var content = '# ' + (data.title || 'Snippet Import')
@@ -1470,15 +1472,19 @@ $('#snippetImportModalConfirm').click(function () {
14701472
$('#snippetExportModalConfirm').click(function () {
14711473
var accesstoken = $('#snippetExportModalAccessToken').val()
14721474
var baseURL = $('#snippetExportModalBaseURL').val()
1475+
var version = $('#snippetExportModalVersion').val()
1476+
14731477
var data = {
14741478
title: $('#snippetExportModalTitle').val(),
14751479
file_name: $('#snippetExportModalFileName').val(),
14761480
code: editor.getValue(),
1477-
visibility_level: $('#snippetExportModalVisibility').val()
1481+
visibility_level: $('#snippetExportModalVisibility').val(),
1482+
visibility: $('#snippetExportModalVisibility').val() === 0 ? 'private' : ($('#snippetExportModalVisibility').val() === 10 ? 'internal' : '')
14781483
}
1484+
14791485
if (!data.title || !data.file_name || !data.code || !data.visibility_level || !$('#snippetExportModalProjects').val()) return
14801486
$('#snippetExportModalLoading').show()
1481-
var fullURL = baseURL + '/api/v3/projects/' + $('#snippetExportModalProjects').val() + '/snippets?access_token=' + accesstoken
1487+
var fullURL = baseURL + '/api/' + version + '/projects/' + $('#snippetExportModalProjects').val() + '/snippets?access_token=' + accesstoken
14821488
$.post(fullURL
14831489
, data
14841490
, function (ret) {
@@ -1487,7 +1493,6 @@ $('#snippetExportModalConfirm').click(function () {
14871493
var redirect = baseURL + '/' + $("#snippetExportModalProjects option[value='" + $('#snippetExportModalProjects').val() + "']").text() + '/snippets/' + ret.id
14881494
showMessageModal('<i class="fa fa-gitlab"></i> Export to Snippet', 'Export Successful!', redirect, 'View Snippet Here', true)
14891495
}
1490-
, 'json'
14911496
)
14921497
})
14931498

public/views/codimd/body.ejs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@
153153
<div class="modal-body">
154154
<input type="hidden" id="snippetImportModalAccessToken" />
155155
<input type="hidden" id="snippetImportModalBaseURL" />
156+
<input type="hidden" id="snippetImportModalVersion" />
156157
<div class="ui-field-contain" style="display:table;margin-bottom:10px;width:100%;">
157158
<div style="display:table-row;margin-bottom:5px;">
158159
<label style="display:table-cell;">Project:</label>
@@ -191,6 +192,7 @@
191192
<div class="modal-body">
192193
<input type="hidden" id="snippetExportModalAccessToken" />
193194
<input type="hidden" id="snippetExportModalBaseURL" />
195+
<input type="hidden" id="snippetExportModalVersion" />
194196
<div class="ui-field-contain" style="display:table;margin-bottom:10px;width:100%;">
195197
<div style="display:table-row;margin-bottom:5px;">
196198
<label style="display:table-cell;">Title:</label>

0 commit comments

Comments
 (0)