Skip to content

Commit

Permalink
fix: replace deprecated Tags API call (#184) thanks @nfriend
Browse files Browse the repository at this point in the history
This commit replaces calls to the deprecated Tags API endpoints with
equivalent calls to the Releases API.
  • Loading branch information
Nathan Friend authored Dec 8, 2020
1 parent ff48a65 commit f82b613
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 37 deletions.
31 changes: 16 additions & 15 deletions lib/publish.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'};
Expand Down
10 changes: 8 additions & 2 deletions test/integration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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);

Expand Down
51 changes: 31 additions & 20 deletions test/publish.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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});

Expand All @@ -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 => {
Expand All @@ -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});
Expand All @@ -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});

Expand All @@ -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'};
Expand All @@ -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);

Expand Down

0 comments on commit f82b613

Please sign in to comment.