From dd836ac87dc6c6244c4a88efb1e8fa08da6c1e71 Mon Sep 17 00:00:00 2001 From: Jeffrey Chien Date: Fri, 17 Dec 2021 13:15:21 -0500 Subject: [PATCH] Add optional commit SHA in config. --- README.md | 11 +++++++++-- action.yml | 3 +++ src/config.ts | 6 ++++++ src/extract.ts | 14 +++++++++----- test/config.spec.ts | 10 +++++++++- test/extract.spec.ts | 2 +- test/write.spec.ts | 2 ++ 7 files changed, 39 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 6bb19870a..b8b3d84ca 100644 --- a/README.md +++ b/README.md @@ -72,7 +72,7 @@ context) properties. Like this: "unit": "Megabytes", "value": 100, "range": "3", - "extra": "Value for Tooltip: 25\nOptional Num #2: 100\nAnything Else!", + "extra": "Value for Tooltip: 25\nOptional Num #2: 100\nAnything Else!" } ] ``` @@ -469,9 +469,16 @@ with `actions/cache` action. Please read 'Minimal setup' section above. Max number of data points in a chart for avoiding too busy chart. This value must be unsigned integer larger than zero. If the number of benchmark results for some benchmark suite exceeds this value, -the oldest one will be removed before storing the results to file. By default this value is empty +the oldest one will be removed before storing the results to file. By default, this value is empty which means there is no limit. +#### `commit-sha` (Optional) + +- Type: String +- Default: N/A + +The commit SHA to tag the benchmark with. Uses this in place of the payload if provided. + ### Action outputs diff --git a/action.yml b/action.yml index 6d5489169..acd52c71d 100644 --- a/action.yml +++ b/action.yml @@ -69,6 +69,9 @@ inputs: max-items-in-chart: description: 'Max data points in a benchmark chart to avoid making the chart too busy. Value must be unsigned integer. No limit by default' required: false + commit-sha: + description: 'The commit SHA to tag the benchmark with. If not provided, uses GitHub context.' + required: false runs: using: 'node12' diff --git a/src/config.ts b/src/config.ts index 33ef32a5d..9e6d65d80 100644 --- a/src/config.ts +++ b/src/config.ts @@ -31,6 +31,7 @@ export interface Config { alertCommentCcUsers: string[]; externalDataJsonPath: string | undefined; maxItemsInChart: number | null; + commitSha: string | undefined; } export const VALID_TOOLS: ToolType[] = [ @@ -240,6 +241,7 @@ export async function configFromJobInput(): Promise { let externalDataJsonPath: undefined | string = core.getInput('external-data-json-path'); const maxItemsInChart = getUintInput('max-items-in-chart'); let failThreshold = getPercentageInput('fail-threshold'); + const commitSha: string | undefined = core.getInput('commit-sha') || undefined; validateToolType(tool); outputFilePath = await validateOutputFilePath(outputFilePath); @@ -255,6 +257,9 @@ export async function configFromJobInput(): Promise { if (commentOnAlert) { validateGitHubToken('comment-on-alert', githubToken, 'to send commit comment on alert'); } + if (commitSha) { + validateGitHubToken('commit-sha', githubToken, 'to retrieve the commit info'); + } validateAlertThreshold(alertThreshold, failThreshold); validateAlertCommentCcUsers(alertCommentCcUsers); externalDataJsonPath = await validateExternalDataJsonPath(externalDataJsonPath, autoPush); @@ -281,5 +286,6 @@ export async function configFromJobInput(): Promise { externalDataJsonPath, maxItemsInChart, failThreshold, + commitSha, }; } diff --git a/src/extract.ts b/src/extract.ts index a897d7a4c..61d33fc18 100644 --- a/src/extract.ts +++ b/src/extract.ts @@ -202,13 +202,13 @@ function getCommitFromPullRequestPayload(pr: PullRequest): Commit { }; } -async function getCommitFromGitHubAPIRequest(githubToken: string): Promise { +async function getCommitFromGitHubAPIRequest(githubToken: string, commitSha?: string): Promise { const octocat = new github.GitHub(githubToken); const { status, data } = await octocat.repos.getCommit({ owner: github.context.repo.owner, repo: github.context.repo.repo, - ref: github.context.ref, + ref: commitSha ?? github.context.ref, }); if (!(status === 200 || status === 304)) { @@ -235,7 +235,11 @@ async function getCommitFromGitHubAPIRequest(githubToken: string): Promise { +async function getCommit(githubToken?: string, commitSha?: string): Promise { + if (commitSha && githubToken) { + return getCommitFromGitHubAPIRequest(githubToken, commitSha); + } + if (github.context.payload.head_commit) { return github.context.payload.head_commit; } @@ -564,7 +568,7 @@ function extractCustomBenchmarkResult(output: string): BenchmarkResult[] { export async function extractResult(config: Config): Promise { const output = await fs.readFile(config.outputFilePath, 'utf8'); - const { tool, githubToken } = config; + const { tool, githubToken, commitSha } = config; let benches: BenchmarkResult[]; switch (tool) { @@ -603,7 +607,7 @@ export async function extractResult(config: Config): Promise { throw new Error(`No benchmark result was found in ${config.outputFilePath}. Benchmark output was '${output}'`); } - const commit = await getCommit(githubToken); + const commit = await getCommit(githubToken, commitSha); return { commit, diff --git a/test/config.spec.ts b/test/config.spec.ts index cdb77c7f3..5920d51a9 100644 --- a/test/config.spec.ts +++ b/test/config.spec.ts @@ -44,6 +44,7 @@ describe('configFromJobInput()', function () { 'alert-comment-cc-users': '', 'external-data-json-path': '', 'max-items-in-chart': '', + 'commit-sha': '', }; const validation_tests = [ @@ -158,6 +159,11 @@ describe('configFromJobInput()', function () { inputs: { ...defaultInputs, 'alert-threshold': '150%', 'fail-threshold': '120%' }, expected: /'alert-threshold' value must be smaller than 'fail-threshold' value but got 1.5 > 1.2/, }, + { + what: 'commit-sha is set but github-token is not set', + inputs: { ...defaultInputs, 'commit-sha': 'dummy sha', 'github-token': '' }, + expected: /'commit-sha' is enabled but 'github-token' is not set/, + }, ] as Array<{ what: string; inputs: Inputs; @@ -185,6 +191,7 @@ describe('configFromJobInput()', function () { hasExternalDataJsonPath: boolean; maxItemsInChart: null | number; failThreshold: number | null; + commitSha: string | undefined; } const defaultExpected: ExpectedResult = { @@ -201,6 +208,7 @@ describe('configFromJobInput()', function () { hasExternalDataJsonPath: false, maxItemsInChart: null, failThreshold: null, + commitSha: undefined, }; const returnedConfigTests = [ @@ -350,7 +358,7 @@ describe('configFromJobInput()', function () { const absCwd = process.cwd(); if (!absCwd.startsWith(home)) { // Test was not run under home directory so "~" in paths cannot be tested - fail('Test was not run under home directory so "~" in paths cannot be tested'); + A.fail('Test was not run under home directory so "~" in paths cannot be tested'); } const cwd = path.join('~', absCwd.slice(home.length)); diff --git a/test/extract.spec.ts b/test/extract.spec.ts index 9aeef4a7d..8d78ae4a1 100644 --- a/test/extract.spec.ts +++ b/test/extract.spec.ts @@ -16,7 +16,7 @@ const dummyWebhookPayload = { let dummyCommitData = {}; class DummyGitHub { repos = { - getCommit: () => { + getCommit: async () => { return { status: 200, data: dummyCommitData, diff --git a/test/write.spec.ts b/test/write.spec.ts index 82e17ce93..7da3ccc05 100644 --- a/test/write.spec.ts +++ b/test/write.spec.ts @@ -202,6 +202,7 @@ describe('writeBenchmark()', function () { externalDataJsonPath: dataJson, maxItemsInChart: null, failThreshold: 2.0, + commitSha: undefined, }; const savedRepository = gitHubContext.payload.repository; @@ -885,6 +886,7 @@ describe('writeBenchmark()', function () { externalDataJsonPath: undefined, maxItemsInChart: null, failThreshold: 2.0, + commitSha: 'dummy sha', }; function gitHistory(