/** * Copyright (c) Microsoft Corporation. * Licensed under the MIT License. * * GitHub operations via the gh CLI. * * @format */ import fs from 'node:fs'; import os from 'os'; import path from 'path'; import {exec} from './proc'; export interface PRInfo { number: number; url: string; } /** * Run a callback with a temporary file containing the given content. * The file is cleaned up after the callback completes (success or failure). */ async function withTempFile( content: string, fn: (filePath: string) => Promise, ): Promise { const tmpFile = path.join( os.tmpdir(), `gh-pr-body-${process.pid}-${Date.now()}.md`, ); fs.writeFileSync(tmpFile, content, 'utf8'); try { return await fn(tmpFile); } finally { try { fs.unlinkSync(tmpFile); } catch { // Ignore cleanup errors } } } /** * Find an existing open PR by head branch name. * Returns null if no matching PR exists. */ export async function findPR(opts: { head: string; cwd: string; repo?: string; }): Promise { const repoFlag = opts.repo ? ` --repo "${opts.repo}"` : ''; const result = await exec( `gh pr list --head "${opts.head}" --json number,url --limit 1${repoFlag}`, {cwd: opts.cwd, fallback: '[]'}, ); let prs: Array<{number: number; url: string}>; try { prs = JSON.parse(result); } catch { return null; } if (!Array.isArray(prs) || prs.length === 0) { return null; } return {number: prs[0].number, url: prs[0].url}; } /** * Create a new pull request. Returns the PR number and URL. * * Uses --body-file with a temp file to avoid shell escaping issues * with multiline markdown content on Windows cmd.exe. */ export async function createPR(opts: { head: string; base: string; title: string; body: string; cwd: string; repo?: string; }): Promise { return withTempFile(opts.body, async bodyFile => { const repoFlag = opts.repo ? ` --repo "${opts.repo}"` : ''; const result = await exec( `gh pr create --head "${opts.head}" --base "${opts.base}"` + ` --title "${escapeForShell(opts.title)}"` + ` --body-file "${bodyFile}"${repoFlag} --label release`, {cwd: opts.cwd}, ); // gh pr create outputs the PR URL on success const url = result.trim(); const match = url.match(/\/pull\/(\d+)/); const number = match ? parseInt(match[1], 10) : 0; return {number, url}; }); } /** * Update an existing pull request's title, body and release label. * * Uses --body-file with a temp file to avoid shell escaping issues * with multiline markdown content on Windows cmd.exe. */ export async function updatePR(opts: { number: number; title: string; body: string; cwd: string; repo?: string; }): Promise { await withTempFile(opts.body, async bodyFile => { const repoFlag = opts.repo ? ` --repo "${opts.repo}"` : ''; await exec( `gh pr edit ${opts.number} --title "${escapeForShell(opts.title)}"` + ` --body-file "${bodyFile}"${repoFlag} --add-label release`, {cwd: opts.cwd}, ); }); } /** * Escape a string for safe inclusion in a double-quoted shell argument. * Used for single-line values like titles; multiline content uses --body-file. */ function escapeForShell(str: string): string { return str .replace(/\\/g, '\\\\') .replace(/"/g, '\\"') .replace(/\$/g, '\\$') .replace(/`/g, '\\`'); }