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

Release/v1.0.3 #226

Merged
merged 3 commits into from
Feb 9, 2020
Merged
Show file tree
Hide file tree
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
20 changes: 19 additions & 1 deletion __tests__/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Utils } from '../src';
const {getWorkspace, getActor, escapeRegExp, getRegExp, getPrefixRegExp, getSuffixRegExp, useNpm, versionCompare, getOctokit} = Utils;
const {isSemanticVersioningTagName, isPrRef, getPrMergeRef, getBoolValue, replaceAll, getPrHeadRef, arrayChunk, sleep} = Utils;
const {getBranch, getRefForUpdate, uniqueArray, getBuildInfo, split, getArrayInput, generateNewPatchVersion, getPrBranch} = Utils;
const {isBranch, isTagRef, normalizeRef, trimRef, getTag, getRefspec} = Utils;
const {isBranch, isTagRef, normalizeRef, trimRef, getTag, getRefspec, getRemoteRefspec, getLocalRefspec} = Utils;

jest.useFakeTimers();

Expand Down Expand Up @@ -543,3 +543,21 @@ describe('getRefspec', () => {
expect(getRefspec('refs/pull/123/merge')).toBe('refs/pull/123/merge:refs/pull/123/merge');
});
});

describe('getRemoteRefspec', () => {
it('should get remote refspec', () => {
expect(getRemoteRefspec('master')).toBe('refs/heads/master');
expect(getRemoteRefspec('refs/heads/master')).toBe('refs/heads/master');
expect(getRemoteRefspec('refs/tags/v1.2.3')).toBe('refs/tags/v1.2.3');
expect(getRemoteRefspec('refs/pull/123/merge')).toBe('refs/pull/123/merge');
});
});

describe('getLocalRefspec', () => {
it('should get remote refspec', () => {
expect(getLocalRefspec('master')).toBe('origin/master');
expect(getLocalRefspec('refs/heads/master', 'test')).toBe('test/master');
expect(getLocalRefspec('refs/tags/v1.2.3')).toBe('tags/v1.2.3');
expect(getLocalRefspec('refs/pull/123/merge')).toBe('pull/123/merge');
});
});
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@technote-space/github-action-helper",
"version": "1.0.2",
"version": "1.0.3",
"description": "Helper to filter GitHub Action.",
"author": {
"name": "Technote",
Expand Down
5 changes: 4 additions & 1 deletion src/git-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,10 @@ export default class GitHelper {
altCommand: `git fetch origin ${context.ref}`,
stderrToStdout: true,
},
'git checkout -qf FETCH_HEAD',
{
command: 'git checkout',
args: ['-qf', 'FETCH_HEAD'],
},
]);
};

Expand Down
26 changes: 22 additions & 4 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ export const isCloned = (workDir: string): boolean => fs.existsSync(path.resolve

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

export const isRef = (ref: string | Context): boolean => /^refs\//.test(getRef(ref));

export const isBranch = (ref: string | Context): boolean => /^(refs\/)?heads\//.test(getRef(ref));

export const isTagRef = (ref: string | Context): boolean => /^refs\/?tags\//.test(getRef(ref));
Expand All @@ -58,15 +60,31 @@ export const getBranch = (ref: string | Context, defaultIsEmpty = true): string

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

export const normalizeRef = (ref: string | Context): string => /^refs\//.test(getRef(ref)) ? getRef(ref) : `refs/heads/${getRef(ref)}`;
export const normalizeRef = (ref: string | Context): string => isRef(ref) ? getRef(ref) : `refs/heads/${getRef(ref)}`;

export const trimRef = (ref: string | Context): string => getRef(ref).replace(/^refs\/(heads|tags|pull)\//, '');

export const getTag = (ref: string | Context): string => isTagRef(ref) ? trimRef(ref) : '';

const saveTarget = (ref: string | Context, origin: string): string => isTagRef(ref) ? 'tags' : isPrRef(ref) ? 'pull' : `remotes/${origin}`;

export const getRefspec = (ref: string | Context, origin = 'origin'): string => `${normalizeRef(ref)}:refs/${saveTarget(ref, origin)}/${trimRef(ref)}`;
const saveTarget = (ref: string | Context, origin: string): string => isTagRef(ref) ? 'tags' : isPrRef(ref) ? 'pull' : origin;

// e.g.
// refs/heads/master
// refs/pull/123/merge
// refs/tags/v1.2.3
export const getRemoteRefspec = (ref: string | Context): string => normalizeRef(ref);

// e.g.
// origin/master
// pull/123/merge
// tags/v1.2.3
export const getLocalRefspec = (ref: string | Context, origin = 'origin'): string => `${saveTarget(ref, origin)}/${trimRef(ref)}`;

// e.g.
// refs/heads/master:refs/remotes/origin/master
// refs/pull/123/merge:refs/pull/123/merge
// refs/tags/v1.2.3:refs/tags/v1.2.3
export const getRefspec = (ref: string | Context, origin = 'origin'): string => `${getRemoteRefspec(ref)}:refs/${getLocalRefspec(ref, `remotes/${origin}`)}`;

export const getAccessToken = (required: boolean): string => getInput('GITHUB_TOKEN', {required});

Expand Down