diff --git a/lib/api/gitlab.js b/lib/api/gitlab.js index 859f5f1fe8bdec646e31aae9119bd59379944fd7..2a27bac6d02e985eb82bdb6bada88e5a560a9d9a 100644 --- a/lib/api/gitlab.js +++ b/lib/api/gitlab.js @@ -64,6 +64,15 @@ async function initRepo(repoName, token, endpoint) { if (endpoint) { process.env.GITLAB_ENDPOINT = endpoint; } + try { + logger.debug(`Determining Gitlab API version`); + // projects/owned route deprecated in v4 + await glGot(`projects/owned`); + config.apiVersion = 'v3'; + } catch (err) { + config.apiVersion = 'v4'; + } + logger.debug(`Detected Gitlab API ${config.apiVersion}`); config.repoName = repoName.replace('/', '%2F'); try { const res = await glGot(`projects/${config.repoName}`); @@ -275,9 +284,14 @@ async function mergePr(pr) { // Generic File operations async function getFile(filePath, branchName = config.defaultBranch) { - const res = await glGot( - `projects/${config.repoName}/repository/files?file_path=${filePath}&ref=${branchName}` - ); + // Gitlab API v3 support + let url; + if (config.apiVersion === 'v3') { + url = `projects/${config.repoName}/repository/files?file_path=${filePath}&ref=${branchName}`; + } else { + url = `projects/${config.repoName}/repository/files/${filePath}?ref=${branchName}`; + } + const res = await glGot(url); return res.body.content; } @@ -310,27 +324,53 @@ async function getFileJson(filePath, branchName) { } async function createFile(branchName, filePath, fileContents, message) { - await glGot.post(`projects/${config.repoName}/repository/files`, { - body: { + // Gitlab API v3 support + let url; + const opts = {}; + if (config.apiVersion === 'v3') { + url = `projects/${config.repoName}/repository/files`; + opts.body = { file_path: filePath, branch_name: branchName, commit_message: message, encoding: 'base64', content: new Buffer(fileContents).toString('base64'), - }, - }); + }; + } else { + url = `projects/${config.repoName}/repository/files/${filePath}`; + opts.body = { + branch: branchName, + commit_message: message, + encoding: 'base64', + content: new Buffer(fileContents).toString('base64'), + }; + } + await glGot.post(url, opts); } async function updateFile(branchName, filePath, fileContents, message) { - await glGot.put(`projects/${config.repoName}/repository/files`, { - body: { + // Gitlab API v3 support + let url; + const opts = {}; + if (config.apiVersion === 'v3') { + url = `projects/${config.repoName}/repository/files`; + opts.body = { file_path: filePath, branch_name: branchName, commit_message: message, encoding: 'base64', content: new Buffer(fileContents).toString('base64'), - }, - }); + }; + } else { + url = `projects/${config.repoName}/repository/files/${filePath}`; + opts.body = { + branch: branchName, + commit_message: message, + encoding: 'base64', + content: new Buffer(fileContents).toString('base64'), + }; + } + await glGot.put(url, opts); } // Add a new commit, create branch if not existing @@ -368,10 +408,18 @@ async function commitFilesToBranch( // Creates a new branch with provided commit async function createBranch(branchName, ref = config.defaultBranch) { - await glGot.post(`projects/${config.repoName}/repository/branches`, { - body: { + // Gitlab API v3 support + const opts = {}; + if (config.apiVersion === 'v3') { + opts.body = { branch_name: branchName, ref, - }, - }); + }; + } else { + opts.body = { + branch: branchName, + ref, + }; + } + await glGot.post(`projects/${config.repoName}/repository/branches`, opts); }