From 4c716c2c7fa7b5dd5a06913f245d4b3a1814e14d Mon Sep 17 00:00:00 2001 From: Nathan Friend Date: Tue, 8 Dec 2020 09:12:15 -0500 Subject: [PATCH] refactor: replace deprecated Tags API call This commit replaces calls to the deprecated Tags API endpoints with equivalent calls to the Releases API. --- lib/publish.js | 31 ++++++++++++------------ test/integration.test.js | 10 ++++++-- test/publish.test.js | 51 ++++++++++++++++++++++++---------------- 3 files changed, 55 insertions(+), 37 deletions(-) diff --git a/lib/publish.js b/lib/publish.js index ba4dbf36..97c0c5ec 100644 --- a/lib/publish.js +++ b/lib/publish.js @@ -67,24 +67,25 @@ module.exports = async (pluginConfig, context) => { ); } - debug('Update git tag %o with commit %o and release description', gitTag, gitHead); - await got.post(urlJoin(gitlabApiUrl, `/projects/${encodedRepoId}/repository/tags/${encodedGitTag}/release`), { + debug('Create a release for git tag %o with commit %o', gitTag, gitHead); + await got.post(urlJoin(gitlabApiUrl, `/projects/${encodedRepoId}/releases`), { ...apiOptions, - json: {tag_name: gitTag, description: notes && notes.trim() ? notes : gitTag}, // eslint-disable-line camelcase + json: { + /* eslint-disable camelcase */ + tag_name: gitTag, + description: notes && notes.trim() ? notes : gitTag, + assets: { + links: assetsList.map(({label, alt, url}) => { + return { + name: label || alt, + url: urlJoin(gitlabUrl, repoId, url), + }; + }), + }, + /* eslint-enable camelcase */ + }, }); - if (assetsList.length > 0) { - await Promise.all( - assetsList.map(({label, alt, url}) => { - debug('Add link to asset %o', label || alt); - return got.post(urlJoin(gitlabApiUrl, `/projects/${encodedRepoId}/releases/${encodedGitTag}/assets/links`), { - ...apiOptions, - json: {name: label || alt, url: urlJoin(gitlabUrl, repoId, url)}, - }); - }) - ); - } - logger.log('Published GitLab release: %s', gitTag); return {url: urlJoin(gitlabUrl, encodedRepoId, `/tags/${encodedGitTag}`), name: 'GitLab release'}; diff --git a/test/integration.test.js b/test/integration.test.js index 319dfe5f..88847139 100644 --- a/test/integration.test.js +++ b/test/integration.test.js @@ -65,9 +65,12 @@ test.serial('Publish a release', async t => { const gitlab = authenticate(env) .get(`/projects/${encodedRepoId}`) .reply(200, {permissions: {project_access: {access_level: 30}}}) - .post(`/projects/${encodedRepoId}/repository/tags/${nextRelease.gitTag}/release`, { + .post(`/projects/${encodedRepoId}/releases`, { tag_name: nextRelease.gitTag, description: nextRelease.notes, + assets: { + links: [], + }, }) .reply(200); @@ -90,9 +93,12 @@ test.serial('Verify Github auth and release', async t => { const gitlab = authenticate(env) .get(`/projects/${encodedRepoId}`) .reply(200, {permissions: {project_access: {access_level: 30}}}) - .post(`/projects/${encodedRepoId}/repository/tags/${nextRelease.gitTag}/release`, { + .post(`/projects/${encodedRepoId}/releases`, { tag_name: nextRelease.gitTag, description: nextRelease.notes, + assets: { + links: [], + }, }) .reply(200); diff --git a/test/publish.test.js b/test/publish.test.js index 270ea728..f24c01f3 100644 --- a/test/publish.test.js +++ b/test/publish.test.js @@ -29,9 +29,12 @@ test.serial('Publish a release', async t => { const encodedRepoId = encodeURIComponent(`${owner}/${repo}`); const encodedGitTag = encodeURIComponent(nextRelease.gitTag); const gitlab = authenticate(env) - .post(`/projects/${encodedRepoId}/repository/tags/${encodedGitTag}/release`, { + .post(`/projects/${encodedRepoId}/releases`, { tag_name: nextRelease.gitTag, description: nextRelease.notes, + assets: { + links: [], + }, }) .reply(200); @@ -54,20 +57,22 @@ test.serial('Publish a release with assets', async t => { const uploaded = {url: '/uploads/file.css', alt: 'file.css'}; const assets = [['**', '!**/*.txt', '!.dotfile']]; const gitlab = authenticate(env) - .post(`/projects/${encodedRepoId}/repository/tags/${encodedGitTag}/release`, { + .post(`/projects/${encodedRepoId}/releases`, { tag_name: nextRelease.gitTag, description: nextRelease.notes, + assets: { + links: [ + { + name: uploaded.alt, + url: `https://gitlab.com/${owner}/${repo}${uploaded.url}`, + }, + ], + }, }) .reply(200); const gitlabUpload = authenticate(env) .post(`/projects/${encodedRepoId}/uploads`, /filename="file.css"/gm) .reply(200, uploaded); - const gitlabAssetLink = authenticate(env) - .post(`/projects/${encodedRepoId}/releases/${encodedGitTag}/assets/links`, { - url: `https://gitlab.com/${owner}/${repo}${uploaded.url}`, - name: uploaded.alt, - }) - .reply(200, {}); const result = await publish({assets}, {env, cwd, options, nextRelease, logger: t.context.logger}); @@ -76,7 +81,6 @@ test.serial('Publish a release with assets', async t => { t.deepEqual(t.context.log.args[1], ['Published GitLab release: %s', nextRelease.gitTag]); t.true(gitlabUpload.isDone()); t.true(gitlab.isDone()); - t.true(gitlabAssetLink.isDone()); }); test.serial('Publish a release with array of missing assets', async t => { @@ -91,9 +95,12 @@ test.serial('Publish a release with array of missing assets', async t => { const emptyDirectory = tempy.directory(); const assets = [emptyDirectory, {path: 'missing.txt', label: 'missing.txt'}]; const gitlab = authenticate(env) - .post(`/projects/${encodedRepoId}/repository/tags/${encodedGitTag}/release`, { + .post(`/projects/${encodedRepoId}/releases`, { tag_name: nextRelease.gitTag, description: nextRelease.notes, + assets: { + links: [], + }, }) .reply(200); const result = await publish({assets}, {env, cwd, options, nextRelease, logger: t.context.logger}); @@ -116,20 +123,22 @@ test.serial('Publish a release with one asset and custom label', async t => { const assetLabel = 'Custom Label'; const assets = [{path: 'upload.txt', label: assetLabel}]; const gitlab = authenticate(env) - .post(`/projects/${encodedRepoId}/repository/tags/${encodedGitTag}/release`, { + .post(`/projects/${encodedRepoId}/releases`, { tag_name: nextRelease.gitTag, description: nextRelease.notes, + assets: { + links: [ + { + name: assetLabel, + url: `https://gitlab.com/${owner}/${repo}${uploaded.url}`, + }, + ], + }, }) .reply(200); const gitlabUpload = authenticate(env) .post(`/projects/${encodedRepoId}/uploads`, /filename="upload.txt"/gm) .reply(200, uploaded); - const gitlabAssetLink = authenticate(env) - .post(`/projects/${encodedRepoId}/releases/${encodedGitTag}/assets/links`, { - url: `https://gitlab.com/${owner}/${repo}${uploaded.url}`, - name: assetLabel, - }) - .reply(200, {}); const result = await publish({assets}, {env, cwd, options, nextRelease, logger: t.context.logger}); @@ -138,10 +147,9 @@ test.serial('Publish a release with one asset and custom label', async t => { t.deepEqual(t.context.log.args[1], ['Published GitLab release: %s', nextRelease.gitTag]); t.true(gitlabUpload.isDone()); t.true(gitlab.isDone()); - t.true(gitlabAssetLink.isDone()); }); -test.serial('Publish a release with missing releasae notes', async t => { +test.serial('Publish a release with missing release notes', async t => { const owner = 'test_user'; const repo = 'test_repo'; const env = {GITLAB_TOKEN: 'gitlab_token'}; @@ -151,9 +159,12 @@ test.serial('Publish a release with missing releasae notes', async t => { const encodedRepoId = encodeURIComponent(`${owner}/${repo}`); const encodedGitTag = encodeURIComponent(nextRelease.gitTag); const gitlab = authenticate(env) - .post(`/projects/${encodedRepoId}/repository/tags/${encodedGitTag}/release`, { + .post(`/projects/${encodedRepoId}/releases`, { tag_name: nextRelease.gitTag, description: nextRelease.gitTag, + assets: { + links: [], + }, }) .reply(200);