From ec44ef4a75ffe562e7f2e9cda49242a8cfdae4c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=BD=D0=B0=D0=B1?= Date: Sat, 15 Aug 2020 18:00:17 +0200 Subject: [PATCH] Allow push options description/visibility for setting description/visibility Also gofmt cleanup Ref: ~sircmpwn/git.sr.ht#293 --- gitsrht-update-hook/post-update.go | 51 +++++++++++++++++++++++------- gitsrht-update-hook/submitter.go | 2 +- gitsrht-update-hook/update.go | 2 +- 3 files changed, 41 insertions(+), 14 deletions(-) diff --git a/gitsrht-update-hook/post-update.go b/gitsrht-update-hook/post-update.go index 4d22f46..357ebf6 100644 --- a/gitsrht-update-hook/post-update.go +++ b/gitsrht-update-hook/post-update.go @@ -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) } diff --git a/gitsrht-update-hook/submitter.go b/gitsrht-update-hook/submitter.go index 712661c..8fcd16a 100644 --- a/gitsrht-update-hook/submitter.go +++ b/gitsrht-update-hook/submitter.go @@ -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 ( diff --git a/gitsrht-update-hook/update.go b/gitsrht-update-hook/update.go index 6f9f179..eda7718 100644 --- a/gitsrht-update-hook/update.go +++ b/gitsrht-update-hook/update.go @@ -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) } -- 2.38.4