Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions src/extract.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* eslint-disable @typescript-eslint/naming-convention */
import { promises as fs } from 'fs';
import * as github from '@actions/github';
import { cmd } from './git';
import { Config, ToolType } from './config';
import { z } from 'zod';

Expand Down Expand Up @@ -34,7 +35,7 @@
}

interface PullRequest {
[key: string]: any;

Check warning on line 38 in src/extract.ts

View workflow job for this annotation

GitHub Actions / Run linting and formatting check

Unexpected any. Specify a different type
number: number;
html_url?: string;
body?: string;
Expand Down Expand Up @@ -239,21 +240,24 @@
}
}

function getCommitFromPullRequestPayload(pr: PullRequest): Commit {
// On pull_request hook, head_commit is not available
async function getCommitFromPullRequestPayload(pr: PullRequest): Promise<Commit> {
// On pull_request hook, head_commit is not available.
// HEAD is the merge commit GitHub creates for the PR, always accessible even with shallow clones.
// Its timestamp is within seconds of the actual commit time.
const id: string = pr.head.sha;
const username: string = pr.head.user.login;
const user = {
name: username, // XXX: Fallback, not correct
username,
};
const timestamp = (await cmd([], 'log', '-1', '--format=%aI', 'HEAD')).trim();

return {
author: user,
committer: user,
id,
message: pr.title,
timestamp: pr.head.repo.updated_at,
timestamp,
url: `${pr.html_url}/commits/${id}`,
};
}
Expand Down Expand Up @@ -299,7 +303,7 @@
const pr = github.context.payload.pull_request;

if (pr) {
return getCommitFromPullRequestPayload(pr);
return await getCommitFromPullRequestPayload(pr);
}

if (!githubToken) {
Expand Down Expand Up @@ -465,7 +469,7 @@
const extra = `mean: ${mean} ${meanUnit}\nrounds: ${stats.rounds}`;
return { name, value, unit, range, extra };
});
} catch (err: any) {

Check warning on line 472 in src/extract.ts

View workflow job for this annotation

GitHub Actions / Run linting and formatting check

Unexpected any. Specify a different type
throw new Error(
`Output file for 'pytest' must be JSON file generated by --benchmark-json option: ${err.message}`,
);
Expand All @@ -476,7 +480,7 @@
let json: GoogleCppBenchmarkJson;
try {
json = JSON.parse(output);
} catch (err: any) {

Check warning on line 483 in src/extract.ts

View workflow job for this annotation

GitHub Actions / Run linting and formatting check

Unexpected any. Specify a different type
throw new Error(
`Output file for 'googlecpp' must be JSON file generated by --benchmark_format=json option: ${err.message}`,
);
Expand Down Expand Up @@ -604,7 +608,7 @@
return ret;
}

function extractJuliaBenchmarkHelper([_, bench]: JuliaBenchmarkGroup, labels: string[] = []): BenchmarkResult[] {

Check warning on line 611 in src/extract.ts

View workflow job for this annotation

GitHub Actions / Run linting and formatting check

'_' is defined but never used
const res: BenchmarkResult[] = [];
for (const key in bench.data) {
const value = bench.data[key];
Expand Down Expand Up @@ -635,7 +639,7 @@
let json: JuliaBenchmarkJson;
try {
json = JSON.parse(output);
} catch (err: any) {

Check warning on line 642 in src/extract.ts

View workflow job for this annotation

GitHub Actions / Run linting and formatting check

Unexpected any. Specify a different type
throw new Error(
`Output file for 'julia' must be JSON file generated by BenchmarkTools.save("output.json", suit::BenchmarkGroup) : ${err.message}`,
);
Expand All @@ -653,7 +657,7 @@
let json: JmhBenchmarkJson[];
try {
json = JSON.parse(output);
} catch (err: any) {

Check warning on line 660 in src/extract.ts

View workflow job for this annotation

GitHub Actions / Run linting and formatting check

Unexpected any. Specify a different type
throw new Error(`Output file for 'jmh' must be JSON file generated by -rf json option: ${err.message}`);
}
return json.map((b) => {
Expand All @@ -670,7 +674,7 @@
let json: BenchmarkDotNetBenchmarkJson;
try {
json = JSON.parse(output);
} catch (err: any) {

Check warning on line 677 in src/extract.ts

View workflow job for this annotation

GitHub Actions / Run linting and formatting check

Unexpected any. Specify a different type
throw new Error(
`Output file for 'benchmarkdotnet' must be JSON file generated by '--exporters json' option or by adding the JsonExporter to your run config: ${err.message}`,
);
Expand Down
10 changes: 6 additions & 4 deletions test/extract.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ const dummyGitHubContext = {
ref: 'abcd1234',
};

jest.mock('../src/git', () => ({
cmd: jest.fn().mockResolvedValue('2024-01-15T10:30:00+00:00\n'),
}));

jest.mock('@actions/github', () => ({
get context() {
return dummyGitHubContext;
Expand Down Expand Up @@ -218,9 +222,7 @@ describe('extractResult()', function () {
user: {
login: 'user',
},
repo: {
updated_at: 'repo updated at timestamp',
},
repo: {},
},
},
};
Expand All @@ -238,7 +240,7 @@ describe('extractResult()', function () {
A.deepEqual(commit.committer, expectedUser);
A.equal(commit.id, 'abcdef0123456789');
A.equal(commit.message, 'this is title');
A.equal(commit.timestamp, 'repo updated at timestamp');
A.equal(commit.timestamp, '2024-01-15T10:30:00+00:00');
A.equal(commit.url, 'https://github.com/dummy/repo/pull/1/commits/abcdef0123456789');
});

Expand Down
Loading