Skip to content

Commit

Permalink
Read commit id from local
Browse files Browse the repository at this point in the history
  • Loading branch information
pksunkara committed Apr 10, 2020
1 parent 7cadfa1 commit f705f6c
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 6 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,14 @@ This option is useful if you don't want to put benchmark results in GitHub Pages
you need to keep the JSON file persistently among job runs. One option is using a workflow cache
with `actions/cache` action. Please read 'Minimal setup' section above.

#### `read-commit-id` (Optional)

- Type: Boolean
- Default: `false`

Read the commit id from the workspace. This is useful when you have switched the repository away
from the commit that started this action.

#### `max-items-in-chart` (Optional)

- Type: Number
Expand Down
3 changes: 3 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export interface Config {
failThreshold: number;
alertCommentCcUsers: string[];
externalDataJsonPath: string | undefined;
readCommitId: boolean;
maxItemsInChart: number | null;
}

Expand Down Expand Up @@ -219,6 +220,7 @@ export async function configFromJobInput(): Promise<Config> {
const failOnAlert = getBoolInput('fail-on-alert');
const alertCommentCcUsers = getCommaSeparatedInput('alert-comment-cc-users');
let externalDataJsonPath: undefined | string = core.getInput('external-data-json-path');
const readCommitId = getBoolInput('read-commit-id');
const maxItemsInChart = getUintInput('max-items-in-chart');
let failThreshold = getPercentageInput('fail-threshold');

Expand Down Expand Up @@ -260,6 +262,7 @@ export async function configFromJobInput(): Promise<Config> {
failOnAlert,
alertCommentCcUsers,
externalDataJsonPath,
readCommitId,
maxItemsInChart,
failThreshold,
};
Expand Down
23 changes: 17 additions & 6 deletions src/extract.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { promises as fs } from 'fs';
import * as github from '@actions/github';
import * as git from './git';
import { Config, ToolType } from './config';

export interface BenchmarkResult {
Expand All @@ -17,12 +18,12 @@ interface GitHubUser {
}

interface Commit {
author: GitHubUser;
committer: GitHubUser;
author?: GitHubUser;
committer?: GitHubUser;
distinct?: unknown; // Unused
id: string;
message: string;
timestamp: string;
message?: string;
timestamp?: string;
tree_id?: unknown; // Unused
url: string;
}
Expand Down Expand Up @@ -138,7 +139,17 @@ function getHumanReadableUnitValue(seconds: number): [number, string] {
}
}

function getCommit(): Commit {
async function getCommit(config: Config): Promise<Commit> {
if (config.readCommitId) {
const id = (await git.readCommitId()).trim();
const repo = github.context.repo;

return {
id,
url: `https://github.com/${repo.owner}/${repo.repo}/commits/${id}`,
};
}

/* eslint-disable @typescript-eslint/camelcase */
if (github.context.payload.head_commit) {
return github.context.payload.head_commit;
Expand Down Expand Up @@ -447,7 +458,7 @@ export async function extractResult(config: Config): Promise<Benchmark> {
throw new Error(`No benchmark result was found in ${config.outputFilePath}. Benchmark output was '${output}'`);
}

const commit = getCommit();
const commit = await getCommit(config);

return {
commit,
Expand Down
11 changes: 11 additions & 0 deletions src/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,17 @@ function getRemoteUrl(token: string): string {
return `https://x-access-token:${token}@github.com/${fullName}.git`;
}

export async function readCommitId(...options: string[]): Promise<string> {
core.debug(`Executing 'git rev-parse HEAD' with options '${options.join(' ')}'`);

let args = ['rev-parse', 'HEAD'];
if (options.length > 0) {
args = args.concat(options);
}

return cmd(...args);
}

export async function push(token: string, branch: string, ...options: string[]): Promise<string> {
core.debug(`Executing 'git push' to branch '${branch}' with token and options '${options.join(' ')}'`);

Expand Down
2 changes: 2 additions & 0 deletions test/write.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ describe('writeBenchmark()', function() {
failOnAlert: true,
alertCommentCcUsers: ['@user'],
externalDataJsonPath: dataJson,
readCommitId: false,
maxItemsInChart: null,
failThreshold: 2.0,
};
Expand Down Expand Up @@ -903,6 +904,7 @@ describe('writeBenchmark()', function() {
failOnAlert: true,
alertCommentCcUsers: [],
externalDataJsonPath: undefined,
readCommitId: false,
maxItemsInChart: null,
failThreshold: 2.0,
};
Expand Down

0 comments on commit f705f6c

Please sign in to comment.