import { RequestError } from "@octokit/request-error" ; import { ProbotOctokit } from "probot" ; import { DevError } from "../logging/dev-error" ; import { LoggedError } from "../logging/logged-error" ; import { createCommit , FileToCreate } from "./create-commit" ; export async function createPullRequest ( args ) { let parentBranchSHA = "" ; const refArgs = { owner : args . org , ref : `heads/${ args . parentBranch }` , repo : args . repo , } ; try { const getRefResult = await args . octokit . git . getRef ( refArgs ) ; parentBranchSHA = getRefResult . data . object . sha ; } catch ( e ) { throw new DevError ( `CreatePullRequest: cannot get the SHA of the parent branch` , e , { ... args , callArgs : refArgs } ) ; } const createBranchArgs = { owner : args . org , ref : `refs/heads/${ args . branch }` , repo : args . repo , sha : parentBranchSHA , } ; try { await args . octokit . git . createRef ( createBranchArgs ) ; } catch ( e ) { throw new DevError ( `CreatePullRequest: cannot create a new branch for the changes` , e , { ... args , callArgs : createBranchArgs , } ) ; } try { await createCommit ( args ) ; } catch ( e ) { if ( e instanceof RequestError ) { if ( e . status === 403 && e . message === "Resource not accessible by integration" ) { console . log ( `${ args . org }|${ args . repo }|${ args . branch }: USER HASN'T ACCEPTED THE PERMISSIONS TO EDIT CONTENT` ) ; throw new LoggedError ( ) ; } } throw new DevError ( `CreatePullRequest: cannot commit corrections into the branch for pull requests` , e , { ... args , callArgs : args , } ) ; } const createPRArgs = { base : args . parentBranch , body : args . body , head : args . branch , owner : args . org , repo : args . repo , title : args . message , } ; try { await args . octokit . pulls . create ( createPRArgs ) ; } catch ( e ) { throw new DevError ( `CreatePullRequest: cannot create the pull request` , e , { ... args , callArgs : createPRArgs , } ) ; } }