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
|
|
|
|
// - Trigger SourceGraph (cs.tvl.fyi) repository index updates
|
|
|
|
//
|
|
|
|
// 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"
|
|
|
|
"os"
|
2020-06-13 06:59:15 +02:00
|
|
|
"path"
|
2020-06-28 04:37:34 +02:00
|
|
|
"regexp"
|
2020-02-13 23:38:34 +01:00
|
|
|
)
|
|
|
|
|
2020-06-28 04:37:34 +02:00
|
|
|
var branchRegexp = regexp.MustCompile(`^refs/heads/(.*)$`)
|
2020-06-30 02:13:13 +02:00
|
|
|
var metaRegexp = regexp.MustCompile(`^refs/changes/\d{0,2}/(\d+)/meta$`)
|
2020-06-28 04:37:34 +02:00
|
|
|
var patchsetRegexp = regexp.MustCompile(`^refs/changes/\d{0,2}/(\d+)/(\d+)$`)
|
2020-02-22 00:30:05 +01:00
|
|
|
|
2020-06-28 03:11:10 +02:00
|
|
|
// refUpdated is a struct representing the information passed to
|
|
|
|
// besadii when it is invoked as Gerrit's refUpdated hook.
|
2020-02-21 23:32:23 +01:00
|
|
|
//
|
2020-06-13 06:59:15 +02:00
|
|
|
// https://gerrit.googlesource.com/plugins/hooks/+/HEAD/src/main/resources/Documentation/hooks.md#ref_updated
|
2020-06-28 03:11:10 +02:00
|
|
|
type refUpdated struct {
|
2020-06-13 06:59:15 +02:00
|
|
|
project string
|
2020-06-28 04:37:34 +02:00
|
|
|
ref string
|
2020-06-13 06:59:15 +02:00
|
|
|
commit string
|
|
|
|
submitter string
|
2020-06-27 19:57:55 +02:00
|
|
|
email string
|
2020-06-28 04:37:34 +02: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 {
|
2020-06-29 03:16:48 +02:00
|
|
|
Commit string `json:"commit"`
|
|
|
|
Branch string `json:"branch"`
|
|
|
|
Message string `json:"message"`
|
|
|
|
Author Author `json:"author"`
|
|
|
|
Env map[string]string `json:"env"`
|
2020-02-13 23:38:34 +01:00
|
|
|
}
|
|
|
|
|
2020-06-27 19:57:55 +02:00
|
|
|
// Trigger a build of a given branch & commit on Buildkite
|
2020-06-28 03:11:10 +02:00
|
|
|
func triggerBuild(log *syslog.Writer, token string, update *refUpdated) error {
|
2020-06-28 04:37:34 +02:00
|
|
|
var message string
|
2020-06-29 03:16:48 +02:00
|
|
|
env := make(map[string]string)
|
2020-06-28 04:37:34 +02:00
|
|
|
|
|
|
|
if update.changeId != nil && update.patchset != nil {
|
2020-06-29 03:16:48 +02:00
|
|
|
env["GERRIT_CHANGE_ID"] = *update.changeId
|
|
|
|
env["GERRIT_PATCHSET"] = *update.patchset
|
2020-06-28 04:37:34 +02:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2020-02-13 23:38:34 +01:00
|
|
|
build := Build{
|
2020-06-27 19:57:55 +02:00
|
|
|
Commit: update.commit,
|
2020-06-28 04:37:34 +02:00
|
|
|
Branch: update.ref,
|
|
|
|
Message: message,
|
2020-06-29 03:16:48 +02:00
|
|
|
Env: env,
|
2020-06-27 19:57:55 +02:00
|
|
|
Author: Author{
|
|
|
|
Name: update.submitter,
|
|
|
|
Email: update.email,
|
2020-02-13 23:38:34 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
body, _ := json.Marshal(build)
|
|
|
|
reader := ioutil.NopCloser(bytes.NewReader(body))
|
|
|
|
|
2020-06-27 19:57:55 +02:00
|
|
|
req, err := http.NewRequest("POST", "https://api.buildkite.com/v2/organizations/tvl/pipelines/depot/builds", 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
|
|
|
}
|
|
|
|
|
2020-06-27 19:57:55 +02:00
|
|
|
req.Header.Add("Authorization", "Bearer "+token)
|
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()
|
|
|
|
|
2020-06-27 19:57:55 +02:00
|
|
|
if resp.StatusCode != 201 {
|
2020-02-22 00:01:42 +01:00
|
|
|
respBody, _ := ioutil.ReadAll(resp.Body)
|
2020-06-27 19:57:55 +02:00
|
|
|
log.Err(fmt.Sprintf("received non-success response from Buildkite: %s (%v)", respBody, resp.Status))
|
2020-02-13 23:38:34 +01:00
|
|
|
} else {
|
2020-06-28 04:37:34 +02:00
|
|
|
fmt.Fprintf(log, "triggered Buildkite build for ref %q at commit %q", update.ref, update.commit)
|
2020-02-13 23:38:34 +01:00
|
|
|
}
|
2020-06-19 02:27:32 +02:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Trigger a Sourcegraph repository index update on cs.tvl.fyi.
|
|
|
|
//
|
|
|
|
// https://docs.sourcegraph.com/admin/repo/webhooks
|
|
|
|
func triggerIndexUpdate(token string) error {
|
|
|
|
req, err := http.NewRequest("POST", "https://cs.tvl.fyi/.api/repos/depot/-/refresh", nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
req.Header.Add("Authorization", "token "+token)
|
|
|
|
|
|
|
|
_, err = http.DefaultClient.Do(req)
|
|
|
|
return err
|
2020-02-13 23:38:34 +01:00
|
|
|
}
|
|
|
|
|
2020-06-28 03:11:10 +02:00
|
|
|
func refUpdatedFromFlags() (*refUpdated, error) {
|
|
|
|
var update refUpdated
|
2020-06-13 06:59:15 +02:00
|
|
|
|
|
|
|
flag.StringVar(&update.project, "project", "", "Gerrit project")
|
|
|
|
flag.StringVar(&update.commit, "newrev", "", "new revision")
|
2020-06-27 19:57:55 +02:00
|
|
|
flag.StringVar(&update.email, "submitter", "", "Submitter email")
|
2020-06-13 06:59:15 +02:00
|
|
|
flag.StringVar(&update.submitter, "submitter-username", "", "Submitter username")
|
2020-06-28 04:37:34 +02:00
|
|
|
flag.StringVar(&update.ref, "refname", "", "updated reference name")
|
2020-06-13 06:59:15 +02: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". The following code ignores the
|
|
|
|
// flags.
|
|
|
|
//
|
|
|
|
// [0]: https://github.com/golang/go/issues/6112#issuecomment-66083768
|
2020-06-27 19:57:55 +02:00
|
|
|
var _old string
|
2020-06-13 06:59:15 +02:00
|
|
|
flag.StringVar(&_old, "oldrev", "", "")
|
|
|
|
|
|
|
|
flag.Parse()
|
|
|
|
|
2020-06-28 04:37:34 +02:00
|
|
|
if update.project == "" || update.ref == "" || update.commit == "" || update.submitter == "" {
|
2020-06-13 06:59:15 +02:00
|
|
|
// 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")
|
|
|
|
}
|
2020-02-22 00:30:05 +01:00
|
|
|
|
2020-07-01 02:16:27 +02:00
|
|
|
if update.project != "depot" || metaRegexp.MatchString(update.ref) {
|
2020-06-13 06:59:15 +02:00
|
|
|
// this is not an error, but also not something we handle.
|
|
|
|
return nil, nil
|
2020-02-21 23:32:23 +01:00
|
|
|
}
|
|
|
|
|
2020-07-01 02:16:27 +02:00
|
|
|
if branchRegexp.MatchString(update.ref) {
|
2020-06-30 02:13:13 +02:00
|
|
|
// these refs don't need special handling, just move on
|
2020-06-28 04:37:34 +02:00
|
|
|
return &update, nil
|
2020-02-21 23:32:23 +01:00
|
|
|
}
|
|
|
|
|
2020-06-28 04:37:34 +02:00
|
|
|
if matches := patchsetRegexp.FindStringSubmatch(update.ref); matches != nil {
|
|
|
|
update.changeId = &matches[1]
|
|
|
|
update.patchset = &matches[2]
|
|
|
|
return &update, nil
|
|
|
|
}
|
2020-06-13 06:59:15 +02:00
|
|
|
|
2020-06-28 04:37:34 +02:00
|
|
|
return nil, fmt.Errorf("besadii does not support updates for this type of ref (%q)", update.ref)
|
2020-02-21 23:32:23 +01:00
|
|
|
}
|
|
|
|
|
2020-06-29 04:35:39 +02:00
|
|
|
func refUpdatedMain() {
|
|
|
|
// Logging happens in syslog for Gerrit hooks because we don't want
|
|
|
|
// the hook output to be intermingled with Gerrit's own output
|
|
|
|
// stream
|
2020-02-21 23:46:34 +01:00
|
|
|
log, err := syslog.New(syslog.LOG_INFO|syslog.LOG_USER, "besadii")
|
|
|
|
if err != nil {
|
2020-06-29 04:35:39 +02:00
|
|
|
fmt.Fprintf(os.Stderr, "failed to open syslog: %s\n", err)
|
2020-02-21 23:46:34 +01:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2020-06-29 04:35:39 +02:00
|
|
|
update, err := refUpdatedFromFlags()
|
|
|
|
if err != nil {
|
|
|
|
log.Err(fmt.Sprintf("failed to parse ref update: %s", err))
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
if update == nil { // the project was not 'depot'
|
|
|
|
log.Err("build triggers are only supported for the 'depot' project")
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
|
2020-06-27 19:57:55 +02:00
|
|
|
buildkiteToken, err := ioutil.ReadFile("/etc/secrets/buildkite-besadii")
|
2020-02-21 23:31:57 +01:00
|
|
|
if err != nil {
|
2020-06-27 19:57:55 +02:00
|
|
|
log.Alert(fmt.Sprintf("buildkite token could not be read: %s", err))
|
2020-06-19 02:27:32 +02:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
sourcegraphToken, err := ioutil.ReadFile("/etc/secrets/sourcegraph-token")
|
|
|
|
if err != nil {
|
2020-06-27 19:57:55 +02:00
|
|
|
log.Alert(fmt.Sprintf("sourcegraph token could not be read: %s", err))
|
2020-02-21 23:46:34 +01:00
|
|
|
os.Exit(1)
|
2020-02-21 23:31:57 +01:00
|
|
|
}
|
|
|
|
|
2020-06-29 04:35:39 +02:00
|
|
|
err = triggerBuild(log, string(buildkiteToken), update)
|
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))
|
|
|
|
}
|
|
|
|
|
|
|
|
err = triggerIndexUpdate(string(sourcegraphToken))
|
|
|
|
if err != nil {
|
|
|
|
log.Err(fmt.Sprintf("failed to trigger sourcegraph index update: %s", err))
|
|
|
|
}
|
|
|
|
log.Info("triggered sourcegraph index update")
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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"`
|
|
|
|
OmitDuplicateComments bool `json:"omit_duplicate_comments"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func postCommandMain() {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
gerritPassword, err := ioutil.ReadFile("/etc/secrets/buildkite-gerrit")
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "Gerrit password could not be read: %s", err)
|
2020-02-21 23:46:34 +01:00
|
|
|
os.Exit(1)
|
2020-02-21 23:32:23 +01:00
|
|
|
}
|
|
|
|
|
2020-06-29 04:35:39 +02:00
|
|
|
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-06-29 19:54:29 +02:00
|
|
|
// TODO(tazjin): The Code-Review label is only used here
|
|
|
|
// temporarily, as our build rules do not check for 'Verified'
|
|
|
|
// right now (because CI doesn't run 24/7).
|
|
|
|
//
|
|
|
|
// Having a Code-Review label lets us signal the build status on
|
|
|
|
// the review, but without having to always require it.
|
|
|
|
"Code-Review": verified,
|
2020-06-29 04:35:39 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
body, _ := json.Marshal(review)
|
|
|
|
reader := ioutil.NopCloser(bytes.NewReader(body))
|
|
|
|
|
|
|
|
url := fmt.Sprintf("https://cl.tvl.fyi/a/changes/%s/revisions/%s/review", changeId, patchset)
|
|
|
|
req, err := http.NewRequest("POST", url, reader)
|
2020-06-27 19:57:55 +02:00
|
|
|
if err != nil {
|
2020-06-29 04:35:39 +02:00
|
|
|
fmt.Fprintf(os.Stderr, "failed to create an HTTP request: %w", err)
|
|
|
|
os.Exit(1)
|
2020-06-19 02:27:32 +02:00
|
|
|
}
|
|
|
|
|
2020-06-29 04:35:39 +02:00
|
|
|
req.SetBasicAuth("buildkite", string(gerritPassword))
|
|
|
|
req.Header.Add("Content-Type", "application/json")
|
|
|
|
|
|
|
|
resp, err := http.DefaultClient.Do(req)
|
2020-06-19 02:27:32 +02:00
|
|
|
if err != nil {
|
2020-06-29 04:35:39 +02:00
|
|
|
fmt.Errorf("failed to update CL on Gerrit: %w", err)
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
if resp.StatusCode != 200 {
|
|
|
|
respBody, _ := ioutil.ReadAll(resp.Body)
|
|
|
|
fmt.Fprintf(os.Stderr, "received non-success response from Gerrit: %s (%v)", respBody, resp.Status)
|
|
|
|
} else {
|
|
|
|
fmt.Printf("Updated CI status on https://cl.tvl.fyi/c/depot/+/%s/%s", changeId, patchset)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
bin := path.Base(os.Args[0])
|
|
|
|
|
|
|
|
if bin == "ref-updated" {
|
|
|
|
refUpdatedMain()
|
|
|
|
} else if bin == "post-command" {
|
|
|
|
postCommandMain()
|
|
|
|
} 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
|
|
|
}
|