1
- import got , { GotInstance , GotJSONFn } from 'got'
1
+ import got , { Got , HTTPError } from 'got'
2
2
import * as yaml from 'js-yaml'
3
3
import { exists , writeFile } from 'mz/fs'
4
4
import { GitHubClient } from './github'
5
5
6
- export type BuildkiteClient = GotInstance < GotJSONFn >
6
+ export type BuildkiteClient = Got
7
7
8
8
export const createBuildkiteClient = ( { token } : { token : string } ) : BuildkiteClient =>
9
9
got . extend ( {
10
- baseUrl : 'https://api.buildkite.com/v2/' ,
11
- json : true ,
10
+ prefixUrl : 'https://api.buildkite.com/v2/' ,
12
11
headers : {
13
12
Authorization : 'Bearer ' + token ,
14
13
} ,
15
14
} )
16
15
16
+ interface BuildkitePipeline {
17
+ provider : { webhook_url : string }
18
+ badge_url : string
19
+ web_url : string
20
+ }
21
+
17
22
export async function initBuildkite ( {
18
23
hasTests,
19
24
repoName,
@@ -73,24 +78,25 @@ export async function initBuildkite({
73
78
} ,
74
79
}
75
80
76
- let pipeline : { provider : { webhook_url : string } ; badge_url : string ; web_url : string }
81
+ let pipeline : BuildkitePipeline
77
82
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
+ } )
84
88
} catch ( err ) {
85
89
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
+ )
91
94
) {
92
95
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
+ } )
94
100
} else {
95
101
throw err
96
102
}
@@ -99,7 +105,7 @@ export async function initBuildkite({
99
105
console . log ( '🔗 Creating GitHub webhook for pipeline' )
100
106
try {
101
107
await githubClient . post ( `/repos/sourcegraph/${ repoName } /hooks` , {
102
- body : {
108
+ json : {
103
109
name : 'web' ,
104
110
events : [ 'push' , 'pull_request' , 'deployment' ] ,
105
111
config : {
@@ -110,9 +116,10 @@ export async function initBuildkite({
110
116
} )
111
117
} catch ( err ) {
112
118
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
+ )
116
123
) {
117
124
console . log ( 'Webhook already exists' )
118
125
} else {
@@ -123,7 +130,7 @@ export async function initBuildkite({
123
130
console . log ( '🔐 Granting Buildkite team pull access to repo' )
124
131
// buildkite team, see https://api.github.com/orgs/sourcegraph/teams
125
132
await githubClient . put ( `/teams/2444623/repos/sourcegraph/${ repoName } ` , {
126
- body : {
133
+ json : {
127
134
permission : 'pull' ,
128
135
} ,
129
136
} )
0 commit comments