Skip to content
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
6 changes: 3 additions & 3 deletions __tests__/utils/variables.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,15 +202,15 @@ describe('getPrBranchName', () => {
}))).toBe('test-ref');
});

it('should throw error', async() => {
await expect(getPrBranchName(helper, octokit, generateActionContext({}))).rejects.toThrow();
await expect(getPrBranchName(helper, octokit, generateActionContext({}, {}, {prBranchName: ''}))).rejects.toThrow();
it('should get run number', async() => {
expect(await getPrBranchName(helper, octokit, generateActionContext({}), true)).toBe('1');
});

it('should throw error', async() => {
await expect(getPrBranchName(helper, octokit, generateActionContext({event: 'pull_request'}, {}, {
prBranchName: '${PR_NUMBER}::${PR_NUMBER_REF}::${PR_ID}::${PR_HEAD_REF}::${PR_BASE_REF}::${PR_TITLE}::${PR_URL}::${PR_MERGE_REF}::${PATCH_VERSION}::${MINOR_VERSION}::${MAJOR_VERSION}::${CURRENT_VERSION}::${PR_LINK}',
}))).rejects.toThrow();
await expect(getPrBranchName(helper, octokit, generateActionContext({}))).rejects.toThrow();
});
});

Expand Down
26 changes: 13 additions & 13 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@technote-space/github-action-pr-helper",
"version": "1.10.3",
"version": "1.11.0",
"description": "PullRequest Helper for GitHub Actions.",
"keywords": [
"github",
Expand Down Expand Up @@ -36,25 +36,25 @@
"dependencies": {
"@actions/core": "^1.2.6",
"@actions/github": "^4.0.0",
"@technote-space/filter-github-action": "^0.5.6",
"@technote-space/github-action-helper": "^4.0.5",
"@technote-space/filter-github-action": "^0.5.7",
"@technote-space/github-action-helper": "^4.2.7",
"moment": "^2.29.1"
},
"devDependencies": {
"@commitlint/cli": "^11.0.0",
"@commitlint/config-conventional": "^11.0.0",
"@technote-space/github-action-test-helper": "^0.6.2",
"@technote-space/github-action-test-helper": "^0.6.4",
"@types/jest": "^26.0.15",
"@types/node": "^14.14.6",
"@typescript-eslint/eslint-plugin": "^4.6.0",
"@typescript-eslint/parser": "^4.6.0",
"eslint": "^7.12.1",
"@types/node": "^14.14.8",
"@typescript-eslint/eslint-plugin": "^4.8.1",
"@typescript-eslint/parser": "^4.8.1",
"eslint": "^7.13.0",
"husky": "^4.3.0",
"jest": "^26.6.1",
"jest-circus": "^26.6.1",
"lint-staged": "^10.5.0",
"nock": "^13.0.4",
"ts-jest": "^26.4.3",
"jest": "^26.6.3",
"jest-circus": "^26.6.3",
"lint-staged": "^10.5.1",
"nock": "^13.0.5",
"ts-jest": "^26.4.4",
"typescript": "^4.0.5"
},
"publishConfig": {
Expand Down
18 changes: 16 additions & 2 deletions src/utils/misc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,27 @@ const {getWorkspace, getPrefixRegExp, getAccessToken} = Util
const {escapeRegExp, replaceAll, getBranch} = Utils;
const {isPr, isCron, isPush, isCustomEvent, isManualEvent, isWorkflowRun} = ContextHelper;

/**
* ParameterRequiredError
*/
export class ParameterRequiredError extends Error {
/**
* @param {string} target target
*/
constructor(target: string) {
super(`parameter [${target}] is required.`);

Object.setPrototypeOf(this, ParameterRequiredError.prototype);
}
}

export const getActionDetail = <T>(key: string, context: ActionContext, defaultValue?: () => T): T => {
if (undefined === defaultValue && !(key in context.actionDetail)) {
throw new Error(`parameter [${key}] is required.`);
throw new ParameterRequiredError(key);
}

if (undefined === defaultValue && typeof context.actionDetail[key] === 'string' && context.actionDetail[key].trim() === '') {
throw new Error(`parameter [${key}] is required.`);
throw new ParameterRequiredError(key);
}

return context.actionDetail[key] || (typeof defaultValue === 'function' ? defaultValue() : undefined);
Expand Down
2 changes: 1 addition & 1 deletion src/utils/process.ts
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ const runCreatePr = async(isClose: boolean, getPulls: (Octokit, ActionContext) =
}

const helper = getHelper(actionContext);
const target = context.actionDetail.prBranchName ? await getPrBranchName(helper, octokit, actionContext) : actionContext.actionContext.payload.number;
const target = await getPrBranchName(helper, octokit, actionContext, true);
if (target in processed) {
results.push(getResult('skipped', `duplicated (${target})`, actionContext));
continue;
Expand Down
43 changes: 33 additions & 10 deletions src/utils/variables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
getPrBranchPrefix,
getPrBranchPrefixForDefaultBranch,
isNotCreatePR,
ParameterRequiredError,
} from './misc';

const {getBranch} = Utils;
Expand Down Expand Up @@ -96,16 +97,38 @@ const contextVariables = async(isComment: boolean, helper: GitHelper, octokit: O
*/
const replaceContextVariables = async(string: string, helper: GitHelper, octokit: Octokit, context: ActionContext): Promise<string> => Utils.replaceVariables(string, await contextVariables(false, helper, octokit, context));

export const getPrBranchName = async(helper: GitHelper, octokit: Octokit, context: ActionContext): Promise<string> =>
isPush(context.actionContext) ?
getBranch(context.actionContext) :
(
isActionPr(context) || isNotCreatePR(context) ? getPrHeadRef(context) : (
await isDefaultBranch(octokit, context) ?
getPrBranchPrefixForDefaultBranch(context) + await replaceContextVariables(getActionDetail<string>('prBranchNameForDefaultBranch', context, () => getActionDetail<string>('prBranchName', context)), helper, octokit, context) :
getPrBranchPrefix(context) + await replaceContextVariables(getActionDetail<string>('prBranchName', context), helper, octokit, context)
)
);
export const getPrBranchName = async(helper: GitHelper, octokit: Octokit, context: ActionContext, isDuplicateCheck = false): Promise<string> => {
if (isPush(context.actionContext)) {
return getBranch(context.actionContext);
}

if (isActionPr(context) || isNotCreatePR(context)) {
return getPrHeadRef(context);
}

let prefix: string, branch: string;
if (await isDefaultBranch(octokit, context)) {
prefix = getPrBranchPrefixForDefaultBranch(context);
} else {
prefix = getPrBranchPrefix(context);
}

try {
if (await isDefaultBranch(octokit, context)) {
branch = getActionDetail<string>('prBranchNameForDefaultBranch', context, () => getActionDetail<string>('prBranchName', context));
} else {
branch = getActionDetail<string>('prBranchName', context);
}
} catch (error) {
if (isDuplicateCheck && (error instanceof ParameterRequiredError)) {
return `${context.actionContext.runNumber}`;
}

throw error;
}

return prefix + await replaceContextVariables(branch, helper, octokit, context);
};

export const getPrTitle = async(helper: GitHelper, octokit: Octokit, context: ActionContext): Promise<string> => await replaceContextVariables(
(
Expand Down
Loading