feat(besadii): Adapt into a Gerrit ref-updated-hook

Besadii was previously invoked as a git post-update hook, but Gerrit
does not use these hooks and instead has its own concept of hooks.

This change adapts besadii to be compatible with the way Gerrit hooks
are invoked (arguments being passed as flags, rather than via stdin).

Change-Id: I487b3a9e15810583bc5442fdc024ee2771c580cb
This commit is contained in:
Vincent Ambo 2020-06-13 05:59:15 +01:00
parent 8735c63e97
commit b094e65bfc
2 changed files with 62 additions and 67 deletions

View file

@ -5,8 +5,4 @@
depot.buildGo.program { depot.buildGo.program {
name = "besadii"; name = "besadii";
srcs = [ ./main.go ]; srcs = [ ./main.go ];
x_defs = {
"main.gitBin" = "${depot.third_party.git}/bin/git";
};
} }

View file

@ -1,40 +1,35 @@
// Copyright 2019 Google LLC. // Copyright 2019-2020 Google LLC.
// SPDX-License-Identifier: Apache-2.0 // SPDX-License-Identifier: Apache-2.0
// //
// besadii is a small CLI tool that triggers depot builds on // besadii is a small CLI tool that triggers depot builds on
// builds.sr.ht // builds.sr.ht
// //
// It is designed to run as a post-update git hook on the server // It is designed to run as a Gerrit hook (ref-updated).
// hosting the depot.
package main package main
import ( import (
"bufio"
"bytes" "bytes"
"encoding/json" "encoding/json"
"flag"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"log/syslog" "log/syslog"
"net/http" "net/http"
"os" "os"
"os/exec" "path"
"strings" "strings"
) )
var gitBin = "git"
var branchPrefix = "refs/heads/" var branchPrefix = "refs/heads/"
// This value is set by the git hook invocation when a branch is // Represents an updated branch, as passed to besadii by Gerrit.
// removed, builds should not be triggered in that case.
var deletedBranch = "0000000000000000000000000000000000000000"
// Represents an updated reference as passed to besadii by git
// //
// https://git-scm.com/docs/githooks#pre-receive // https://gerrit.googlesource.com/plugins/hooks/+/HEAD/src/main/resources/Documentation/hooks.md#ref_updated
type refUpdate struct { type branchUpdate struct {
name string project string
old string branch string
new string commit string
submitter string
} }
// Represents a builds.sr.ht build object as described on // Represents a builds.sr.ht build object as described on
@ -45,8 +40,8 @@ type Build struct {
Tags []string `json:"tags"` Tags []string `json:"tags"`
} }
// Represents a build trigger object as described on <the docs for // Represents a build trigger object as described on
// this are currently down> // https://man.sr.ht/builds.sr.ht/triggers.md
type Trigger struct { type Trigger struct {
Action string `json:"action"` Action string `json:"action"`
Condition string `json:"condition"` Condition string `json:"condition"`
@ -96,14 +91,14 @@ cat built-paths | cachix push tazjin`},
} }
// Trigger a build of a given branch & commit on builds.sr.ht // Trigger a build of a given branch & commit on builds.sr.ht
func triggerBuild(log *syslog.Writer, token, branch, commit string) { func triggerBuild(log *syslog.Writer, token string, update *branchUpdate) {
build := Build{ build := Build{
Manifest: prepareManifest(commit), Manifest: prepareManifest(update.commit),
Note: fmt.Sprintf("Build of '%s' at '%s'", branch, commit), Note: fmt.Sprintf("build of %q at %q, submitted by %q", update.branch, update.commit, update.submitter),
Tags: []string{ Tags: []string{
// my branch names tend to contain slashes, which are not valid // my branch names tend to contain slashes, which are not valid
// identifiers in sourcehut. // identifiers in sourcehut.
"depot", strings.ReplaceAll(branch, "/", "_"), "depot", strings.ReplaceAll(update.branch, "/", "_"),
}, },
} }
@ -132,39 +127,52 @@ func triggerBuild(log *syslog.Writer, token, branch, commit string) {
respBody, _ := ioutil.ReadAll(resp.Body) respBody, _ := ioutil.ReadAll(resp.Body)
log.Err(fmt.Sprintf("received non-success response from builds.sr.ht: %s (%v)", respBody, resp.Status)) log.Err(fmt.Sprintf("received non-success response from builds.sr.ht: %s (%v)", respBody, resp.Status))
} else { } else {
fmt.Fprintf(log, "triggered builds.sr.ht job for branch '%s' at commit '%s'", branch, commit) fmt.Fprintf(log, "triggered builds.sr.ht job for branch %q at commit %q", update.branch, update.commit)
} }
} }
func parseRefUpdates() ([]refUpdate, error) { func branchUpdateFromFlags() (*branchUpdate, error) {
var updates []refUpdate if path.Base(os.Args[0]) != "ref-updated" {
return nil, fmt.Errorf("besadii must be invoked as the 'ref-updated' hook")
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
line := scanner.Text()
fragments := strings.Split(line, " ")
if len(fragments) != 3 {
return nil, fmt.Errorf("invalid ref update: '%s'", line)
} }
update := refUpdate{ var update branchUpdate
old: fragments[0],
new: fragments[1], flag.StringVar(&update.project, "project", "", "Gerrit project")
name: fragments[2], flag.StringVar(&update.commit, "newrev", "", "new revision")
flag.StringVar(&update.submitter, "submitter-username", "", "Submitter username")
ref := flag.String("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
// because he "doesn't like it". The following code ignores the
// flags.
//
// [0]: https://github.com/golang/go/issues/6112#issuecomment-66083768
var _old, _submitter string
flag.StringVar(&_old, "oldrev", "", "")
flag.StringVar(&_submitter, "submitter", "", "")
flag.Parse()
if update.project == "" || *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")
} }
if strings.HasPrefix(update.name, branchPrefix) && update.new != deletedBranch { if update.project != "depot" {
update.name = strings.TrimPrefix(update.name, branchPrefix) // this is not an error, but also not something we handle.
updates = append(updates, update) return nil, nil
}
} }
if err := scanner.Err(); err != nil { if !strings.HasPrefix(*ref, branchPrefix) {
return nil, err return nil, fmt.Errorf("besadii only supports branch updates at the moment")
} }
return updates, nil update.branch = strings.TrimPrefix(*ref, branchPrefix)
return &update, nil
} }
func main() { func main() {
@ -174,30 +182,21 @@ func main() {
os.Exit(1) os.Exit(1)
} }
// Before triggering builds, it is important that git
// update-server-info is run so that cgit correctly serves the
// repository.
err = exec.Command(gitBin, "update-server-info").Run()
if err != nil {
log.Alert("failed to run 'git update-server-info' for depot!")
os.Exit(1)
}
token, err := ioutil.ReadFile("/etc/secrets/srht-token") token, err := ioutil.ReadFile("/etc/secrets/srht-token")
if err != nil { if err != nil {
log.Alert("sourcehot token could not be read") log.Alert("sourcehot token could not be read")
os.Exit(1) os.Exit(1)
} }
updates, err := parseRefUpdates() update, err := branchUpdateFromFlags()
if err != nil { if err != nil {
log.Err(fmt.Sprintf("could not parse updated refs:", err)) log.Err(fmt.Sprintf("failed to parse ref update: %s", err))
os.Exit(1) os.Exit(1)
} }
fmt.Fprintf(log, "triggering builds for %v refs", len(updates)) if update == nil { // the project was not 'depot'
os.Exit(0)
for _, update := range updates {
triggerBuild(log, string(token), update.name, update.new)
} }
triggerBuild(log, string(token), update)
} }