2020-06-13 06:59:15 +02:00
|
|
|
// Copyright 2019-2020 Google LLC.
|
2020-02-13 23:38:34 +01:00
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
//
|
2020-06-29 04:35:39 +02:00
|
|
|
// besadii is a small CLI tool that is invoked as a hook by various
|
|
|
|
// programs to cause CI-related actions.
|
2020-02-13 23:38:34 +01:00
|
|
|
//
|
2020-06-29 04:35:39 +02:00
|
|
|
// It supports the following modes & operations:
|
|
|
|
//
|
|
|
|
// Gerrit (ref-updated) hook:
|
|
|
|
// - Trigger Buildkite CI builds
|
2021-11-29 16:22:59 +01:00
|
|
|
// - Trigger SourceGraph repository index updates
|
2020-06-29 04:35:39 +02:00
|
|
|
//
|
|
|
|
// Buildkite (post-command) hook:
|
|
|
|
// - Submit CL verification status back to Gerrit
|
2020-02-13 23:38:34 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
2020-06-13 06:59:15 +02:00
|
|
|
"flag"
|
2020-02-13 23:38:34 +01:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
2020-02-21 23:46:34 +01:00
|
|
|
"log/syslog"
|
2020-02-13 23:38:34 +01:00
|
|
|
"net/http"
|
2021-12-06 15:42:57 +01:00
|
|
|
"net/mail"
|
2020-02-13 23:38:34 +01:00
|
|
|
"os"
|
2021-12-02 15:13:49 +01:00
|
|
|
"os/user"
|
2020-06-13 06:59:15 +02:00
|
|
|
"path"
|
2020-06-28 04:37:34 +02:00
|
|
|
"regexp"
|
2021-11-27 18:36:14 +01:00
|
|
|
"strconv"
|
2020-02-13 23:38:34 +01:00
|
|
|
)
|
|
|
|
|
2021-11-27 18:36:14 +01:00
|
|
|
// Regular expression to extract change ID out of a URL
|
|
|
|
var changeIdRegexp = regexp.MustCompile(`^.*/(\d+)$`)
|
2020-02-22 00:30:05 +01:00
|
|
|
|
2021-11-29 16:22:59 +01:00
|
|
|
// besadii configuration file structure
|
|
|
|
type config struct {
|
2021-11-29 17:06:09 +01:00
|
|
|
// Required configuration for Buildkite<>Gerrit monorepo
|
|
|
|
// integration.
|
|
|
|
Repository string `json:"repository"`
|
|
|
|
Branch string `json:"branch"`
|
|
|
|
GerritUrl string `json:"gerritUrl"`
|
|
|
|
GerritUser string `json:"gerritUser"`
|
|
|
|
GerritPassword string `json:"gerritPassword"`
|
|
|
|
BuildkiteOrg string `json:"buildkiteOrg"`
|
|
|
|
BuildkiteProject string `json:"buildkiteProject"`
|
|
|
|
BuildkiteToken string `json:"buildkiteToken"`
|
|
|
|
|
|
|
|
// Optional configuration for Sourcegraph trigger updates.
|
2021-11-29 16:22:59 +01:00
|
|
|
SourcegraphUrl string `json:"sourcegraphUrl"`
|
|
|
|
SourcegraphToken string `json:"sourcegraphToken"`
|
|
|
|
}
|
|
|
|
|
2021-11-27 17:24:24 +01:00
|
|
|
// buildTrigger represents the information passed to besadii when it
|
|
|
|
// is invoked as a Gerrit hook.
|
2020-02-21 23:32:23 +01:00
|
|
|
//
|
2021-11-27 17:24:24 +01:00
|
|
|
// https://gerrit.googlesource.com/plugins/hooks/+/HEAD/src/main/resources/Documentation/hooks.md
|
|
|
|
type buildTrigger struct {
|
2021-11-27 18:36:14 +01:00
|
|
|
project string
|
|
|
|
ref string
|
|
|
|
commit string
|
2021-12-06 15:42:57 +01:00
|
|
|
author string
|
|
|
|
email string
|
2021-11-27 18:36:14 +01:00
|
|
|
|
|
|
|
changeId string
|
|
|
|
patchset string
|
2020-02-21 23:32:23 +01:00
|
|
|
}
|
|
|
|
|
2020-06-27 19:57:55 +02:00
|
|
|
type Author struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
Email string `json:"email"`
|
2020-02-13 23:38:34 +01:00
|
|
|
}
|
|
|
|
|
2020-06-27 19:57:55 +02:00
|
|
|
// Build is the representation of a Buildkite build as described on
|
|
|
|
// https://buildkite.com/docs/apis/rest-api/builds#create-a-build
|
|
|
|
type Build struct {
|
2021-08-18 16:20:19 +02:00
|
|
|
Commit string `json:"commit"`
|
|
|
|
Branch string `json:"branch"`
|
|
|
|
Author Author `json:"author"`
|
|
|
|
Env map[string]string `json:"env"`
|
2020-02-13 23:38:34 +01:00
|
|
|
}
|
|
|
|
|
2021-09-18 15:07:24 +02:00
|
|
|
// BuildResponse is the representation of Buildkite's success response
|
|
|
|
// after triggering a build. This has many fields, but we only need
|
|
|
|
// one of them.
|
|
|
|
type buildResponse struct {
|
|
|
|
WebUrl string `json:"web_url"`
|
|
|
|
}
|
|
|
|
|
2021-09-18 14:49:39 +02:00
|
|
|
// reviewInput is a struct representing the data submitted to Gerrit
|
|
|
|
// to post a review on a CL.
|
|
|
|
//
|
|
|
|
// https://gerrit-review.googlesource.com/Documentation/rest-api-changes.html#review-input
|
|
|
|
type reviewInput struct {
|
|
|
|
Message string `json:"message"`
|
|
|
|
Labels map[string]int `json:"labels,omitempty"`
|
|
|
|
OmitDuplicateComments bool `json:"omit_duplicate_comments"`
|
|
|
|
IgnoreDefaultAttentionSetRules bool `json:"ignore_default_attention_set_rules"`
|
|
|
|
Tag string `json:"tag"`
|
|
|
|
}
|
|
|
|
|
2021-12-02 15:13:49 +01:00
|
|
|
func defaultConfigLocation() (string, error) {
|
|
|
|
usr, err := user.Current()
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("failed to get current user: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return path.Join(usr.HomeDir, "besadii.json"), nil
|
|
|
|
}
|
|
|
|
|
2021-11-29 16:22:59 +01:00
|
|
|
func loadConfig() (*config, error) {
|
|
|
|
configPath := os.Getenv("BESADII_CONFIG")
|
2021-12-02 15:13:49 +01:00
|
|
|
|
2021-11-29 16:22:59 +01:00
|
|
|
if configPath == "" {
|
2021-12-02 15:13:49 +01:00
|
|
|
var err error
|
|
|
|
configPath, err = defaultConfigLocation()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to get config location: %w", err)
|
|
|
|
}
|
2021-11-29 16:22:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
configJson, err := ioutil.ReadFile(configPath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to load besadii config: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var cfg config
|
|
|
|
err = json.Unmarshal(configJson, &cfg)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to unmarshal besadii config: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Rudimentary config validation logic
|
|
|
|
if cfg.SourcegraphUrl != "" && cfg.SourcegraphToken == "" {
|
|
|
|
return nil, fmt.Errorf("'SourcegraphToken' must be set if 'SourcegraphUrl' is set")
|
|
|
|
}
|
|
|
|
|
2021-11-29 17:06:09 +01:00
|
|
|
if cfg.Repository == "" || cfg.Branch == "" {
|
|
|
|
return nil, fmt.Errorf("missing repository configuration (required: repository, branch)")
|
|
|
|
}
|
|
|
|
|
|
|
|
if cfg.GerritUrl == "" || cfg.GerritUser == "" || cfg.GerritPassword == "" {
|
|
|
|
return nil, fmt.Errorf("missing Gerrit configuration (required: gerritUrl, gerritUser, gerritPassword)")
|
|
|
|
}
|
|
|
|
|
|
|
|
if cfg.BuildkiteOrg == "" || cfg.BuildkiteProject == "" || cfg.BuildkiteToken == "" {
|
|
|
|
return nil, fmt.Errorf("mising Buildkite configuration (required: buildkiteOrg, buildkiteProject, buildkiteToken)")
|
|
|
|
}
|
|
|
|
|
2021-11-29 16:22:59 +01:00
|
|
|
return &cfg, nil
|
|
|
|
}
|
|
|
|
|
2021-09-18 14:49:39 +02:00
|
|
|
// updateGerrit posts a comment on a Gerrit CL to indicate the current build status.
|
2021-11-29 17:20:06 +01:00
|
|
|
func updateGerrit(cfg *config, review reviewInput, changeId, patchset string) {
|
2021-09-18 14:49:39 +02:00
|
|
|
body, _ := json.Marshal(review)
|
|
|
|
reader := ioutil.NopCloser(bytes.NewReader(body))
|
|
|
|
|
2021-11-29 17:20:06 +01:00
|
|
|
url := fmt.Sprintf("%s/a/changes/%s/revisions/%s/review", cfg.GerritUrl, changeId, patchset)
|
2021-09-18 14:49:39 +02:00
|
|
|
req, err := http.NewRequest("POST", url, reader)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "failed to create an HTTP request: %w", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2021-11-29 17:20:06 +01:00
|
|
|
req.SetBasicAuth(cfg.GerritUser, cfg.GerritPassword)
|
2021-09-18 14:49:39 +02:00
|
|
|
req.Header.Add("Content-Type", "application/json")
|
|
|
|
|
|
|
|
resp, err := http.DefaultClient.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Errorf("failed to update CL on Gerrit: %w", err)
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
respBody, _ := ioutil.ReadAll(resp.Body)
|
|
|
|
fmt.Fprintf(os.Stderr, "received non-success response from Gerrit: %s (%v)", respBody, resp.Status)
|
|
|
|
} else {
|
2021-11-29 17:20:06 +01:00
|
|
|
fmt.Printf("Added CI status comment on %s/c/%s/+/%s/%s", cfg.GerritUrl, cfg.Repository, changeId, patchset)
|
2021-09-18 14:49:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-27 19:57:55 +02:00
|
|
|
// Trigger a build of a given branch & commit on Buildkite
|
2021-11-29 17:20:06 +01:00
|
|
|
func triggerBuild(cfg *config, log *syslog.Writer, trigger *buildTrigger) error {
|
2020-06-29 03:16:48 +02:00
|
|
|
env := make(map[string]string)
|
2020-06-28 04:37:34 +02:00
|
|
|
|
2021-11-27 17:24:24 +01:00
|
|
|
// Pass information about the originating Gerrit change to the
|
|
|
|
// build, if it is for a patchset.
|
|
|
|
//
|
|
|
|
// This information is later used by besadii when invoked by Gerrit
|
|
|
|
// to communicate the build status back to Gerrit.
|
2021-11-29 17:20:06 +01:00
|
|
|
headBuild := true
|
2021-11-27 18:36:14 +01:00
|
|
|
if trigger.changeId != "" && trigger.patchset != "" {
|
|
|
|
env["GERRIT_CHANGE_ID"] = trigger.changeId
|
|
|
|
env["GERRIT_PATCHSET"] = trigger.patchset
|
2021-11-29 17:20:06 +01:00
|
|
|
headBuild = false
|
2020-06-28 04:37:34 +02:00
|
|
|
}
|
|
|
|
|
2020-02-13 23:38:34 +01:00
|
|
|
build := Build{
|
2021-11-27 17:24:24 +01:00
|
|
|
Commit: trigger.commit,
|
|
|
|
Branch: trigger.ref,
|
2021-08-18 16:20:19 +02:00
|
|
|
Env: env,
|
2020-06-27 19:57:55 +02:00
|
|
|
Author: Author{
|
2021-12-06 15:42:57 +01:00
|
|
|
Name: trigger.author,
|
|
|
|
Email: trigger.email,
|
2020-02-13 23:38:34 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
body, _ := json.Marshal(build)
|
|
|
|
reader := ioutil.NopCloser(bytes.NewReader(body))
|
|
|
|
|
2021-11-29 17:20:06 +01:00
|
|
|
bkUrl := fmt.Sprintf("https://api.buildkite.com/v2/organizations/%s/pipelines/%s/builds", cfg.BuildkiteOrg, cfg.BuildkiteProject)
|
|
|
|
req, err := http.NewRequest("POST", bkUrl, reader)
|
2020-02-13 23:38:34 +01:00
|
|
|
if err != nil {
|
2020-06-19 02:27:32 +02:00
|
|
|
return fmt.Errorf("failed to create an HTTP request: %w", err)
|
2020-02-13 23:38:34 +01:00
|
|
|
}
|
|
|
|
|
2021-11-29 17:20:06 +01:00
|
|
|
req.Header.Add("Authorization", "Bearer "+cfg.BuildkiteToken)
|
2020-02-13 23:38:34 +01:00
|
|
|
req.Header.Add("Content-Type", "application/json")
|
|
|
|
|
|
|
|
resp, err := http.DefaultClient.Do(req)
|
|
|
|
if err != nil {
|
2020-06-27 19:57:55 +02:00
|
|
|
// This might indicate a temporary error on the Buildkite side.
|
|
|
|
return fmt.Errorf("failed to send Buildkite request: %w", err)
|
2020-02-13 23:38:34 +01:00
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
2021-09-18 15:07:24 +02:00
|
|
|
respBody, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to read Buildkite response body: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp.StatusCode != http.StatusCreated {
|
|
|
|
return fmt.Errorf("received non-success response from Buildkite: %s (%v)", respBody, resp.Status)
|
|
|
|
}
|
|
|
|
|
|
|
|
var buildResp buildResponse
|
|
|
|
err = json.Unmarshal(respBody, &buildResp)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to unmarshal build response: %w", err)
|
|
|
|
}
|
|
|
|
|
2021-11-27 17:24:24 +01:00
|
|
|
fmt.Fprintf(log, "triggered build for ref %q at commit %q: %s", trigger.ref, trigger.commit, buildResp.WebUrl)
|
2021-09-18 15:07:24 +02:00
|
|
|
|
2021-11-29 17:20:06 +01:00
|
|
|
// For builds of the HEAD branch there is nothing else to do
|
|
|
|
if headBuild {
|
2021-11-27 18:36:14 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-09-18 15:07:24 +02:00
|
|
|
// Report the status back to the Gerrit CL so that users can click
|
|
|
|
// through to the running build.
|
2021-11-27 18:36:14 +01:00
|
|
|
msg := fmt.Sprintf("Started build for patchset #%s of cl/%s: %s", trigger.patchset, trigger.changeId, buildResp.WebUrl)
|
2021-09-18 15:07:24 +02:00
|
|
|
review := reviewInput{
|
|
|
|
Message: msg,
|
|
|
|
OmitDuplicateComments: true,
|
|
|
|
Tag: "autogenerated:buildkite~trigger",
|
|
|
|
|
|
|
|
// Do not update the attention set for this comment.
|
|
|
|
IgnoreDefaultAttentionSetRules: true,
|
2020-02-13 23:38:34 +01:00
|
|
|
}
|
2021-11-29 17:20:06 +01:00
|
|
|
updateGerrit(cfg, review, trigger.changeId, trigger.patchset)
|
2020-06-19 02:27:32 +02:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-11-29 16:22:59 +01:00
|
|
|
// Trigger a Sourcegraph repository index update.
|
2020-06-19 02:27:32 +02:00
|
|
|
//
|
|
|
|
// https://docs.sourcegraph.com/admin/repo/webhooks
|
2021-11-29 16:22:59 +01:00
|
|
|
func triggerIndexUpdate(cfg *config, log *syslog.Writer) error {
|
|
|
|
req, err := http.NewRequest("POST", cfg.SourcegraphUrl, nil)
|
2020-06-19 02:27:32 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-11-29 16:22:59 +01:00
|
|
|
req.Header.Add("Authorization", "token "+cfg.SourcegraphToken)
|
2020-06-19 02:27:32 +02:00
|
|
|
|
|
|
|
_, err = http.DefaultClient.Do(req)
|
2021-11-29 16:22:59 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to trigger Sourcegraph index update: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Info("triggered sourcegraph index update")
|
|
|
|
return nil
|
2020-02-13 23:38:34 +01:00
|
|
|
}
|
|
|
|
|
2021-11-27 18:36:14 +01:00
|
|
|
// 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")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-06 15:42:57 +01:00
|
|
|
// Extract the username & email from Gerrit's uploader flag and set it
|
|
|
|
// on the trigger struct, for display in Buildkite.
|
|
|
|
func extractChangeUploader(uploader string, trigger *buildTrigger) error {
|
2021-12-06 18:03:11 +01:00
|
|
|
// Gerrit passes the uploader in another extra layer of quotes.
|
|
|
|
uploader, err := strconv.Unquote(uploader)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to unquote email - forgot quotes on manual invocation?: %w", err)
|
|
|
|
}
|
|
|
|
|
2021-12-06 15:42:57 +01:00
|
|
|
// Extract the uploader username & email from the input passed by
|
|
|
|
// Gerrit (in RFC 5322 format).
|
|
|
|
addr, err := mail.ParseAddress(uploader)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("invalid change uploader (%s): %w", uploader, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
trigger.author = addr.Name
|
|
|
|
trigger.email = addr.Address
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-11-27 18:36:14 +01:00
|
|
|
// 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.
|
2021-11-29 17:20:06 +01:00
|
|
|
func buildTriggerFromPatchsetCreated(cfg *config) (*buildTrigger, error) {
|
2021-11-27 18:36:14 +01:00
|
|
|
// Information that needs to be returned
|
2021-11-27 17:24:24 +01:00
|
|
|
var trigger buildTrigger
|
2020-06-13 06:59:15 +02:00
|
|
|
|
2021-11-27 18:36:14 +01:00
|
|
|
// Information that is only needed for parsing
|
2021-12-06 15:42:57 +01:00
|
|
|
var targetBranch, changeUrl, uploader string
|
2021-11-27 18:36:14 +01:00
|
|
|
|
2021-11-27 17:24:24 +01:00
|
|
|
flag.StringVar(&trigger.project, "project", "", "Gerrit project")
|
2021-11-27 18:36:14 +01:00
|
|
|
flag.StringVar(&trigger.commit, "commit", "", "commit hash")
|
|
|
|
flag.StringVar(&trigger.patchset, "patchset", "", "patchset ID")
|
2020-06-13 06:59:15 +02:00
|
|
|
|
2021-11-27 18:36:14 +01:00
|
|
|
flag.StringVar(&targetBranch, "branch", "", "CL target branch")
|
|
|
|
flag.StringVar(&changeUrl, "change-url", "", "HTTPS URL of change")
|
2021-12-06 15:42:57 +01:00
|
|
|
flag.StringVar(&uploader, "uploader", "", "Change uploader name & email")
|
2020-06-13 06:59:15 +02:00
|
|
|
|
2021-11-27 18:36:14 +01:00
|
|
|
// patchset-created also passes various flags which we don't need.
|
2021-12-06 15:42:57 +01:00
|
|
|
ignoreFlags([]string{"kind", "topic", "change", "uploader-username", "change-owner", "change-owner-username"})
|
2021-11-27 18:36:14 +01:00
|
|
|
|
|
|
|
flag.Parse()
|
2020-02-22 00:30:05 +01:00
|
|
|
|
2021-12-06 15:42:57 +01:00
|
|
|
// Parse username & email
|
|
|
|
err := extractChangeUploader(uploader, &trigger)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-11-29 17:20:06 +01:00
|
|
|
// If the patchset is not for the HEAD branch of the monorepo, then
|
|
|
|
// we can ignore it. It might be some other kind of change
|
|
|
|
// (refs/meta/config or Gerrit-internal), but it is not an error.
|
|
|
|
if trigger.project != cfg.Repository || targetBranch != cfg.Branch {
|
2020-06-13 06:59:15 +02:00
|
|
|
return nil, nil
|
2020-02-21 23:32:23 +01:00
|
|
|
}
|
|
|
|
|
2021-11-27 18:36:14 +01:00
|
|
|
// Change ID is not directly passed in the numeric format, so we
|
|
|
|
// need to extract it out of the URL
|
|
|
|
matches := changeIdRegexp.FindStringSubmatch(changeUrl)
|
|
|
|
trigger.changeId = matches[1]
|
2020-02-21 23:32:23 +01:00
|
|
|
|
2021-11-27 18:36:14 +01:00
|
|
|
// Construct the CL ref from which the build should happen.
|
|
|
|
changeId, _ := strconv.Atoi(trigger.changeId)
|
|
|
|
trigger.ref = fmt.Sprintf(
|
|
|
|
"refs/changes/%02d/%s/%s",
|
|
|
|
changeId%100, trigger.changeId, trigger.patchset,
|
|
|
|
)
|
2020-06-13 06:59:15 +02:00
|
|
|
|
2021-11-27 18:36:14 +01:00
|
|
|
return &trigger, nil
|
2020-02-21 23:32:23 +01:00
|
|
|
}
|
|
|
|
|
2021-11-27 18:36:14 +01:00
|
|
|
// Extract the buildtrigger struct out of the flags passed to besadii
|
|
|
|
// when invoked as Gerrit's 'change-merged' hook. This hook is used
|
2021-11-29 17:20:06 +01:00
|
|
|
// for triggering HEAD builds after change submission.
|
2021-12-06 15:42:57 +01:00
|
|
|
func buildTriggerFromChangeMerged(cfg *config) (*buildTrigger, error) {
|
2021-11-27 18:36:14 +01:00
|
|
|
// Information that needs to be returned
|
|
|
|
var trigger buildTrigger
|
2020-02-21 23:46:34 +01:00
|
|
|
|
2021-11-27 18:36:14 +01:00
|
|
|
// Information that is only needed for parsing
|
2021-12-06 15:42:57 +01:00
|
|
|
var targetBranch, submitter string
|
2021-11-27 18:36:14 +01:00
|
|
|
|
|
|
|
flag.StringVar(&trigger.project, "project", "", "Gerrit project")
|
|
|
|
flag.StringVar(&trigger.commit, "commit", "", "Commit hash")
|
2021-12-06 15:42:57 +01:00
|
|
|
flag.StringVar(&submitter, "submitter", "", "Submitter email & username")
|
2021-11-27 18:36:14 +01:00
|
|
|
flag.StringVar(&targetBranch, "branch", "", "CL target branch")
|
|
|
|
|
|
|
|
// Ignore extra flags passed by change-merged
|
2021-12-06 15:42:57 +01:00
|
|
|
ignoreFlags([]string{"change", "topic", "change-url", "submitter-username", "newrev", "change-owner", "change-owner-username"})
|
2021-11-27 18:36:14 +01:00
|
|
|
|
|
|
|
flag.Parse()
|
|
|
|
|
2021-12-06 15:42:57 +01:00
|
|
|
// Parse username & email
|
|
|
|
err := extractChangeUploader(submitter, &trigger)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-11-29 17:20:06 +01:00
|
|
|
// If the patchset is not for the HEAD branch of the monorepo, then
|
|
|
|
// we can ignore it.
|
|
|
|
if trigger.project != cfg.Repository || targetBranch != cfg.Branch {
|
2021-12-06 15:42:57 +01:00
|
|
|
return nil, nil
|
2020-06-29 04:35:39 +02:00
|
|
|
}
|
|
|
|
|
2021-11-29 17:20:06 +01:00
|
|
|
trigger.ref = "refs/heads/" + targetBranch
|
2021-11-27 18:36:14 +01:00
|
|
|
|
2021-12-06 15:42:57 +01:00
|
|
|
return &trigger, nil
|
2021-11-27 18:36:14 +01:00
|
|
|
}
|
|
|
|
|
2021-11-29 16:22:59 +01:00
|
|
|
func gerritHookMain(cfg *config, log *syslog.Writer, trigger *buildTrigger) {
|
2021-11-27 18:36:14 +01:00
|
|
|
if trigger == nil {
|
|
|
|
// The hook was not for something we care about.
|
2020-06-29 04:35:39 +02:00
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
|
2021-11-29 17:20:06 +01:00
|
|
|
err := triggerBuild(cfg, log, trigger)
|
2021-11-27 17:24:24 +01:00
|
|
|
|
2020-02-21 23:32:23 +01:00
|
|
|
if err != nil {
|
2020-06-29 04:35:39 +02:00
|
|
|
log.Err(fmt.Sprintf("failed to trigger Buildkite build: %s", err))
|
|
|
|
}
|
|
|
|
|
2021-11-29 16:22:59 +01:00
|
|
|
if cfg.SourcegraphUrl != "" && trigger.ref == "refs/heads/canon" {
|
|
|
|
err = triggerIndexUpdate(cfg, log)
|
|
|
|
if err != nil {
|
|
|
|
log.Err(fmt.Sprintf("failed to trigger sourcegraph index update: %s", err))
|
|
|
|
}
|
2020-06-29 04:35:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-29 17:20:06 +01:00
|
|
|
func postCommandMain(cfg *config) {
|
2020-06-29 04:35:39 +02:00
|
|
|
changeId := os.Getenv("GERRIT_CHANGE_ID")
|
|
|
|
patchset := os.Getenv("GERRIT_PATCHSET")
|
|
|
|
|
|
|
|
if changeId == "" || patchset == "" {
|
|
|
|
// If these variables are unset, but the hook was invoked, the
|
|
|
|
// build was most likely for a branch and not for a CL - no status
|
|
|
|
// needs to be reported back to Gerrit!
|
|
|
|
fmt.Println("This isn't a CL build, nothing to do. Have a nice day!")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if os.Getenv("BUILDKITE_LABEL") != ":duck:" {
|
|
|
|
// this is not the build stage, don't do anything.
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var verified int
|
|
|
|
var verb string
|
|
|
|
|
|
|
|
if os.Getenv("BUILDKITE_COMMAND_EXIT_STATUS") == "0" {
|
|
|
|
verified = 1 // Verified: +1 in Gerrit
|
|
|
|
verb = "passed"
|
|
|
|
} else {
|
|
|
|
verified = -1
|
|
|
|
verb = "failed"
|
2020-02-21 23:32:23 +01:00
|
|
|
}
|
2020-06-13 06:59:15 +02:00
|
|
|
|
2020-06-29 04:35:39 +02:00
|
|
|
msg := fmt.Sprintf("Build of patchset %s %s: %s", patchset, verb, os.Getenv("BUILDKITE_BUILD_URL"))
|
|
|
|
review := reviewInput{
|
|
|
|
Message: msg,
|
|
|
|
OmitDuplicateComments: true,
|
|
|
|
Labels: map[string]int{
|
|
|
|
"Verified": verified,
|
|
|
|
},
|
2020-08-01 16:30:25 +02:00
|
|
|
|
|
|
|
// Update the attention set if we are failing this patchset.
|
|
|
|
IgnoreDefaultAttentionSetRules: verified == 1,
|
2021-08-18 16:20:19 +02:00
|
|
|
|
|
|
|
Tag: "autogenerated:buildkite~result",
|
2020-06-29 04:35:39 +02:00
|
|
|
}
|
2021-11-29 17:20:06 +01:00
|
|
|
updateGerrit(cfg, review, changeId, patchset)
|
2020-06-29 04:35:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2021-11-27 18:36:14 +01:00
|
|
|
// 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))
|
|
|
|
|
2020-06-29 04:35:39 +02:00
|
|
|
bin := path.Base(os.Args[0])
|
2021-11-29 16:22:59 +01:00
|
|
|
cfg, err := loadConfig()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Crit(fmt.Sprintf("besadii configuration error: %v", err))
|
|
|
|
os.Exit(4)
|
|
|
|
}
|
2020-06-29 04:35:39 +02:00
|
|
|
|
2021-11-27 18:36:14 +01:00
|
|
|
if bin == "patchset-created" {
|
2021-11-29 17:20:06 +01:00
|
|
|
trigger, err := buildTriggerFromPatchsetCreated(cfg)
|
2021-11-27 18:36:14 +01:00
|
|
|
if err != nil {
|
2021-12-06 17:34:48 +01:00
|
|
|
log.Crit(fmt.Sprintf("failed to parse 'patchset-created' invocation from args: %v", err))
|
2021-11-27 18:36:14 +01:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
2021-11-29 16:22:59 +01:00
|
|
|
gerritHookMain(cfg, log, trigger)
|
2021-11-27 18:36:14 +01:00
|
|
|
} else if bin == "change-merged" {
|
2021-12-06 15:42:57 +01:00
|
|
|
trigger, err := buildTriggerFromChangeMerged(cfg)
|
|
|
|
if err != nil {
|
2021-12-06 17:34:48 +01:00
|
|
|
log.Crit(fmt.Sprintf("failed to parse 'change-merged' invocation from args: %v", err))
|
2021-12-06 15:42:57 +01:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
2021-11-29 16:22:59 +01:00
|
|
|
gerritHookMain(cfg, log, trigger)
|
2020-06-29 04:35:39 +02:00
|
|
|
} else if bin == "post-command" {
|
2021-11-29 17:20:06 +01:00
|
|
|
postCommandMain(cfg)
|
2020-06-29 04:35:39 +02:00
|
|
|
} else {
|
|
|
|
fmt.Fprintf(os.Stderr, "besadii does not know how to be invoked as %q, sorry!", bin)
|
|
|
|
os.Exit(1)
|
2020-06-19 02:27:32 +02:00
|
|
|
}
|
2020-02-13 23:38:34 +01:00
|
|
|
}
|