Skip to content

Handle ci/cd job token #846

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

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/fail.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const debug = _debug("semantic-release:gitlab");
import resolveConfig from "./resolve-config.js";
import getFailComment from "./get-fail-comment.js";
import getProjectContext from "./get-project-context.js";
import getTokenHeader from "./get-token-header.js"

export default async (pluginConfig, context) => {
const {
Expand All @@ -28,7 +29,7 @@ export default async (pluginConfig, context) => {
const { encodedProjectPath, projectApiUrl } = getProjectContext(context, gitlabUrl, gitlabApiUrl, repositoryUrl);

const apiOptions = {
headers: { "PRIVATE-TOKEN": gitlabToken },
headers: { [getTokenHeader(gitlabToken)]: gitlabToken },
retry: { limit: retryLimit },
};

Expand Down
8 changes: 8 additions & 0 deletions lib/get-token-header.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export default (token) => {
switch(true) {
case token.startsWith("glcbt-"):
return "JOB-TOKEN"
default:
return "PRIVATE-TOKEN"
}
Comment on lines +2 to +7

Choose a reason for hiding this comment

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

Suggested change
switch(true) {
case token.startsWith("glcbt-"):
return "JOB-TOKEN"
default:
return "PRIVATE-TOKEN"
}
if (token.startsWith("glcbt-")) return "JOB-TOKEN";
return "PRIVATE-TOKEN";

I don't really understand the need to use switch(true) here?

Copy link
Author

Choose a reason for hiding this comment

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

Gitlab have many types of tokens that could start with different prefixes (https://docs.gitlab.com/security/tokens/#token-prefixes). It's just to make it's easier to return the correct http header if any change is required in the future. I don't mind to use a "if" statement.

Copy link
Contributor

Choose a reason for hiding this comment

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

GitLab supports configuring custom token prefixes for self-hosted instances. If used this would break the logic here.

Copy link
Author

Choose a reason for hiding this comment

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

@fgreinacher: It's more complicated to support... We would have to add configuration parameters to support this. Currently, there is no regression with the change since if the prefix has been modified, it will continue to fall into the default case with it's the current implementation. It might be necessary to add a note in the documentation that currently CI job tokens with custom prefixes are not supported. I hadn't really planned on making the changes to support custom prefixes.

};
3 changes: 2 additions & 1 deletion lib/publish.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import resolveConfig from "./resolve-config.js";
import getAssets from "./glob-assets.js";
import { RELEASE_NAME } from "./definitions/constants.js";
import getProjectContext from "./get-project-context.js";
import getTokenHeader from "./get-token-header.js"

const isUrlScheme = (value) => /^(https|http|ftp):\/\//.test(value);

Expand All @@ -33,7 +34,7 @@ export default async (pluginConfig, context) => {
const encodedVersion = encodeURIComponent(version);
const apiOptions = {
headers: {
"PRIVATE-TOKEN": gitlabToken,
[getTokenHeader(gitlabToken)]: gitlabToken,
},
hooks: {
beforeError: [
Expand Down
3 changes: 2 additions & 1 deletion lib/success.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const debug = _debug("semantic-release:gitlab");
import resolveConfig from "./resolve-config.js";
import getProjectContext from "./get-project-context.js";
import getSuccessComment from "./get-success-comment.js";
import getTokenHeader from "./get-token-header.js"

export default async (pluginConfig, context) => {
const {
Expand All @@ -19,7 +20,7 @@ export default async (pluginConfig, context) => {
resolveConfig(pluginConfig, context);
const { projectApiUrl } = getProjectContext(context, gitlabUrl, gitlabApiUrl, repositoryUrl);
const apiOptions = {
headers: { "PRIVATE-TOKEN": gitlabToken },
headers: { [getTokenHeader(gitlabToken)]: gitlabToken },
retry: { limit: retryLimit },
};

Expand Down
3 changes: 2 additions & 1 deletion lib/verify.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 getTokenHeader from "./get-token-header.js"

const isNonEmptyString = (value) => isString(value) && value.trim();
const isStringOrStringArray = (value) =>
Expand Down Expand Up @@ -64,7 +65,7 @@ export default async (pluginConfig, context) => {
permissions: { project_access: projectAccess, group_access: groupAccess },
} = await got
.get(projectApiUrl, {
headers: { "PRIVATE-TOKEN": gitlabToken },
headers: { [getTokenHeader(gitlabToken)]: gitlabToken },
...proxy,
})
.json());
Expand Down
3 changes: 2 additions & 1 deletion test/helpers/mock-gitlab.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import nock from 'nock';
import urlJoin from 'url-join';
import getTokenHeader from "./get-token-header.js"

/**
* Retun a `nock` object setup to respond to a GitLab authentication request. Other expectation and responses can be chained.
Expand All @@ -22,5 +23,5 @@ export default function (
: null || '/api/v4',
} = {}
) {
return nock(urlJoin(gitlabUrl, gitlabApiPathPrefix), {reqheaders: {'Private-Token': gitlabToken}});
return nock(urlJoin(gitlabUrl, gitlabApiPathPrefix), {reqheaders: {[getTokenHeader(gitlabToken)]: gitlabToken}});
};