refactor(besadii): Rename refUpdated -> buildTrigger

We are changing the Gerrit hooks which invoke besadii, but this
structure will be used for both kinds.

Change-Id: Idb1cb0c640d2c42db8e7af39f3ab372a97bfef91
This commit is contained in:
Vincent Ambo 2021-11-27 19:24:24 +03:00 committed by tazjin
parent f7c87fd774
commit eca2bc572e

View file

@ -32,11 +32,11 @@ var branchRegexp = regexp.MustCompile(`^refs/heads/(.*)$`)
var metaRegexp = regexp.MustCompile(`^refs/changes/\d{0,2}/(\d+)/meta$`) var metaRegexp = regexp.MustCompile(`^refs/changes/\d{0,2}/(\d+)/meta$`)
var patchsetRegexp = regexp.MustCompile(`^refs/changes/\d{0,2}/(\d+)/(\d+)$`) var patchsetRegexp = regexp.MustCompile(`^refs/changes/\d{0,2}/(\d+)/(\d+)$`)
// refUpdated is a struct representing the information passed to // buildTrigger represents the information passed to besadii when it
// besadii when it is invoked as Gerrit's refUpdated hook. // is invoked as a Gerrit hook.
// //
// https://gerrit.googlesource.com/plugins/hooks/+/HEAD/src/main/resources/Documentation/hooks.md#ref_updated // https://gerrit.googlesource.com/plugins/hooks/+/HEAD/src/main/resources/Documentation/hooks.md
type refUpdated struct { type buildTrigger struct {
project string project string
ref string ref string
commit string commit string
@ -110,21 +110,26 @@ func updateGerrit(review reviewInput, changeId, patchset string) {
} }
// Trigger a build of a given branch & commit on Buildkite // Trigger a build of a given branch & commit on Buildkite
func triggerBuild(log *syslog.Writer, token string, update *refUpdated) error { func triggerBuild(log *syslog.Writer, token string, trigger *buildTrigger) error {
env := make(map[string]string) env := make(map[string]string)
if update.changeId != nil && update.patchset != nil { // Pass information about the originating Gerrit change to the
env["GERRIT_CHANGE_ID"] = *update.changeId // build, if it is for a patchset.
env["GERRIT_PATCHSET"] = *update.patchset //
// This information is later used by besadii when invoked by Gerrit
// to communicate the build status back to Gerrit.
if trigger.changeId != nil && trigger.patchset != nil {
env["GERRIT_CHANGE_ID"] = *trigger.changeId
env["GERRIT_PATCHSET"] = *trigger.patchset
} }
build := Build{ build := Build{
Commit: update.commit, Commit: trigger.commit,
Branch: update.ref, Branch: trigger.ref,
Env: env, Env: env,
Author: Author{ Author: Author{
Name: update.submitter, Name: trigger.submitter,
Email: update.email, Email: trigger.email,
}, },
} }
@ -161,11 +166,11 @@ func triggerBuild(log *syslog.Writer, token string, update *refUpdated) error {
return fmt.Errorf("failed to unmarshal build response: %w", err) 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) fmt.Fprintf(log, "triggered build for ref %q at commit %q: %s", trigger.ref, trigger.commit, buildResp.WebUrl)
// 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", *update.patchset, *update.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,
@ -174,7 +179,7 @@ func triggerBuild(log *syslog.Writer, token string, update *refUpdated) 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, *update.changeId, *update.patchset) updateGerrit(review, *trigger.changeId, *trigger.patchset)
return nil return nil
} }
@ -194,14 +199,14 @@ func triggerIndexUpdate(token string) error {
return err return err
} }
func refUpdatedFromFlags() (*refUpdated, error) { func buildTriggerFromFlags() (*buildTrigger, error) {
var update refUpdated var trigger buildTrigger
flag.StringVar(&update.project, "project", "", "Gerrit project") flag.StringVar(&trigger.project, "project", "", "Gerrit project")
flag.StringVar(&update.commit, "newrev", "", "new revision") flag.StringVar(&trigger.commit, "newrev", "", "new revision")
flag.StringVar(&update.email, "submitter", "", "Submitter email") flag.StringVar(&trigger.email, "submitter", "", "Submitter email")
flag.StringVar(&update.submitter, "submitter-username", "", "Submitter username") flag.StringVar(&trigger.submitter, "submitter-username", "", "Submitter username")
flag.StringVar(&update.ref, "refname", "", "updated reference name") flag.StringVar(&trigger.ref, "refname", "", "updated reference name")
// Gerrit passes more flags than we want, but Rob Pike decided[0] in // 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 // 2013 that the Go art project will not allow users to ignore flags
@ -214,29 +219,29 @@ func refUpdatedFromFlags() (*refUpdated, error) {
flag.Parse() flag.Parse()
if update.project == "" || update.ref == "" || update.commit == "" || update.submitter == "" { if trigger.project == "" || trigger.ref == "" || trigger.commit == "" || trigger.submitter == "" {
// If we get here, the user is probably being a dummy and invoking // If we get here, the user is probably being a dummy and invoking
// this manually - but incorrectly. // this manually - but incorrectly.
return nil, fmt.Errorf("'ref-updated' hook invoked without required arguments") return nil, fmt.Errorf("'ref-updated' hook invoked without required arguments")
} }
if update.project != "depot" || metaRegexp.MatchString(update.ref) { if trigger.project != "depot" || metaRegexp.MatchString(trigger.ref) {
// this is not an error, but also not something we handle. // this is not an error, but also not something we handle.
return nil, nil return nil, nil
} }
if branchRegexp.MatchString(update.ref) { if branchRegexp.MatchString(trigger.ref) {
// these refs don't need special handling, just move on // these refs don't need special handling, just move on
return &update, nil return &trigger, nil
} }
if matches := patchsetRegexp.FindStringSubmatch(update.ref); matches != nil { if matches := patchsetRegexp.FindStringSubmatch(trigger.ref); matches != nil {
update.changeId = &matches[1] trigger.changeId = &matches[1]
update.patchset = &matches[2] trigger.patchset = &matches[2]
return &update, nil return &trigger, nil
} }
return nil, fmt.Errorf("besadii does not support updates for this type of ref (%q)", update.ref) return nil, fmt.Errorf("besadii does not support updates for this type of ref (%q)", trigger.ref)
} }
func refUpdatedMain() { func refUpdatedMain() {
@ -249,13 +254,13 @@ func refUpdatedMain() {
os.Exit(1) os.Exit(1)
} }
update, err := refUpdatedFromFlags() trigger, err := buildTriggerFromFlags()
if err != nil { if err != nil {
log.Err(fmt.Sprintf("failed to parse ref update: %s", err)) log.Err(fmt.Sprintf("failed to parse ref update: %s", err))
os.Exit(1) os.Exit(1)
} }
if update == nil { // the project was not 'depot' if trigger == nil { // the project was not 'depot'
log.Err("build triggers are only supported for the 'depot' project") log.Err("build triggers are only supported for the 'depot' project")
os.Exit(0) os.Exit(0)
} }
@ -274,7 +279,8 @@ func refUpdatedMain() {
} }
sourcegraphToken := strings.TrimSpace(string(sourcegraphTokenBytes)) sourcegraphToken := strings.TrimSpace(string(sourcegraphTokenBytes))
err = triggerBuild(log, buildkiteToken, update) err = triggerBuild(log, buildkiteToken, trigger)
if err != nil { if err != nil {
log.Err(fmt.Sprintf("failed to trigger Buildkite build: %s", err)) log.Err(fmt.Sprintf("failed to trigger Buildkite build: %s", err))
} }