Skip to content

Commit

Permalink
Switches back to previous branch. (#274)
Browse files Browse the repository at this point in the history
This solves the issue to checkout from different origins, e.g., in
pull requests from forks.
  • Loading branch information
armfazh committed May 26, 2022
1 parent 704d438 commit 580a864
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/run.ts
Expand Up @@ -10,7 +10,7 @@ import { generatePRReport } from './report/generatePRReport';
import { checkThreshold } from './stages/checkThreshold';
import { createReport } from './stages/createReport';
import { getCoverage } from './stages/getCoverage';
import { switchBranch } from './stages/switchBranch';
import { switchBack, switchBranch } from './stages/switchBranch';
import { JsonReport } from './typings/JsonReport';
import { getOptions } from './typings/Options';
import { createDataCollector, DataCollector } from './utils/DataCollector';
Expand Down Expand Up @@ -103,7 +103,7 @@ export const run = async (
skip();
}

await switchBranch(context.payload.pull_request!.head.ref);
await switchBack();
});

if (baseCoverage) {
Expand Down
8 changes: 8 additions & 0 deletions src/stages/switchBranch.ts
Expand Up @@ -9,3 +9,11 @@ export const switchBranch = async (branch: string) => {

await exec(`git checkout -f ${branch}`);
};

export const switchBack = async () => {
try {
await exec(`git checkout -`);
} catch (err) {
console.warn('Error checking to branches', err);
}
};
10 changes: 9 additions & 1 deletion tests/stages/switchBranch.test.ts
@@ -1,6 +1,6 @@
import { exec } from '@actions/exec';

import { switchBranch } from '../../src/stages/switchBranch';
import { switchBack, switchBranch } from '../../src/stages/switchBranch';

const clearMocks = () => {
(exec as jest.Mock<any, any>).mockClear();
Expand All @@ -26,3 +26,11 @@ describe('switchBranch', () => {
expect(exec).toBeCalledWith('git checkout -f Test-branch');
});
});

describe('switchBack', () => {
it('should switch to previous branch', async () => {
await switchBack();

expect(exec).toBeCalledWith('git checkout -');
});
});

0 comments on commit 580a864

Please sign in to comment.