UNPKG

backport

Version:

A CLI tool that automates the process of backporting commits

57 lines 2.27 kB
import { RequestError } from '@octokit/request-error'; import { BackportError } from '../../../backport-error.js'; import { logger } from '../../../logger.js'; import { ora } from '../../../ora.js'; import { fetchExistingPullRequest } from '../../v4/fetch-existing-pull-request.js'; import { getGithubV3ErrorMessage } from '../get-github-v3-error-message.js'; import { createOctokitClient, retryOctokitRequest } from '../octokit-client.js'; export async function createPullRequest({ options, prPayload, }) { const msg = `Creating ${options.draft ? 'draft ' : ''}pull request`; logger.info(`${msg} with title: "${prPayload.title}". ${prPayload.head} -> ${prPayload.base}`); const { githubToken, githubApiBaseUrlV3 } = options; const spinner = ora(options.interactive, msg).start(); if (options.dryRun) { spinner.succeed(); return { number: 1337, url: 'this-is-a-dry-run' }; } try { const octokit = createOctokitClient({ githubToken, githubApiBaseUrlV3 }); const res = await retryOctokitRequest(() => octokit.pulls.create(prPayload)); spinner.succeed(); return { url: res.data.html_url, number: res.data.number, }; } catch (error) { // retrieve url for existing try { const existingPR = await fetchExistingPullRequest({ options, prPayload, }); if (existingPR) { spinner.succeed('Updating existing pull request'); return { url: String(existingPR.url), number: existingPR.number, }; } } catch (error_) { logger.error('Could not retrieve existing pull request', error_); // swallow error } spinner.fail(); const message = error instanceof RequestError ? getGithubV3ErrorMessage(error) : error instanceof Error ? error.message : String(error); throw new BackportError({ code: 'pr-creation-exception', message: `Could not create pull request: ${message}`, }); } } //# sourceMappingURL=create-pull-request.js.map