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.
This commit is contained in:
Drew DeVault 2020-02-17 11:50:13 -05:00
parent 281891b07d
commit 0cb7337d6d
5 changed files with 87 additions and 20 deletions

View File

@ -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]
}
}
}

View File

@ -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{})

View File

@ -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)
}
}

View File

@ -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)

View File

@ -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() {