-
Notifications
You must be signed in to change notification settings - Fork 88
feat: enable support for using job token authentication #859
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ import AggregateError from "aggregate-error"; | |
import resolveConfig from "./resolve-config.js"; | ||
import getProjectContext from "./get-project-context.js"; | ||
import getError from "./get-error.js"; | ||
import urlJoin from "url-join"; | ||
|
||
const isNonEmptyString = (value) => isString(value) && value.trim(); | ||
const isStringOrStringArray = (value) => | ||
|
@@ -30,7 +31,10 @@ export default async (pluginConfig, context) => { | |
options: { repositoryUrl }, | ||
logger, | ||
} = context; | ||
const { gitlabToken, gitlabUrl, gitlabApiUrl, proxy, ...options } = resolveConfig(pluginConfig, context); | ||
const { gitlabToken, isJobToken, tokenHeader, successCommentCondition, failCommentCondition, gitlabUrl, gitlabApiUrl, proxy, ...options } = resolveConfig( | ||
pluginConfig, | ||
context | ||
); | ||
const { projectPath, projectApiUrl } = getProjectContext(context, gitlabUrl, gitlabApiUrl, repositoryUrl); | ||
|
||
debug("apiUrl: %o", gitlabApiUrl); | ||
|
@@ -53,30 +57,38 @@ export default async (pluginConfig, context) => { | |
errors.push(getError("ENOGLTOKEN", { repositoryUrl })); | ||
} | ||
|
||
if (isJobToken && !(failCommentCondition === false) && !(successCommentCondition === false)) { | ||
errors.push(getError("EJOBTOKENCOMMENTCONDITION", { projectPath })) | ||
} | ||
Comment on lines
+60
to
+62
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @arvest-bjoneson Will all the other endpoints listed at #846 (comment) work with the job token or do we need more checks like this? |
||
|
||
if (gitlabToken && projectPath) { | ||
let projectAccess; | ||
let groupAccess; | ||
|
||
logger.log("Verify GitLab authentication (%s)", gitlabApiUrl); | ||
|
||
try { | ||
({ | ||
permissions: { project_access: projectAccess, group_access: groupAccess }, | ||
} = await got | ||
.get(projectApiUrl, { | ||
headers: { "PRIVATE-TOKEN": gitlabToken }, | ||
...proxy, | ||
}) | ||
.json()); | ||
if ( | ||
context.options.dryRun && | ||
!((projectAccess && projectAccess.access_level >= 10) || (groupAccess && groupAccess.access_level >= 10)) | ||
) { | ||
errors.push(getError("EGLNOPULLPERMISSION", { projectPath })); | ||
} else if ( | ||
!((projectAccess && projectAccess.access_level >= 30) || (groupAccess && groupAccess.access_level >= 30)) | ||
) { | ||
errors.push(getError("EGLNOPUSHPERMISSION", { projectPath })); | ||
if (isJobToken) { | ||
await got.get(urlJoin(projectApiUrl, "releases"), { headers: { [tokenHeader]: gitlabToken } }); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this enough to check whether the job token has enough permissions to push? |
||
} else { | ||
({ | ||
permissions: { project_access: projectAccess, group_access: groupAccess }, | ||
} = await got | ||
.get(projectApiUrl, { | ||
headers: { [tokenHeader]: gitlabToken }, | ||
...proxy, | ||
}) | ||
.json()); | ||
if ( | ||
context.options.dryRun && | ||
!((projectAccess && projectAccess.access_level >= 10) || (groupAccess && groupAccess.access_level >= 10)) | ||
) { | ||
errors.push(getError("EGLNOPULLPERMISSION", { projectPath })); | ||
} else if ( | ||
!((projectAccess && projectAccess.access_level >= 30) || (groupAccess && groupAccess.access_level >= 30)) | ||
) { | ||
errors.push(getError("EGLNOPUSHPERMISSION", { projectPath })); | ||
} | ||
} | ||
} catch (error) { | ||
if (error.response && error.response.statusCode === 401) { | ||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -112,3 +112,32 @@ test.serial("Verify Github auth and release", async (t) => { | |||||
t.deepEqual(t.context.log.args[1], ["Published GitLab release: %s", nextRelease.gitTag]); | ||||||
t.true(gitlab.isDone()); | ||||||
}); | ||||||
|
||||||
test.serial("Verify GitLab auth and release with Job Token", async (t) => { | ||||||
const env = { GL_TOKEN: "gitlab_token", CI_JOB_TOKEN: "gitlab_token" }; | ||||||
const owner = "test_user"; | ||||||
const repo = "test_repo"; | ||||||
const options = { repositoryUrl: `https://github.com/${owner}/${repo}.git` }; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The repositoryUrl is set to 'github.com' instead of 'gitlab.com', which may lead to incorrect project path resolution in GitLab authentication tests. Consider updating the URL to 'https://gitlab.com/${owner}/${repo}.git'.
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||
const encodedProjectPath = encodeURIComponent(`${owner}/${repo}`); | ||||||
const nextRelease = { gitHead: "123", gitTag: "v1.0.0", notes: "Test release note body" }; | ||||||
|
||||||
const gitlab = authenticate(env) | ||||||
.get(`/projects/${encodedProjectPath}/releases`) | ||||||
.reply(200, []) | ||||||
.post(`/projects/${encodedProjectPath}/releases`, { | ||||||
tag_name: nextRelease.gitTag, | ||||||
description: nextRelease.notes, | ||||||
assets: { | ||||||
links: [], | ||||||
}, | ||||||
}) | ||||||
.reply(200); | ||||||
|
||||||
await t.notThrowsAsync(t.context.m.verifyConditions({ successCommentCondition: false, failCommentCondition: false }, { env, options, logger: t.context.logger })); | ||||||
const result = await t.context.m.publish({}, { env, options, nextRelease, logger: t.context.logger }); | ||||||
|
||||||
t.is(result.url, `https://gitlab.com/${owner}/${repo}/-/releases/${nextRelease.gitTag}`); | ||||||
t.deepEqual(t.context.log.args[0], ["Verify GitLab authentication (%s)", "https://gitlab.com/api/v4"]); | ||||||
t.deepEqual(t.context.log.args[1], ["Published GitLab release: %s", nextRelease.gitTag]); | ||||||
t.true(gitlab.isDone()); | ||||||
}); |
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd like to set the bar a bit higher as long as this is still feature flagged in GitLab. Could you expose a dedicated option to enable this behavior and also mention this more clearly here?