Skip to content

Commit 19546f6

Browse files
feat: add utils for refs
1 parent 1bc03cf commit 19546f6

File tree

2 files changed

+74
-1
lines changed

2 files changed

+74
-1
lines changed

__tests__/utils.test.ts

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { Utils } from '../src';
66
const {getWorkspace, getActor, escapeRegExp, getRegExp, getPrefixRegExp, getSuffixRegExp, useNpm, versionCompare, getOctokit} = Utils;
77
const {isSemanticVersioningTagName, isPrRef, getPrMergeRef, getBoolValue, replaceAll, getPrHeadRef, arrayChunk, sleep} = Utils;
88
const {getBranch, getRefForUpdate, uniqueArray, getBuildInfo, split, getArrayInput, generateNewPatchVersion, getPrBranch} = Utils;
9+
const {isBranch, isTagRef, normalizeRef, trimRef, getTag, getRefspec} = Utils;
910

1011
jest.useFakeTimers();
1112

@@ -482,3 +483,63 @@ describe('getOctokit', () => {
482483
expect(() => getOctokit()).toThrow();
483484
});
484485
});
486+
487+
describe('isBranch', () => {
488+
it('should return true', () => {
489+
expect(isBranch('refs/heads/master')).toBe(true);
490+
expect(isBranch('heads/master')).toBe(true);
491+
});
492+
493+
it('should return false', () => {
494+
expect(isBranch('test')).toBe(false);
495+
expect(isBranch('heads')).toBe(false);
496+
});
497+
});
498+
499+
describe('isTagRef', () => {
500+
it('should return true', () => {
501+
expect(isTagRef('refs/tags/v1.2.3')).toBe(true);
502+
});
503+
504+
it('should return false', () => {
505+
expect(isTagRef('refs/heads/master')).toBe(false);
506+
expect(isTagRef('heads/master')).toBe(false);
507+
});
508+
});
509+
510+
describe('normalizeRef', () => {
511+
it('should normalize ref', () => {
512+
expect(normalizeRef('master')).toBe('refs/heads/master');
513+
expect(normalizeRef('refs/heads/master')).toBe('refs/heads/master');
514+
expect(normalizeRef('refs/tags/v1.2.3')).toBe('refs/tags/v1.2.3');
515+
expect(normalizeRef('refs/pull/123/merge')).toBe('refs/pull/123/merge');
516+
});
517+
});
518+
519+
describe('trimRef', () => {
520+
it('should trim ref', () => {
521+
expect(trimRef('master')).toBe('master');
522+
expect(trimRef('refs/heads/master')).toBe('master');
523+
expect(trimRef('refs/tags/v1.2.3')).toBe('v1.2.3');
524+
expect(trimRef('refs/pull/123/merge')).toBe('123/merge');
525+
});
526+
});
527+
528+
describe('getTag', () => {
529+
it('should get tag', () => {
530+
expect(getTag('master')).toBe('');
531+
expect(getTag('heads/master')).toBe('');
532+
expect(getTag('refs/heads/master')).toBe('');
533+
expect(getTag('refs/tags/v1.2.3')).toBe('v1.2.3');
534+
expect(getTag('refs/pull/123/merge')).toBe('');
535+
});
536+
});
537+
538+
describe('getRefspec', () => {
539+
it('should get refspec', () => {
540+
expect(getRefspec('master')).toBe('refs/heads/master:refs/remotes/origin/master');
541+
expect(getRefspec('refs/heads/master', 'test')).toBe('refs/heads/master:refs/remotes/test/master');
542+
expect(getRefspec('refs/tags/v1.2.3')).toBe('refs/tags/v1.2.3:refs/tags/v1.2.3');
543+
expect(getRefspec('refs/pull/123/merge')).toBe('refs/pull/123/merge:refs/pull/123/merge');
544+
});
545+
});

src/utils.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@ export const isCloned = (workDir: string): boolean => fs.existsSync(path.resolve
3232

3333
export const isSemanticVersioningTagName = (tagName: string): boolean => /^v?\d+(\.\d+)*$/i.test(tagName);
3434

35-
export const isBranch = (ref: string | Context): boolean => /^(refs\/)?heads/.test(getRef(ref));
35+
export const isBranch = (ref: string | Context): boolean => /^(refs\/)?heads\//.test(getRef(ref));
36+
37+
export const isTagRef = (ref: string | Context): boolean => /^refs\/?tags\//.test(getRef(ref));
3638

3739
export const isRemoteBranch = (ref: string | Context): boolean => /^(refs\/)?remotes\/origin\//.test(getRef(ref));
3840

@@ -56,6 +58,16 @@ export const getBranch = (ref: string | Context, defaultIsEmpty = true): string
5658

5759
export const getPrBranch = (context: Context): string => context.payload.pull_request?.head.ref ?? '';
5860

61+
export const normalizeRef = (ref: string | Context): string => /^refs\//.test(getRef(ref)) ? getRef(ref) : `refs/heads/${getRef(ref)}`;
62+
63+
export const trimRef = (ref: string | Context): string => getRef(ref).replace(/^refs\/(heads|tags|pull)\//, '');
64+
65+
export const getTag = (ref: string | Context): string => isTagRef(ref) ? trimRef(ref) : '';
66+
67+
const saveTarget = (ref: string | Context, origin: string): string => isTagRef(ref) ? 'tags' : isPrRef(ref) ? 'pull' : `remotes/${origin}`;
68+
69+
export const getRefspec = (ref: string | Context, origin = 'origin'): string => `${normalizeRef(ref)}:refs/${saveTarget(ref, origin)}/${trimRef(ref)}`;
70+
5971
export const getAccessToken = (required: boolean): string => getInput('GITHUB_TOKEN', {required});
6072

6173
// eslint-disable-next-line @typescript-eslint/ban-ts-ignore

0 commit comments

Comments
 (0)