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 Aug 23, 2023
1 parent c17956f commit b9fd881
Show file tree
Hide file tree
Showing 5 changed files with 199 additions and 115 deletions.
127 changes: 80 additions & 47 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.

41 changes: 20 additions & 21 deletions src/buildExec.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import {version} from '../package.json';

const context = github.context;

test('no arguments', () => {
const {execArgs, failCi} = buildExec();
test('no arguments', async () => {
const opts = await buildExec();

const args = [
'-n',
Expand All @@ -24,12 +24,11 @@ test('no arguments', () => {
if (context.eventName == 'pull_request') {
args.push('-C', `${context.payload.pull_request.head.sha}`);
}

expect(execArgs).toEqual(args);
expect(failCi).toBeFalsy();
expect(opts.execArgs).toEqual(args);
expect(opts.failCi).toBeFalsy();
});

test('all arguments', () => {
test('all arguments', async () => {
const envs = {
'commit_parent': '83231650328f11695dfb754ca0f540516f188d27',
'directory': 'coverage/',
Expand Down Expand Up @@ -72,8 +71,8 @@ test('all arguments', () => {
process.env['INPUT_' + env.toUpperCase()] = envs[env];
}

const {execArgs, failCi} = buildExec();
expect(execArgs).toEqual([
const opts = await buildExec();
expect(opts.execArgs).toEqual([
'-n',
'codecov',
'-Q',
Expand Down Expand Up @@ -141,7 +140,7 @@ test('all arguments', () => {
'/test.xcresult',
'--some --other --args',
]);
expect(failCi).toBeTruthy();
expect(opts.failCi).toBeTruthy();

for (const env of Object.keys(envs)) {
delete process.env['INPUT_' + env.toUpperCase()];
Expand All @@ -156,16 +155,16 @@ describe('trim arguments after splitting them', () => {
expect.stringContaining('github-action'),
];

test('files', () => {
const envs = {files: './client-coverage.txt, ./lcov.info'};
test('files', async () => {
const envs = {'files': './client-coverage.txt, ./lcov.info'};

for (const [name, value] of Object.entries(envs)) {
process.env['INPUT_' + name.toUpperCase()] = value;
}

const {execArgs} = buildExec();
const opts = await buildExec();

expect(execArgs).toEqual(
expect(opts.execArgs).toEqual(
expect.arrayContaining([
...baseExpectation,
'-f',
Expand All @@ -180,16 +179,16 @@ describe('trim arguments after splitting them', () => {
}
});

test('flags', () => {
const envs = {flags: 'ios, mobile'};
test('flags', async () => {
const envs = {'flags': 'ios, mobile'};

for (const [name, value] of Object.entries(envs)) {
process.env['INPUT_' + name.toUpperCase()] = value;
}

const {execArgs} = buildExec();
const opts = await buildExec();

expect(execArgs).toEqual(
expect(opts.execArgs).toEqual(
expect.arrayContaining([
...baseExpectation,
'-F',
Expand All @@ -204,16 +203,16 @@ describe('trim arguments after splitting them', () => {
}
});

test('functionalities', () => {
const envs = {functionalities: 'network, gcov'};
test('functionalities', async () => {
const envs = {'functionalities': 'network, gcov'};

for (const [name, value] of Object.entries(envs)) {
process.env['INPUT_' + name.toUpperCase()] = value;
}

const {execArgs} = buildExec();
const opts = await buildExec();

expect(execArgs).toEqual(
expect(opts.execArgs).toEqual(
expect.arrayContaining([
...baseExpectation,
'-X',
Expand Down
50 changes: 44 additions & 6 deletions src/buildExec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,25 @@ const isTrue = (variable) => {
);
};

const buildExec = () => {
type env = {
[key: string]: string | undefined;
};

type opts = {
cwd?: string;
env?: env;
}

type ExecOptions = {
execArgs: string[];
options: opts;
failCi: boolean;
os: string;
uploaderVersion: string;
verbose: boolean;
};

const buildExec = async (): Promise<ExecOptions> => {
const clean = core.getInput('move_coverage_to_trash');
const commitParent = core.getInput('commit_parent');
const dryRun = isTrue(core.getInput('dry_run'));
Expand Down Expand Up @@ -46,9 +64,20 @@ const buildExec = () => {
const slug = core.getInput('slug');
const swift = core.getInput('swift');
const swiftProject = core.getInput('swift_project');
const token = core.getInput('token');
const upstream = core.getInput('upstream_proxy');
const url = core.getInput('url');
let token = core.getInput('token');
if (token === '') {
let codecovURL = url;
if (codecovURL === '') {
codecovURL = 'https://codecov.io';
}
try {
token = await core.getIDToken(codecovURL);
} catch (error) {
core.debug(`Got error while retrieving id token: ${error}`);
}
}
const upstream = core.getInput('upstream_proxy');
const verbose = isTrue(core.getInput('verbose'));
const workingDir = core.getInput('working-directory');
const xcode = core.getInput('xcode');
Expand All @@ -64,7 +93,7 @@ const buildExec = () => {
`github-action-${version}`,
);

const options:any = {};
const options:opts = {};
options.env = Object.assign(process.env, {
GITHUB_ACTION: process.env.GITHUB_ACTION,
GITHUB_RUN_ID: process.env.GITHUB_RUN_ID,
Expand All @@ -84,7 +113,7 @@ const buildExec = () => {
}

if (token) {
options.env.CODECOV_TOKEN = token;
options.env['CODECOV_TOKEN'] = token;
}
if (clean) {
execArgs.push('-c');
Expand Down Expand Up @@ -215,7 +244,16 @@ const buildExec = () => {
execArgs.push(`${xtraArgs}`);
}

return {execArgs, options, failCi, os, uploaderVersion, verbose};
const opts: ExecOptions = {
execArgs,
options,
failCi,
os,
uploaderVersion,
verbose,
};

return opts;
};

const buildCommitExec = () => {
Expand Down
94 changes: 54 additions & 40 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,49 +17,63 @@ import versionInfo from './version';

let failCi;

try {
const {execArgs, options, failCi, os, uploaderVersion, verbose} = buildExec();
const platform = getPlatform(os);
/**
* Main function of the codecov-action
*/
async function run(): Promise<void> {
try {
const opts = await buildExec();
const platform = getPlatform(opts.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, opts.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,
opts.uploaderVersion,
opts.verbose,
failCi);
await versionInfo(platform, opts.uploaderVersion);
await fs.chmodSync(filename, '777');

const unlink = () => {
fs.unlink(filename, (err) => {
if (err) {
setFailure(
`Codecov: Could not unlink uploader: ${err.message}`,
failCi,
);
}
});
};
await exec.exec(filename, execArgs, options)
.catch((err) => {
setFailure(
`Codecov: Failed to properly upload: ${err.message}`,
failCi,
);
}).then(() => {
unlink();
const unlink = () => {
fs.unlink(filename, (err) => {
if (err) {
setFailure(
`Codecov: Could not unlink uploader: ${err.message}`,
failCi,
);
}
});
});
});
} catch (err) {
setFailure(`Codecov: Encountered an unexpected error ${err.message}`, failCi);
};
await exec.exec(filename, opts.execArgs, opts.options)
.catch((err) => {
setFailure(
`Codecov: Failed to properly upload: ${err.message}`,
failCi,
);
}).then(() => {
unlink();
});
});
});
} catch (err) {
setFailure(
`Codecov: Encountered an unexpected error ${err.message}`,
failCi);
}
}

run();

0 comments on commit b9fd881

Please sign in to comment.