11import fs from 'node:fs' ;
22import path from 'node:path' ;
3- import axios , { AxiosError , AxiosInstance } from 'axios' ;
43import logdown from 'logdown' ;
54
65import type { AutoMergeConfig , ActionResult , GitHubPullRequest , Repository , RepositoryResult } from './types/index.js' ;
@@ -17,9 +16,10 @@ const {bin, version: toolVersion}: PackageJson = JSON.parse(fs.readFileSync(pack
1716const toolName = Object . keys ( bin ) [ 0 ] ;
1817
1918export class AutoMerge {
20- private readonly apiClient : AxiosInstance ;
19+ private readonly baseHeaders : Record < string , string > ;
2120 private readonly config : AutoMergeConfig ;
2221 private readonly logger : logdown . Logger ;
22+ private readonly baseURL : string ;
2323
2424 constructor ( config : AutoMergeConfig ) {
2525 this . config = config ;
@@ -28,14 +28,12 @@ export class AutoMerge {
2828 markdown : false ,
2929 } ) ;
3030 this . logger . state . isEnabled = true ;
31- this . apiClient = axios . create ( {
32- baseURL : 'https://api.github.com' ,
33- headers : {
34- Accept : 'application/vnd.github+json' ,
35- Authorization : `token ${ this . config . authToken } ` ,
36- 'User-Agent' : `${ toolName } v${ toolVersion } ` ,
37- } ,
38- } ) ;
31+ this . baseURL = 'https://api.github.com' ;
32+ this . baseHeaders = {
33+ Accept : 'application/vnd.github+json' ,
34+ Authorization : `token ${ this . config . authToken } ` ,
35+ 'User-Agent' : `${ toolName } v${ toolVersion } ` ,
36+ } ;
3937 this . checkConfig ( this . config ) ;
4038 }
4139
@@ -90,9 +88,12 @@ export class AutoMerge {
9088 }
9189
9290 private async isPullRequestMergeable ( repositorySlug : string , pullNumber : number ) : Promise < boolean > {
93- const resourceUrl = `/repos/${ repositorySlug } /pulls/${ pullNumber } ` ;
94- const response = await this . apiClient . get < GitHubPullRequest > ( resourceUrl ) ;
95- return response . data . mergeable_state === 'clean' ;
91+ const resourceUrl = new URL ( `/repos/${ repositorySlug } /pulls/${ pullNumber } ` , this . baseURL ) ;
92+ const response = await fetch ( resourceUrl , { headers : this . baseHeaders } ) ;
93+ if ( ! response . ok ) {
94+ throw new Error ( `Error while checking merge request: ${ response . statusText } ` ) ;
95+ }
96+ return ( await response . json ( ) ) . mergeable_state === 'clean' ;
9697 }
9798
9899 async mergeByMatch ( regex : RegExp , repositories ?: Repository [ ] ) : Promise < RepositoryResult [ ] > {
@@ -124,11 +125,9 @@ export class AutoMerge {
124125 await this . postReview ( repositorySlug , pullNumber ) ;
125126 }
126127 } catch ( error ) {
127- this . logger . error (
128- `Could not approve request #${ pullNumber } in "${ repositorySlug } ": ${ ( error as AxiosError ) . message } `
129- ) ;
128+ this . logger . error ( `Could not approve request #${ pullNumber } in "${ repositorySlug } ": ${ ( error as Error ) . message } ` ) ;
130129 actionResult . status = 'bad' ;
131- actionResult . error = ( error as AxiosError ) . toString ( ) ;
130+ actionResult . error = ( error as Error ) . toString ( ) ;
132131 }
133132 return actionResult ;
134133 }
@@ -142,10 +141,10 @@ export class AutoMerge {
142141 }
143142 } catch ( error ) {
144143 this . logger . error (
145- `Could not merge pull request #${ pullNumber } in "${ repositorySlug } ": ${ ( error as AxiosError ) . message } `
144+ `Could not merge pull request #${ pullNumber } in "${ repositorySlug } ": ${ ( error as Error ) . message } `
146145 ) ;
147146 actionResult . status = 'bad' ;
148- actionResult . error = ( error as AxiosError ) . toString ( ) ;
147+ actionResult . error = ( error as Error ) . toString ( ) ;
149148 }
150149 return actionResult ;
151150 }
@@ -162,7 +161,7 @@ export class AutoMerge {
162161 const pullRequests = await this . getPullRequestsBySlug ( repositorySlug ) ;
163162 repositories . push ( { pullRequests, repositorySlug} ) ;
164163 } catch ( error ) {
165- this . logger . error ( `Could not get pull requests for "${ repositorySlug } ": ${ ( error as AxiosError ) . message } ` ) ;
164+ this . logger . error ( `Could not get pull requests for "${ repositorySlug } ": ${ ( error as Error ) . message } ` ) ;
166165 }
167166 }
168167
@@ -176,20 +175,34 @@ export class AutoMerge {
176175
177176 /** @see https://docs.github.com/en/rest/reference/pulls#create-a-review-for-a-pull-request */
178177 private async postReview ( repositorySlug : string , pullNumber : number ) : Promise < void > {
179- const resourceUrl = `/repos/${ repositorySlug } /pulls/${ pullNumber } /reviews` ;
180- await this . apiClient . post ( resourceUrl , { event : 'APPROVE' } ) ;
178+ 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' } ) ;
181+ if ( ! response . ok ) {
182+ throw new Error ( `Error while approving pull request: ${ response . statusText } ` ) ;
183+ }
181184 }
182185
183186 /** @see https://docs.github.com/en/rest/reference/issues#create-an-issue-comment */
184187 private async putMerge ( repositorySlug : string , pullNumber : number , squash ?: boolean ) : Promise < void > {
185- const resourceUrl = `/repos/${ repositorySlug } /pulls/${ pullNumber } /merge` ;
186- await this . apiClient . put ( resourceUrl , squash ? { merge_method : 'squash' } : undefined ) ;
188+ const resourceUrl = new URL ( `/repos/${ repositorySlug } /pulls/${ pullNumber } /merge` , this . baseURL ) ;
189+ if ( squash ) {
190+ resourceUrl . search = new URLSearchParams ( { merge_method : 'squash' } ) . toString ( ) ;
191+ }
192+
193+ const response = await fetch ( resourceUrl , { headers : this . baseHeaders , method : 'PUT' } ) ;
194+ if ( ! response . ok ) {
195+ throw new Error ( `Error while merging pull request: ${ response . statusText } ` ) ;
196+ }
187197 }
188198
189199 private async getPullRequestsBySlug ( repositorySlug : string ) : Promise < GitHubPullRequest [ ] > {
190- const resourceUrl = `/repos/${ repositorySlug } /pulls` ;
191- const params = { per_page : 100 , state : 'open' } ;
192- const response = await this . apiClient . get < GitHubPullRequest [ ] > ( resourceUrl , { params} ) ;
193- return response . data ;
200+ const resourceUrl = new URL ( `/repos/${ repositorySlug } /pulls` , this . baseURL ) ;
201+ resourceUrl . search = new URLSearchParams ( { per_page : '100' , state : 'open' } ) . toString ( ) ;
202+ const response = await fetch ( resourceUrl , { headers : this . baseHeaders } ) ;
203+ if ( ! response . ok ) {
204+ throw new Error ( `Error while fetching pull requests: ${ response . statusText } ` ) ;
205+ }
206+ return response . json ( ) ;
194207 }
195208}
0 commit comments