Skip to content

Commit 53734e4

Browse files
feat: add method to get pr branch
1 parent 551b1eb commit 53734e4

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

__tests__/utils.test.ts

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ import path from 'path';
33
import { testEnv, getContext, testFs } from '@technote-space/github-action-test-helper';
44
import { Utils } from '../src';
55

6-
const {getWorkspace, getActor, escapeRegExp, getRegExp, getPrefixRegExp, getSuffixRegExp, useNpm, versionCompare} = Utils;
7-
const {isSemanticVersioningTagName, isPrRef, getPrMergeRef, getBoolValue, replaceAll, getPrHeadRef, arrayChunk} = Utils;
8-
const {getBranch, getRefForUpdate, uniqueArray, getBuildInfo, split, getArrayInput, generateNewPatchVersion, sleep} = Utils;
6+
const {getWorkspace, getActor, escapeRegExp, getRegExp, getPrefixRegExp, getSuffixRegExp, useNpm, versionCompare} = Utils;
7+
const {isSemanticVersioningTagName, isPrRef, getPrMergeRef, getBoolValue, replaceAll, getPrHeadRef, arrayChunk, sleep} = Utils;
8+
const {getBranch, getRefForUpdate, uniqueArray, getBuildInfo, split, getArrayInput, generateNewPatchVersion, getPrBranch} = Utils;
99

1010
jest.useFakeTimers();
1111

@@ -219,6 +219,24 @@ describe('getBranch', () => {
219219
});
220220
});
221221

222+
describe('getPrBranch', () => {
223+
it('should get pr branch', () => {
224+
expect(getPrBranch(getContext({
225+
payload: {
226+
'pull_request': {
227+
head: {
228+
ref: 'test/abc',
229+
},
230+
},
231+
},
232+
}))).toBe('test/abc');
233+
});
234+
235+
it('should not get pr branch', () => {
236+
expect(getPrBranch(getContext({}))).toBe('');
237+
});
238+
});
239+
222240
describe('getRefForUpdate', () => {
223241
// https://github.com/octokit/rest.js/issues/1308#issuecomment-480532468
224242
it('should get ref for update', () => {

src/utils.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ export const getBranch = (ref: string | Context, defaultIsEmpty = true): string
5252
)
5353
);
5454

55+
export const getPrBranch = (context: Context): string => context.payload.pull_request?.head.ref ?? '';
56+
5557
export const getAccessToken = (required: boolean): string => getInput('GITHUB_TOKEN', {required});
5658

5759
export const getActor = (): string => process.env.GITHUB_ACTOR || '';

0 commit comments

Comments
 (0)