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
|
|
|
|
//
|
|
|
|
// besadii is a small CLI tool that triggers depot builds on
|
|
|
|
// builds.sr.ht
|
|
|
|
//
|
2020-06-13 06:59:15 +02:00
|
|
|
// It is designed to run as a Gerrit hook (ref-updated).
|
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-02-21 23:32:23 +01:00
|
|
|
"strings"
|
2020-02-13 23:38:34 +01:00
|
|
|
)
|
|
|
|
|
2020-02-22 00:30:05 +01:00
|
|
|
var branchPrefix = "refs/heads/"
|
|
|
|
|
2020-06-13 06:59:15 +02:00
|
|
|
// Represents an updated branch, as passed to besadii by Gerrit.
|
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
|
|
|
|
type branchUpdate struct {
|
|
|
|
project string
|
|
|
|
branch string
|
|
|
|
commit string
|
|
|
|
submitter string
|
2020-02-21 23:32:23 +01:00
|
|
|
}
|
|
|
|
|
2020-02-13 23:38:34 +01:00
|
|
|
// Represents a builds.sr.ht build object as described on
|
|
|
|
// https://man.sr.ht/builds.sr.ht/api.md
|
|
|
|
type Build struct {
|
|
|
|
Manifest string `json:"manifest"`
|
|
|
|
Note string `json:"note"`
|
|
|
|
Tags []string `json:"tags"`
|
|
|
|
}
|
|
|
|
|
2020-06-13 06:59:15 +02:00
|
|
|
// Represents a build trigger object as described on
|
|
|
|
// https://man.sr.ht/builds.sr.ht/triggers.md
|
2020-02-13 23:38:34 +01:00
|
|
|
type Trigger struct {
|
|
|
|
Action string `json:"action"`
|
|
|
|
Condition string `json:"condition"`
|
|
|
|
To string `json:"to"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// Represents a build manifest for sourcehut.
|
|
|
|
type Manifest struct {
|
|
|
|
Image string `json:"image"`
|
|
|
|
Sources []string `json:"sources"`
|
|
|
|
Secrets []string `json:"secrets"`
|
|
|
|
Tasks [](map[string]string) `json:"tasks"`
|
|
|
|
Triggers []Trigger `json:"triggers"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func prepareManifest(commit string) string {
|
|
|
|
m := Manifest{
|
|
|
|
Image: "nixos/latest",
|
2020-06-12 03:01:08 +02:00
|
|
|
Sources: []string{"https://code.tvl.fyi/"},
|
2020-02-13 23:38:34 +01:00
|
|
|
|
|
|
|
// secret for cachix/tazjin
|
|
|
|
Secrets: []string{"f7f02546-4d95-44f7-a98e-d61fdded8b5b"},
|
|
|
|
|
|
|
|
Tasks: [](map[string]string){
|
|
|
|
{"setup": `# sourcehut does not censor secrets in builds, hence this hack:
|
|
|
|
echo -n 'export CACHIX_SIGNING_KEY=' >> ~/.buildenv
|
|
|
|
cat ~/.cachix-tazjin >> ~/.buildenv
|
2020-06-12 03:01:08 +02:00
|
|
|
nix-env -iA third_party.cachix -f code.tvl.fyi
|
2020-02-13 23:38:34 +01:00
|
|
|
cachix use tazjin
|
2020-06-12 03:01:08 +02:00
|
|
|
cd code.tvl.fyi
|
2020-02-13 23:38:34 +01:00
|
|
|
git checkout ` + commit},
|
|
|
|
|
2020-06-12 03:01:08 +02:00
|
|
|
{"build": `cd code.tvl.fyi
|
2020-02-13 23:38:34 +01:00
|
|
|
nix-build ci-builds.nix > built-paths`},
|
|
|
|
|
2020-06-12 03:01:08 +02:00
|
|
|
{"cache": `cd code.tvl.fyi
|
2020-02-13 23:38:34 +01:00
|
|
|
cat built-paths | cachix push tazjin`},
|
|
|
|
},
|
|
|
|
|
|
|
|
Triggers: []Trigger{
|
|
|
|
Trigger{Action: "email", Condition: "failure", To: "mail@tazj.in"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
j, _ := json.Marshal(m)
|
|
|
|
return string(j)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Trigger a build of a given branch & commit on builds.sr.ht
|
2020-06-13 06:59:15 +02:00
|
|
|
func triggerBuild(log *syslog.Writer, token string, update *branchUpdate) {
|
2020-02-13 23:38:34 +01:00
|
|
|
build := Build{
|
2020-06-13 06:59:15 +02:00
|
|
|
Manifest: prepareManifest(update.commit),
|
|
|
|
Note: fmt.Sprintf("build of %q at %q, submitted by %q", update.branch, update.commit, update.submitter),
|
2020-02-13 23:38:34 +01:00
|
|
|
Tags: []string{
|
2020-02-22 00:01:42 +01:00
|
|
|
// my branch names tend to contain slashes, which are not valid
|
|
|
|
// identifiers in sourcehut.
|
2020-06-13 06:59:15 +02:00
|
|
|
"depot", strings.ReplaceAll(update.branch, "/", "_"),
|
2020-02-13 23:38:34 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
body, _ := json.Marshal(build)
|
|
|
|
reader := ioutil.NopCloser(bytes.NewReader(body))
|
|
|
|
|
|
|
|
req, err := http.NewRequest("POST", "https://builds.sr.ht/api/jobs", reader)
|
|
|
|
if err != nil {
|
2020-02-21 23:46:34 +01:00
|
|
|
log.Err(fmt.Sprintf("failed to create an HTTP request: %s", err))
|
|
|
|
os.Exit(1)
|
2020-02-13 23:38:34 +01:00
|
|
|
}
|
|
|
|
|
2020-02-21 23:57:34 +01:00
|
|
|
req.Header.Add("Authorization", "token "+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-02-21 23:06:56 +01:00
|
|
|
// This might indicate a temporary error on the sourcehut side, do
|
2020-02-13 23:38:34 +01:00
|
|
|
// not fail the whole program.
|
2020-02-21 23:46:34 +01:00
|
|
|
log.Err(fmt.Sprintf("failed to send builds.sr.ht request:", err))
|
2020-02-13 23:38:34 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
if resp.StatusCode != 200 {
|
2020-02-22 00:01:42 +01:00
|
|
|
respBody, _ := ioutil.ReadAll(resp.Body)
|
|
|
|
log.Err(fmt.Sprintf("received non-success response from builds.sr.ht: %s (%v)", respBody, resp.Status))
|
2020-02-13 23:38:34 +01:00
|
|
|
} else {
|
2020-06-13 06:59:15 +02:00
|
|
|
fmt.Fprintf(log, "triggered builds.sr.ht job for branch %q at commit %q", update.branch, update.commit)
|
2020-02-13 23:38:34 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-13 06:59:15 +02:00
|
|
|
func branchUpdateFromFlags() (*branchUpdate, error) {
|
|
|
|
if path.Base(os.Args[0]) != "ref-updated" {
|
|
|
|
return nil, fmt.Errorf("besadii must be invoked as the 'ref-updated' hook")
|
|
|
|
}
|
2020-02-21 23:32:23 +01:00
|
|
|
|
2020-06-13 06:59:15 +02:00
|
|
|
var update branchUpdate
|
|
|
|
|
|
|
|
flag.StringVar(&update.project, "project", "", "Gerrit project")
|
|
|
|
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")
|
|
|
|
}
|
2020-02-22 00:30:05 +01:00
|
|
|
|
2020-06-13 06:59:15 +02:00
|
|
|
if update.project != "depot" {
|
|
|
|
// this is not an error, but also not something we handle.
|
|
|
|
return nil, nil
|
2020-02-21 23:32:23 +01:00
|
|
|
}
|
|
|
|
|
2020-06-13 06:59:15 +02:00
|
|
|
if !strings.HasPrefix(*ref, branchPrefix) {
|
|
|
|
return nil, fmt.Errorf("besadii only supports branch updates at the moment")
|
2020-02-21 23:32:23 +01:00
|
|
|
}
|
|
|
|
|
2020-06-13 06:59:15 +02:00
|
|
|
update.branch = strings.TrimPrefix(*ref, branchPrefix)
|
|
|
|
|
|
|
|
return &update, nil
|
2020-02-21 23:32:23 +01:00
|
|
|
}
|
|
|
|
|
2020-02-13 23:38:34 +01:00
|
|
|
func main() {
|
2020-02-21 23:46:34 +01:00
|
|
|
log, err := syslog.New(syslog.LOG_INFO|syslog.LOG_USER, "besadii")
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("failed to open syslog: %s\n", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2020-02-21 23:31:57 +01:00
|
|
|
token, err := ioutil.ReadFile("/etc/secrets/srht-token")
|
|
|
|
if err != nil {
|
2020-02-21 23:46:34 +01:00
|
|
|
log.Alert("sourcehot token could not be read")
|
|
|
|
os.Exit(1)
|
2020-02-21 23:31:57 +01:00
|
|
|
}
|
|
|
|
|
2020-06-13 06:59:15 +02:00
|
|
|
update, err := branchUpdateFromFlags()
|
2020-02-21 23:32:23 +01:00
|
|
|
if err != nil {
|
2020-06-13 06:59:15 +02:00
|
|
|
log.Err(fmt.Sprintf("failed to parse ref update: %s", err))
|
2020-02-21 23:46:34 +01:00
|
|
|
os.Exit(1)
|
2020-02-21 23:32:23 +01:00
|
|
|
}
|
|
|
|
|
2020-06-13 06:59:15 +02:00
|
|
|
if update == nil { // the project was not 'depot'
|
|
|
|
os.Exit(0)
|
2020-02-21 23:32:23 +01:00
|
|
|
}
|
2020-06-13 06:59:15 +02:00
|
|
|
|
|
|
|
triggerBuild(log, string(token), update)
|
2020-02-13 23:38:34 +01:00
|
|
|
}
|