From 0baf1efabc4686208de4a3587080045dd04ae228 Mon Sep 17 00:00:00 2001 From: Quentin Dufour Date: Wed, 15 Mar 2023 08:51:13 +0100 Subject: [PATCH] Dispatch passed arguments --- example/albatros.json | 7 +++++++ example.json => example/gitea.json | 0 main.go | 16 ++++++++++++---- 3 files changed, 19 insertions(+), 4 deletions(-) create mode 100644 example/albatros.json rename example.json => example/gitea.json (100%) diff --git a/example/albatros.json b/example/albatros.json new file mode 100644 index 0000000..54cfa61 --- /dev/null +++ b/example/albatros.json @@ -0,0 +1,7 @@ +{ + "ref": "refs/heads/main", + "after": "3fff73597f8ca18ef04c0d9bf64132ba55aadcaa", + "repository": { + "clone_url": "https://git.deuxfleurs.fr/quentin/albatros.git" + } +} diff --git a/example.json b/example/gitea.json similarity index 100% rename from example.json rename to example/gitea.json diff --git a/main.go b/main.go index 760830c..6402f9b 100644 --- a/main.go +++ b/main.go @@ -5,6 +5,7 @@ import ( "fmt" "io" "log" + "strings" "net/http" nomad "github.com/hashicorp/nomad/api" //"code.gitea.io/sdk/gitea" @@ -77,14 +78,21 @@ func hook(w http.ResponseWriter, r *http.Request) { return } + log.Printf("Gitea notification: %+v\n", notification) meta := map[string]string{ - "REPO_URL": "https://git.deuxfleurs.fr/quentin/albatros.git", - "COMMIT": "9fc771827e94e89e28107a0a13ec8c11b6f740b8", - "BRANCH": "poc/nomad-ci", + "REPO_URL": notification.Repository.CloneUrl, + "COMMIT": notification.After, + // @FIXME: this code is not correct, this is a hack + "BRANCH": strings.ReplaceAll(notification.Ref, "refs/heads/", ""), } jobs := NomadClient.Jobs() - jobs.Dispatch("builder", meta, []byte{}, "test", &nomad.WriteOptions{}) + dres, dmeta, err := jobs.Dispatch("builder", meta, []byte{}, "albatros", &nomad.WriteOptions{}) + if err != nil { + http.Error(w, "Can't submit your job to Nomad", http.StatusInternalServerError) + } + log.Printf("Dispatch info: %+v\n", dmeta) + log.Printf("Job info: %+v\n", dres) fmt.Println(notification.CompareUrl) io.WriteString(w, "ok")