diff --git a/__tests__/utils/variables.test.ts b/__tests__/utils/variables.test.ts index b684fb3..222819c 100644 --- a/__tests__/utils/variables.test.ts +++ b/__tests__/utils/variables.test.ts @@ -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(); }); }); diff --git a/src/utils/misc.ts b/src/utils/misc.ts index 48058ed..470090c 100644 --- a/src/utils/misc.ts +++ b/src/utils/misc.ts @@ -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 = (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); diff --git a/src/utils/process.ts b/src/utils/process.ts index d674b4c..55dd4e4 100644 --- a/src/utils/process.ts +++ b/src/utils/process.ts @@ -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; diff --git a/src/utils/variables.ts b/src/utils/variables.ts index df947f6..d80196c 100644 --- a/src/utils/variables.ts +++ b/src/utils/variables.ts @@ -16,6 +16,7 @@ import { getPrBranchPrefix, getPrBranchPrefixForDefaultBranch, isNotCreatePR, + ParameterRequiredError, } from './misc'; const {getBranch} = Utils; @@ -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 => Utils.replaceVariables(string, await contextVariables(false, helper, octokit, context)); -export const getPrBranchName = async(helper: GitHelper, octokit: Octokit, context: ActionContext): Promise => - isPush(context.actionContext) ? - getBranch(context.actionContext) : - ( - isActionPr(context) || isNotCreatePR(context) ? getPrHeadRef(context) : ( - await isDefaultBranch(octokit, context) ? - getPrBranchPrefixForDefaultBranch(context) + await replaceContextVariables(getActionDetail('prBranchNameForDefaultBranch', context, () => getActionDetail('prBranchName', context)), helper, octokit, context) : - getPrBranchPrefix(context) + await replaceContextVariables(getActionDetail('prBranchName', context), helper, octokit, context) - ) - ); +export const getPrBranchName = async(helper: GitHelper, octokit: Octokit, context: ActionContext, isDuplicateCheck = false): Promise => { + 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('prBranchNameForDefaultBranch', context, () => getActionDetail('prBranchName', context)); + } else { + branch = getActionDetail('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 => await replaceContextVariables( (