Skip to content

Commit dd836ac

Browse files
committed
Add optional commit SHA in config.
1 parent ab49d1a commit dd836ac

File tree

7 files changed

+39
-9
lines changed

7 files changed

+39
-9
lines changed

README.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ context) properties. Like this:
7272
"unit": "Megabytes",
7373
"value": 100,
7474
"range": "3",
75-
"extra": "Value for Tooltip: 25\nOptional Num #2: 100\nAnything Else!",
75+
"extra": "Value for Tooltip: 25\nOptional Num #2: 100\nAnything Else!"
7676
}
7777
]
7878
```
@@ -469,9 +469,16 @@ with `actions/cache` action. Please read 'Minimal setup' section above.
469469

470470
Max number of data points in a chart for avoiding too busy chart. This value must be unsigned integer
471471
larger than zero. If the number of benchmark results for some benchmark suite exceeds this value,
472-
the oldest one will be removed before storing the results to file. By default this value is empty
472+
the oldest one will be removed before storing the results to file. By default, this value is empty
473473
which means there is no limit.
474474

475+
#### `commit-sha` (Optional)
476+
477+
- Type: String
478+
- Default: N/A
479+
480+
The commit SHA to tag the benchmark with. Uses this in place of the payload if provided.
481+
475482

476483
### Action outputs
477484

action.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ inputs:
6969
max-items-in-chart:
7070
description: 'Max data points in a benchmark chart to avoid making the chart too busy. Value must be unsigned integer. No limit by default'
7171
required: false
72+
commit-sha:
73+
description: 'The commit SHA to tag the benchmark with. If not provided, uses GitHub context.'
74+
required: false
7275

7376
runs:
7477
using: 'node12'

src/config.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export interface Config {
3131
alertCommentCcUsers: string[];
3232
externalDataJsonPath: string | undefined;
3333
maxItemsInChart: number | null;
34+
commitSha: string | undefined;
3435
}
3536

3637
export const VALID_TOOLS: ToolType[] = [
@@ -240,6 +241,7 @@ export async function configFromJobInput(): Promise<Config> {
240241
let externalDataJsonPath: undefined | string = core.getInput('external-data-json-path');
241242
const maxItemsInChart = getUintInput('max-items-in-chart');
242243
let failThreshold = getPercentageInput('fail-threshold');
244+
const commitSha: string | undefined = core.getInput('commit-sha') || undefined;
243245

244246
validateToolType(tool);
245247
outputFilePath = await validateOutputFilePath(outputFilePath);
@@ -255,6 +257,9 @@ export async function configFromJobInput(): Promise<Config> {
255257
if (commentOnAlert) {
256258
validateGitHubToken('comment-on-alert', githubToken, 'to send commit comment on alert');
257259
}
260+
if (commitSha) {
261+
validateGitHubToken('commit-sha', githubToken, 'to retrieve the commit info');
262+
}
258263
validateAlertThreshold(alertThreshold, failThreshold);
259264
validateAlertCommentCcUsers(alertCommentCcUsers);
260265
externalDataJsonPath = await validateExternalDataJsonPath(externalDataJsonPath, autoPush);
@@ -281,5 +286,6 @@ export async function configFromJobInput(): Promise<Config> {
281286
externalDataJsonPath,
282287
maxItemsInChart,
283288
failThreshold,
289+
commitSha,
284290
};
285291
}

src/extract.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -202,13 +202,13 @@ function getCommitFromPullRequestPayload(pr: PullRequest): Commit {
202202
};
203203
}
204204

205-
async function getCommitFromGitHubAPIRequest(githubToken: string): Promise<Commit> {
205+
async function getCommitFromGitHubAPIRequest(githubToken: string, commitSha?: string): Promise<Commit> {
206206
const octocat = new github.GitHub(githubToken);
207207

208208
const { status, data } = await octocat.repos.getCommit({
209209
owner: github.context.repo.owner,
210210
repo: github.context.repo.repo,
211-
ref: github.context.ref,
211+
ref: commitSha ?? github.context.ref,
212212
});
213213

214214
if (!(status === 200 || status === 304)) {
@@ -235,7 +235,11 @@ async function getCommitFromGitHubAPIRequest(githubToken: string): Promise<Commi
235235
};
236236
}
237237

238-
async function getCommit(githubToken?: string): Promise<Commit> {
238+
async function getCommit(githubToken?: string, commitSha?: string): Promise<Commit> {
239+
if (commitSha && githubToken) {
240+
return getCommitFromGitHubAPIRequest(githubToken, commitSha);
241+
}
242+
239243
if (github.context.payload.head_commit) {
240244
return github.context.payload.head_commit;
241245
}
@@ -564,7 +568,7 @@ function extractCustomBenchmarkResult(output: string): BenchmarkResult[] {
564568

565569
export async function extractResult(config: Config): Promise<Benchmark> {
566570
const output = await fs.readFile(config.outputFilePath, 'utf8');
567-
const { tool, githubToken } = config;
571+
const { tool, githubToken, commitSha } = config;
568572
let benches: BenchmarkResult[];
569573

570574
switch (tool) {
@@ -603,7 +607,7 @@ export async function extractResult(config: Config): Promise<Benchmark> {
603607
throw new Error(`No benchmark result was found in ${config.outputFilePath}. Benchmark output was '${output}'`);
604608
}
605609

606-
const commit = await getCommit(githubToken);
610+
const commit = await getCommit(githubToken, commitSha);
607611

608612
return {
609613
commit,

test/config.spec.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ describe('configFromJobInput()', function () {
4444
'alert-comment-cc-users': '',
4545
'external-data-json-path': '',
4646
'max-items-in-chart': '',
47+
'commit-sha': '',
4748
};
4849

4950
const validation_tests = [
@@ -158,6 +159,11 @@ describe('configFromJobInput()', function () {
158159
inputs: { ...defaultInputs, 'alert-threshold': '150%', 'fail-threshold': '120%' },
159160
expected: /'alert-threshold' value must be smaller than 'fail-threshold' value but got 1.5 > 1.2/,
160161
},
162+
{
163+
what: 'commit-sha is set but github-token is not set',
164+
inputs: { ...defaultInputs, 'commit-sha': 'dummy sha', 'github-token': '' },
165+
expected: /'commit-sha' is enabled but 'github-token' is not set/,
166+
},
161167
] as Array<{
162168
what: string;
163169
inputs: Inputs;
@@ -185,6 +191,7 @@ describe('configFromJobInput()', function () {
185191
hasExternalDataJsonPath: boolean;
186192
maxItemsInChart: null | number;
187193
failThreshold: number | null;
194+
commitSha: string | undefined;
188195
}
189196

190197
const defaultExpected: ExpectedResult = {
@@ -201,6 +208,7 @@ describe('configFromJobInput()', function () {
201208
hasExternalDataJsonPath: false,
202209
maxItemsInChart: null,
203210
failThreshold: null,
211+
commitSha: undefined,
204212
};
205213

206214
const returnedConfigTests = [
@@ -350,7 +358,7 @@ describe('configFromJobInput()', function () {
350358
const absCwd = process.cwd();
351359
if (!absCwd.startsWith(home)) {
352360
// Test was not run under home directory so "~" in paths cannot be tested
353-
fail('Test was not run under home directory so "~" in paths cannot be tested');
361+
A.fail('Test was not run under home directory so "~" in paths cannot be tested');
354362
}
355363

356364
const cwd = path.join('~', absCwd.slice(home.length));

test/extract.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const dummyWebhookPayload = {
1616
let dummyCommitData = {};
1717
class DummyGitHub {
1818
repos = {
19-
getCommit: () => {
19+
getCommit: async () => {
2020
return {
2121
status: 200,
2222
data: dummyCommitData,

test/write.spec.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ describe('writeBenchmark()', function () {
202202
externalDataJsonPath: dataJson,
203203
maxItemsInChart: null,
204204
failThreshold: 2.0,
205+
commitSha: undefined,
205206
};
206207

207208
const savedRepository = gitHubContext.payload.repository;
@@ -885,6 +886,7 @@ describe('writeBenchmark()', function () {
885886
externalDataJsonPath: undefined,
886887
maxItemsInChart: null,
887888
failThreshold: 2.0,
889+
commitSha: 'dummy sha',
888890
};
889891

890892
function gitHistory(

0 commit comments

Comments
 (0)