about summary refs log tree commit diff
diff options
context:
space:
mode:
authorFranck Cuny <franck@fcuny.net>2022-06-09 13:31:09 -0700
committerFranck Cuny <franck@fcuny.net>2022-06-09 13:40:24 -0700
commit47a36577cf18e83a9d242f791fe4c98fd0522f70 (patch)
tree8bdf1c0597c67eb05d68ffde5cbb1f796ba629b4
parentfeat(gtk): add bookmarks (diff)
downloadworld-47a36577cf18e83a9d242f791fe4c98fd0522f70.tar.gz
feat(gerrit): update CL when buildKite build is finished
buildKite can call specific hooks at various stages ([1]). We add a hook
to run after each command. For now we only care if the label of the
command is `:hammer:', since this is what we've defined for our pipeline.

After a successful build, the agent will post a review with +1 if it's a
success, or -1 if the build results in failure.

[1] https://buildkite.com/docs/agent/v3/hooks#job-lifecycle-hooks

Change-Id: I6b2b886c13e6f23ddbc96fd3e865f0d50d625446
Reviewed-on: https://cl.fcuny.net/c/world/+/305
Reviewed-by: Franck Cuny <franck@fcuny.net>
-rw-r--r--tools/gerrit-hook/buildkite.go44
-rw-r--r--tools/gerrit-hook/main.go2
2 files changed, 46 insertions, 0 deletions
diff --git a/tools/gerrit-hook/buildkite.go b/tools/gerrit-hook/buildkite.go
index d8723b6..35ad31c 100644
--- a/tools/gerrit-hook/buildkite.go
+++ b/tools/gerrit-hook/buildkite.go
@@ -8,6 +8,7 @@ import (
 	"io/ioutil"
 	"log/syslog"
 	"net/http"
+	"os"
 	"time"
 )
 
@@ -79,3 +80,46 @@ func triggerBuild(cfg *config, log *syslog.Writer, trigger *buildTrigger) error
 	updateGerrit(cfg, review, trigger.changeId, trigger.patchset)
 	return nil
 }
+
+func postCommand(cfg *config) {
+	changeId := os.Getenv("GERRIT_CHANGE_ID")
+	patchSet := os.Getenv("GERRIT_PATCHSET")
+
+	if changeId == "" || patchSet == "" {
+		fmt.Println("nothing to do")
+		return
+	}
+
+	// our build stage has the label :hammer:
+	if os.Getenv("BUILDKITE_LABEL") != ":hammer:" {
+		return
+	}
+
+	var vote int
+	var verb string
+	var notify string
+
+	if os.Getenv("BUILDKITE_COMMAND_EXIT_STATUS") == "0" {
+		vote = 1
+		verb = "passed"
+		notify = "NONE"
+	} else {
+		vote = -1
+		verb = "failed"
+		notify = "OWNER"
+	}
+
+	msg := fmt.Sprintf("Build of patchset %s %s: %s", patchSet, verb, os.Getenv("BUILDKITE_BUILD_URL"))
+	review := reviewInput{
+		Message:                        msg,
+		OmitDuplicateComments:          true,
+		IgnoreDefaultAttentionSetRules: vote == 1,
+		Tag:                            "autogenerated:buildkite~result",
+		Notify:                         notify,
+		Labels: map[string]int{
+			"Verified": vote,
+		},
+	}
+
+	updateGerrit(cfg, review, changeId, patchSet)
+}
diff --git a/tools/gerrit-hook/main.go b/tools/gerrit-hook/main.go
index f8ed687..aae73ba 100644
--- a/tools/gerrit-hook/main.go
+++ b/tools/gerrit-hook/main.go
@@ -57,6 +57,8 @@ func main() {
 			os.Exit(1)
 		}
 		gerritHookMain(cfg, log, trigger)
+	} else if cmd == "post-command" {
+		postCommand(cfg)
 	} else {
 		log.Info(fmt.Sprintf("`%s' is not a supported command", cmd))
 		os.Exit(1)