about summary refs log tree commit diff
diff options
context:
space:
mode:
authorVincent Ambo <mail@tazj.in>2021-09-18T13·07+0300
committertazjin <mail@tazj.in>2021-09-18T14·28+0000
commit98e4d4b18f57dcc3729f7b73cfe105b2af1c3494 (patch)
tree64540090d3f91681273981717f87bcb4ad766b8a
parentae93094ebfa18fbbb92efffe42a391782ae9cd35 (diff)
feat(besadii): Link to started builds in CL comments r/2888
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 <lukegb@tvl.fyi>
-rw-r--r--ops/besadii/main.go40
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
 }