From 0cb7337d6daa8c8ec89af5ed54a09857f23d6bf8 Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Mon, 17 Feb 2020 11:50:13 -0500 Subject: [PATCH] hooks: expand support for push options This refactors push options to be a bit more general and makes them available to each step of the push process. The `skip-ci` option has been added, and push options are now included in webhook deliveries. --- gitsrht-update-hook/options.go | 68 ++++++++++++++++++++++++++++++ gitsrht-update-hook/post-update.go | 8 ++-- gitsrht-update-hook/pre-receive.go | 17 ++------ gitsrht-update-hook/submitter.go | 7 +++ gitsrht-update-hook/webhooks.go | 7 +-- 5 files changed, 87 insertions(+), 20 deletions(-) create mode 100644 gitsrht-update-hook/options.go diff --git a/gitsrht-update-hook/options.go b/gitsrht-update-hook/options.go new file mode 100644 index 0000000..8efbb0a --- /dev/null +++ b/gitsrht-update-hook/options.go @@ -0,0 +1,68 @@ +package main + +import ( + "fmt" + "os" + "strconv" + "strings" + "time" + + goredis "github.com/go-redis/redis" +) + +var options map[string]string + +func loadOptions() { + if options != nil { + return + } + + uuid, ok := os.LookupEnv("SRHT_PUSH") + if !ok { + return + } + + redisHost, ok := config.Get("sr.ht", "redis-host") + if !ok { + redisHost = "redis://localhost:6379" + } + ropts, err := goredis.ParseURL(redisHost) + if err != nil { + logger.Fatalf("Failed to parse redis host: %v", err) + } + redis := goredis.NewClient(ropts) + + var n int + if nopts, ok := os.LookupEnv("GIT_PUSH_OPTION_COUNT"); ok { + n, _ = strconv.Atoi(nopts) + redis.Set(fmt.Sprintf("git.sr.ht.options.%s", uuid), + nopts, 10*time.Minute) + } else { + nopts, err := redis.Get(fmt.Sprintf( + "git.sr.ht.options.%s", uuid)).Result() + if err != nil { + return + } + n, _ = strconv.Atoi(nopts) + } + + options = make(map[string]string) + for i := 0; i < n; i++ { + opt, ok := os.LookupEnv(fmt.Sprintf("GIT_PUSH_OPTION_%d", i)) + optkey := fmt.Sprintf("git.sr.ht.options.%s.%d", uuid, i) + if !ok { + opt, err = redis.Get(optkey).Result() + if err != nil { + return + } + } else { + redis.Set(optkey, opt, 10*time.Minute) + } + parts := strings.SplitN(opt, "=", 2) + if len(parts) == 1 { + options[parts[0]] = "" + } else { + options[parts[0]] = parts[1] + } + } +} diff --git a/gitsrht-update-hook/post-update.go b/gitsrht-update-hook/post-update.go index 90ffc67..dcacaf6 100644 --- a/gitsrht-update-hook/post-update.go +++ b/gitsrht-update-hook/post-update.go @@ -138,10 +138,12 @@ func postUpdate() { initSubmitter() + loadOptions() payload := WebhookPayload{ - Push: pushUuid, - Pusher: context.User, - Refs: make([]UpdatedRef, len(refs)), + Push: pushUuid, + PushOpts: options, + Pusher: context.User, + Refs: make([]UpdatedRef, len(refs)), } oids := make(map[string]interface{}) diff --git a/gitsrht-update-hook/pre-receive.go b/gitsrht-update-hook/pre-receive.go index 1a3abed..a98885c 100644 --- a/gitsrht-update-hook/pre-receive.go +++ b/gitsrht-update-hook/pre-receive.go @@ -1,10 +1,8 @@ package main import ( - "fmt" "log" "os" - "strconv" ) func preReceive() { @@ -16,17 +14,8 @@ func preReceive() { } logger.Printf("Running pre-receive for push %s", pushUuid) - if nopts, ok := os.LookupEnv("GIT_PUSH_OPTION_COUNT"); ok { - n, _ := strconv.Atoi(nopts) - configureOpts(pushUuid, n) - } -} - -func configureOpts(pushUuid string, nopts int) { - for i := 0; i < nopts; i++ { - opt := os.Getenv(fmt.Sprintf("GIT_PUSH_OPTION_%d", i)) - if opt == "debug" { - log.Printf("debug: %s", pushUuid) - } + loadOptions() + if _, ok := options["debug"]; ok { + log.Printf("debug: %s", pushUuid) } } diff --git a/gitsrht-update-hook/submitter.go b/gitsrht-update-hook/submitter.go index 254de41..b2e9529 100644 --- a/gitsrht-update-hook/submitter.go +++ b/gitsrht-update-hook/submitter.go @@ -7,6 +7,7 @@ import ( "fmt" "io" "io/ioutil" + "log" "net/http" "path" "strings" @@ -230,6 +231,12 @@ func SubmitBuild(submitter BuildSubmitter) ([]BuildSubmission, error) { return nil, err } + loadOptions() + if _, ok := options["skip-ci"]; ok { + log.Println("skip-ci was requested - not submitting build jobs") + return nil, nil + } + var results []BuildSubmission for name, contents := range manifests { manifest, err := ManifestFromYAML(contents) diff --git a/gitsrht-update-hook/webhooks.go b/gitsrht-update-hook/webhooks.go index 4549e41..b4f4404 100644 --- a/gitsrht-update-hook/webhooks.go +++ b/gitsrht-update-hook/webhooks.go @@ -50,9 +50,10 @@ type UpdatedRef struct { } type WebhookPayload struct { - Push string `json:"push"` - Pusher UserContext `json:"pusher"` - Refs []UpdatedRef `json:"refs"` + Push string `json:"push"` + PushOpts map[string]string `json:"push-options"` + Pusher UserContext `json:"pusher"` + Refs []UpdatedRef `json:"refs"` } func initWebhookKey() { -- 2.38.4