Skip to content

Commit

Permalink
fetch id token from github
Browse files Browse the repository at this point in the history
  • Loading branch information
zetaab committed Oct 20, 2023
1 parent 525bbff commit 9756dc8
Show file tree
Hide file tree
Showing 6 changed files with 234 additions and 163 deletions.
6 changes: 6 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ inputs:
token:
description: 'Repository upload token - get it from codecov.io. Required only for private repositories'
required: false
use_oidc:
description: 'Uses Github OIDC to provision upload token.'
required: false
url:
description: 'URL to codecov installation. Defaults to https://codecov.io'
required: false
file:
description: 'Path to coverage file to upload'
required: false
Expand Down
175 changes: 105 additions & 70 deletions dist/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions src/buildExec.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ test('upload args', () => {
});


test('report args', () => {
test('report args', async () => {
const envs = {
override_commit: '9caabca5474b49de74ef5667deabaf74cdacc244',
slug: 'fakeOwner/fakeRepo',
Expand All @@ -134,7 +134,7 @@ test('report args', () => {
process.env['INPUT_' + env.toUpperCase()] = envs[env];
}

const {reportExecArgs, reportCommand} = buildReportExec();
const {reportExecArgs, reportCommand} = await buildReportExec();

expect(reportExecArgs).toEqual(
expect.arrayContaining([
Expand All @@ -150,7 +150,7 @@ test('report args', () => {
});


test('report args using context', () => {
test('report args using context', async () => {
const envs = {
token: 'd3859757-ab80-4664-924d-aef22fa7557b',
};
Expand All @@ -162,7 +162,7 @@ test('report args using context', () => {
expectedArgs.push('-C', `${context.payload.pull_request.head.sha}`);
}

const {reportExecArgs, reportCommand} = buildReportExec();
const {reportExecArgs, reportCommand} = await buildReportExec();

expect(reportExecArgs).toEqual(expectedArgs);
expect(reportCommand).toEqual('create-report');
Expand Down
35 changes: 24 additions & 11 deletions src/buildExec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,12 @@ const isTrue = (variable) => {
};


const buildCommitExec = () => {
const buildCommitExec = async () => {
const commitParent = core.getInput('commit_parent');
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 = core.getInput('token');


const commitCommand = 'create-commit';
const commitExecArgs = [];
Expand All @@ -40,7 +38,7 @@ const buildCommitExec = () => {
GITHUB_HEAD_REF: process.env.GITHUB_HEAD_REF || '',
});


const token = await fetchToken();
if (token) {
commitOptions.env.CODECOV_TOKEN = token;
}
Expand Down Expand Up @@ -88,12 +86,9 @@ const buildGeneralExec = () => {
return {args, verbose};
};

const buildReportExec = () => {
const buildReportExec = async () => {
const overrideCommit = core.getInput('override_commit');
const slug = core.getInput('slug');
const token = core.getInput('token');


const reportCommand = 'create-report';
const reportExecArgs = [];

Expand All @@ -107,7 +102,7 @@ const buildReportExec = () => {
GITHUB_HEAD_REF: process.env.GITHUB_HEAD_REF || '',
});


const token = await fetchToken();
if (token) {
reportOptions.env.CODECOV_TOKEN = token;
}
Expand All @@ -126,7 +121,24 @@ const buildReportExec = () => {
return {reportExecArgs, reportOptions, reportCommand};
};

const buildUploadExec = () => {
const fetchToken = async (): Promise<string> => {
let token = core.getInput('token');
const useOIDC = isTrue(core.getInput('use_oidc'));
if (useOIDC) {
let codecovURL = core.getInput('url');
if (codecovURL === '') {
codecovURL = 'https://codecov.io';
}
try {
token = await core.getIDToken(codecovURL);
} catch (error) {
core.debug(`Got error while retrieving id token: ${error}`);
}
}
return token;
};

const buildUploadExec = async () => {
const envVars = core.getInput('env_vars');
const dryRun = isTrue(core.getInput('dry_run'));
const failCi = isTrue(core.getInput('fail_ci_if_error'));
Expand All @@ -143,7 +155,6 @@ const buildUploadExec = () => {
const rootDir = core.getInput('root_dir');
const searchDir = core.getInput('directory');
const slug = core.getInput('slug');
const token = core.getInput('token');
let uploaderVersion = core.getInput('version');
const workingDir = core.getInput('working-directory');
const plugin = core.getInput('plugin');
Expand Down Expand Up @@ -175,6 +186,8 @@ const buildUploadExec = () => {
`${name}`,
);
}

const token = await fetchToken();
if (token) {
uploadOptions.env.CODECOV_TOKEN = token;
}
Expand Down
171 changes: 94 additions & 77 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,98 +23,115 @@ import versionInfo from './version';

let failCi;

try {
const {commitExecArgs, commitOptions, commitCommand} = buildCommitExec();
const {reportExecArgs, reportOptions, reportCommand} = buildReportExec();
const {
uploadExecArgs,
uploadOptions,
failCi,
os,
uploaderVersion,
uploadCommand,
} = buildUploadExec();
const {args, verbose} = buildGeneralExec();
/**
* Main function of the codecov-action
*/
async function run(): Promise<void> {
try {
const {
commitExecArgs,
commitOptions,
commitCommand,
} = await buildCommitExec();
const {
reportExecArgs,
reportOptions,
reportCommand,
} = await buildReportExec();
const {
uploadExecArgs,
uploadOptions,
failCi,
os,
uploaderVersion,
uploadCommand,
} = await buildUploadExec();
const {args, verbose} = buildGeneralExec();

const platform = getPlatform(os);
const platform = getPlatform(os);

const filename = path.join( __dirname, getUploaderName(platform));
https.get(getBaseUrl(platform, uploaderVersion), (res) => {
const filename = path.join( __dirname, getUploaderName(platform));
https.get(getBaseUrl(platform, uploaderVersion), (res) => {
// Image will be stored at this path
const filePath = fs.createWriteStream(filename);
res.pipe(filePath);
filePath
.on('error', (err) => {
setFailure(
`Codecov: Failed to write uploader binary: ${err.message}`,
true,
);
}).on('finish', async () => {
filePath.close();
const filePath = fs.createWriteStream(filename);
res.pipe(filePath);
filePath
.on('error', (err) => {
setFailure(
`Codecov: Failed to write uploader binary: ${err.message}`,
true,
);
}).on('finish', async () => {
filePath.close();

await verify(filename, platform, uploaderVersion, verbose, failCi);
await versionInfo(platform, uploaderVersion);
await fs.chmodSync(filename, '777');
await verify(filename, platform, uploaderVersion, verbose, failCi);
await versionInfo(platform, uploaderVersion);
await fs.chmodSync(filename, '777');

const unlink = () => {
fs.unlink(filename, (err) => {
if (err) {
setFailure(
`Codecov: Could not unlink uploader: ${err.message}`,
failCi,
);
}
});
};
const doUpload = async () => {
await exec.exec(getCommand(filename, args, uploadCommand).join(' '),
uploadExecArgs,
uploadOptions)
.catch((err) => {
const unlink = () => {
fs.unlink(filename, (err) => {
if (err) {
setFailure(
`Codecov:
Failed to properly upload report: ${err.message}`,
`Codecov: Could not unlink uploader: ${err.message}`,
failCi,
);
});
};
const createReport = async () => {
}
});
};
const doUpload = async () => {
await exec.exec(
getCommand(filename, args, uploadCommand).join(' '),
uploadExecArgs,
uploadOptions)
.catch((err) => {
setFailure(
`Codecov:
Failed to properly upload report: ${err.message}`,
failCi,
);
});
};
const createReport = async () => {
await exec.exec(
getCommand(filename, args, reportCommand).join(' '),
reportExecArgs,
reportOptions)
.then(async (exitCode) => {
if (exitCode == 0) {
await doUpload();
}
}).catch((err) => {
setFailure(
`Codecov:
Failed to properly create report: ${err.message}`,
failCi,
);
});
};
await exec.exec(
getCommand(filename, args, reportCommand).join(' '),
reportExecArgs,
reportOptions)
getCommand(
filename,
args,
commitCommand,
).join(' '),
commitExecArgs, commitOptions)
.then(async (exitCode) => {
if (exitCode == 0) {
await doUpload();
await createReport();
}
unlink();
}).catch((err) => {
setFailure(
`Codecov:
Failed to properly create report: ${err.message}`,
`Codecov: Failed to properly create commit:
${err.message}`,
failCi,
);
});
};
await exec.exec(
getCommand(
filename,
args,
commitCommand,
).join(' '),
commitExecArgs, commitOptions)
.then(async (exitCode) => {
if (exitCode == 0) {
await createReport();
}
unlink();
}).catch((err) => {
setFailure(
`Codecov: Failed to properly create commit: ${err.message}`,
failCi,
);
});
});
});
} catch (err) {
setFailure(`Codecov: Encountered an unexpected error ${err.message}`, failCi);
});
});
} catch (err) {
setFailure(`Codecov: Encountered an unexpected error ${err.message}`,
failCi);
}
}
run();

0 comments on commit 9756dc8

Please sign in to comment.