Skip to content

Commit

Permalink
feat: add release comments to issues
Browse files Browse the repository at this point in the history
  • Loading branch information
fgreinacher authored Feb 6, 2022
1 parent 18336a9 commit 63383ad
Show file tree
Hide file tree
Showing 5 changed files with 155 additions and 11 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ Create a [personal access token](https://docs.gitlab.com/ce/user/profile/persona
| `gitlabApiPathPrefix` | The GitLab API prefix. | `GL_PREFIX` or `GITLAB_PREFIX` environment variable or CI provided environment variables if running on [GitLab CI/CD](https://docs.gitlab.com/ee/ci) or `/api/v4`. |
| `assets` | An array of files to upload to the release. See [assets](#assets). | - |
| `milestones` | An array of milestone titles to associate to the release. See [GitLab Release API](https://docs.gitlab.com/ee/api/releases/#create-a-release). | - |
| `postComments` | Post comments to issues and MRs associated with a release. | `false` |

#### assets

Expand Down
79 changes: 76 additions & 3 deletions lib/publish.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const {createReadStream} = require('fs');
const {resolve} = require('path');
const {stat} = require('fs-extra');
const {isPlainObject} = require('lodash');
const {isPlainObject, uniqWith, isEqual} = require('lodash');
const FormData = require('form-data');
const urlJoin = require('url-join');
const got = require('got');
Expand All @@ -16,8 +16,9 @@ module.exports = async (pluginConfig, context) => {
options: {repositoryUrl},
nextRelease: {gitTag, gitHead, notes},
logger,
commits,
} = context;
const {gitlabToken, gitlabUrl, gitlabApiUrl, assets, milestones} = resolveConfig(pluginConfig, context);
const {gitlabToken, gitlabUrl, gitlabApiUrl, assets, milestones, postComments} = resolveConfig(pluginConfig, context);
const assetsList = [];
const repoId = getRepoId(context, gitlabUrl, repositoryUrl);
const encodedRepoId = encodeURIComponent(repoId);
Expand Down Expand Up @@ -117,5 +118,77 @@ module.exports = async (pluginConfig, context) => {

logger.log('Published GitLab release: %s', gitTag);

return {url: urlJoin(gitlabUrl, encodedRepoId, `/-/releases/${encodedGitTag}`), name: 'GitLab release'};
const releaseUrl = urlJoin(gitlabUrl, encodedRepoId, `/-/releases/${encodedGitTag}`);

if (postComments) {
try {
const postCommentToIssue = issue => {
const issueNotesEndpoint = urlJoin(gitlabApiUrl, `/projects/${issue.project_id}/issues/${issue.iid}/notes`);
debug('Posting issue note to %s', issueNotesEndpoint);
return got.post(issueNotesEndpoint, {
...apiOptions,
json: {
body: `:tada: This issue has been resolved in version ${gitTag} :tada:\n\nThe release is available on [Gitlab Release](${releaseUrl})`,
},
});
};

const postCommentToMergeRequest = mergeRequest => {
const mergeRequestNotesEndpoint = urlJoin(
gitlabApiUrl,
`/projects/${mergeRequest.project_id}/merge_requests/${mergeRequest.iid}/notes`
);
debug('Posting MR note to %s', mergeRequestNotesEndpoint);
return got.post(mergeRequestNotesEndpoint, {
...apiOptions,
json: {
body: `:tada: This MR is included in version ${gitTag} :tada:\n\nThe release is available on [Gitlab Release](${releaseUrl})`,
},
});
};

const getRelatedMergeRequests = async commitHash => {
const relatedMergeRequestsEndpoint = urlJoin(
gitlabApiUrl,
`/projects/${encodedRepoId}/repository/commits/${commitHash}/merge_requests`
);
debug('Getting MRs from %s', relatedMergeRequestsEndpoint);
const relatedMergeRequests = await got
.get(relatedMergeRequestsEndpoint, {
...apiOptions,
})
.json();

return relatedMergeRequests.filter(x => x.state === 'merged');
};

const getRelatedIssues = async mergeRequest => {
const relatedIssuesEndpoint = urlJoin(
gitlabApiUrl,
`/projects/${mergeRequest.project_id}/merge_requests/${mergeRequest.iid}/closes_issues`
);
debug('Getting related issues from %s', relatedIssuesEndpoint);
const relatedIssues = await got
.get(relatedIssuesEndpoint, {
...apiOptions,
})
.json();

return relatedIssues.filter(x => x.state === 'closed');
};

const relatedMergeRequests = uniqWith(
(await Promise.all(commits.map(x => x.hash).map(getRelatedMergeRequests))).flat(),
isEqual
);
const relatedIssues = uniqWith((await Promise.all(relatedMergeRequests.map(getRelatedIssues))).flat(), isEqual);
await Promise.all(relatedIssues.map(postCommentToIssue));
await Promise.all(relatedMergeRequests.map(postCommentToMergeRequest));
} catch (error) {
logger.error('An error occurred while posting comments to related issues and merge requests:\n%O', error);
throw error;
}
}

return {url: releaseUrl, name: 'GitLab release'};
};
3 changes: 2 additions & 1 deletion lib/resolve-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const {castArray, isNil} = require('lodash');
const urlJoin = require('url-join');

module.exports = (
{gitlabUrl, gitlabApiPathPrefix, assets, milestones},
{gitlabUrl, gitlabApiPathPrefix, assets, milestones, postComments},
{
envCi: {service} = {},
env: {
Expand Down Expand Up @@ -41,5 +41,6 @@ module.exports = (
: urlJoin(defaultedGitlabUrl, isNil(userGitlabApiPathPrefix) ? '/api/v4' : userGitlabApiPathPrefix),
assets: assets ? castArray(assets) : assets,
milestones: milestones ? castArray(milestones) : milestones,
postComments: postComments === undefined ? false : postComments,
};
};
54 changes: 54 additions & 0 deletions test/publish.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,60 @@ test.serial('Publish a release', async t => {
t.true(gitlab.isDone());
});

test.serial('Publish a release and post comments', async t => {
const owner = 'test_user';
const repo = 'test_repo';
const env = {GITLAB_TOKEN: 'gitlab_token'};
const pluginConfig = {postComments: true};
const nextRelease = {gitHead: '123', gitTag: 'v1.0.0', notes: 'Test release note body'};
const options = {repositoryUrl: `https://gitlab.com/${owner}/${repo}.git`};
const encodedRepoId = encodeURIComponent(`${owner}/${repo}`);
const commits = [{hash: 'abcdef'}, {hash: 'fedcba'}];
const gitlab = authenticate(env)
.post(`/projects/${encodedRepoId}/releases`, {
tag_name: nextRelease.gitTag,
description: nextRelease.notes,
assets: {
links: [],
},
})
.reply(200)
.get(`/projects/${encodedRepoId}/repository/commits/abcdef/merge_requests`)
.reply(200, [
{project_id: 100, iid: 1, state: 'merged'},
{project_id: 200, iid: 2, state: 'closed'},
{project_id: 300, iid: 3, state: 'merged'},
])
.get(`/projects/${encodedRepoId}/repository/commits/fedcba/merge_requests`)
.reply(200, [{project_id: 100, iid: 1, state: 'merged'}])
.get(`/projects/100/merge_requests/1/closes_issues`)
.reply(200, [
{project_id: 100, iid: 11, state: 'closed'},
{project_id: 100, iid: 12, state: 'open'},
{project_id: 100, iid: 13, state: 'closed'},
])
.get(`/projects/300/merge_requests/3/closes_issues`)
.reply(200, [])
.post(`/projects/100/merge_requests/1/notes`, {
body:
':tada: This MR is included in version v1.0.0 :tada:\n\nThe release is available on [Gitlab Release](https://gitlab.com/test_user%2Ftest_repo/-/releases/v1.0.0)',
})
.reply(200)
.post(`/projects/300/merge_requests/3/notes`)
.reply(200)
.post(`/projects/100/issues/11/notes`, {
body:
':tada: This issue has been resolved in version v1.0.0 :tada:\n\nThe release is available on [Gitlab Release](https://gitlab.com/test_user%2Ftest_repo/-/releases/v1.0.0)',
})
.reply(200)
.post(`/projects/100/issues/13/notes`)
.reply(200);

await publish(pluginConfig, {env, options, nextRelease, logger: t.context.logger, commits});

t.true(gitlab.isDone());
});

test.serial('Publish a release with assets', async t => {
const cwd = 'test/fixtures/files';
const owner = 'test_user';
Expand Down
29 changes: 22 additions & 7 deletions test/resolve-config.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,19 @@ test('Returns user config', t => {
const gitlabUrl = 'https://host.com';
const gitlabApiPathPrefix = '/api/prefix';
const assets = ['file.js'];
const postComments = true;

t.deepEqual(resolveConfig({gitlabUrl, gitlabApiPathPrefix, assets}, {env: {GITLAB_TOKEN: gitlabToken}}), {
gitlabToken,
gitlabUrl,
gitlabApiUrl: urlJoin(gitlabUrl, gitlabApiPathPrefix),
assets,
milestones: undefined,
});
t.deepEqual(
resolveConfig({gitlabUrl, gitlabApiPathPrefix, assets, postComments}, {env: {GITLAB_TOKEN: gitlabToken}}),
{
gitlabToken,
gitlabUrl,
gitlabApiUrl: urlJoin(gitlabUrl, gitlabApiPathPrefix),
assets,
milestones: undefined,
postComments,
}
);
});

test('Returns user config via environment variables', t => {
Expand All @@ -35,6 +40,7 @@ test('Returns user config via environment variables', t => {
gitlabApiUrl: urlJoin(gitlabUrl, gitlabApiPathPrefix),
assets,
milestones,
postComments: false,
}
);
});
Expand All @@ -53,6 +59,7 @@ test('Returns user config via alternative environment variables', t => {
gitlabApiUrl: urlJoin(gitlabUrl, gitlabApiPathPrefix),
assets,
milestones: undefined,
postComments: false,
}
);
});
Expand All @@ -68,6 +75,7 @@ test('Returns default config', t => {
gitlabApiUrl: urlJoin('https://gitlab.com', '/api/v4'),
assets: undefined,
milestones: undefined,
postComments: false,
});

t.deepEqual(resolveConfig({gitlabApiPathPrefix}, {env: {GL_TOKEN: gitlabToken}}), {
Expand All @@ -76,6 +84,7 @@ test('Returns default config', t => {
gitlabApiUrl: urlJoin('https://gitlab.com', gitlabApiPathPrefix),
assets: undefined,
milestones: undefined,
postComments: false,
});

t.deepEqual(resolveConfig({gitlabUrl}, {env: {GL_TOKEN: gitlabToken}}), {
Expand All @@ -84,6 +93,7 @@ test('Returns default config', t => {
gitlabApiUrl: urlJoin(gitlabUrl, '/api/v4'),
assets: undefined,
milestones: undefined,
postComments: false,
});
});

Expand All @@ -107,6 +117,7 @@ test('Returns default config via GitLab CI/CD environment variables', t => {
gitlabApiUrl: CI_API_V4_URL,
assets: undefined,
milestones: undefined,
postComments: false,
}
);
});
Expand Down Expand Up @@ -134,6 +145,7 @@ test('Returns user config over GitLab CI/CD environment variables', t => {
gitlabApiUrl: urlJoin(gitlabUrl, gitlabApiPathPrefix),
assets,
milestones: undefined,
postComments: false,
}
);
});
Expand Down Expand Up @@ -167,6 +179,7 @@ test('Returns user config via environment variables over GitLab CI/CD environmen
gitlabApiUrl: urlJoin(gitlabUrl, gitlabApiPathPrefix),
assets: undefined,
milestones: undefined,
postComments: false,
}
);
});
Expand Down Expand Up @@ -200,6 +213,7 @@ test('Returns user config via alternative environment variables over GitLab CI/C
gitlabApiUrl: urlJoin(gitlabUrl, gitlabApiPathPrefix),
assets: undefined,
milestones: undefined,
postComments: false,
}
);
});
Expand All @@ -224,6 +238,7 @@ test('Ignore GitLab CI/CD environment variables if not running on GitLab CI/CD',
gitlabApiUrl: urlJoin('https://gitlab.com', '/api/v4'),
assets: undefined,
milestones: undefined,
postComments: false,
}
);
});

0 comments on commit 63383ad

Please sign in to comment.