@@ -95,7 +95,7 @@ export class BitbucketProvider extends Utils {
95
95
fileName : string ,
96
96
fileContent : string ,
97
97
message = "Automatic commit generated from tests"
98
- ) :Promise < boolean > {
98
+ ) :Promise < string > {
99
99
try {
100
100
101
101
const commitData = qs . stringify ( {
@@ -114,12 +114,17 @@ export class BitbucketProvider extends Utils {
114
114
}
115
115
) ;
116
116
117
- console . log ( `Changes in file ${ fileName } successfully committed for branch ${ repoBranch } ` ) ;
118
- return response . status === 201 ;
117
+ // Extract the revision from the header
118
+ const commitSha = response . headers . location . split ( '/' ) . pop ( ) ;
119
+
120
+ console . log ( `Changes in file ${ fileName } successfully committed in ${ commitSha } for branch ${ repoBranch } ` ) ;
121
+ if ( response . status != 201 ) {
122
+ throw new Error ( `Failed to create commit, request failed with ${ response . status } ` ) ;
123
+ }
119
124
125
+ return commitSha ;
120
126
} catch ( error ) {
121
- console . error ( 'Error committing file:' , error ) ;
122
- return false ;
127
+ throw new Error ( `Error committing file: ${ error } ` ) ;
123
128
}
124
129
}
125
130
@@ -157,7 +162,7 @@ export class BitbucketProvider extends Utils {
157
162
* @param fileName file name in bitbucket repo to add/update in PR
158
163
* @param fileContent file content to be committed in file
159
164
*/
160
- public async createPullrequest ( workspace : string , repoSlug : string , fileName : string , fileContent : string ) :Promise < number > {
165
+ public async createPullrequest ( workspace : string , repoSlug : string , fileName : string , fileContent : string ) :Promise < [ number , string ] > {
161
166
const testBranch = `test-${ generateRandomChars ( 4 ) } ` ;
162
167
163
168
// create new branch
@@ -173,7 +178,7 @@ export class BitbucketProvider extends Utils {
173
178
) ;
174
179
175
180
// Make changes in new branch
176
- await this . createCommit ( workspace , repoSlug , testBranch , fileName , fileContent ) ;
181
+ const commitSha = await this . createCommit ( workspace , repoSlug , testBranch , fileName , fileContent ) ;
177
182
178
183
// Open PR to merge new branch into main branch
179
184
const prData = {
@@ -196,7 +201,7 @@ export class BitbucketProvider extends Utils {
196
201
) ;
197
202
198
203
console . log ( `Pull request ${ prResponse . data . id } created in ${ repoSlug } repository` ) ;
199
- return prResponse . data . id ;
204
+ return [ prResponse . data . id , commitSha ] ;
200
205
201
206
} catch ( error ) {
202
207
console . log ( error ) ;
@@ -210,7 +215,7 @@ export class BitbucketProvider extends Utils {
210
215
* @param repoSlug valid bitbucket repository where PR is open
211
216
* @param pullRequestID valid ID of pull request to merge
212
217
*/
213
- public async mergePullrequest ( workspace : string , repoSlug : string , pullRequestID : number ) {
218
+ public async mergePullrequest ( workspace : string , repoSlug : string , pullRequestID : number ) : Promise < string > {
214
219
const mergeData = {
215
220
"type" : "commit" ,
216
221
"message" : "PR merge by automated tests" ,
@@ -219,14 +224,18 @@ export class BitbucketProvider extends Utils {
219
224
} ;
220
225
221
226
try {
222
- await this . bitbucket . post (
227
+ const response = await this . bitbucket . post (
223
228
`repositories/${ workspace } /${ repoSlug } /pullrequests/${ pullRequestID } /merge` ,
224
229
mergeData
225
230
) ;
226
231
227
- console . log ( `Pull request "${ pullRequestID } " merged successfully in ${ repoSlug } repository` ) ;
232
+ const commitSha = response . data . merge_commit . hash ;
233
+
234
+ console . log ( `Pull request "${ pullRequestID } " merged successfully in ${ repoSlug } repository with merge commit ${ commitSha } ` ) ;
235
+
236
+ return commitSha ;
228
237
} catch ( error ) {
229
- console . log ( " Error merging PR" , error ) ;
238
+ throw new Error ( ` Error merging PR: ${ error } ` ) ;
230
239
}
231
240
}
232
241
@@ -237,7 +246,7 @@ export class BitbucketProvider extends Utils {
237
246
* @param fromEnvironment valid environment name from which image will be promoted (dev, stage)
238
247
* @param toEnvironment valid environment name to which image will be promoted (stage, prod)
239
248
*/
240
- public async createPromotionPullrequest ( workspace : string , componentName : string , fromEnvironment : string , toEnvironment : string ) :Promise < number > {
249
+ public async createPromotionPullrequest ( workspace : string , componentName : string , fromEnvironment : string , toEnvironment : string ) :Promise < [ number , string ] > {
241
250
const pattern = / - i m a g e : ( .* ) / ;
242
251
let extractedImage ;
243
252
@@ -284,7 +293,7 @@ export class BitbucketProvider extends Utils {
284
293
* @param workspace valid workspace in Bitbucket
285
294
* @param repoSlug valid Bitbucket repository slug
286
295
*/
287
- public async updateJenkinsfileForCI ( workspace : string , repoSlug : string ) : Promise < boolean > {
296
+ public async updateJenkinsfileForCI ( workspace : string , repoSlug : string ) : Promise < string > {
288
297
const filePath = 'Jenkinsfile' ;
289
298
let currentContent = await this . getFileContent ( workspace , repoSlug , 'main' , filePath ) ;
290
299
const stringReplaceContent = [
@@ -328,7 +337,7 @@ export class BitbucketProvider extends Utils {
328
337
roxCentralEndpoint :string ,
329
338
cosignPublicKey : string ,
330
339
imageRegistryUser : string
331
- ) : Promise < boolean > {
340
+ ) : Promise < string > {
332
341
const filePath = 'rhtap/env.sh' ;
333
342
let fileContent = await this . getFileContent ( workspace , repoSlug , 'main' , filePath ) ;
334
343
console . log ( `File before all changes: ${ filePath } \n${ fileContent } ` ) ;
0 commit comments