From 15335e7b3dad497dd45633d91db62015b67f08e3 Mon Sep 17 00:00:00 2001 From: Vincent Ambo Date: Sun, 28 Jun 2020 03:37:34 +0100 Subject: feat(besadii): Enable automatic builds for CLs This expands builds to also be triggered for updates to CL refs. The message displayed on Buildkite will contain a link back to the CL (& patchset) from which the build was triggered. Change-Id: Ib36dee454aeb11d623b89c78b384359ee7ea3477 Reviewed-on: https://cl.tvl.fyi/c/depot/+/708 Reviewed-by: ericvolp12 Reviewed-by: isomer --- ops/besadii/main.go2 | 41 +++++++++++++++++++++++++++++------------ ops/pipelines/depot.nix | 2 +- 2 files changed, 30 insertions(+), 13 deletions(-) (limited to 'ops') diff --git a/ops/besadii/main.go2 b/ops/besadii/main.go2 index 1a1dacced80a..bbbecda85d8a 100644 --- a/ops/besadii/main.go2 +++ b/ops/besadii/main.go2 @@ -18,10 +18,11 @@ import ( "net/http" "os" "path" - "strings" + "regexp" ) -var branchPrefix = "refs/heads/" +var branchRegexp = regexp.MustCompile(`^refs/heads/(.*)$`) +var patchsetRegexp = regexp.MustCompile(`^refs/changes/\d{0,2}/(\d+)/(\d+)$`) // refUpdated is a struct representing the information passed to // besadii when it is invoked as Gerrit's refUpdated hook. @@ -29,10 +30,13 @@ var branchPrefix = "refs/heads/" // https://gerrit.googlesource.com/plugins/hooks/+/HEAD/src/main/resources/Documentation/hooks.md#ref_updated type refUpdated struct { project string - branch string + ref string commit string submitter string email string + + changeId *string + patchset *string } type Author struct { @@ -51,10 +55,18 @@ type Build struct { // Trigger a build of a given branch & commit on Buildkite func triggerBuild(log *syslog.Writer, token string, update *refUpdated) error { + var message string + + if update.changeId != nil && update.patchset != nil { + message = fmt.Sprintf(":llama: depot @ https://cl.tvl.fyi/c/depot/+/%s/%s", *update.changeId, *update.patchset) + } else { + message = fmt.Sprintf(":llama: depot @ %s", update.commit) + } + build := Build{ Commit: update.commit, - Branch: update.branch, - Message: fmt.Sprintf("Build of %q branch %q at %q", update.project, update.branch, update.commit), + Branch: update.ref, + Message: message, Author: Author{ Name: update.submitter, Email: update.email, @@ -83,7 +95,7 @@ func triggerBuild(log *syslog.Writer, token string, update *refUpdated) error { 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 branch %q at commit %q", update.branch, update.commit) + fmt.Fprintf(log, "triggered Buildkite build for ref %q at commit %q", update.ref, update.commit) } return nil @@ -115,7 +127,7 @@ func refUpdatedFromFlags() (*refUpdated, error) { flag.StringVar(&update.commit, "newrev", "", "new revision") flag.StringVar(&update.email, "submitter", "", "Submitter email") flag.StringVar(&update.submitter, "submitter-username", "", "Submitter username") - ref := flag.String("refname", "", "updated reference name") + flag.StringVar(&update.ref, "refname", "", "updated reference name") // Gerrit passes more flags than we want, but Rob Pike decided[0] in // 2013 that the Go art project will not allow users to ignore flags @@ -128,7 +140,7 @@ func refUpdatedFromFlags() (*refUpdated, error) { flag.Parse() - if update.project == "" || *ref == "" || update.commit == "" || update.submitter == "" { + if update.project == "" || update.ref == "" || update.commit == "" || update.submitter == "" { // If we get here, the user is probably being a dummy and invoking // this manually - but incorrectly. return nil, fmt.Errorf("'ref-updated' hook invoked without required arguments") @@ -139,13 +151,18 @@ func refUpdatedFromFlags() (*refUpdated, error) { return nil, nil } - if !strings.HasPrefix(*ref, branchPrefix) { - return nil, fmt.Errorf("besadii only supports branch updates at the moment (got %q)", *ref) + if branchRegexp.MatchString(update.ref) { + // branches don't need special handling -> just move on + return &update, nil } - update.branch = strings.TrimPrefix(*ref, branchPrefix) + if matches := patchsetRegexp.FindStringSubmatch(update.ref); matches != nil { + update.changeId = &matches[1] + update.patchset = &matches[2] + return &update, nil + } - return &update, nil + return nil, fmt.Errorf("besadii does not support updates for this type of ref (%q)", update.ref) } func main() { diff --git a/ops/pipelines/depot.nix b/ops/pipelines/depot.nix index 40de18b5de36..4c964b4df9b5 100644 --- a/ops/pipelines/depot.nix +++ b/ops/pipelines/depot.nix @@ -15,7 +15,7 @@ let pipeline.steps = [ { command = "nix-build -A ciBuilds.__allTargets"; - label = "all-targets"; + label = ":duck:"; } ]; in writeText "depot.yaml" (toJSON pipeline) -- cgit 1.4.1