Skip to content

Commit

Permalink
feat: pass tokenless value as branch override (#1511)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
joseph-sentry authored Aug 29, 2024
1 parent 2439dfc commit 4b21c32
Show file tree
Hide file tree
Showing 5 changed files with 163 additions and 74 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ jobs:
file: ./coverage/coverage-final.json
flags: version,${{ matrix.os }}
name: codecov-version
version: v0.6.0
version: v0.7.3
verbose: true
token: ${{ secrets.CODECOV_TOKEN }}

Expand Down
139 changes: 78 additions & 61 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32341,22 +32341,16 @@ const getGitService = () => {
};
const isPullRequestFromFork = () => {
core.info(`evenName: ${context.eventName}`);
if (`${context.eventName}` !== 'pull_request' &&
`${context.eventName}` !== 'pull_request_target') {
if (!['pull_request', 'pull_request_target'].includes(context.eventName)) {
return false;
}
const baseLabel = context.payload.pull_request.base.label;
const headLabel = context.payload.pull_request.head.label;
core.info(`baseRef: ${baseLabel} | headRef: ${headLabel}`);
return (baseLabel.split(':')[0] !== headLabel.split(':')[0]);
return baseLabel.split(':')[0] !== headLabel.split(':')[0];
};
const getToken = () => buildExec_awaiter(void 0, void 0, void 0, function* () {
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) {
Expand All @@ -32365,22 +32359,32 @@ const getToken = () => buildExec_awaiter(void 0, void 0, void 0, function* () {
}
try {
token = yield core.getIDToken(url);
return token;
return Promise.resolve(token);
}
catch (err) {
setFailure(`Codecov: Failed to get OIDC token with url: ${url}. ${err.message}`, true);
}
}
return token;
});
const getOverrideBranch = (token) => {
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 = () => buildExec_awaiter(void 0, void 0, void 0, function* () {
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 = yield getToken();
const overrideBranch = getOverrideBranch(token);
const failCi = isTrue(core.getInput('fail_ci_if_error'));
const workingDir = core.getInput('working-directory');
const commitCommand = 'create-commit';
Expand All @@ -32398,27 +32402,28 @@ const buildCommitExec = () => buildExec_awaiter(void 0, void 0, void 0, function
commitOptions.env.CODECOV_TOKEN = token;
}
if (commitParent) {
commitExecArgs.push('--parent-sha', `${commitParent}`);
commitExecArgs.push('--parent-sha', commitParent);
}
commitExecArgs.push('--git-service', `${gitService}`);
commitExecArgs.push('--git-service', gitService);
if (overrideBranch) {
commitExecArgs.push('-B', `${overrideBranch}`);
commitExecArgs.push('-B', overrideBranch);
}
if (overrideCommit) {
commitExecArgs.push('-C', `${overrideCommit}`);
commitExecArgs.push('-C', overrideCommit);
}
else if (`${context.eventName}` == 'pull_request' ||
`${context.eventName}` == 'pull_request_target') {
commitExecArgs.push('-C', `${context.payload.pull_request.head.sha}`);
else if (['pull_request', 'pull_request_target'].includes(context.eventName)) {
const payload = context.payload;
commitExecArgs.push('-C', payload.pull_request.head.sha);
}
if (overridePr) {
commitExecArgs.push('--pr', `${overridePr}`);
commitExecArgs.push('--pr', overridePr);
}
else if (`${context.eventName}` == 'pull_request_target') {
commitExecArgs.push('--pr', `${context.payload.number}`);
else if (context.eventName === 'pull_request_target') {
const payload = context.payload;
commitExecArgs.push('--pr', payload.number.toString());
}
if (slug) {
commitExecArgs.push('--slug', `${slug}`);
commitExecArgs.push('--slug', slug);
}
if (failCi) {
commitExecArgs.push('-Z');
Expand All @@ -32434,10 +32439,10 @@ const buildGeneralExec = () => {
const verbose = isTrue(core.getInput('verbose'));
const args = [];
if (codecovYmlPath) {
args.push('--codecov-yml-path', `${codecovYmlPath}`);
args.push('--codecov-yml-path', codecovYmlPath);
}
if (url) {
args.push('--enterprise-url', `${url}`);
args.push('--enterprise-url', url);
}
if (verbose) {
args.push('-v');
Expand Down Expand Up @@ -32466,22 +32471,23 @@ const buildReportExec = () => buildExec_awaiter(void 0, void 0, void 0, function
if (token) {
reportOptions.env.CODECOV_TOKEN = token;
}
reportExecArgs.push('--git-service', `${gitService}`);
reportExecArgs.push('--git-service', gitService);
if (overrideCommit) {
reportExecArgs.push('-C', `${overrideCommit}`);
reportExecArgs.push('-C', overrideCommit);
}
else if (`${context.eventName}` == 'pull_request' ||
`${context.eventName}` == 'pull_request_target') {
reportExecArgs.push('-C', `${context.payload.pull_request.head.sha}`);
else if (['pull_request', 'pull_request_target'].includes(context.eventName)) {
const payload = context.payload;
reportExecArgs.push('-C', payload.pull_request.head.sha);
}
if (overridePr) {
reportExecArgs.push('-P', `${overridePr}`);
reportExecArgs.push('-P', overridePr);
}
else if (`${context.eventName}` == 'pull_request_target') {
reportExecArgs.push('-P', `${context.payload.number}`);
else if (context.eventName == 'pull_request_target') {
const payload = context.payload;
reportExecArgs.push('-P', payload.number.toString());
}
if (slug) {
reportExecArgs.push('--slug', `${slug}`);
reportExecArgs.push('--slug', slug);
}
if (failCi) {
reportExecArgs.push('-Z');
Expand Down Expand Up @@ -32559,83 +32565,94 @@ const buildUploadExec = () => buildExec_awaiter(void 0, void 0, void 0, function
uploadExecArgs.push('-e', envVarsArg.join(','));
}
if (exclude) {
uploadExecArgs.push('--exclude', `${exclude}`);
uploadExecArgs.push('--exclude', exclude);
}
if (failCi) {
uploadExecArgs.push('-Z');
}
if (file) {
uploadExecArgs.push('-f', `${file}`);
uploadExecArgs.push('-f', file);
}
if (files) {
files.split(',').map((f) => f.trim()).forEach((f) => {
if (f.length > 0) { // this handles trailing commas
uploadExecArgs.push('-f', `${f}`);
files
.split(',')
.map((f) => f.trim())
.forEach((f) => {
if (f.length > 0) {
// this handles trailing commas
uploadExecArgs.push('-f', f);
}
});
}
if (flags) {
flags.split(',').map((f) => f.trim()).forEach((f) => {
uploadExecArgs.push('-F', `${f}`);
flags
.split(',')
.map((f) => f.trim())
.forEach((f) => {
uploadExecArgs.push('-F', f);
});
}
uploadExecArgs.push('--git-service', `${gitService}`);
uploadExecArgs.push('--git-service', gitService);
if (handleNoReportsFound) {
uploadExecArgs.push('--handle-no-reports-found');
}
if (jobCode) {
uploadExecArgs.push('--job-code', `${jobCode}`);
uploadExecArgs.push('--job-code', jobCode);
}
if (name) {
uploadExecArgs.push('-n', `${name}`);
uploadExecArgs.push('-n', name);
}
if (networkFilter) {
uploadExecArgs.push('--network-filter', `${networkFilter}`);
uploadExecArgs.push('--network-filter', networkFilter);
}
if (networkPrefix) {
uploadExecArgs.push('--network-prefix', `${networkPrefix}`);
uploadExecArgs.push('--network-prefix', networkPrefix);
}
if (overrideBranch) {
uploadExecArgs.push('-B', `${overrideBranch}`);
uploadExecArgs.push('-B', overrideBranch);
}
if (overrideBuild) {
uploadExecArgs.push('-b', `${overrideBuild}`);
uploadExecArgs.push('-b', overrideBuild);
}
if (overrideBuildUrl) {
uploadExecArgs.push('--build-url', `${overrideBuildUrl}`);
uploadExecArgs.push('--build-url', overrideBuildUrl);
}
if (overrideCommit) {
uploadExecArgs.push('-C', `${overrideCommit}`);
uploadExecArgs.push('-C', overrideCommit);
}
else if (`${context.eventName}` == 'pull_request' ||
`${context.eventName}` == 'pull_request_target') {
uploadExecArgs.push('-C', `${context.payload.pull_request.head.sha}`);
else if (['pull_request', 'pull_request_target'].includes(context.eventName)) {
const payload = context.payload;
uploadExecArgs.push('-C', payload.pull_request.head.sha);
}
if (overridePr) {
uploadExecArgs.push('-P', `${overridePr}`);
uploadExecArgs.push('-P', overridePr);
}
else if (`${context.eventName}` == 'pull_request_target') {
uploadExecArgs.push('-P', `${context.payload.number}`);
else if (context.eventName == 'pull_request_target') {
const payload = context.payload;
uploadExecArgs.push('-P', payload.number.toString());
}
if (plugin) {
uploadExecArgs.push('--plugin', `${plugin}`);
uploadExecArgs.push('--plugin', plugin);
}
if (plugins) {
plugins.split(',').map((p) => p.trim()).forEach((p) => {
uploadExecArgs.push('--plugin', `${p}`);
plugins
.split(',')
.map((p) => p.trim())
.forEach((p) => {
uploadExecArgs.push('--plugin', p);
});
}
if (reportCode) {
uploadExecArgs.push('--report-code', `${reportCode}`);
uploadExecArgs.push('--report-code', reportCode);
}
if (rootDir) {
uploadExecArgs.push('--network-root-folder', `${rootDir}`);
uploadExecArgs.push('--network-root-folder', rootDir);
}
if (searchDir) {
uploadExecArgs.push('-s', `${searchDir}`);
uploadExecArgs.push('-s', searchDir);
}
if (slug) {
uploadExecArgs.push('-r', `${slug}`);
uploadExecArgs.push('-r', slug);
}
if (workingDir) {
uploadOptions.cwd = workingDir;
Expand Down
2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

65 changes: 62 additions & 3 deletions src/buildExec.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
buildGeneralExec,
buildReportExec,
buildUploadExec,
getToken,
} from './buildExec';

const context = github.context;
Expand Down Expand Up @@ -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',
];
Expand Down Expand Up @@ -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}`);
}
Expand All @@ -289,21 +296,73 @@ test('commit args using context', async () => {
});

test('commit args using github server url', async () => {
const expectedArgs :string[] = [
const expectedArgs: string[] = [
'--git-service',
'github_enterprise',
];

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('');
});
Loading

0 comments on commit 4b21c32

Please sign in to comment.