~edwargix/git.sr.ht

0cb7337d6daa8c8ec89af5ed54a09857f23d6bf8 — Drew DeVault 5 years ago 281891b
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.
A gitsrht-update-hook/options.go => gitsrht-update-hook/options.go +68 -0
@@ 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]
		}
	}
}

M gitsrht-update-hook/post-update.go => gitsrht-update-hook/post-update.go +5 -3
@@ 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{})

M gitsrht-update-hook/pre-receive.go => gitsrht-update-hook/pre-receive.go +3 -14
@@ 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)
	}
}

M gitsrht-update-hook/submitter.go => gitsrht-update-hook/submitter.go +7 -0
@@ 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)

M gitsrht-update-hook/webhooks.go => gitsrht-update-hook/webhooks.go +4 -3
@@ 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() {