feat: pass tokenless value as branch override (#1511)

* feat: pass tokenless value as branch override

instead of only passing the tokenless branch value as an environment
variable we want to pass it as the branch value to the CLI

* refactor: change getToken to return nullable output

* fix: quick fix to use Promise resolve in getToken

* test: add test for tokenless build commit exec

* fix: don't overwrite overrideBranch & add comments decribing getToken
This commit is contained in:
joseph-sentry
2024-08-29 08:04:04 -04:00
committed by GitHub
parent 2439dfc05c
commit 4b21c320b5
5 changed files with 163 additions and 74 deletions

View File

@@ -5,6 +5,7 @@ import {
buildGeneralExec,
buildReportExec,
buildUploadExec,
getToken,
} from './buildExec';
const context = github.context;
@@ -213,7 +214,7 @@ test('report args using context', async () => {
for (const env of Object.keys(envs)) {
process.env['INPUT_' + env.toUpperCase()] = envs[env];
}
const expectedArgs : string[] = [
const expectedArgs: string[] = [
'--git-service',
'github',
];
@@ -271,12 +272,18 @@ test('commit args', async () => {
});
test('commit args using context', async () => {
const expectedArgs :string[] = [
const expectedArgs: string[] = [
'--git-service',
'github',
];
const {commitExecArgs, commitCommand} = await buildCommitExec();
if (
(context.eventName == 'pull_request' || context.eventName == 'pull_request_target') &&
context.payload.pull_request?.base.label.split(':')[0] != context.payload.pull_request?.head.label.split(':')[0]
) {
expectedArgs.push('-B', `${context.payload.pull_request?.head.label}`);
}
if (context.eventName == 'pull_request') {
expectedArgs.push('-C', `${context.payload.pull_request?.head.sha}`);
}
@@ -289,7 +296,7 @@ test('commit args using context', async () => {
});
test('commit args using github server url', async () => {
const expectedArgs :string[] = [
const expectedArgs: string[] = [
'--git-service',
'github_enterprise',
];
@@ -297,13 +304,65 @@ test('commit args using github server url', async () => {
process.env.GITHUB_SERVER_URL = 'https://example.com';
const {commitExecArgs, commitCommand} = await buildCommitExec();
if (
(context.eventName == 'pull_request' || context.eventName == 'pull_request_target') &&
context.payload.pull_request?.base.label.split(':')[0] != context.payload.pull_request?.head.label.split(':')[0]
) {
expectedArgs.push('-B', `${context.payload.pull_request?.head.label}`);
}
if (context.eventName == 'pull_request') {
expectedArgs.push('-C', `${context.payload.pull_request?.head.sha}`);
}
if (context.eventName == 'pull_request_target') {
expectedArgs.push('-P', `${context.payload.number}`);
}
expect(commitExecArgs).toEqual(expectedArgs);
expect(commitCommand).toEqual('create-commit');
});
test('build commit args when token arg is unset and from fork', async () => {
context.eventName = 'pull_request';
context.payload.pull_request = {
'number': 1,
'base': {
'label': 'hello:main',
},
'head': {
'label': 'world:feat',
'sha': 'aaaaaa',
},
};
const expectedArgs: string[] = [
'--git-service',
'github_enterprise',
'-B',
'world:feat',
'-C',
`${context.payload.pull_request?.head.sha}`,
];
const {commitExecArgs, commitCommand} = await buildCommitExec();
expect(commitExecArgs).toEqual(expectedArgs);
expect(commitCommand).toEqual('create-commit');
});
test('get token when token arg is unset and from fork', async () => {
context.eventName = 'pull_request';
context.payload.pull_request = {
'number': 1,
'base': {
'label': 'hello:main',
},
'head': {
'label': 'world:feat',
'sha': 'aaaaaa',
},
};
const token = await getToken();
expect(token).toEqual('');
});

View File

@@ -45,11 +45,6 @@ const isPullRequestFromFork = (): boolean => {
const getToken = async (): Promise<string> => {
let token = core.getInput('token');
if (!token && isPullRequestFromFork()) {
core.info('==> Fork detected, tokenless uploading used');
process.env['TOKENLESS'] = context.payload.pull_request.head.label;
return Promise.resolve('');
}
let url = core.getInput('url');
const useOIDC = isTrue(core.getInput('use_oidc'));
if (useOIDC) {
@@ -58,7 +53,7 @@ const getToken = async (): Promise<string> => {
}
try {
token = await core.getIDToken(url);
return token;
return Promise.resolve(token);
} catch (err) {
setFailure(
`Codecov: Failed to get OIDC token with url: ${url}. ${err.message}`,
@@ -69,6 +64,17 @@ const getToken = async (): Promise<string> => {
return token;
};
const getOverrideBranch = (token: string): string => {
let overrideBranch = core.getInput('override_branch');
if (!overrideBranch && !token && isPullRequestFromFork()) {
core.info('==> Fork detected, tokenless uploading used');
// backwards compatibility with certain versions of the CLI that expect this
process.env['TOKENLESS'] = context.payload.pull_request.head.label;
overrideBranch =context.payload.pull_request.head.label;
}
return overrideBranch;
};
const buildCommitExec = async (): Promise<{
commitExecArgs: any[];
commitOptions: any;
@@ -76,11 +82,11 @@ const buildCommitExec = async (): Promise<{
}> => {
const commitParent = core.getInput('commit_parent');
const gitService = getGitService();
const overrideBranch = core.getInput('override_branch');
const overrideCommit = core.getInput('override_commit');
const overridePr = core.getInput('override_pr');
const slug = core.getInput('slug');
const token = await getToken();
const overrideBranch = getOverrideBranch(token);
const failCi = isTrue(core.getInput('fail_ci_if_error'));
const workingDir = core.getInput('working-directory');
@@ -404,4 +410,11 @@ const buildUploadExec = async (): Promise<{
};
};
export {buildCommitExec, buildGeneralExec, buildReportExec, buildUploadExec};
export {
buildCommitExec,
buildGeneralExec,
buildReportExec,
buildUploadExec,
getToken,
};