Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fetch gh-pages branch using github token #38

Closed
wants to merge 1 commit into from
Closed
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: 12 additions & 0 deletions src/git.ts
Original file line number Diff line number Diff line change
@@ -87,3 +87,15 @@ export async function pull(token: string | undefined, branch: string, ...options

return cmd(...args);
}

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

const remote = token !== undefined ? getRemoteUrl(token) : 'origin';
let args = ['fetch', remote, `${branch}:${branch}`];
if (options.length > 0) {
args = args.concat(options);
}

return cmd(...args);
}
4 changes: 2 additions & 2 deletions src/write.ts
Original file line number Diff line number Diff line change
@@ -430,9 +430,9 @@ async function writeBenchmarkToGitHubPagesWithRetry(
}

async function writeBenchmarkToGitHubPages(bench: Benchmark, config: Config): Promise<Benchmark | null> {
const { ghPagesBranch, skipFetchGhPages } = config;
const { ghPagesBranch, skipFetchGhPages, githubToken } = config;
if (!skipFetchGhPages) {
await git.cmd('fetch', 'origin', `${ghPagesBranch}:${ghPagesBranch}`);
await git.fetch(githubToken, ghPagesBranch);
}
await git.cmd('switch', ghPagesBranch);
try {
46 changes: 45 additions & 1 deletion test/git.ts
Original file line number Diff line number Diff line change
@@ -71,7 +71,7 @@ mock('@actions/github', {
context: gitHubContext,
});

const { cmd, pull, push } = require('../src/git');
const { cmd, pull, push, fetch } = require('../src/git');
const ok: (x: any) => asserts x = A.ok;
const userArgs = [
'-c',
@@ -203,4 +203,48 @@ describe('git', function() {
eq(fakedExec.lastArgs, null);
});
});

describe('fetch()', function() {
afterEach(function() {
gitHubContext.payload.repository = { full_name: 'user/repo' };
});

it('runs `git fetch` with given branch and options with token', async function() {
const stdout = await fetch('this-is-token', 'my-branch', 'opt1', 'opt2');
const args = fakedExec.lastArgs;

eq(stdout, 'this is test');
ok(args);
eq(args[0], 'git');
eq(
args[1],
userArgs.concat([
'fetch',
'https://x-access-token:[email protected]/user/repo.git',
'my-branch:my-branch',
'opt1',
'opt2',
]),
);
});

it('runs `git fetch` with given branch and options without token', async function() {
const stdout = await fetch(undefined, 'my-branch', 'opt1', 'opt2');
const args = fakedExec.lastArgs;

eq(stdout, 'this is test');
ok(args);
eq(args[0], 'git');
eq(args[1], userArgs.concat(['fetch', 'origin', 'my-branch:my-branch', 'opt1', 'opt2']));
});

it('raises an error when repository info is not included in payload', async function() {
gitHubContext.payload.repository = null;
await A.rejects(
() => fetch('this-is-token', 'my-branch', 'opt1', 'opt2'),
/^Error: Repository info is not available in payload/,
);
eq(fakedExec.lastArgs, null);
});
});
});
8 changes: 6 additions & 2 deletions test/write.ts
Original file line number Diff line number Diff line change
@@ -45,7 +45,7 @@ class FakedOctokit {
}
}

type GitFunc = 'cmd' | 'push' | 'pull';
type GitFunc = 'cmd' | 'push' | 'pull' | 'fetch';
class GitSpy {
history: [GitFunc, unknown[]][];
pushFailure: null | string;
@@ -124,6 +124,10 @@ mock('../src/git', {
gitSpy.call('pull', args);
return '';
},
async fetch(...args: unknown[]) {
gitSpy.call('fetch', args);
return '';
},
});

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