21
21
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
22
* SOFTWARE.
23
23
*/
24
- const { runCommandSync, resolveBin , error } = require ( '@instructure/command-utils' )
24
+ const { runCommandSync, error , info } = require ( '@instructure/command-utils' )
25
25
const validateMessage = require ( 'validate-commit-msg' )
26
26
27
27
const runGitCommand = exports . runGitCommand = function runGitCommand ( args = [ ] ) {
@@ -49,7 +49,7 @@ exports.setupGit = function setupGit () {
49
49
remotes = runGitCommand ( [ 'remote' ] ) . split ( / ( \s + ) / )
50
50
} catch ( err ) {
51
51
error ( err )
52
- process . exit ( )
52
+ process . exit ( 1 )
53
53
}
54
54
55
55
if ( GIT_REMOTE_URL ) {
@@ -79,21 +79,27 @@ exports.isReleaseCommit = function isReleaseCommit (version) {
79
79
let result
80
80
81
81
try {
82
- const { stdout } = runCommandSync ( 'git' , [
83
- 'log' , '--oneline' , '--format=%B' , '-n' , '1' , 'HEAD' , '|' ,
84
- resolveBin ( 'head' ) , '-n' , '1' , '|' ,
85
- resolveBin ( 'grep' ) , '"chore(release)"'
86
- ] , [ ] , { stdio : 'pipe' } )
87
- result = stdout
82
+ result = runGitCommand ( [ 'log' , '-1' , '--pretty=format:%B' ] )
83
+ result = result . split ( '\n' ) [ 0 ] . trim ( )
88
84
} catch ( e ) {
89
- return false
85
+ error ( e )
86
+ process . exit ( 1 )
90
87
}
91
88
92
- return result && ( result . trim ( ) === `chore(release): ${ version } ` )
89
+ info ( result )
90
+
91
+ return result && ( result . startsWith ( `chore(release): ${ version } ` ) )
93
92
}
94
93
95
94
exports . checkWorkingDirectory = function checkWorkingDirectory ( ) {
96
- const result = runGitCommand ( [ 'status' , '--porcelain' ] )
95
+ let result
96
+
97
+ try {
98
+ result = runGitCommand ( [ 'status' , '--porcelain' ] )
99
+ } catch ( e ) {
100
+ error ( e )
101
+ process . exit ( 1 )
102
+ }
97
103
98
104
if ( result ) {
99
105
error ( `Refusing to operate on unclean working directory!` )
@@ -104,7 +110,14 @@ exports.checkWorkingDirectory = function checkWorkingDirectory () {
104
110
105
111
exports . checkIfGitTagExists = function checkIfGitTagExists ( version ) {
106
112
const tag = `v${ version } `
107
- const result = runGitCommand ( [ 'tag' , '--list' , tag ] )
113
+ let result
114
+
115
+ try {
116
+ result = runGitCommand ( [ 'tag' , '--list' , tag ] )
117
+ } catch ( e ) {
118
+ error ( e )
119
+ process . exit ( 1 )
120
+ }
108
121
109
122
if ( result ) {
110
123
error ( `Git tag ${ tag } already exists!` )
@@ -114,9 +127,16 @@ exports.checkIfGitTagExists = function checkIfGitTagExists (version) {
114
127
}
115
128
116
129
exports . checkIfCommitIsReviewed = function checkIfCommitIsReviewed ( ) {
117
- const result = runGitCommand ( [ 'log' , '-n' , '1' , '|' , resolveBin ( 'grep' ) , 'Reviewed-on' ] )
130
+ let result
118
131
119
- if ( ! result ) {
132
+ try {
133
+ result = runGitCommand ( [ 'log' , '-1' , '--pretty=format:%B' ] )
134
+ } catch ( e ) {
135
+ error ( e )
136
+ process . exit ( 1 )
137
+ }
138
+
139
+ if ( ! result || result . indexOf ( 'Reviewed-on' ) < 0 ) {
120
140
error ( 'The release commit must be reviewed and merged prior to running the release!' )
121
141
error ( 'Use "git pull --rebase" to pull down the latest from the remote.' )
122
142
process . exit ( 1 )
@@ -128,9 +148,13 @@ exports.createGitTagForRelease = function createGitTagForRelease (version) {
128
148
const { GIT_REMOTE_NAME } = process . env
129
149
const origin = GIT_REMOTE_NAME || 'origin'
130
150
131
- runGitCommand ( [ 'tag' , '-am' , `Version ${ version } ` , tag ] )
132
-
133
- runGitCommand ( [ 'push' , origin , tag ] )
151
+ try {
152
+ runGitCommand ( [ 'tag' , '-am' , `Version ${ version } ` , tag ] )
153
+ runGitCommand ( [ 'push' , origin , tag ] )
154
+ } catch ( e ) {
155
+ error ( e )
156
+ process . exit ( 1 )
157
+ }
134
158
}
135
159
136
160
exports . commitVersionBump = function commitVersionBump ( releaseVersion ) {
@@ -140,3 +164,79 @@ exports.commitVersionBump = function commitVersionBump (releaseVersion) {
140
164
exports . resetToCommit = function resetToCommit ( commitish = 'HEAD' ) {
141
165
runGitCommand ( [ 'reset' , '--hard' , commitish ] )
142
166
}
167
+
168
+ function getPreviousReleaseCommit ( ) {
169
+ return runGitCommand ( [ 'rev-list' , '--tags' , '--skip=1' , '--max-count=1' ] )
170
+ }
171
+ exports . getPreviousReleaseCommit = getPreviousReleaseCommit
172
+
173
+ function getCurrentReleaseTag ( ) {
174
+ return runGitCommand ( [ 'describe' , '--exact-match' ] )
175
+ }
176
+ exports . getCurrentReleaseTag = getCurrentReleaseTag
177
+
178
+ function getPreviousReleaseTag ( ) {
179
+ return runGitCommand ( [ 'describe' , '--abbrev=0' , '--tags' , getPreviousReleaseCommit ( ) ] )
180
+ }
181
+ exports . getPreviousReleaseTag = getPreviousReleaseTag
182
+
183
+ const jiraMatcher = / ( (? ! ( [ A - Z 0 - 9 a - z ] { 1 , 10 } ) - ? $ ) [ A - Z ] { 1 } [ A - Z 0 - 9 ] + - \d + ) / g
184
+
185
+ exports . getIssuesInRelease = function getIssuesInRelease ( jiraProjectKey ) {
186
+ info ( `Looking up issues for the ${ jiraProjectKey } project...` )
187
+ let currentReleaseTag , previousReleaseTag
188
+
189
+ try {
190
+ currentReleaseTag = getCurrentReleaseTag ( )
191
+ previousReleaseTag = getPreviousReleaseTag ( )
192
+ } catch ( e ) {
193
+ error ( e )
194
+ process . exit ( 1 )
195
+ }
196
+
197
+ let result
198
+
199
+ info ( `${ previousReleaseTag } ..${ currentReleaseTag } ` )
200
+
201
+ try {
202
+ result = runGitCommand ( [ 'log' , `${ previousReleaseTag } ..${ currentReleaseTag } ` ] )
203
+ } catch ( e ) {
204
+ error ( e )
205
+ process . exit ( 1 )
206
+ }
207
+
208
+ let issueKeys = result . match ( jiraMatcher ) || [ ]
209
+
210
+ issueKeys = issueKeys
211
+ . filter ( key => key . indexOf ( jiraProjectKey ) != - 1 )
212
+
213
+ if ( issueKeys . length > 0 ) {
214
+ issueKeys = Array . from ( new Set ( issueKeys ) )
215
+ info ( `Issues in this release: ${ issueKeys . join ( ', ' ) } ` )
216
+ }
217
+
218
+ return issueKeys
219
+ }
220
+
221
+ exports . getIssuesInCommit = function getIssuesInCommit ( jiraProjectKey ) {
222
+ let result
223
+
224
+ try {
225
+ result = runGitCommand ( [ 'log' , '-1' , '--pretty=format:%B' ] )
226
+ } catch ( e ) {
227
+ error ( e )
228
+ process . exit ( 1 )
229
+ }
230
+
231
+ let issueKeys = result . match ( jiraMatcher ) || [ ]
232
+
233
+ issueKeys = issueKeys
234
+ . filter ( key => key . indexOf ( jiraProjectKey ) != - 1 )
235
+
236
+ if ( issueKeys . length > 0 ) {
237
+ issueKeys = Array . from ( new Set ( issueKeys ) )
238
+ info ( `Issues in this release: ${ issueKeys . join ( ', ' ) } ` )
239
+ }
240
+
241
+ return issueKeys
242
+ }
0 commit comments