11import { existsSync , rmSync } from 'node:fs'
22import { Octokit } from '@octokit/rest'
33
4- import { cherryPickCommits , cloneAndCacheRepo , hasEmptyCommits , hasSkipCiCommits , pushBranch } from './gitUtils'
4+ import { cherryPickCommits , cloneAndCacheRepo , hasDiff , hasEmptyCommits , hasSkipCiCommits , pushBranch } from './gitUtils'
55import { CherryPickResult , Task } from './constants'
66import { debug , error , info , warn } from './logUtils'
77import { Reaction , addReaction , getAuthToken , getAvailableLabels , getLabelsFromPR , getAvailableMilestones , requestReviewers , getReviewers , createBackportPullRequest , setPRLabels , setPRMilestone , getChangesFromPR , updatePRBody , commentOnPR , assignToPR } from './githubUtils'
@@ -27,8 +27,7 @@ export const backport = (task: Task) => new Promise<void>((resolve, reject) => {
2727 tmpDir = await cloneAndCacheRepo ( task , backportBranch )
2828 info ( task , `Cloned to ${ tmpDir } ` )
2929 } catch ( e ) {
30- reject ( `Failed to clone repository: ${ e . message } ` )
31- throw e
30+ throw new Error ( `Failed to clone repository: ${ e . message } ` )
3231 }
3332
3433 // Cherry pick the commits
@@ -40,17 +39,20 @@ export const backport = (task: Task) => new Promise<void>((resolve, reject) => {
4039 info ( task , `Cherry picking commits successful` )
4140 }
4241 } catch ( e ) {
43- reject ( `Failed to cherry pick commits: ${ e . message } ` )
44- throw e
42+ throw new Error ( `Failed to cherry pick commits: ${ e . message } ` )
43+ }
44+
45+ // Check if there are any changes to backport
46+ if ( ! await hasDiff ( tmpDir , task . branch , backportBranch ) ) {
47+ throw new Error ( `No changes found in backport branch` )
4548 }
4649
4750 // Push the branch
4851 try {
4952 await pushBranch ( task , tmpDir , token )
5053 info ( task , `Pushed branch ${ backportBranch } ` )
5154 } catch ( e ) {
52- reject ( `Failed to push branch: ${ e . message } ` )
53- throw e
55+ throw new Error ( `Failed to push branch: ${ e . message } ` )
5456 }
5557
5658 // Create the pull request
@@ -71,11 +73,10 @@ export const backport = (task: Task) => new Promise<void>((resolve, reject) => {
7173 await requestReviewers ( octokit , task , prNumber , [ task . author ] )
7274 info ( task , `Requested reviews from ${ [ ...reviewers , task . author ] . join ( ', ' ) } ` )
7375 } catch ( e ) {
74- reject ( `Failed to request reviews: ${ e . message } ` )
76+ throw new Error ( `Failed to request reviews: ${ e . message } ` )
7577 }
7678 } catch ( e ) {
77- reject ( `Failed to create pull request: ${ e . message } ` )
78- throw e
79+ throw new Error ( `Failed to create pull request: ${ e . message } ` )
7980 }
8081
8182 // Get labels from original PR and set them on the new PR
@@ -86,7 +87,8 @@ export const backport = (task: Task) => new Promise<void>((resolve, reject) => {
8687 await setPRLabels ( octokit , task , prNumber , labels )
8788 info ( task , `Set labels: ${ labels . join ( ', ' ) } ` )
8889 } catch ( e ) {
89- reject ( `Failed to get labels: ${ e . message } ` )
90+ error ( task , `Failed to get and set labels: ${ e . message } ` )
91+ // continue, this is not a fatal error
9092 }
9193
9294 // Find new appropriate Milestone and set it on the new PR
@@ -96,15 +98,17 @@ export const backport = (task: Task) => new Promise<void>((resolve, reject) => {
9698 await setPRMilestone ( octokit , task , prNumber , milestone )
9799 info ( task , `Set milestone: ${ milestone . title } ` )
98100 } catch ( e ) {
99- warn ( task , `Failed to find appropriate milestone: ${ e . message } ` )
101+ error ( task , `Failed to find appropriate milestone: ${ e . message } ` )
102+ // continue, this is not a fatal error
100103 }
101104
102105 // Assign the PR to the author of the original PR
103106 try {
104107 await assignToPR ( octokit , task , prNumber , [ task . author ] )
105108 info ( task , `Assigned original author: ${ task . author } ` )
106109 } catch ( e ) {
107- reject ( `Failed to assign PR: ${ e . message } ` )
110+ error ( task , `Failed to assign PR: ${ e . message } ` )
111+ // continue, this is not a fatal error
108112 }
109113
110114 // Compare the original PR with the new PR
@@ -125,10 +129,12 @@ export const backport = (task: Task) => new Promise<void>((resolve, reject) => {
125129 await updatePRBody ( octokit , task , prNumber , newBody )
126130 }
127131 } catch ( e ) {
128- reject ( `Failed to update PR body: ${ e . message } ` )
132+ error ( task , `Failed to update PR body: ${ e . message } ` )
133+ // continue, this is not a fatal error
129134 }
130135 } catch ( e ) {
131- reject ( `Failed to compare changes: ${ e . message } ` )
136+ error ( task , `Failed to compare changes: ${ e . message } ` )
137+ // continue, this is not a fatal error
132138 }
133139
134140 // Success! We're done here
@@ -140,8 +146,11 @@ export const backport = (task: Task) => new Promise<void>((resolve, reject) => {
140146 const failureComment = getFailureCommentBody ( task , backportBranch , e ?. message )
141147 await commentOnPR ( octokit , task , failureComment )
142148 } catch ( e ) {
143- reject ( `Failed to comment failure on PR: ${ e . message } ` )
149+ error ( task , `Failed to comment failure on PR: ${ e . message } ` )
150+ // continue, this is not a fatal error
144151 }
152+
153+ reject ( `Failed to backport: ${ e . message } ` )
145154 }
146155
147156 // Remove the temp dir if it exists
0 commit comments