import type { MergeStrategy } from '../../../config/types.ts'; import { logger } from '../../../logger/index.ts'; import * as hostRules from '../../../util/host-rules.ts'; import { regEx } from '../../../util/regex.ts'; import { parseUrl } from '../../../util/url.ts'; import type { GitUrlOption, Pr } from '../types.ts'; import type { PrMergeMethod, Repo } from './schema.ts'; import type { PrFilterByState } from './types.ts'; export function mapPrState( state: 'open' | 'closed' | undefined, ): 'OPEN' | 'REJECTED' | undefined { switch (state) { case 'open': return 'OPEN'; case 'closed': return 'REJECTED'; default: return undefined; } } export function matchPrState(pr: Pr, state: PrFilterByState): boolean { if (state === 'all') { return true; } if (state === '!open' && (pr.state === 'closed' || pr.state === 'merged')) { return true; } return state === pr.state; } export function smartLinks(body: string): string { return body.replace(regEx(/]\(\.\.\/pull\//g), '](pulls/'); } export function getRepoUrl( repo: Repo, gitUrl: GitUrlOption | undefined, endpoint: string, ): string { const protocolLinks = repo._links.protocol; if (!protocolLinks) { throw new Error('Missing protocol links.'); } if (!Array.isArray(protocolLinks)) { throw new Error('Expected protocol links to be an array of links.'); } if (gitUrl === 'ssh') { const sshUrl = protocolLinks.find((l) => l.name === 'ssh')?.href; if (!sshUrl) { throw new Error('MISSING_SSH_LINKS'); } logger.debug(`Using SSH URL: ${sshUrl}`); return sshUrl; } const httpUrl = protocolLinks.find((l) => l.name === 'http')?.href; if (!httpUrl) { throw new Error('MISSING_HTTP_LINK'); } logger.debug(`Using HTTP URL: ${httpUrl}`); const repoUrl = parseUrl(httpUrl); if (!repoUrl) { throw new Error('MALFORMED_HTTP_LINK'); } const hostOptions = hostRules.find({ hostType: 'scm-manager', url: endpoint, }); repoUrl.username = hostOptions.username ?? ''; repoUrl.password = hostOptions.token ?? ''; return repoUrl.toString(); } export function getMergeMethod( strategy: MergeStrategy | undefined, ): PrMergeMethod | null { switch (strategy) { case 'fast-forward': return 'FAST_FORWARD_ONLY'; case 'merge-commit': return 'MERGE_COMMIT'; case 'rebase': return 'REBASE'; case 'squash': return 'SQUASH'; default: return null; } }