Skip to content

armgit / orca

Public Imported from GitHub
0

Orca is the place for working with a fleet of agents (e.g. Grok Bot, Claude Code). Run any coding agent with your own subscription. Available on desktop, mobile and remote runtime.

2.3 KB
Raw Blame
import type { GitHubPRMergeMethod, GitHubPRMergeMethodSettings } from './pull-request-types'

export const GITHUB_PR_MERGE_METHODS = ['squash', 'merge', 'rebase'] 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]
  }
}