forked from buildkite/cli
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcmd_init.go
387 lines (326 loc) · 9.48 KB
/
cmd_init.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
package cli
import (
"context"
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
githubclient "github.com/google/go-github/github"
"github.com/buildkite/cli/v2/git"
"github.com/buildkite/cli/v2/github"
"github.com/buildkite/cli/v2/graphql"
"github.com/fatih/color"
)
const defaultPipelineYAML = `# Default pipeline from cli
steps:
- label: ":pipeline:"
command: buildkite-agent pipeline upload
`
type InitCommandContext struct {
TerminalContext
ConfigContext
Debug bool
DebugHTTP bool
Dir string
PipelineSlug string
}
func InitCommand(ctx InitCommandContext) error {
dir, err := filepath.Abs(ctx.Dir)
if err != nil {
return NewExitError(err, 1)
}
pipelineFileTry := ctx.Try()
pipelineFileTry.Start("Checking for pipeline file")
pipelineFile := filepath.Join(dir, ".buildkite", "pipeline.yml")
pipelineFileAdded := false
// make sure we've got the directory in place for .buildkite/
_ = os.Mkdir(filepath.Dir(pipelineFile), 0770)
// create a .buildkite/pipeline.yml if one doesn't exist
if _, err := os.Stat(pipelineFile); err == nil {
pipelineFileTry.Success(".buildkite/pipeline.yml")
} else {
if err = ioutil.WriteFile(pipelineFile, []byte(defaultPipelineYAML), 0660); err != nil {
return NewExitError(err, 1)
}
pipelineFileAdded = true
pipelineFileTry.Success("Created .buildkite/pipeline.yml")
}
// check we have a git directory
if _, err := os.Stat(filepath.Join(dir, ".git")); err != nil {
return NewExitError(fmt.Errorf("%s isn't a git managed project! Try `git init`", dir), 1)
}
gitRepoTry := ctx.Try()
gitRepoTry.Start("Checking for git repository and remote")
gitRemote, err := git.Remote(dir)
if err != nil {
gitRepoTry.Failure(err.Error())
return NewExitError(err, 1)
}
org, repo, err := github.ParseRemote(gitRemote)
if err != nil {
gitRepoTry.Failure(err.Error())
return NewExitError(err, 1)
}
gitRepoTry.Success(fmt.Sprintf("https://github.com/%s/%s", org, repo))
bk, err := ctx.BuildkiteGraphQLClient()
if err != nil {
gitRepoTry.Failure(err.Error())
return NewExitError(err, 1)
}
// by default, use the orgname and repo from the github project
pipelineOrg := org
pipelineRepo := repo
if ctx.PipelineSlug != "" {
debugf("Using %s as pipeline", ctx.PipelineSlug)
parts := strings.SplitN(ctx.PipelineSlug, "/", 2)
pipelineOrg = parts[0]
pipelineRepo = parts[1]
}
pipelineTry := ctx.Try()
pipelineTry.Start("Checking for buildkite pipeline")
isPipelineCreated, err := isBuildkitePipelineCreated(bk, pipelineOrg, pipelineRepo)
if err != nil {
pipelineTry.Failure(err.Error())
return NewExitError(err, 1)
}
var pipeline buildkitePipelineDetails
if isPipelineCreated {
var err error
pipeline, err = getBuildkitePipeline(bk, buildkitePipelineSlug(pipelineOrg, pipelineRepo))
if err != nil {
pipelineTry.Failure(err.Error())
return NewExitError(err, 1)
}
pipelineTry.Success(pipeline.URL)
} else {
debugf("[init] Buildkite pipeline %s/%s doesn't exist", pipelineOrg, pipelineRepo)
var err error
pipeline, err = createBuildkitePipeline(bk, pipelineOrg, pipelineRepo,
defaultPipelineYAML,
gitRemote,
)
if err != nil {
pipelineTry.Failure(err.Error())
return NewExitError(err, 1)
}
pipelineTry.Success("Created " + pipeline.URL)
}
gh, err := ctx.GithubClient()
if err != nil {
return NewExitError(err, 1)
}
githubWebhooksTry := ctx.Try()
githubWebhooksTry.Start(fmt.Sprintf("Checking github repository config for %s/%s", org, repo))
hooks, _, err := gh.Repositories.ListHooks(context.Background(), org, repo, &githubclient.ListOptions{})
if err != nil {
githubWebhooksTry.Failure(err.Error())
return NewExitError(err, 1)
}
isGitHubWebhookSetup := false
debugf("[init] Found %d webhooks", len(hooks))
for _, hook := range hooks {
webhookURL, ok := hook.Config["url"].(string)
if ok && strings.Contains(webhookURL, "webhook.buildbox.io") || strings.Contains(webhookURL, "webhook.buildkite.com") {
isGitHubWebhookSetup = true
break
}
}
if isGitHubWebhookSetup {
githubWebhooksTry.Success("Webhook exists")
} else {
debugf("[init] Creating a webhook with %s", pipeline.WebhookURL)
// https://developer.github.com/v3/repos/hooks/#create-a-hook
_, _, err := gh.Repositories.CreateHook(context.Background(), org, repo, &githubclient.Hook{
Name: githubclient.String(`web`),
Events: []string{`push`, `pull_request`, `deployment`},
Config: map[string]interface{}{
"url": pipeline.WebhookURL,
"content_type": "json",
},
})
if err != nil {
githubWebhooksTry.Failure(err.Error())
return NewExitError(err, 1)
}
githubWebhooksTry.Success("Created webhook")
}
if pipelineFileAdded {
ctx.Println("\nA pipeline.yml file was created in .buildkite, " +
"you will need to manually commit this with " +
"`git commit .buildkite -m 'Buildkite skeleton'")
}
ctx.Printf(color.GreenString("\nOk! Your project is ready to go at %s 🚀\n"), pipeline.URL)
return nil
}
func getBuildkiteOrgID(client *graphql.Client, orgSlug string) (string, error) {
resp, err := client.Do(`
query($slug:ID!) {
organization(slug: $slug) {
id
}
}
`, map[string]interface{}{
"slug": orgSlug,
})
if err != nil {
return "", err
}
var organizationQueryResponse struct {
Data struct {
Organization struct {
ID string `json:"id"`
} `json:"organization"`
} `json:"data"`
}
if err = resp.DecodeInto(&organizationQueryResponse); err != nil {
return "", fmt.Errorf("Failed to parse GraphQL response: %v", err)
}
if organizationQueryResponse.Data.Organization.ID == "" {
return "", fmt.Errorf("Failed to find organization id for slug %q", orgSlug)
}
return organizationQueryResponse.Data.Organization.ID, nil
}
func isBuildkitePipelineCreated(client *graphql.Client, org, pipeline string) (bool, error) {
resp, err := client.Do(`
query($slug:ID!) {
pipeline(slug: $slug) {
repository {
url
}
}
}
`, map[string]interface{}{
"slug": fmt.Sprintf("%s/%s", org, pipeline),
})
if err != nil {
return false, err
}
var pipelineQueryResponse struct {
Data struct {
Pipeline map[string]interface{} `json:"pipeline"`
} `json:"data"`
}
if err = resp.DecodeInto(&pipelineQueryResponse); err != nil {
return false, fmt.Errorf("Failed to parse GraphQL response: %v", err)
}
if len(pipelineQueryResponse.Data.Pipeline) > 0 {
return true, nil
}
return false, nil
}
type buildkitePipelineDetails struct {
Name string
Slug string
URL string
WebhookURL string
}
func createBuildkitePipeline(client *graphql.Client, org, pipeline, steps, repository string) (buildkitePipelineDetails, error) {
orgId, err := getBuildkiteOrgID(client, org)
if err != nil {
return buildkitePipelineDetails{}, err
}
resp, err := client.Do(`
mutation($input:PipelineCreateInput!) {
pipelineCreate(input:$input) {
pipeline {
name
slug
url
repository {
provider {
webhookUrl
}
}
}
}
}
`, map[string]interface{}{
"input": map[string]interface{}{
"name": pipeline,
"organizationId": orgId,
"steps": map[string]interface{}{"yaml": steps},
"repository": map[string]interface{}{"url": repository},
}})
if err != nil {
return buildkitePipelineDetails{}, err
}
var parsedResp struct {
Data struct {
PipelineCreate struct {
Pipeline struct {
Name string `json:"name"`
Slug string `json:"slug"`
URL string `json:"url"`
Repository struct {
Provider struct {
WebhookURL string `json:"webhookUrl"`
} `json:"provider"`
} `json:"repository"`
} `json:"pipeline"`
} `json:"pipelineCreate"`
} `json:"data"`
}
if err = resp.DecodeInto(&parsedResp); err != nil {
return buildkitePipelineDetails{},
fmt.Errorf("Failed to parse GraphQL response: %v", err)
}
return buildkitePipelineDetails{
Name: parsedResp.Data.PipelineCreate.Pipeline.Name,
Slug: parsedResp.Data.PipelineCreate.Pipeline.Slug,
URL: parsedResp.Data.PipelineCreate.Pipeline.URL,
WebhookURL: parsedResp.Data.PipelineCreate.Pipeline.Repository.Provider.WebhookURL,
}, nil
}
func buildkitePipelineSlug(org, pipeline string) string {
return fmt.Sprintf("%s/%s", org, pipeline)
}
var errPipelineDoesntExist = errors.New("Pipeline doesn't exist")
func getBuildkitePipeline(client *graphql.Client, slug string) (buildkitePipelineDetails, error) {
resp, err := client.Do(`
query($slug:ID!) {
pipeline(slug: $slug) {
name
slug
url
repository {
provider {
webhookUrl
}
}
}
}
`, map[string]interface{}{
"slug": slug,
})
if err != nil {
return buildkitePipelineDetails{}, err
}
var parsedResp struct {
Data struct {
Pipeline struct {
Name string `json:"name"`
Slug string `json:"slug"`
URL string `json:"url"`
Repository struct {
Provider struct {
WebhookURL string `json:"webhookUrl"`
} `json:"provider"`
} `json:"repository"`
} `json:"pipeline"`
} `json:"data"`
}
if err = resp.DecodeInto(&parsedResp); err != nil {
return buildkitePipelineDetails{}, fmt.Errorf("Failed to parse GraphQL response: %v", err)
}
if parsedResp.Data.Pipeline.Slug == "" {
return buildkitePipelineDetails{}, errPipelineDoesntExist
}
return buildkitePipelineDetails{
Name: parsedResp.Data.Pipeline.Name,
Slug: parsedResp.Data.Pipeline.Slug,
URL: parsedResp.Data.Pipeline.URL,
WebhookURL: parsedResp.Data.Pipeline.Repository.Provider.WebhookURL,
}, nil
}