diff options
Diffstat (limited to 'ops/besadii')
-rw-r--r-- | ops/besadii/main.go | 40 |
1 files changed, 35 insertions, 5 deletions
diff --git a/ops/besadii/main.go b/ops/besadii/main.go index a04595c6c539..f86275750473 100644 --- a/ops/besadii/main.go +++ b/ops/besadii/main.go @@ -60,6 +60,13 @@ type Build struct { Env map[string]string `json:"env"` } +// BuildResponse is the representation of Buildkite's success response +// after triggering a build. This has many fields, but we only need +// one of them. +type buildResponse struct { + WebUrl string `json:"web_url"` +} + // reviewInput is a struct representing the data submitted to Gerrit // to post a review on a CL. // @@ -138,12 +145,35 @@ func triggerBuild(log *syslog.Writer, token string, update *refUpdated) error { } defer resp.Body.Close() - if resp.StatusCode != 201 { - respBody, _ := ioutil.ReadAll(resp.Body) - log.Err(fmt.Sprintf("received non-success response from Buildkite: %s (%v)", respBody, resp.Status)) - } else { - fmt.Fprintf(log, "triggered Buildkite build for ref %q at commit %q", update.ref, update.commit) + respBody, err := ioutil.ReadAll(resp.Body) + if err != nil { + return fmt.Errorf("failed to read Buildkite response body: %w", err) + } + + if resp.StatusCode != http.StatusCreated { + return fmt.Errorf("received non-success response from Buildkite: %s (%v)", respBody, resp.Status) + } + + var buildResp buildResponse + err = json.Unmarshal(respBody, &buildResp) + if err != nil { + return fmt.Errorf("failed to unmarshal build response: %w", err) + } + + fmt.Fprintf(log, "triggered build for ref %q at commit %q: %s", update.ref, update.commit, buildResp.WebUrl) + + // Report the status back to the Gerrit CL so that users can click + // through to the running build. + msg := fmt.Sprintf("Started build for patchset #%s of cl/%s: %s", *update.patchset, *update.changeId, buildResp.WebUrl) + review := reviewInput{ + Message: msg, + OmitDuplicateComments: true, + Tag: "autogenerated:buildkite~trigger", + + // Do not update the attention set for this comment. + IgnoreDefaultAttentionSetRules: true, } + updateGerrit(review, *update.changeId, *update.patchset) return nil } |