1
1
// @ts -check
2
2
const vBranchRegex = / ^ v \d { 1 , 3 } \. x $ / ;
3
- const transferLabels = [ 'cherry-pick' ] ;
3
+ const targetBranches = [ ] ;
4
4
5
5
/**
6
6
* @param {Object } params
@@ -22,90 +22,48 @@ module.exports = async ({ core, context, github }) => {
22
22
23
23
core . info ( `>>> PR fetched: ${ pr . number } ` ) ;
24
24
25
- const targetLabels = pr . labels
26
- ?. map ( ( label ) => label . name )
27
- . filter ( ( label ) => vBranchRegex . test ( label ) ) ;
25
+ const prLabels = pr . labels . map ( ( label ) => label . name ) ;
26
+
27
+ // filter the target labels from the original PR
28
+ const targetLabels = prLabels . filter ( ( label ) => vBranchRegex . test ( label ) ) ;
29
+ const otherLabels = prLabels . filter (
30
+ ( label ) => label !== 'needs cherry-pick' && ! vBranchRegex . test ( label ) ,
31
+ ) ;
28
32
29
33
if ( targetLabels . length === 0 ) {
30
34
// there was no target branch present
31
- core . info ( '>>> No target branch label found' ) ;
35
+ core . info ( '>>> No target label found' ) ;
32
36
33
37
if ( vBranchRegex . test ( pr . head_ref ) ) {
34
38
// the branch this is coming from is a version branch, so the cherry-pick target should be master
35
39
core . info ( '>>> Head Ref is a version branch, setting `master` as target' ) ;
36
- core . setOutput ( 'TARGET_BRANCH' , 'master' ) ;
37
- core . setOutput ( 'TRANSFER_LABELS' , transferLabels . join ( ',' ) ) ;
40
+ core . setOutput ( 'TARGET_BRANCHES' , 'master' ) ;
38
41
return ;
39
42
}
40
43
41
- core . setOutput ( 'TARGET_BRANCH' , '' ) ;
42
- core . setOutput ( 'TRANSFER_LABELS ' , transferLabels . join ( ',' ) ) ;
44
+ // the PR is not coming from a version branch
45
+ core . setOutput ( 'TARGET_BRANCHES ' , '' ) ;
43
46
return ;
44
47
}
45
48
46
49
core . info ( `>>> Target labels found: ${ targetLabels . join ( ', ' ) } ` ) ;
47
- let target = '' ;
48
-
49
- // there was a target branch label present
50
- // filter the highest available target number and remove the others from the PR when present
51
- if ( targetLabels . length > 1 ) {
52
- core . info ( `>>> Multiple target labels found.` ) ;
53
- targetLabels . sort ( ( a , b ) => {
54
- const aNum = parseInt ( a . match ( / \d + / ) [ 0 ] , 10 ) ;
55
- const bNum = parseInt ( b . match ( / \d + / ) [ 0 ] , 10 ) ;
56
- return bNum - aNum ;
57
- } ) ;
58
-
59
- target = targetLabels . shift ( ) ;
60
-
61
- core . info ( `>>> Sorting and setting the highest as 'TARGET_BRANCH' output.` ) ;
62
- core . setOutput ( 'TARGET_BRANCH' , target ) ;
63
-
64
- // since we have multiple targets we need to add the "needs cherry-pick" label
65
- // this makes this workflow de-facto recursive
66
- transferLabels . push ( 'needs cherry-pick' ) ;
67
50
68
- // add the other targets to the transfer labels
69
- transferLabels . push ( ...targetLabels ) ;
70
- core . setOutput ( 'TRANSFER_LABELS' , transferLabels . join ( ',' ) ) ;
71
-
72
- // the others will be removed from the PR
73
- core . info ( `>>> Removing the other target labels from the PR` ) ;
74
- for ( const label of targetLabels ) {
75
- await github . rest . issues . removeLabel ( {
76
- owner,
77
- repo,
78
- issue_number : pullNumber ,
79
- name : label ,
80
- } ) ;
81
- }
82
-
83
- core . info ( `>>> Creating explanatory comment on PR` ) ;
84
- await github . rest . issues . createComment ( {
85
- owner,
86
- repo,
87
- issue_number : pullNumber ,
88
- body : [
89
- `The target branch for the cherry-pick PR has been set to \`${ target } \`.` ,
90
- `Branches that will be created after merging are: ${ targetLabels . join ( ', ' ) } ` ,
91
- `Thank you!` ,
92
- ] . join ( '\n\n' ) ,
93
- } ) ;
94
- return ;
95
- }
51
+ // get a list of the original reviewers
52
+ const reviewers = pr . requested_reviewers . map ( ( reviewer ) => reviewer . login ) ;
53
+ core . info ( `>>> Reviewers from original PR: ${ reviewers . join ( ', ' ) } ` ) ;
96
54
97
- core . info ( `>>> Removing the "needs cherry-pick" label from the PR` ) ;
98
- await github . rest . issues . removeLabel ( {
55
+ core . info ( `>>> Creating explanatory comment on PR` ) ;
56
+ await github . rest . issues . createComment ( {
99
57
owner,
100
58
repo,
101
59
issue_number : pullNumber ,
102
- name : 'needs cherry -pick' ,
60
+ body : `Cherry -pick PRs will be created targeting branches: ${ targetLabels . join ( ', ' ) } ` ,
103
61
} ) ;
104
62
105
- target = targetLabels [ 0 ] ;
106
- core . info ( `>>> Setting found 'TARGET_BRANCH' output.` ) ;
107
- core . setOutput ( 'TARGET_BRANCH ' , target ) ;
108
- core . setOutput ( 'TRANSFER_LABELS ' , transferLabels . join ( ',' ) ) ;
63
+ // set the target branches as output to be used as an input for the next step
64
+ core . setOutput ( 'TARGET_BRANCHES' , targetLabels . join ( ',' ) ) ;
65
+ core . setOutput ( 'LABELS ' , [ 'cherry-pick' , ... otherLabels ] . join ( ',' ) ) ;
66
+ core . setOutput ( 'REVIEWERS ' , reviewers . join ( ',' ) ) ;
109
67
} catch ( error ) {
110
68
core . error ( `>>> Workflow failed with: ${ error . message } ` ) ;
111
69
core . setFailed ( error . message ) ;
0 commit comments