@@ -11,23 +11,118 @@ module.exports = async ({ github, context, core }) => {
11
11
const RE_TAG_NIGHTLY = / ^ v ( \d + ) \. ( \d + ) \. ( \d + ) - n i g h t l y $ / ;
12
12
const RE_TAG_PATCH = / ^ v ( \d + ) \. ( \d + ) \. ( \d + ) - p ( \d + ) $ / ;
13
13
14
- switch ( TYPE ) {
15
- case "" :
16
- case "nightly" : {
17
- core . setOutput ( "sha" , context . sha ) ;
18
- core . info ( `Nightly release triggered by (${ context . sha } )` ) ;
14
+ async function getPreviousNightlyRelease ( github , context ) {
15
+ const releases = await github . rest . repos . listReleases ( {
16
+ owner : context . repo . owner ,
17
+ repo : context . repo . repo ,
18
+ } ) ;
19
+ for ( const release of releases . data ) {
20
+ const ret = RE_TAG_NIGHTLY . exec ( release . tag_name ) ;
21
+ if ( ret ) {
22
+ return release . tag_name ;
23
+ }
24
+ }
25
+ }
26
+
27
+ function getNextNightlyRelease ( previous ) {
28
+ const nightly = RE_TAG_NIGHTLY . exec ( previous ) ;
29
+ if ( nightly ) {
30
+ const major = nightly [ 1 ] ;
31
+ const minor = nightly [ 2 ] ;
32
+ const patch = parseInt ( nightly [ 3 ] ) + 1 ;
33
+ return `v${ major } .${ minor } .${ patch } -nightly` ;
34
+ }
35
+ }
19
36
20
- let previous = null ;
37
+ async function getPreviousStableRelease ( github , context ) {
38
+ let page = 1 ;
39
+ while ( true ) {
21
40
const releases = await github . rest . repos . listReleases ( {
22
41
owner : context . repo . owner ,
23
42
repo : context . repo . repo ,
43
+ page,
24
44
} ) ;
45
+ if ( releases . data . length === 0 ) {
46
+ break ;
47
+ }
48
+ page ++ ;
25
49
for ( const release of releases . data ) {
26
- const ret = RE_TAG_NIGHTLY . exec ( release . tag_name ) ;
50
+ const ret = RE_TAG_STABLE . exec ( release . tag_name ) ;
27
51
if ( ret ) {
28
- previous = release . tag_name ;
29
- break ;
52
+ return release . tag_name ;
53
+ }
54
+ }
55
+ }
56
+ }
57
+
58
+ function getNextStableRelease ( ) {
59
+ const nightly = RE_TAG_NIGHTLY . exec ( TAG ) ;
60
+ if ( nightly ) {
61
+ const major = nightly [ 1 ] ;
62
+ const minor = nightly [ 2 ] ;
63
+ const patch = nightly [ 3 ] ;
64
+ return `v${ major } .${ minor } .${ patch } ` ;
65
+ }
66
+ }
67
+
68
+ async function getPreviousPatchRelease ( github , context ) {
69
+ let page = 1 ;
70
+ while ( true ) {
71
+ const releases = await github . rest . repos . listReleases ( {
72
+ owner : context . repo . owner ,
73
+ repo : context . repo . repo ,
74
+ page,
75
+ } ) ;
76
+ if ( releases . data . length === 0 ) {
77
+ break ;
78
+ }
79
+ page ++ ;
80
+ for ( const release of releases . data ) {
81
+ if ( ! release . tag_name . startsWith ( TAG ) ) {
82
+ continue ;
83
+ }
84
+ if ( release . tag_name === TAG ) {
85
+ // no previous patch release, use the previous stable release
86
+ return release . tag_name ;
87
+ }
88
+ const ret = RE_TAG_PATCH . exec ( release . tag_name ) ;
89
+ if ( ! ret ) {
90
+ core . warning ( `Ignore invalid patch release ${ release . tag_name } ` ) ;
91
+ continue ;
30
92
}
93
+ return release . tag_name ;
94
+ }
95
+ }
96
+ }
97
+
98
+ function getNextPatchRelease ( previous ) {
99
+ const stable = RE_TAG_STABLE . exec ( previous ) ;
100
+ if ( stable ) {
101
+ const major = stable [ 1 ] ;
102
+ const minor = stable [ 2 ] ;
103
+ const patch = stable [ 3 ] ;
104
+ return `v${ major } .${ minor } .${ patch } -p1` ;
105
+ }
106
+ const patch = RE_TAG_PATCH . exec ( previous ) ;
107
+ if ( patch ) {
108
+ const major = patch [ 1 ] ;
109
+ const minor = patch [ 2 ] ;
110
+ const patch = patch [ 3 ] ;
111
+ const pv = parseInt ( patch [ 4 ] ) ;
112
+ return `v${ major } .${ minor } .${ patch } -p${ pv + 1 } ` ;
113
+ }
114
+ }
115
+
116
+ switch ( TYPE ) {
117
+ case "" :
118
+ case "nightly" : {
119
+ core . setOutput ( "sha" , context . sha ) ;
120
+ core . info ( `Nightly release triggered by (${ context . sha } )` ) ;
121
+
122
+ const previous = await getPreviousNightlyRelease ( github , context ) ;
123
+ if ( ! previous ) {
124
+ core . setFailed ( `No previous nightly release found, ignoring` ) ;
125
+ return ;
31
126
}
32
127
core . setOutput ( "previous" , previous ) ;
33
128
core . info ( `Nightly release with previous release: ${ previous } ` ) ;
@@ -37,15 +132,11 @@ module.exports = async ({ github, context, core }) => {
37
132
core . info ( `Release create manually with tag ${ TAG } ` ) ;
38
133
return ;
39
134
}
40
- const result = RE_TAG_NIGHTLY . exec ( previous ) ;
41
- if ( ! result ) {
42
- core . setFailed ( `The previous tag ${ previous } is invalid. ` ) ;
135
+ const nextTag = getNextNightlyRelease ( previous ) ;
136
+ if ( ! nextTag ) {
137
+ core . setFailed ( `No next nightly release from ${ previous } ` ) ;
43
138
return ;
44
139
}
45
- const major = result [ 1 ] ;
46
- const minor = result [ 2 ] ;
47
- const patch = ( parseInt ( result [ 3 ] ) + 1 ) . toString ( ) ;
48
- const nextTag = `v${ major } .${ minor } .${ patch } -nightly` ;
49
140
core . setOutput ( "tag" , nextTag ) ;
50
141
core . info ( `Release create new nightly ${ nextTag } ` ) ;
51
142
return ;
@@ -58,38 +149,15 @@ module.exports = async ({ github, context, core }) => {
58
149
return ;
59
150
}
60
151
core . info ( `Stable release triggered by ${ TAG } (${ context . sha } )` ) ;
61
- const result = RE_TAG_NIGHTLY . exec ( TAG ) ;
62
- if ( ! result ) {
63
- core . setFailed ( `The tag ${ TAG } is invalid, ignoring ` ) ;
152
+ const nextTag = getNextStableRelease ( ) ;
153
+ if ( ! nextTag ) {
154
+ core . setFailed ( `No stable release from ${ TAG } ` ) ;
64
155
return ;
65
156
}
66
- const major = result [ 1 ] ;
67
- const minor = result [ 2 ] ;
68
- const patch = result [ 3 ] ;
69
- const nextTag = `v${ major } .${ minor } .${ patch } ` ;
70
157
core . setOutput ( "tag" , nextTag ) ;
71
158
core . info ( `Stable release ${ nextTag } from ${ TAG } ` ) ;
72
159
73
- let previous = null ;
74
- let page = 1 ;
75
- while ( true ) {
76
- const releases = await github . rest . repos . listReleases ( {
77
- owner : context . repo . owner ,
78
- repo : context . repo . repo ,
79
- page,
80
- } ) ;
81
- if ( releases . data . length === 0 ) {
82
- break ;
83
- }
84
- page ++ ;
85
- for ( const release of releases . data ) {
86
- const ret = RE_TAG_STABLE . exec ( release . tag_name ) ;
87
- if ( ret ) {
88
- previous = release . tag_name ;
89
- break ;
90
- }
91
- }
92
- }
160
+ const previous = await getPreviousStableRelease ( github , context ) ;
93
161
if ( ! previous ) {
94
162
core . setFailed ( `No previous stable release found, ignoring` ) ;
95
163
return ;
@@ -120,49 +188,21 @@ module.exports = async ({ github, context, core }) => {
120
188
`Patch release triggered by ${ TAG } (${ branch . data . commit . sha } )`
121
189
) ;
122
190
123
- let pv = 1 ;
124
- let previous = null ;
125
- let page = 1 ;
126
- while ( true ) {
127
- const releases = await github . rest . repos . listReleases ( {
128
- owner : context . repo . owner ,
129
- repo : context . repo . repo ,
130
- page,
131
- } ) ;
132
- if ( releases . data . length === 0 ) {
133
- break ;
134
- }
135
- page ++ ;
136
- for ( const release of releases . data ) {
137
- if ( ! release . tag_name . startsWith ( TAG ) ) {
138
- continue ;
139
- }
140
- if ( release . tag_name === TAG ) {
141
- previous = release . tag_name ;
142
- break ;
143
- }
144
- const ret = RE_TAG_PATCH . exec ( release . tag_name ) ;
145
- if ( ! ret ) {
146
- core . warning ( `Ignore previous release ${ release . tag_name } ` ) ;
147
- continue ;
148
- }
149
- pv = parseInt ( result [ 4 ] ) + 1 ;
150
- previous = release . tag_name ;
151
- }
152
- }
191
+ const previous = await getPreviousPatchRelease ( github , context ) ;
153
192
if ( ! previous ) {
154
- core . setFailed ( `No previous stable release found, ignoring` ) ;
193
+ core . setFailed ( `No previous patch release found, ignoring` ) ;
155
194
return ;
156
195
}
157
196
core . setOutput ( "previous" , previous ) ;
158
197
core . info ( `Patch release with previous release: ${ previous } ` ) ;
159
198
160
- const major = result [ 1 ] ;
161
- const minor = result [ 2 ] ;
162
- const patch = result [ 3 ] ;
163
- const nextTag = `v${ major } .${ minor } .${ patch } -p${ pv } ` ;
199
+ const nextTag = getNextPatchRelease ( previous ) ;
200
+ if ( ! nextTag ) {
201
+ core . setFailed ( `No next patch release from ${ previous } ` ) ;
202
+ return ;
203
+ }
164
204
core . setOutput ( "tag" , nextTag ) ;
165
- core . info ( `Patch release ${ nextTag } from ${ TAG } ` ) ;
205
+ core . info ( `Patch release ${ nextTag } from ${ previous } ` ) ;
166
206
return ;
167
207
}
168
208
0 commit comments