Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit b9fd881

Browse files
committedAug 23, 2023·
fetch id token from github
1 parent c17956f commit b9fd881

File tree

5 files changed

+199
-115
lines changed

5 files changed

+199
-115
lines changed
 

‎dist/index.js

+80-47
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎dist/index.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/buildExec.test.ts

+20-21
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ import {version} from '../package.json';
1212

1313
const context = github.context;
1414

15-
test('no arguments', () => {
16-
const {execArgs, failCi} = buildExec();
15+
test('no arguments', async () => {
16+
const opts = await buildExec();
1717

1818
const args = [
1919
'-n',
@@ -24,12 +24,11 @@ test('no arguments', () => {
2424
if (context.eventName == 'pull_request') {
2525
args.push('-C', `${context.payload.pull_request.head.sha}`);
2626
}
27-
28-
expect(execArgs).toEqual(args);
29-
expect(failCi).toBeFalsy();
27+
expect(opts.execArgs).toEqual(args);
28+
expect(opts.failCi).toBeFalsy();
3029
});
3130

32-
test('all arguments', () => {
31+
test('all arguments', async () => {
3332
const envs = {
3433
'commit_parent': '83231650328f11695dfb754ca0f540516f188d27',
3534
'directory': 'coverage/',
@@ -72,8 +71,8 @@ test('all arguments', () => {
7271
process.env['INPUT_' + env.toUpperCase()] = envs[env];
7372
}
7473

75-
const {execArgs, failCi} = buildExec();
76-
expect(execArgs).toEqual([
74+
const opts = await buildExec();
75+
expect(opts.execArgs).toEqual([
7776
'-n',
7877
'codecov',
7978
'-Q',
@@ -141,7 +140,7 @@ test('all arguments', () => {
141140
'/test.xcresult',
142141
'--some --other --args',
143142
]);
144-
expect(failCi).toBeTruthy();
143+
expect(opts.failCi).toBeTruthy();
145144

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

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

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

166-
const {execArgs} = buildExec();
165+
const opts = await buildExec();
167166

168-
expect(execArgs).toEqual(
167+
expect(opts.execArgs).toEqual(
169168
expect.arrayContaining([
170169
...baseExpectation,
171170
'-f',
@@ -180,16 +179,16 @@ describe('trim arguments after splitting them', () => {
180179
}
181180
});
182181

183-
test('flags', () => {
184-
const envs = {flags: 'ios, mobile'};
182+
test('flags', async () => {
183+
const envs = {'flags': 'ios, mobile'};
185184

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

190-
const {execArgs} = buildExec();
189+
const opts = await buildExec();
191190

192-
expect(execArgs).toEqual(
191+
expect(opts.execArgs).toEqual(
193192
expect.arrayContaining([
194193
...baseExpectation,
195194
'-F',
@@ -204,16 +203,16 @@ describe('trim arguments after splitting them', () => {
204203
}
205204
});
206205

207-
test('functionalities', () => {
208-
const envs = {functionalities: 'network, gcov'};
206+
test('functionalities', async () => {
207+
const envs = {'functionalities': 'network, gcov'};
209208

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

214-
const {execArgs} = buildExec();
213+
const opts = await buildExec();
215214

216-
expect(execArgs).toEqual(
215+
expect(opts.execArgs).toEqual(
217216
expect.arrayContaining([
218217
...baseExpectation,
219218
'-X',

‎src/buildExec.ts

+44-6
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,25 @@ const isTrue = (variable) => {
1616
);
1717
};
1818

19-
const buildExec = () => {
19+
type env = {
20+
[key: string]: string | undefined;
21+
};
22+
23+
type opts = {
24+
cwd?: string;
25+
env?: env;
26+
}
27+
28+
type ExecOptions = {
29+
execArgs: string[];
30+
options: opts;
31+
failCi: boolean;
32+
os: string;
33+
uploaderVersion: string;
34+
verbose: boolean;
35+
};
36+
37+
const buildExec = async (): Promise<ExecOptions> => {
2038
const clean = core.getInput('move_coverage_to_trash');
2139
const commitParent = core.getInput('commit_parent');
2240
const dryRun = isTrue(core.getInput('dry_run'));
@@ -46,9 +64,20 @@ const buildExec = () => {
4664
const slug = core.getInput('slug');
4765
const swift = core.getInput('swift');
4866
const swiftProject = core.getInput('swift_project');
49-
const token = core.getInput('token');
50-
const upstream = core.getInput('upstream_proxy');
5167
const url = core.getInput('url');
68+
let token = core.getInput('token');
69+
if (token === '') {
70+
let codecovURL = url;
71+
if (codecovURL === '') {
72+
codecovURL = 'https://codecov.io';
73+
}
74+
try {
75+
token = await core.getIDToken(codecovURL);
76+
} catch (error) {
77+
core.debug(`Got error while retrieving id token: ${error}`);
78+
}
79+
}
80+
const upstream = core.getInput('upstream_proxy');
5281
const verbose = isTrue(core.getInput('verbose'));
5382
const workingDir = core.getInput('working-directory');
5483
const xcode = core.getInput('xcode');
@@ -64,7 +93,7 @@ const buildExec = () => {
6493
`github-action-${version}`,
6594
);
6695

67-
const options:any = {};
96+
const options:opts = {};
6897
options.env = Object.assign(process.env, {
6998
GITHUB_ACTION: process.env.GITHUB_ACTION,
7099
GITHUB_RUN_ID: process.env.GITHUB_RUN_ID,
@@ -84,7 +113,7 @@ const buildExec = () => {
84113
}
85114

86115
if (token) {
87-
options.env.CODECOV_TOKEN = token;
116+
options.env['CODECOV_TOKEN'] = token;
88117
}
89118
if (clean) {
90119
execArgs.push('-c');
@@ -215,7 +244,16 @@ const buildExec = () => {
215244
execArgs.push(`${xtraArgs}`);
216245
}
217246

218-
return {execArgs, options, failCi, os, uploaderVersion, verbose};
247+
const opts: ExecOptions = {
248+
execArgs,
249+
options,
250+
failCi,
251+
os,
252+
uploaderVersion,
253+
verbose,
254+
};
255+
256+
return opts;
219257
};
220258

221259
const buildCommitExec = () => {

‎src/index.ts

+54-40
Original file line numberDiff line numberDiff line change
@@ -17,49 +17,63 @@ import versionInfo from './version';
1717

1818
let failCi;
1919

20-
try {
21-
const {execArgs, options, failCi, os, uploaderVersion, verbose} = buildExec();
22-
const platform = getPlatform(os);
20+
/**
21+
* Main function of the codecov-action
22+
*/
23+
async function run(): Promise<void> {
24+
try {
25+
const opts = await buildExec();
26+
const platform = getPlatform(opts.os);
2327

24-
const filename = path.join( __dirname, getUploaderName(platform));
25-
https.get(getBaseUrl(platform, uploaderVersion), (res) => {
28+
const filename = path.join( __dirname, getUploaderName(platform));
29+
https.get(getBaseUrl(platform, opts.uploaderVersion), (res) => {
2630
// Image will be stored at this path
27-
const filePath = fs.createWriteStream(filename);
28-
res.pipe(filePath);
29-
filePath
30-
.on('error', (err) => {
31-
setFailure(
32-
`Codecov: Failed to write uploader binary: ${err.message}`,
33-
true,
34-
);
35-
}).on('finish', async () => {
36-
filePath.close();
31+
const filePath = fs.createWriteStream(filename);
32+
res.pipe(filePath);
33+
filePath
34+
.on('error', (err) => {
35+
setFailure(
36+
`Codecov: Failed to write uploader binary: ${err.message}`,
37+
true,
38+
);
39+
}).on('finish', async () => {
40+
filePath.close();
3741

38-
await verify(filename, platform, uploaderVersion, verbose, failCi);
39-
await versionInfo(platform, uploaderVersion);
40-
await fs.chmodSync(filename, '777');
42+
await verify(
43+
filename,
44+
platform,
45+
opts.uploaderVersion,
46+
opts.verbose,
47+
failCi);
48+
await versionInfo(platform, opts.uploaderVersion);
49+
await fs.chmodSync(filename, '777');
4150

42-
const unlink = () => {
43-
fs.unlink(filename, (err) => {
44-
if (err) {
45-
setFailure(
46-
`Codecov: Could not unlink uploader: ${err.message}`,
47-
failCi,
48-
);
49-
}
50-
});
51-
};
52-
await exec.exec(filename, execArgs, options)
53-
.catch((err) => {
54-
setFailure(
55-
`Codecov: Failed to properly upload: ${err.message}`,
56-
failCi,
57-
);
58-
}).then(() => {
59-
unlink();
51+
const unlink = () => {
52+
fs.unlink(filename, (err) => {
53+
if (err) {
54+
setFailure(
55+
`Codecov: Could not unlink uploader: ${err.message}`,
56+
failCi,
57+
);
58+
}
6059
});
61-
});
62-
});
63-
} catch (err) {
64-
setFailure(`Codecov: Encountered an unexpected error ${err.message}`, failCi);
60+
};
61+
await exec.exec(filename, opts.execArgs, opts.options)
62+
.catch((err) => {
63+
setFailure(
64+
`Codecov: Failed to properly upload: ${err.message}`,
65+
failCi,
66+
);
67+
}).then(() => {
68+
unlink();
69+
});
70+
});
71+
});
72+
} catch (err) {
73+
setFailure(
74+
`Codecov: Encountered an unexpected error ${err.message}`,
75+
failCi);
76+
}
6577
}
78+
79+
run();

0 commit comments

Comments
 (0)
Please sign in to comment.