Files
orca/src/shared/github/pull-requests/merge/pull-request-merge-methods.ts
T

82 lines
2.6 KiB
TypeScript

import type { GitHubPRMergeMethod, GitHubPRMergeMethodSettings } from '../pull-request-types'
export const GITHUB_PR_MERGE_METHODS = ['squash', 'merge', 'rebase'] as const
// Shared by the mobile hosted-review surfaces, whose catalogs carry these keys.
export const GITHUB_PR_MERGE_METHOD_I18N_KEYS = {
squash: 'pullRequest.prActionsState.methodSquash',
merge: 'pullRequest.prActionsState.methodMerge',
rebase: 'pullRequest.prActionsState.methodRebase'
} as const
export type GitHubPRMergeMethodPresentation = {
defaultMethod: GitHubPRMergeMethod
methods: GitHubPRMergeMethod[]
}
function allMethodsAllowed(): Record<GitHubPRMergeMethod, boolean> {
return {
squash: true,
merge: true,
rebase: true
}
}
export function mapGitHubDefaultMergeMethod(value: unknown): GitHubPRMergeMethod | null {
switch (typeof value === 'string' ? value.toUpperCase() : '') {
case 'MERGE':
return 'merge'
case 'SQUASH':
return 'squash'
case 'REBASE':
return 'rebase'
default:
return null
}
}
export function normalizeGitHubPRMergeMethodSettings(args: {
defaultMethod: unknown
mergeCommitAllowed: unknown
rebaseMergeAllowed: unknown
squashMergeAllowed: unknown
}): GitHubPRMergeMethodSettings | undefined {
const allowedMethods = {
squash: args.squashMergeAllowed === true,
merge: args.mergeCommitAllowed === true,
rebase: args.rebaseMergeAllowed === true
}
const defaultMethod = mapGitHubDefaultMergeMethod(args.defaultMethod)
const firstAllowedMethod = GITHUB_PR_MERGE_METHODS.find((method) => allowedMethods[method])
const resolvedDefault =
defaultMethod && allowedMethods[defaultMethod]
? defaultMethod
: (firstAllowedMethod ?? defaultMethod)
if (!resolvedDefault) {
return undefined
}
return {
defaultMethod: resolvedDefault,
allowedMethods
}
}
export function resolveGitHubPRMergeMethods(
settings?: GitHubPRMergeMethodSettings | null
): GitHubPRMergeMethodPresentation {
const allowedMethods = settings?.allowedMethods ?? allMethodsAllowed()
const firstAllowedMethod = GITHUB_PR_MERGE_METHODS.find((method) => allowedMethods[method])
const defaultMethod =
settings?.defaultMethod && allowedMethods[settings.defaultMethod]
? settings.defaultMethod
: (firstAllowedMethod ?? 'squash')
const orderedMethods = [
defaultMethod,
...GITHUB_PR_MERGE_METHODS.filter((method) => method !== defaultMethod)
].filter((method) => allowedMethods[method])
return {
defaultMethod,
methods: orderedMethods.length > 0 ? orderedMethods : [...GITHUB_PR_MERGE_METHODS]
}
}