---
gitsrht-update-hook/post-update.go | 25 +++----
gitsrht-update-hook/submitter.go | 110 ++++++++++++++---------------
2 files changed, 68 insertions(+), 67 deletions(-)
diff --git a/gitsrht-update-hook/post-update.go b/gitsrht-update-hook/post-update.go
index ef2e992..676df81 100644
--- a/gitsrht-update-hook/post-update.go
+++ b/gitsrht-update-hook/post-update.go
@@ -176,7 +176,7 @@ func parseUpdatables() (*string, *string) {
}
func postUpdate() {
- var context PushContext
+ var pcontext PushContext
refs := os.Args[1:]
contextJson, ctxOk := os.LookupEnv("SRHT_PUSH_CTX")
@@ -188,29 +188,29 @@ func postUpdate() {
logger.Printf("Running post-update for push %s", pushUuid)
- if err := json.Unmarshal([]byte(contextJson), &context); err != nil {
+ if err := json.Unmarshal([]byte(contextJson), &pcontext); err != nil {
logger.Fatalf("unmarshal SRHT_PUSH_CTX: %v", err)
}
initSubmitter()
newDescription, newVisibility := parseUpdatables()
- if context.Repo.Autocreated && newVisibility == nil {
- printAutocreateInfo(context)
+ if pcontext.Repo.Autocreated && newVisibility == nil {
+ printAutocreateInfo(pcontext)
}
loadOptions()
payload := WebhookPayload{
Push: pushUuid,
PushOpts: options,
- Pusher: context.User,
+ Pusher: pcontext.User,
Refs: make([]UpdatedRef, len(refs)),
}
oids := make(map[string]interface{})
- repo, err := git.PlainOpen(context.Repo.AbsolutePath)
+ repo, err := git.PlainOpen(pcontext.Repo.AbsolutePath)
if err != nil {
- logger.Fatalf("git.PlainOpen(%q): %v", context.Repo.AbsolutePath, err)
+ logger.Fatalf("git.PlainOpen(%q): %v", pcontext.Repo.AbsolutePath, err)
}
db, err := sql.Open("postgres", pgcs)
@@ -218,7 +218,7 @@ func postUpdate() {
logger.Fatalf("Failed to open a database connection: %v", err)
}
- dbinfo, err := fetchInfoForPush(db, context.Repo.OwnerName, context.Repo.Id, context.Repo.Name, context.Repo.Visibility, newDescription, newVisibility)
+ dbinfo, err := fetchInfoForPush(db, pcontext.Repo.OwnerName, pcontext.Repo.Id, pcontext.Repo.Name, pcontext.Repo.Visibility, newDescription, newVisibility)
if err != nil {
logger.Fatalf("Failed to fetch info from database: %v", err)
}
@@ -322,7 +322,11 @@ func postUpdate() {
Visibility: dbinfo.Visibility,
Ref: refname,
}
- results, err := SubmitBuild(submitter)
+
+ ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
+ defer cancel()
+ ctx = coreconfig.Context(ctx, config, "git.sr.ht")
+ results, err := SubmitBuild(ctx, submitter)
if err != nil {
logger.Printf("Error submitting build job: %v", err)
log.Fatalf("Error submitting build job: %v", err)
@@ -337,9 +341,6 @@ func postUpdate() {
logger.Printf("Submitted %d builds for %s",
len(results), refname)
for _, result := range results {
- if _, ok := options["debug"]; ok {
- log.Printf("[debug] builds.sr.ht response: \n%s", result.Response)
- }
log.Printf("\033[94m%s\033[0m [%s]", result.Url, result.Name)
}
nbuilds += len(results)
diff --git a/gitsrht-update-hook/submitter.go b/gitsrht-update-hook/submitter.go
index 075b9a5..ebc4692 100644
--- a/gitsrht-update-hook/submitter.go
+++ b/gitsrht-update-hook/submitter.go
@@ -3,6 +3,7 @@ package main
import (
"bufio"
"bytes"
+ "context"
"encoding/json"
"fmt"
"io"
@@ -13,6 +14,7 @@ import (
"strings"
"unicode/utf8"
+ "git.sr.ht/~sircmpwn/core-go/client"
"git.sr.ht/~turminal/go-fnmatch"
"github.com/fernet/fernet-go"
"github.com/go-git/go-git/v5"
@@ -20,6 +22,7 @@ import (
"github.com/go-git/go-git/v5/plumbing/filemode"
"github.com/go-git/go-git/v5/plumbing/object"
"github.com/pkg/errors"
+ "github.com/vektah/gqlparser/gqlerror"
)
var (
@@ -69,6 +72,8 @@ type BuildSubmitter interface {
GetOwnerName() string
// Get the job tags to use for this commit
GetJobTags() []string
+ // Get the build visibility
+ GetVisibility() string
}
// SQL notes
@@ -202,6 +207,10 @@ func (submitter GitBuildSubmitter) GetCommitId() string {
return submitter.Commit.Hash.String()
}
+func (submitter GitBuildSubmitter) GetVisibility() string {
+ return submitter.Visibility
+}
+
func firstLine(text string) string {
buf := bytes.NewBufferString(text)
scanner := bufio.NewScanner(buf)
@@ -272,9 +281,8 @@ func (submitter GitBuildSubmitter) GetOwnerName() string {
type BuildSubmission struct {
// TODO: Move errors into this struct and set up per-submission error
// tracking
- Name string
- Response string
- Url string
+ Name string
+ Url string
}
func configureRequestAuthorization(submitter BuildSubmitter,
@@ -299,7 +307,7 @@ func configureRequestAuthorization(submitter BuildSubmitter,
// TODO: Move this to scm.sr.ht
var submitBuildSkipCiPrinted bool
-func SubmitBuild(submitter BuildSubmitter) ([]BuildSubmission, error) {
+func SubmitBuild(ctx context.Context, submitter BuildSubmitter) ([]BuildSubmission, error) {
manifests, err := submitter.FindManifests()
if err != nil || manifests == nil {
return nil, err
@@ -332,59 +340,52 @@ func SubmitBuild(submitter BuildSubmitter) ([]BuildSubmission, error) {
return nil, errors.Wrap(err, name)
}
- client := &http.Client{}
-
- submission := struct {
- Manifest string `json:"manifest"`
- Note string `json:"note"`
- Tags []string `json:"tags"`
- }{
- Manifest: yaml,
- Tags: append(submitter.GetJobTags(), name),
- Note: submitter.GetCommitNote(),
- }
- bodyBytes, err := json.Marshal(&submission)
- if err != nil {
- return nil, errors.Wrap(err, "preparing job")
- }
- body := bytes.NewBuffer(bodyBytes)
-
- req, err := http.NewRequest("POST", fmt.Sprintf("%s/api/jobs",
- submitter.GetBuildsOrigin()), body)
- configureRequestAuthorization(submitter, req)
- req.Header.Add("Content-Type", "application/json")
- resp, err := client.Do(req)
- if err != nil {
- return nil, errors.Wrap(err, "job submission")
- }
-
- if resp.StatusCode == 403 {
- return nil, errors.New("builds.sr.ht returned 403\n" +
- "Log out and back into the website to authorize " +
- "builds integration.")
+ query := client.GraphQLQuery{
+ Query: `
+ mutation SubmitBuild(
+ $manifest: String!,
+ $note: String,
+ $tags: [String!],
+ $secrets: Boolean,
+ $execute: Boolean,
+ $visibility: Visibility,
+ ) {
+ submit(
+ manifest: $manifest,
+ note: $note,
+ tags: $tags,
+ secrets: $secrets,
+ execute: $execute,
+ visibility: $visibility,
+ ) {
+ id
+ }
+ }`,
+ Variables: map[string]interface{}{
+ "manifest": yaml,
+ "tags": append(submitter.GetJobTags(), name),
+ "note": submitter.GetCommitNote(),
+ "visibility": submitter.GetVisibility(),
+ },
}
- defer resp.Body.Close()
- respBytes, err := ioutil.ReadAll(resp.Body)
- if err != nil {
- return nil, errors.Wrap(err, "read response")
- }
+ resp := struct {
+ Data struct {
+ Submit struct {
+ ID int `json:"id"`
+ } `json:"submit"`
+ } `json:"data"`
+ Errors gqlerror.List `json:"errors"`
+ }{}
- if resp.StatusCode == 400 {
- return nil, errors.New(fmt.Sprintf(
- "builds.sr.ht returned %d\n", resp.StatusCode) +
- string(respBytes))
- }
- if resp.StatusCode == 402 {
- return nil, errors.New("Payment is required. Set up billing at https://meta.sr.ht/billing/initial")
- }
-
- var job struct {
- Id int `json:"id"`
- }
- err = json.Unmarshal(respBytes, &job)
+ err = client.Execute(ctx, submitter.GetOwnerName(), "builds.sr.ht", query, &resp)
if err != nil {
- return nil, errors.Wrap(err, "interpret response")
+ return nil, err
+ } else if len(resp.Errors) > 0 {
+ for _, err := range resp.Errors {
+ logger.Printf("Error submitting build: %s", err.Error())
+ }
+ return nil, fmt.Errorf("%s", resp.Errors[0].Message)
}
results = append(results, BuildSubmission{
@@ -392,8 +393,7 @@ func SubmitBuild(submitter BuildSubmitter) ([]BuildSubmission, error) {
Url: fmt.Sprintf("%s/~%s/job/%d",
submitter.GetBuildsOrigin(),
submitter.GetOwnerName(),
- job.Id),
- Response: string(respBytes),
+ resp.Data.Submit.ID),
})
}
base-commit: cba1e4ca92af59badee1db52a62ae4c7593371cf
--
2.40.0