~edwargix/git.sr.ht

ec44ef4a75ffe562e7f2e9cda49242a8cfdae4c3 — наб 4 years ago 342ae75
Allow push options description/visibility for setting description/visibility

Also gofmt cleanup

Ref: ~sircmpwn/git.sr.ht#293
M gitsrht-update-hook/post-update.go => gitsrht-update-hook/post-update.go +39 -12
@@ 10,12 10,12 @@ import (
	"strings"
	"syscall"

	goredis "github.com/go-redis/redis"
	_ "github.com/lib/pq"
	"github.com/go-git/go-git/v5"
	"github.com/go-git/go-git/v5/plumbing"
	"github.com/go-git/go-git/v5/plumbing/object"
	"github.com/go-git/go-git/v5/plumbing/storer"
	goredis "github.com/go-redis/redis"
	_ "github.com/lib/pq"
)

func printAutocreateInfo(context PushContext) {


@@ 39,19 39,22 @@ type DbInfo struct {
	SyncWebhooks  []WebhookSubscription
}

func fetchInfoForPush(db *sql.DB, repoId int) (DbInfo, error) {
func fetchInfoForPush(db *sql.DB, repoId int, newDescription *string,
	newVisibility *string) (DbInfo, error) {
	var dbinfo DbInfo = DbInfo{RepoId: repoId}

	// With this query, we:
	// 1. Fetch the owner's username and OAuth token
	// 2. Fetch the repository's name and visibility
	// 3. Update the repository's mtime
	// 3. Update the repository's mtime, description, visibility
	// 4. Determine how many webhooks this repo has: if there are zero sync
	//    webhooks then we can defer looking them up until after we've sent the
	//    user on their way.
	query, err := db.Prepare(`
		UPDATE repository repo
		SET updated = NOW() AT TIME ZONE 'UTC'
		SET updated     = NOW() AT TIME ZONE 'UTC',
		    description = COALESCE($2, repo.description),
		    visibility  = COALESCE($3, repo.visibility)
		FROM (
			SELECT "user".username, "user".oauth_token
			FROM "user"


@@ 79,9 82,9 @@ func fetchInfoForPush(db *sql.DB, repoId int) (DbInfo, error) {
	defer query.Close()

	var nasync, nsync int
	if err = query.QueryRow(repoId).Scan(&dbinfo.RepoName, &dbinfo.Visibility,
		&dbinfo.OwnerUsername, &dbinfo.OwnerToken,
		&nsync, &nasync); err != nil {
	if err = query.QueryRow(repoId, newDescription, newVisibility).Scan(
		&dbinfo.RepoName, &dbinfo.Visibility, &dbinfo.OwnerUsername,
		&dbinfo.OwnerToken, &nsync, &nasync); err != nil {

		return dbinfo, err
	}


@@ 117,6 120,29 @@ func fetchInfoForPush(db *sql.DB, repoId int) (DbInfo, error) {
	return dbinfo, nil
}

func parseUpdatables() (*string, *string) {
	loadOptions()
	var desc, vis *string

	if newDescription, ok := options["description"]; ok {
		desc = &newDescription
	}
	if newVisibility, ok := options["visibility"]; ok {
		vis = &newVisibility
	}

	if vis != nil {
		lvis := strings.ToLower(*vis)
		vis = &lvis
		if *vis != "public" && *vis != "unlisted" && *vis != "private" {
			log.Printf("Invalid visibility %v, can be one of: public, unlisted, private", *vis)
			vis = nil
		}
	}

	return desc, vis
}

func postUpdate() {
	var context PushContext
	refs := os.Args[1:]


@@ 134,12 160,13 @@ func postUpdate() {
		logger.Fatalf("unmarshal SRHT_PUSH_CTX: %v", err)
	}

	if context.Repo.Visibility == "autocreated" {
	initSubmitter()

	newDescription, newVisibility := parseUpdatables()
	if context.Repo.Visibility == "autocreated" && newVisibility == nil {
		printAutocreateInfo(context)
	}

	initSubmitter()

	loadOptions()
	payload := WebhookPayload{
		Push:     pushUuid,


@@ 159,7 186,7 @@ func postUpdate() {
		logger.Fatalf("Failed to open a database connection: %v", err)
	}

	dbinfo, err := fetchInfoForPush(db, context.Repo.Id)
	dbinfo, err := fetchInfoForPush(db, context.Repo.Id, newDescription, newVisibility)
	if err != nil {
		logger.Fatalf("Failed to fetch info from database: %v", err)
	}

M gitsrht-update-hook/submitter.go => gitsrht-update-hook/submitter.go +1 -1
@@ 14,9 14,9 @@ import (
	"unicode/utf8"

	"github.com/fernet/fernet-go"
	"github.com/pkg/errors"
	"github.com/go-git/go-git/v5"
	"github.com/go-git/go-git/v5/plumbing/object"
	"github.com/pkg/errors"
)

var (

M gitsrht-update-hook/update.go => gitsrht-update-hook/update.go +1 -1
@@ 26,7 26,7 @@ func update() {

	if !utf8.ValidString(refname) {
		logger.Printf("Refusing ref '%s': not UTF-8", refname)
		log.Printf("%s not valid UTF-8, see https://github.com/libgit2/pygit2/issues/1028 for more information", refname);
		log.Printf("%s not valid UTF-8, see https://github.com/libgit2/pygit2/issues/1028 for more information", refname)
		os.Exit(1)
	}