Skip to content

Commit c076a65

Browse files
leoholzspl
authored andcommitted
fetch gh-pages branch using github token
Source: benchmark-action/github-action-benchmark#38
1 parent b30a985 commit c076a65

File tree

4 files changed

+65
-5
lines changed

4 files changed

+65
-5
lines changed

src/git.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,15 @@ export async function pull(token: string | undefined, branch: string, ...options
8585

8686
return cmd(...args);
8787
}
88+
89+
export async function fetch(token: string | undefined, branch: string, ...options: string[]): Promise<string> {
90+
core.debug(`Executing 'git fetch' for branch '${branch}' with token and options '${options.join(' ')}'`);
91+
92+
const remote = token !== undefined ? getRemoteUrl(token) : 'origin';
93+
let args = ['fetch', remote, `${branch}:${branch}`];
94+
if (options.length > 0) {
95+
args = args.concat(options);
96+
}
97+
98+
return cmd(...args);
99+
}

src/write.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -428,9 +428,9 @@ async function writeBenchmarkToGitHubPagesWithRetry(
428428
}
429429

430430
async function writeBenchmarkToGitHubPages(bench: Benchmark, config: Config): Promise<Benchmark | null> {
431-
const { ghPagesBranch, skipFetchGhPages } = config;
431+
const { ghPagesBranch, skipFetchGhPages, githubToken } = config;
432432
if (!skipFetchGhPages) {
433-
await git.cmd('fetch', 'origin', `${ghPagesBranch}:${ghPagesBranch}`);
433+
await git.fetch(githubToken, ghPagesBranch);
434434
}
435435
await git.cmd('switch', ghPagesBranch);
436436
try {

test/git.ts

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ mock('@actions/github', {
7676
context: gitHubContext,
7777
});
7878

79-
const { cmd, pull, push } = require('../src/git');
79+
const { cmd, pull, push, fetch } = require('../src/git');
8080
const ok: (x: any) => asserts x = A.ok;
8181
const userArgs = [
8282
'-c',
@@ -209,4 +209,48 @@ describe('git', function () {
209209
eq(fakedExec.lastArgs, null);
210210
});
211211
});
212+
213+
describe('fetch()', function () {
214+
afterEach(function () {
215+
gitHubContext.payload.repository = { full_name: 'user/repo' };
216+
});
217+
218+
it('runs `git fetch` with given branch and options with token', async function () {
219+
const stdout = await fetch('this-is-token', 'my-branch', 'opt1', 'opt2');
220+
const args = fakedExec.lastArgs;
221+
222+
eq(stdout, 'this is test');
223+
ok(args);
224+
eq(args[0], 'git');
225+
eq(
226+
args[1],
227+
userArgs.concat([
228+
'fetch',
229+
'https://x-access-token:[email protected]/user/repo.git',
230+
'my-branch:my-branch',
231+
'opt1',
232+
'opt2',
233+
]),
234+
);
235+
});
236+
237+
it('runs `git fetch` with given branch and options without token', async function () {
238+
const stdout = await fetch(undefined, 'my-branch', 'opt1', 'opt2');
239+
const args = fakedExec.lastArgs;
240+
241+
eq(stdout, 'this is test');
242+
ok(args);
243+
eq(args[0], 'git');
244+
eq(args[1], userArgs.concat(['fetch', 'origin', 'my-branch:my-branch', 'opt1', 'opt2']));
245+
});
246+
247+
it('raises an error when repository info is not included in payload', async function () {
248+
gitHubContext.payload.repository = null;
249+
await A.rejects(
250+
() => fetch('this-is-token', 'my-branch', 'opt1', 'opt2'),
251+
/^Error: Repository info is not available in payload/,
252+
);
253+
eq(fakedExec.lastArgs, null);
254+
});
255+
});
212256
});

test/write.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class FakedOctokit {
5050
}
5151
}
5252

53-
type GitFunc = 'cmd' | 'push' | 'pull';
53+
type GitFunc = 'cmd' | 'push' | 'pull' | 'fetch';
5454
class GitSpy {
5555
history: [GitFunc, unknown[]][];
5656
pushFailure: null | string;
@@ -134,6 +134,10 @@ mock('../src/git', {
134134
gitSpy.call('pull', args);
135135
return '';
136136
},
137+
async fetch(...args: unknown[]) {
138+
gitSpy.call('fetch', args);
139+
return '';
140+
},
137141
});
138142

139143
const writeBenchmark: (b: Benchmark, c: Config) => Promise<any> = require('../src/write').writeBenchmark;
@@ -933,7 +937,7 @@ describe('writeBenchmark()', function () {
933937
const autoPush = cfg.autoPush ?? true;
934938
const skipFetch = cfg.skipFetch ?? false;
935939
const hist: Array<[GitFunc, unknown[]] | undefined> = [
936-
skipFetch ? undefined : ['cmd', ['fetch', 'origin', 'gh-pages:gh-pages']],
940+
skipFetch ? undefined : ['fetch', [token, 'gh-pages']],
937941
['cmd', ['switch', 'gh-pages']],
938942
fetch ? ['pull', [token, 'gh-pages']] : undefined,
939943
['cmd', ['add', path.join(dir, 'data.js')]],

0 commit comments

Comments
 (0)