Skip to content

Commit 3ea2b4a

Browse files
committed
chore: switch codecov action to upload script
1 parent f08e6cc commit 3ea2b4a

File tree

2 files changed

+42
-40
lines changed

2 files changed

+42
-40
lines changed

.github/workflows/test.yml

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -285,9 +285,11 @@ jobs:
285285
fi
286286
- name: Merge coverage (Delta)
287287
run: npm run test-merge-coverage:ci:affected
288-
- name: Report Coverage
289-
uses: codecov/codecov-action@v5
290-
env:
291-
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
292-
with:
293-
verbose: true
288+
- name: Report Coverage with Flags
289+
run: node ./scripts/codecov-upload-flags.mjs
290+
# - name: Report Coverage
291+
# uses: codecov/codecov-action@v5
292+
# env:
293+
# CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
294+
# with:
295+
# verbose: true

scripts/codecov-upload-flags.mjs

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,48 @@
11
import { execSync } from 'child_process';
22
import { globSync } from 'glob';
3-
import { chmod, chmodSync, createWriteStream, existsSync, readFileSync } from 'fs';
3+
import { chmodSync, existsSync, readFileSync } from 'fs';
44
import path from 'path';
5-
import os from 'os';
6-
import { Readable } from 'stream';
75

6+
const ROOT_DIR = process.cwd();
87
const readPkg = (dir) => JSON.parse(readFileSync(path.join(dir, 'package.json'), 'utf8'));
9-
const download = async (url, dst) => {
10-
const resp = await fetch(url);
11-
return new Promise((res, rej) => {
12-
if (resp.ok && resp.body) {
13-
console.log("Writing to file:", dst);
14-
let writer = createWriteStream(dst);
15-
Readable.fromWeb(resp.body).pipe(writer);
16-
writer.on('finish', res);
17-
writer.on('error', rej);
18-
} else {
19-
rej(new Error('Could not get body from request'));
20-
}
21-
});
22-
};
8+
const execCmd = (cmd, opts = {}) => execSync(cmd, {cwd: ROOT_DIR, encoding: 'utf-8', stdio: 'inherit', ...opts})
239

24-
const TOP = process.cwd();
25-
const pkgInfo = readPkg(TOP);
10+
const commitSha = execCmd('git rev-parse HEAD', {stdio: 'pipe'}).replace('\n', '');
11+
const branchName = execCmd('git rev-parse --abbrev-ref HEAD', {stdio: 'pipe'}).replace('\n', '');
12+
const pkgInfo = readPkg(ROOT_DIR);
2613
const pkgFiles = pkgInfo.workspaces.map((exp) => globSync(path.join(exp, 'package.json')));
27-
const codecovPath = path.resolve(TOP, 'codecov');
14+
const codecovPath = path.resolve(ROOT_DIR, 'codecov');
2815
const pkgsWithFlag = pkgFiles.flat().map((f) => {
2916
const path = f.replace('package.json', '');
3017
const info = readPkg(path);
3118
const name = info.name;
3219
const flag = name.replace('@opentelemetry/', '');
3320
const report = path + 'coverage/coverage-final.json';
34-
const command = `./codecov do-upload -t <token> -f ${report} --disable-search -F ${flag} -d`;
21+
// NOTE: command extracted fromt the codecov action. You can see an example in
22+
// https://github.com/open-telemetry/opentelemetry-js-contrib/actions/runs/17320649481/job/49176411722?pr=2866
23+
//
24+
// Example:
25+
// ./codecov --verbose upload-coverage --git-service github --sha f08e6cceec6f39d61b1a9c35aed2e53b54a55d36 --branch david-luna:dluna-ci-pr-speed-and-coverage --gcov-executable gcov
26+
const command = [
27+
'./codecov --verbose',
28+
'upload-coverage',
29+
'--git-service github',
30+
'--gcov-executable gcov',
31+
'--sha', commitSha,
32+
'--branch', branchName,
33+
'--dry-run',
34+
].join(' ');
3535
return { name, flag, len: flag.length, path, report, command };
3636
});
3737

3838
// Download codecov
39+
const baseUrl = 'https://cli.codecov.io/latest/';
3940
const urlMap = {
40-
linux: 'https://cli.codecov.io/latest/linux/codecov',
41-
darwin: 'https://cli.codecov.io/latest/macos/codecov',
41+
linux: `${baseUrl}linux/codecov`,
42+
darwin: `${baseUrl}macos/codecov`,
4243
};
44+
4345
const url = urlMap[process.platform];
44-
const token = process.argv[2];
45-
// Validations
46-
if (typeof token !== 'string') {
47-
console.log('Token is missing. Usage:');
48-
console.log('node ./scripts/codecov-upload-flags.mjs my-codecov-token');
49-
process.exit(-1);
50-
}
5146
if (!url) {
5247
console.log(`No codecov binary available for platform "${process.platform}"`);
5348
console.log(`Available platforms are "${Object.keys(urlMap)}"`);
@@ -59,18 +54,23 @@ if (existsSync(codecovPath)) {
5954
console.log(`Codecov binary found.`);
6055
} else {
6156
console.log(`Codecov binary missing. Downloading from ${url}`);
62-
await download(url, codecovPath);
63-
console.log(`Codecov binary downloaded to ${codecovPath}`);
57+
execCmd(`curl -O "${url}"`);
58+
console.log(`Verifying codecov binary downloaded to ${codecovPath}`);
59+
execCmd(`echo "$(curl -s https://keybase.io/codecovsecurity/pgp_keys.asc)" | gpg --no-default-keyring --import`);
60+
execCmd(`curl -O "${url}.SHA256SUM"`);
61+
execCmd(`curl -O "${url}.SHA256SUM.sig"`);
62+
execCmd(`gpg --verify "${codecovPath}.SHA256SUM.sig" "${codecovPath}.SHA256SUM"`);
6463
}
6564
// make sure we have exec perms
6665
chmodSync(codecovPath, 0o555);
6766

6867
// Compute the commands to run
6968
for (const pkg of pkgsWithFlag) {
7069
if (existsSync(pkg.report)) {
71-
const command = pkg.command.replace('<token>', token)
7270
console.log(`Uploading report of ${pkg.name} with flag ${pkg.flag}`);
73-
execSync(command, {cwd: TOP, encoding: 'utf-8'});
71+
const command = pkg.command.replace('<sha>', 'Oxffff').replace('<branch>', 'my-branch');
72+
// execCmd(command, {cwd: TOP, encoding: 'utf-8'});
73+
console.log(command)
7474
} else {
7575
console.log(`Report of ${pkg.name} not found. Expected existence of ${pkg.report}`);
7676
}

0 commit comments

Comments
 (0)