@@ -5,15 +5,14 @@ import logdown from 'logdown';
55import type { AutoMergeConfig , ActionResult , GitHubPullRequest , Repository , RepositoryResult } from './types/index.js' ;
66
77interface PackageJson {
8- bin : Record < string , string > ;
8+ name : string ;
99 version : string ;
1010}
1111
1212const __dirname = import . meta. dirname ;
1313const packageJsonPath = path . join ( __dirname , '../package.json' ) ;
1414
15- const { bin, version : toolVersion } : PackageJson = JSON . parse ( fs . readFileSync ( packageJsonPath , 'utf-8' ) ) ;
16- const toolName = Object . keys ( bin ) [ 0 ] ;
15+ const { name : toolName , version : toolVersion } : PackageJson = JSON . parse ( fs . readFileSync ( packageJsonPath , 'utf-8' ) ) ;
1716
1817export class AutoMerge {
1918 private readonly baseHeaders : Record < string , string > ;
@@ -31,8 +30,9 @@ export class AutoMerge {
3130 this . baseURL = 'https://api.github.com' ;
3231 this . baseHeaders = {
3332 Accept : 'application/vnd.github+json' ,
34- Authorization : `token ${ this . config . authToken } ` ,
33+ Authorization : `Bearer ${ this . config . authToken } ` ,
3534 'User-Agent' : `${ toolName } v${ toolVersion } ` ,
35+ 'X-GitHub-Api-Version' : '2022-11-28' ,
3636 } ;
3737 this . checkConfig ( this . config ) ;
3838 }
@@ -135,17 +135,21 @@ export class AutoMerge {
135135 async mergePullRequest ( repositorySlug : string , pullNumber : number , squash : boolean = false ) : Promise < ActionResult > {
136136 const actionResult : ActionResult = { pullNumber, status : 'good' } ;
137137
138- try {
139- if ( ! this . config . dryRun ) {
140- await this . putMerge ( repositorySlug , pullNumber , squash ) ;
138+ if ( ! this . config . dryRun ) {
139+ try {
140+ const isMerged = await this . putMerge ( repositorySlug , pullNumber , squash ) ;
141+ if ( ! isMerged ) {
142+ actionResult . status = 'bad' ;
143+ }
144+ } catch ( error ) {
145+ this . logger . error (
146+ `Could not merge pull request #${ pullNumber } in "${ repositorySlug } ": ${ ( error as Error ) . message } `
147+ ) ;
148+ actionResult . status = 'bad' ;
149+ actionResult . error = ( error as Error ) . toString ( ) ;
141150 }
142- } catch ( error ) {
143- this . logger . error (
144- `Could not merge pull request #${ pullNumber } in "${ repositorySlug } ": ${ ( error as Error ) . message } `
145- ) ;
146- actionResult . status = 'bad' ;
147- actionResult . error = ( error as Error ) . toString ( ) ;
148151 }
152+
149153 return actionResult ;
150154 }
151155
@@ -176,24 +180,30 @@ export class AutoMerge {
176180 /** @see https://docs.github.com/en/rest/reference/pulls#create-a-review-for-a-pull-request */
177181 private async postReview ( repositorySlug : string , pullNumber : number ) : Promise < void > {
178182 const resourceUrl = new URL ( `/repos/${ repositorySlug } /pulls/${ pullNumber } /reviews` , this . baseURL ) ;
179- resourceUrl . search = new URLSearchParams ( { event : 'APPROVE' } ) . toString ( ) ;
180- const response = await fetch ( resourceUrl , { headers : this . baseHeaders , method : 'POST' } ) ;
183+ const body = JSON . stringify ( { event : 'APPROVE' } ) ;
184+ const response = await fetch ( resourceUrl , { body , headers : this . baseHeaders , method : 'POST' } ) ;
181185 if ( ! response . ok ) {
182186 throw new Error ( `Error while approving pull request: ${ response . statusText } ` ) ;
183187 }
184188 }
185189
186190 /** @see https://docs.github.com/en/rest/reference/issues#create-an-issue-comment */
187- private async putMerge ( repositorySlug : string , pullNumber : number , squash ?: boolean ) : Promise < void > {
191+ private async putMerge ( repositorySlug : string , pullNumber : number , squash ?: boolean ) : Promise < boolean > {
188192 const resourceUrl = new URL ( `/repos/${ repositorySlug } /pulls/${ pullNumber } /merge` , this . baseURL ) ;
193+ const config : RequestInit = { headers : this . baseHeaders , method : 'PUT' } ;
194+
189195 if ( squash ) {
190- resourceUrl . search = new URLSearchParams ( { merge_method : 'squash' } ) . toString ( ) ;
196+ const mergeMethod = { merge_method : 'squash' } ;
197+ config . body = JSON . stringify ( mergeMethod ) ;
191198 }
192199
193- const response = await fetch ( resourceUrl , { headers : this . baseHeaders , method : 'PUT' } ) ;
200+ const response = await fetch ( resourceUrl , config ) ;
194201 if ( ! response . ok ) {
195202 throw new Error ( `Error while merging pull request: ${ response . statusText } ` ) ;
196203 }
204+
205+ const { merged} = await response . json ( ) ;
206+ return merged ;
197207 }
198208
199209 private async getPullRequestsBySlug ( repositorySlug : string ) : Promise < GitHubPullRequest [ ] > {
0 commit comments