mirror of
https://github.com/codecov/codecov-action.git
synced 2026-02-10 04:57:24 +00:00
fetch id token from github
This commit is contained in:
parent
525bbfffeb
commit
8049f50179
7 changed files with 240 additions and 169 deletions
|
|
@ -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
|
||||
|
|
|
|||
181
dist/index.js
vendored
181
dist/index.js
vendored
File diff suppressed because one or more lines are too long
2
dist/index.js.map
vendored
2
dist/index.js.map
vendored
File diff suppressed because one or more lines are too long
|
|
@ -124,7 +124,7 @@ test('upload args', () => {
|
|||
});
|
||||
|
||||
|
||||
test('report args', () => {
|
||||
test('report args', async () => {
|
||||
const envs = {
|
||||
override_commit: '9caabca5474b49de74ef5667deabaf74cdacc244',
|
||||
slug: 'fakeOwner/fakeRepo',
|
||||
|
|
@ -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([
|
||||
|
|
@ -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',
|
||||
};
|
||||
|
|
@ -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');
|
||||
|
|
|
|||
|
|
@ -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 = [];
|
||||
|
|
@ -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;
|
||||
}
|
||||
|
|
@ -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 = [];
|
||||
|
||||
|
|
@ -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;
|
||||
}
|
||||
|
|
@ -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'));
|
||||
|
|
@ -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');
|
||||
|
|
@ -175,6 +186,8 @@ const buildUploadExec = () => {
|
|||
`${name}`,
|
||||
);
|
||||
}
|
||||
|
||||
const token = await fetchToken();
|
||||
if (token) {
|
||||
uploadOptions.env.CODECOV_TOKEN = token;
|
||||
}
|
||||
|
|
|
|||
171
src/index.ts
171
src/index.ts
|
|
@ -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();
|
||||
|
|
|
|||
|
|
@ -6,17 +6,17 @@ const versionInfo = async (
|
|||
version?: string,
|
||||
): Promise<void> => {
|
||||
if (version) {
|
||||
core.info(`==> Running version ${version}`);
|
||||
core.info(`==> Using override version ${version}`);
|
||||
}
|
||||
|
||||
try {
|
||||
const metadataRes = await fetch.default( `https://uploader.codecov.io/${platform}/latest`, {
|
||||
const metadataRes = await fetch.default( `https://uploader.codecov.io/${platform}/${version}`, {
|
||||
headers: {'Accept': 'application/json'},
|
||||
});
|
||||
const metadata = await metadataRes.json();
|
||||
core.info(`==> Running version ${metadata['version']}`);
|
||||
} catch (err) {
|
||||
core.info(`Could not pull latest version information: ${err}`);
|
||||
core.info(`Could not pull ${version} version information: ${err}`);
|
||||
}
|
||||
};
|
||||
export default versionInfo;
|
||||
|
|
|
|||
Loading…
Reference in a new issue