11import { error as logError , group , warning , info } from "@actions/core" ;
22import { exec } from "@actions/exec" ;
3- import { GitHub } from "@actions/github" ;
4- import { WebhookPayloadPullRequest } from "@octokit/webhooks" ;
3+ import { getOctokit } from "@actions/github" ;
4+ import { GitHub } from "@actions/github/lib/utils" ;
5+ import { EventPayloads } from "@octokit/webhooks" ;
56import escapeRegExp from "lodash.escaperegexp" ;
67
78const labelRegExp = / ^ b a c k p o r t ( [ ^ ] + ) (?: ( [ ^ ] + ) ) ? $ / ;
@@ -11,9 +12,9 @@ const getLabelNames = ({
1112 label,
1213 labels,
1314} : {
14- action : WebhookPayloadPullRequest [ "action" ] ;
15+ action : EventPayloads . WebhookPayloadPullRequest [ "action" ] ;
1516 label : { name : string } ;
16- labels : WebhookPayloadPullRequest [ "pull_request" ] [ "labels" ] ;
17+ labels : EventPayloads . WebhookPayloadPullRequest [ "pull_request" ] [ "labels" ] ;
1718} ) : string [ ] => {
1819 switch ( action ) {
1920 case "closed" :
@@ -31,27 +32,35 @@ const getBackportBaseToHead = ({
3132 labels,
3233 pullRequestNumber,
3334} : {
34- action : WebhookPayloadPullRequest [ "action" ] ;
35+ action : EventPayloads . WebhookPayloadPullRequest [ "action" ] ;
3536 label : { name : string } ;
36- labels : WebhookPayloadPullRequest [ "pull_request" ] [ "labels" ] ;
37+ labels : EventPayloads . WebhookPayloadPullRequest [ "pull_request" ] [ "labels" ] ;
3738 pullRequestNumber : number ;
38- } ) : { [ base : string ] : string } =>
39- getLabelNames ( { action, label, labels } ) . reduce ( ( baseToHead , labelName ) => {
39+ } ) : { [ base : string ] : string } => {
40+ const baseToHead : { [ base : string ] : string } = { } ;
41+
42+ getLabelNames ( { action, label, labels } ) . forEach ( ( labelName ) => {
4043 const matches = labelRegExp . exec ( labelName ) ;
41- if ( matches === null ) {
42- return baseToHead ;
44+
45+ if ( matches !== null ) {
46+ const [
47+ ,
48+ base ,
49+ head = `backport-${ pullRequestNumber } -to-${ base } ` ,
50+ ] = matches ;
51+ baseToHead [ base ] = head ;
4352 }
53+ } ) ;
4454
45- const [ , base , head = `backport-${ pullRequestNumber } -to-${ base } ` ] = matches ;
46- return { ...baseToHead , [ base ] : head } ;
47- } , { } ) ;
55+ return baseToHead ;
56+ } ;
4857
4958const warnIfSquashIsNotTheOnlyAllowedMergeMethod = async ( {
5059 github,
5160 owner,
5261 repo,
5362} : {
54- github : GitHub ;
63+ github : InstanceType < typeof GitHub > ;
5564 owner : string ;
5665 repo : string ;
5766} ) => {
@@ -84,7 +93,7 @@ const backportOnce = async ({
8493 base : string ;
8594 body : string ;
8695 commitToBackport : string ;
87- github : GitHub ;
96+ github : InstanceType < typeof GitHub > ;
8897 head : string ;
8998 labelsToAdd : string [ ] ;
9099 owner : string ;
@@ -153,7 +162,7 @@ const getFailedBackportCommentBody = ({
153162 "# Create a new branch" ,
154163 `git switch --create ${ head } ` ,
155164 "# Cherry-pick the merged commit of this pull request and resolve the conflicts" ,
156- `git cherry-pick ${ commitToBackport } ` ,
165+ `git cherry-pick ---mainline 1 ${ commitToBackport } ` ,
157166 "# Push it to GitHub" ,
158167 `git push --set-upstream origin ${ head } ` ,
159168 "# Go back to the original working tree" ,
@@ -169,8 +178,6 @@ const backport = async ({
169178 labelsToAdd,
170179 payload : {
171180 action,
172- // The payload has a label property when the action is "labeled".
173- // @ts -ignore
174181 label,
175182 pull_request : {
176183 labels,
@@ -188,7 +195,7 @@ const backport = async ({
188195 token,
189196} : {
190197 labelsToAdd : string [ ] ;
191- payload : WebhookPayloadPullRequest ;
198+ payload : EventPayloads . WebhookPayloadPullRequest ;
192199 titleTemplate : string ;
193200 token : string ;
194201} ) => {
@@ -198,7 +205,8 @@ const backport = async ({
198205
199206 const backportBaseToHead = getBackportBaseToHead ( {
200207 action,
201- label,
208+ // The payload has a label property when the action is "labeled".
209+ label : label ! ,
202210 labels,
203211 pullRequestNumber,
204212 } ) ;
@@ -207,7 +215,7 @@ const backport = async ({
207215 return ;
208216 }
209217
210- const github = new GitHub ( token ) ;
218+ const github = getOctokit ( token ) ;
211219
212220 await warnIfSquashIsNotTheOnlyAllowedMergeMethod ( { github, owner, repo } ) ;
213221
@@ -229,15 +237,18 @@ const backport = async ({
229237
230238 for ( const [ base , head ] of Object . entries ( backportBaseToHead ) ) {
231239 const body = `Backport ${ commitToBackport } from #${ pullRequestNumber } ` ;
232- const titleVariables = {
240+
241+ let title = titleTemplate ;
242+ Object . entries ( {
233243 base,
234244 originalTitle,
235- } ;
236- const title = Object . entries ( titleVariables ) . reduce (
237- ( variable , [ name , value ] ) =>
238- variable . replace ( new RegExp ( escapeRegExp ( `{{${ name } }}` ) , "g" ) , value ) ,
239- titleTemplate ,
240- ) ;
245+ } ) . forEach ( ( [ name , value ] ) => {
246+ title = title . replace (
247+ new RegExp ( escapeRegExp ( `{{${ name } }}` ) , "g" ) ,
248+ value ,
249+ ) ;
250+ } ) ;
251+
241252 await group ( `Backporting to ${ base } on ${ head } ` , async ( ) => {
242253 try {
243254 await backportOnce ( {
@@ -252,8 +263,8 @@ const backport = async ({
252263 title,
253264 } ) ;
254265 } catch ( error ) {
255- const errorMessage = error . message ;
256- logError ( `Backport failed: ${ errorMessage } ` ) ;
266+ const errorMessage : string = error . message ;
267+ logError ( error ) ;
257268 await github . issues . createComment ( {
258269 body : getFailedBackportCommentBody ( {
259270 base,
0 commit comments