1- import got , { GotInstance , GotJSONFn } from 'got'
1+ import got , { Got , HTTPError } from 'got'
22import * as yaml from 'js-yaml'
33import { exists , writeFile } from 'mz/fs'
44import { GitHubClient } from './github'
55
6- export type BuildkiteClient = GotInstance < GotJSONFn >
6+ export type BuildkiteClient = Got
77
88export const createBuildkiteClient = ( { token } : { token : string } ) : BuildkiteClient =>
99 got . extend ( {
10- baseUrl : 'https://api.buildkite.com/v2/' ,
11- json : true ,
10+ prefixUrl : 'https://api.buildkite.com/v2/' ,
1211 headers : {
1312 Authorization : 'Bearer ' + token ,
1413 } ,
1514 } )
1615
16+ interface BuildkitePipeline {
17+ provider : { webhook_url : string }
18+ badge_url : string
19+ web_url : string
20+ }
21+
1722export async function initBuildkite ( {
1823 hasTests,
1924 repoName,
@@ -73,24 +78,25 @@ export async function initBuildkite({
7378 } ,
7479 }
7580
76- let pipeline : { provider : { webhook_url : string } ; badge_url : string ; web_url : string }
81+ let pipeline : BuildkitePipeline
7782 try {
78- pipeline = (
79- await buildkiteClient . post ( 'organizations/sourcegraph/pipelines' , {
80- body : buildkitePipeline ,
81- json : true ,
82- } )
83- ) . body
83+ pipeline = await buildkiteClient . post < BuildkitePipeline > ( 'organizations/sourcegraph/pipelines' , {
84+ json : buildkitePipeline ,
85+ responseType : 'json' ,
86+ resolveBodyOnly : true ,
87+ } )
8488 } catch ( err ) {
8589 if (
86- err . error &&
87- err . error . errors &&
88- err . error . errors [ 0 ] &&
89- err . error . errors [ 0 ] . field === 'name' &&
90- err . error . errors [ 0 ] . code === 'already_exists'
90+ err instanceof HTTPError &&
91+ ( err . response . body as any ) ?. errors ?. some ?.(
92+ ( err : any ) => err ?. field === 'name' && err ?. code === 'already_exists'
93+ )
9194 ) {
9295 console . log ( `Buildkite pipeline "${ repoName } " already exists, skipping creation` )
93- pipeline = ( await buildkiteClient . get ( `organizations/sourcegraph/pipelines/${ repoName } ` ) ) . body
96+ pipeline = await buildkiteClient . get < BuildkitePipeline > ( `organizations/sourcegraph/pipelines/${ repoName } ` , {
97+ responseType : 'json' ,
98+ resolveBodyOnly : true ,
99+ } )
94100 } else {
95101 throw err
96102 }
@@ -99,7 +105,7 @@ export async function initBuildkite({
99105 console . log ( '🔗 Creating GitHub webhook for pipeline' )
100106 try {
101107 await githubClient . post ( `/repos/sourcegraph/${ repoName } /hooks` , {
102- body : {
108+ json : {
103109 name : 'web' ,
104110 events : [ 'push' , 'pull_request' , 'deployment' ] ,
105111 config : {
@@ -110,9 +116,10 @@ export async function initBuildkite({
110116 } )
111117 } catch ( err ) {
112118 if (
113- err . error &&
114- Array . isArray ( err . error . errors ) &&
115- err . error . errors . some ( ( err : any ) => / h o o k a l r e a d y e x i s t s / i. test ( err . message ) )
119+ err instanceof HTTPError &&
120+ ( err . response . body as any ) ?. errors ?. some ?.(
121+ ( err : any ) => typeof err ?. message === 'string' && / h o o k a l r e a d y e x i s t s / i. test ( err . message )
122+ )
116123 ) {
117124 console . log ( 'Webhook already exists' )
118125 } else {
@@ -123,7 +130,7 @@ export async function initBuildkite({
123130 console . log ( '🔐 Granting Buildkite team pull access to repo' )
124131 // buildkite team, see https://api.github.com/orgs/sourcegraph/teams
125132 await githubClient . put ( `/teams/2444623/repos/sourcegraph/${ repoName } ` , {
126- body : {
133+ json : {
127134 permission : 'pull' ,
128135 } ,
129136 } )
0 commit comments