Skip to content

Commit cd1e41a

Browse files
committed
feat: enhance job token support in GitLab integration
1 parent 542a2eb commit cd1e41a

File tree

6 files changed

+27
-17
lines changed

6 files changed

+27
-17
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ Create a [project access token](https://docs.gitlab.com/ee/user/project/settings
6363
**Note**: When running with [`dryRun`](https://semantic-release.gitbook.io/semantic-release/usage/configuration#dryrun) only `read_repository` scope is required.
6464

6565
#### Job Token
66+
6667
Ensure your project is configured to [allow git push requests for job tokens](https://docs.gitlab.com/ci/jobs/ci_job_token/#allow-git-push-requests-to-your-project-repository), and assign the value of `CI_JOB_TOKEN` to `GL_TOKEN`.
6768

6869
**Note**: Due to limitations on [job token](https://docs.gitlab.com/ci/jobs/ci_job_token/) access, comments on merge requests and issues must be explicitly disabled. See: [successCommentCondition](#successcommentcondition) and [failCommentCondition](#failcommentcondition).

lib/resolve-config.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,11 @@ export default (
5151
(service === "gitlab" && CI_PROJECT_URL && CI_PROJECT_PATH
5252
? CI_PROJECT_URL.replace(new RegExp(`/${CI_PROJECT_PATH}$`), "")
5353
: "https://gitlab.com");
54+
const isJobToken = !!CI_JOB_TOKEN && (GL_TOKEN || GITLAB_TOKEN) === CI_JOB_TOKEN;
5455
return {
5556
gitlabToken: GL_TOKEN || GITLAB_TOKEN,
56-
isJobToken: (!!CI_JOB_TOKEN && (GL_TOKEN || GITLAB_TOKEN) === CI_JOB_TOKEN),
57-
tokenHeader: (!!CI_JOB_TOKEN && (GL_TOKEN || GITLAB_TOKEN) === CI_JOB_TOKEN) ? "JOB-TOKEN" : "PRIVATE-TOKEN",
57+
isJobToken,
58+
tokenHeader: isJobToken ? "JOB-TOKEN" : "PRIVATE-TOKEN",
5859
gitlabUrl: defaultedGitlabUrl,
5960
gitlabApiUrl:
6061
userGitlabUrl && userGitlabApiPathPrefix

lib/verify.js

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,17 @@ export default async (pluginConfig, context) => {
3131
options: { repositoryUrl },
3232
logger,
3333
} = context;
34-
const { gitlabToken, isJobToken, tokenHeader, successCommentCondition, failCommentCondition, gitlabUrl, gitlabApiUrl, proxy, ...options } = resolveConfig(
35-
pluginConfig,
36-
context
37-
);
34+
const {
35+
gitlabToken,
36+
isJobToken,
37+
tokenHeader,
38+
successCommentCondition,
39+
failCommentCondition,
40+
gitlabUrl,
41+
gitlabApiUrl,
42+
proxy,
43+
...options
44+
} = resolveConfig(pluginConfig, context);
3845
const { projectPath, projectApiUrl } = getProjectContext(context, gitlabUrl, gitlabApiUrl, repositoryUrl);
3946

4047
debug("apiUrl: %o", gitlabApiUrl);
@@ -58,7 +65,7 @@ export default async (pluginConfig, context) => {
5865
}
5966

6067
if (isJobToken && !(failCommentCondition === false) && !(successCommentCondition === false)) {
61-
errors.push(getError("EJOBTOKENCOMMENTCONDITION", { projectPath }))
68+
errors.push(getError("EJOBTOKENCOMMENTCONDITION", { projectPath }));
6269
}
6370

6471
if (gitlabToken && projectPath) {

test/integration.test.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ test.serial("Verify GitLab auth and release with Job Token", async (t) => {
117117
const env = { GL_TOKEN: "gitlab_token", CI_JOB_TOKEN: "gitlab_token" };
118118
const owner = "test_user";
119119
const repo = "test_repo";
120-
const options = { repositoryUrl: `https://github.com/${owner}/${repo}.git` };
120+
const options = { repositoryUrl: `https://gitlab.com/${owner}/${repo}.git` };
121121
const encodedProjectPath = encodeURIComponent(`${owner}/${repo}`);
122122
const nextRelease = { gitHead: "123", gitTag: "v1.0.0", notes: "Test release note body" };
123123

@@ -133,7 +133,12 @@ test.serial("Verify GitLab auth and release with Job Token", async (t) => {
133133
})
134134
.reply(200);
135135

136-
await t.notThrowsAsync(t.context.m.verifyConditions({ successCommentCondition: false, failCommentCondition: false }, { env, options, logger: t.context.logger }));
136+
await t.notThrowsAsync(
137+
t.context.m.verifyConditions(
138+
{ successCommentCondition: false, failCommentCondition: false },
139+
{ env, options, logger: t.context.logger }
140+
)
141+
);
137142
const result = await t.context.m.publish({}, { env, options, nextRelease, logger: t.context.logger });
138143

139144
t.is(result.url, `https://gitlab.com/${owner}/${repo}/-/releases/${nextRelease.gitTag}`);

test/resolve-config.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -512,7 +512,7 @@ test("Ignore GitLab CI/CD environment variables if not running on GitLab CI/CD",
512512
});
513513

514514
test("Use job token when GitLab token equals CI_JOB_TOKEN", (t) => {
515-
const jobToken = "TOKEN"
515+
const jobToken = "TOKEN";
516516

517517
t.deepEqual(
518518
resolveConfig(

test/verify.test.js

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -995,15 +995,13 @@ test.serial(
995995
const owner = "test_user";
996996
const repo = "test_repo";
997997
const env = { GITLAB_TOKEN: "gitlab_token", CI_JOB_TOKEN: "gitlab_token" };
998-
const gitlab = authenticate(env)
999-
.get(`/projects/${owner}%2F${repo}/releases`)
1000-
.reply(200, []);
998+
const gitlab = authenticate(env).get(`/projects/${owner}%2F${repo}/releases`).reply(200, []);
1001999

10021000
const {
10031001
errors: [error],
10041002
} = await t.throwsAsync(
10051003
verify(
1006-
{ },
1004+
{},
10071005
{ env, options: { repositoryUrl: `https://gitlab.com/${owner}/${repo}.git` }, logger: t.context.logger }
10081006
)
10091007
);
@@ -1019,9 +1017,7 @@ test.serial(
10191017
const owner = "test_user";
10201018
const repo = "test_repo";
10211019
const env = { GITLAB_TOKEN: "gitlab_token", CI_JOB_TOKEN: "gitlab_token" };
1022-
const gitlab = authenticate(env)
1023-
.get(`/projects/${owner}%2F${repo}/releases`)
1024-
.reply(200, []);
1020+
const gitlab = authenticate(env).get(`/projects/${owner}%2F${repo}/releases`).reply(200, []);
10251021

10261022
await t.notThrowsAsync(
10271023
verify(

0 commit comments

Comments
 (0)