From 98e4d4b18f57dcc3729f7b73cfe105b2af1c3494 Mon Sep 17 00:00:00 2001 From: Vincent Ambo Date: Sat, 18 Sep 2021 16:07:24 +0300 Subject: feat(besadii): Link to started builds in CL comments This makes it easier to click through to a build from Gerrit after submitting a CL. Change-Id: Ic5c6eeb81c87bc4ea23c5c5ca25704434b081fd0 Reviewed-on: https://cl.tvl.fyi/c/depot/+/3572 Tested-by: BuildkiteCI Reviewed-by: lukegb --- ops/besadii/main.go | 40 +++++++++++++++++++++++++++++++++++----- 1 file changed, 35 insertions(+), 5 deletions(-) diff --git a/ops/besadii/main.go b/ops/besadii/main.go index a04595c6c5..f862757504 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 } -- cgit 1.4.1