Skip to content

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

arvest-bjoneson
Copy link

@arvest-bjoneson arvest-bjoneson commented Jun 5, 2025

This feature adds support for using CI Job Tokens for authentication to the gitlab API. This will work as-is, provided the user is able to enable: https://docs.gitlab.com/ci/jobs/ci_job_token/#allow-git-push-requests-to-your-project-repository

This is feature flagged, but set to be enabled on gitlab.com in the near future.

Job tokens are unable to comment on merge requests and issues, so there is an explicit check in the verify stage for this.

Since the base project endpoint is unavailable, it uses the releases endpoint to verify auth when using a job token.

This addresses outstanding issues in: #846

Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds support for authenticating to GitLab via CI Job Tokens with corresponding configuration, error handling, and documentation updates. Key changes include:

  • New tests verifying correct behavior when using job tokens, including error handling for commenting.
  • Updates to configuration and API calls across multiple modules to correctly use token headers based on the token type.
  • Documentation updates in README and error messages to explain job token usage.

Reviewed Changes

Copilot reviewed 11 out of 11 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
test/verify.test.js Added tests to verify error throwing when comment conditions are not set for job tokens.
test/resolve-config.test.js Introduced job token related options and configuration properties.
test/integration.test.js Added integration test for GitLab authentication using job tokens.
test/helpers/mock-gitlab.js Updated mock to conditionally use "Job-Token" header for job tokens.
lib/verify.js Updated verification flow to handle job token authentication.
lib/success.js Updated token header usage in success flow.
lib/resolve-config.js Extended configuration resolution for job token support.
lib/publish.js Updated token header usage when publishing releases.
lib/fail.js Updated token header usage in failure handling.
lib/definitions/errors.js Extended error definitions to support job token related issues.
README.md Clarified documentation regarding different token types and usage.

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` };
Copy link
Preview

Copilot AI Jun 15, 2025

Choose a reason for hiding this comment

The 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
const options = { repositoryUrl: `https://github.com/${owner}/${repo}.git` };
const options = { repositoryUrl: `https://gitlab.com/${owner}/${repo}.git` };

Copilot uses AI. Check for mistakes.

Comment on lines +60 to +62
if (isJobToken && !(failCommentCondition === false) && !(successCommentCondition === false)) {
errors.push(getError("EJOBTOKENCOMMENTCONDITION", { projectPath }))
}
Copy link
Contributor

Choose a reason for hiding this comment

The 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?

) {
errors.push(getError("EGLNOPUSHPERMISSION", { projectPath }));
if (isJobToken) {
await got.get(urlJoin(projectApiUrl, "releases"), { headers: { [tokenHeader]: gitlabToken } });
Copy link
Contributor

Choose a reason for hiding this comment

The 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?

Comment on lines +65 to +68
#### Job Token
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`.

**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).
Copy link
Contributor

@fgreinacher fgreinacher Jun 15, 2025

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?

Copy link
Contributor

@fgreinacher fgreinacher left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your contribution @arvest-bjoneson, I did a first pass and left some questions 🙇

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants