feat(besadii): Support invocation as different Gerrit hooks

Removes besadii support for the previously used 'ref-updated' hook and
instead introduces support for the 'change-merged' and
'patchset-created' hooks.

These hooks more accurately capture the semantics of when besadii
should trigger CI builds and using them will avoid problems such as
skipping 'canon' builds if chains of CLs are submitted together.

Change-Id: Ib90356c069780bf0c0250e56b927e46a5b31ce7f
This commit is contained in:
Vincent Ambo 2021-11-27 20:36:14 +03:00
parent 8318178b5f
commit c1aab56a02
2 changed files with 118 additions and 67 deletions

View file

@ -25,12 +25,12 @@ import (
"os" "os"
"path" "path"
"regexp" "regexp"
"strconv"
"strings" "strings"
) )
var branchRegexp = regexp.MustCompile(`^refs/heads/(.*)$`) // Regular expression to extract change ID out of a URL
var metaRegexp = regexp.MustCompile(`^refs/changes/\d{0,2}/(\d+)/meta$`) var changeIdRegexp = regexp.MustCompile(`^.*/(\d+)$`)
var patchsetRegexp = regexp.MustCompile(`^refs/changes/\d{0,2}/(\d+)/(\d+)$`)
// buildTrigger represents the information passed to besadii when it // buildTrigger represents the information passed to besadii when it
// is invoked as a Gerrit hook. // is invoked as a Gerrit hook.
@ -40,11 +40,10 @@ type buildTrigger struct {
project string project string
ref string ref string
commit string commit string
submitter string owner string
email string
changeId *string changeId string
patchset *string patchset string
} }
type Author struct { type Author struct {
@ -118,9 +117,11 @@ func triggerBuild(log *syslog.Writer, token string, trigger *buildTrigger) error
// //
// This information is later used by besadii when invoked by Gerrit // This information is later used by besadii when invoked by Gerrit
// to communicate the build status back to Gerrit. // to communicate the build status back to Gerrit.
if trigger.changeId != nil && trigger.patchset != nil { canonBuild := true
env["GERRIT_CHANGE_ID"] = *trigger.changeId if trigger.changeId != "" && trigger.patchset != "" {
env["GERRIT_PATCHSET"] = *trigger.patchset env["GERRIT_CHANGE_ID"] = trigger.changeId
env["GERRIT_PATCHSET"] = trigger.patchset
canonBuild = false
} }
build := Build{ build := Build{
@ -128,8 +129,7 @@ func triggerBuild(log *syslog.Writer, token string, trigger *buildTrigger) error
Branch: trigger.ref, Branch: trigger.ref,
Env: env, Env: env,
Author: Author{ Author: Author{
Name: trigger.submitter, Name: trigger.owner,
Email: trigger.email,
}, },
} }
@ -168,9 +168,14 @@ func triggerBuild(log *syslog.Writer, token string, trigger *buildTrigger) error
fmt.Fprintf(log, "triggered build for ref %q at commit %q: %s", trigger.ref, trigger.commit, buildResp.WebUrl) fmt.Fprintf(log, "triggered build for ref %q at commit %q: %s", trigger.ref, trigger.commit, buildResp.WebUrl)
// For builds of canon there is nothing else to do
if canonBuild {
return nil
}
// Report the status back to the Gerrit CL so that users can click // Report the status back to the Gerrit CL so that users can click
// through to the running build. // through to the running build.
msg := fmt.Sprintf("Started build for patchset #%s of cl/%s: %s", *trigger.patchset, *trigger.changeId, buildResp.WebUrl) msg := fmt.Sprintf("Started build for patchset #%s of cl/%s: %s", trigger.patchset, trigger.changeId, buildResp.WebUrl)
review := reviewInput{ review := reviewInput{
Message: msg, Message: msg,
OmitDuplicateComments: true, OmitDuplicateComments: true,
@ -179,7 +184,7 @@ func triggerBuild(log *syslog.Writer, token string, trigger *buildTrigger) error
// Do not update the attention set for this comment. // Do not update the attention set for this comment.
IgnoreDefaultAttentionSetRules: true, IgnoreDefaultAttentionSetRules: true,
} }
updateGerrit(review, *trigger.changeId, *trigger.patchset) updateGerrit(review, trigger.changeId, trigger.patchset)
return nil return nil
} }
@ -199,69 +204,96 @@ func triggerIndexUpdate(token string) error {
return err return err
} }
func buildTriggerFromFlags() (*buildTrigger, error) { // 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". This function allows users to ignore
// flags.
//
// [0]: https://github.com/golang/go/issues/6112#issuecomment-66083768
func ignoreFlags(ignore []string) {
for _, f := range ignore {
flag.String(f, "", "flag to ignore")
}
}
// Extract the buildtrigger struct out of the flags passed to besadii
// when invoked as Gerrit's 'patchset-created' hook. This hook is used
// for triggering CI on in-progress CLs.
func buildTriggerFromPatchsetCreated() (*buildTrigger, error) {
// Information that needs to be returned
var trigger buildTrigger var trigger buildTrigger
flag.StringVar(&trigger.project, "project", "", "Gerrit project") // Information that is only needed for parsing
flag.StringVar(&trigger.commit, "newrev", "", "new revision") var targetBranch, changeUrl string
flag.StringVar(&trigger.email, "submitter", "", "Submitter email")
flag.StringVar(&trigger.submitter, "submitter-username", "", "Submitter username")
flag.StringVar(&trigger.ref, "refname", "", "updated reference name")
// Gerrit passes more flags than we want, but Rob Pike decided[0] in flag.StringVar(&trigger.project, "project", "", "Gerrit project")
// 2013 that the Go art project will not allow users to ignore flags flag.StringVar(&trigger.commit, "commit", "", "commit hash")
// because he "doesn't like it". The following code ignores the flag.StringVar(&trigger.owner, "change-owner", "", "change owner")
// flags. flag.StringVar(&trigger.patchset, "patchset", "", "patchset ID")
//
// [0]: https://github.com/golang/go/issues/6112#issuecomment-66083768 flag.StringVar(&targetBranch, "branch", "", "CL target branch")
var _old string flag.StringVar(&changeUrl, "change-url", "", "HTTPS URL of change")
flag.StringVar(&_old, "oldrev", "", "")
// patchset-created also passes various flags which we don't need.
ignoreFlags([]string{"kind", "topic", "change", "uploader", "uploader-username", "change-owner-username"})
flag.Parse() flag.Parse()
if trigger.project == "" || trigger.ref == "" || trigger.commit == "" || trigger.submitter == "" { // If the patchset is not for depot@canon then we can ignore it. It
// If we get here, the user is probably being a dummy and invoking // might be some other kind of change (refs/meta/config or
// this manually - but incorrectly. // Gerrit-internal), but it is not an error.
return nil, fmt.Errorf("'ref-updated' hook invoked without required arguments") if trigger.project != "depot" || targetBranch != "canon" {
}
if trigger.project != "depot" || metaRegexp.MatchString(trigger.ref) {
// this is not an error, but also not something we handle.
return nil, nil return nil, nil
} }
if branchRegexp.MatchString(trigger.ref) { // Change ID is not directly passed in the numeric format, so we
// these refs don't need special handling, just move on // need to extract it out of the URL
return &trigger, nil matches := changeIdRegexp.FindStringSubmatch(changeUrl)
} trigger.changeId = matches[1]
if matches := patchsetRegexp.FindStringSubmatch(trigger.ref); matches != nil { // Construct the CL ref from which the build should happen.
trigger.changeId = &matches[1] changeId, _ := strconv.Atoi(trigger.changeId)
trigger.patchset = &matches[2] trigger.ref = fmt.Sprintf(
return &trigger, nil "refs/changes/%02d/%s/%s",
} changeId%100, trigger.changeId, trigger.patchset,
)
return nil, fmt.Errorf("besadii does not support updates for this type of ref (%q)", trigger.ref) return &trigger, nil
} }
func refUpdatedMain() { // Extract the buildtrigger struct out of the flags passed to besadii
// Logging happens in syslog for Gerrit hooks because we don't want // when invoked as Gerrit's 'change-merged' hook. This hook is used
// the hook output to be intermingled with Gerrit's own output // for triggering canon builds after change submission.
// stream func buildTriggerFromChangeMerged() *buildTrigger {
log, err := syslog.New(syslog.LOG_INFO|syslog.LOG_USER, "besadii") // Information that needs to be returned
if err != nil { var trigger buildTrigger
fmt.Fprintf(os.Stderr, "failed to open syslog: %s\n", err)
os.Exit(1) // Information that is only needed for parsing
var targetBranch string
flag.StringVar(&trigger.project, "project", "", "Gerrit project")
flag.StringVar(&trigger.commit, "commit", "", "Commit hash")
flag.StringVar(&trigger.owner, "change-owner", "", "Change owner")
flag.StringVar(&targetBranch, "branch", "", "CL target branch")
// Ignore extra flags passed by change-merged
ignoreFlags([]string{"change", "topic", "change-url", "submitter", "submitter-username", "newrev", "change-owner-username"})
flag.Parse()
// Skip builds for anything other than depot@canon
if trigger.project != "depot" || targetBranch != "canon" {
return nil
} }
trigger, err := buildTriggerFromFlags() trigger.ref = "refs/heads/canon"
if err != nil {
log.Err(fmt.Sprintf("failed to parse ref update: %s", err))
os.Exit(1)
}
if trigger == nil { // the project was not 'depot' return &trigger
log.Err("build triggers are only supported for the 'depot' project") }
func gerritHookMain(log *syslog.Writer, trigger *buildTrigger) {
if trigger == nil {
// The hook was not for something we care about.
os.Exit(0) os.Exit(0)
} }
@ -347,10 +379,28 @@ func postCommandMain() {
} }
func main() { func main() {
// Logging happens in syslog because it's almost impossible to get
// output out of Gerrit hooks otherwise.
log, err := syslog.New(syslog.LOG_INFO|syslog.LOG_USER, "besadii")
if err != nil {
fmt.Fprintf(os.Stderr, "failed to open syslog: %s\n", err)
os.Exit(1)
}
log.Info(fmt.Sprintf("besadii called with arguments: %v", os.Args))
bin := path.Base(os.Args[0]) bin := path.Base(os.Args[0])
if bin == "ref-updated" { if bin == "patchset-created" {
refUpdatedMain() trigger, err := buildTriggerFromPatchsetCreated()
if err != nil {
log.Crit("failed to parse 'patchset-created' invocation from args")
os.Exit(1)
}
gerritHookMain(log, trigger)
} else if bin == "change-merged" {
trigger := buildTriggerFromChangeMerged()
gerritHookMain(log, trigger)
} else if bin == "post-command" { } else if bin == "post-command" {
postCommandMain() postCommandMain()
} else { } else {

View file

@ -5,7 +5,8 @@ let
cfg = config.services.gerrit; cfg = config.services.gerrit;
gerritHooks = pkgs.runCommandNoCC "gerrit-hooks" {} '' gerritHooks = pkgs.runCommandNoCC "gerrit-hooks" {} ''
mkdir -p $out mkdir -p $out
ln -s ${depot.ops.besadii}/bin/besadii $out/ref-updated ln -s ${depot.ops.besadii}/bin/besadii $out/change-merged
ln -s ${depot.ops.besadii}/bin/besadii $out/patchset-created
''; '';
in { in {
services.gerrit = { services.gerrit = {