From 289048d72582e961dec55291b15075b5cf115f32 Mon Sep 17 00:00:00 2001 From: James Pace Date: Tue, 21 Mar 2023 02:08:46 +0000 Subject: [PATCH] Add push hook reciever. --- cmd/j7s-gitea-push-listener/main.go | 95 +++++++++++++++++++++++++++++ go.mod | 5 +- go.sum | 3 + 3 files changed, 102 insertions(+), 1 deletion(-) create mode 100644 cmd/j7s-gitea-push-listener/main.go diff --git a/cmd/j7s-gitea-push-listener/main.go b/cmd/j7s-gitea-push-listener/main.go new file mode 100644 index 0000000..1419b54 --- /dev/null +++ b/cmd/j7s-gitea-push-listener/main.go @@ -0,0 +1,95 @@ +// Copyright 2023 James Pace +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +package main + +import ( + "bytes" + api "code.gitea.io/gitea/modules/structs" + "encoding/json" + "flag" + "io/ioutil" + "log" + "net/http" +) + +func main() { + postUrl := flag.String("postUrl", "", "URL for Texton Trigger.") + flag.Parse() + context := NewHandlerContext(*postUrl) + http.HandleFunc("/hook", context.hook) + log.Println("Listening on :8080/hook") + log.Fatal(http.ListenAndServe(":8080", nil)) +} + +func (ctx *HandlerContext) hook(w http.ResponseWriter, r *http.Request) { + defer r.Body.Close() + + // Make sure we're dealing with the right type of event. + event := r.Header.Get("X-Gitea-Event") + if event != "push" { + log.Println("Got unsupported event of type", event) + http.Error(w, "Unsupported event", 400) + return + } + // Now unmarshall. + body, err := ioutil.ReadAll(r.Body) + if err != nil { + log.Println("Failed to read body.") + http.Error(w, "Failed to read body", 400) + return + } + var inPayload api.PushPayload + err = json.Unmarshal(body, &inPayload) + if err != nil { + log.Printf("Failed to parse body.") + http.Error(w, "Failed to parse payload.", 400) + return + } + + // Make map of stuff to send to the trigger. + outMap := make(map[string]string) + outMap["sha"] = inPayload.HeadCommit.ID + outMap["repo_owner"] = inPayload.Repo.Owner.UserName + outMap["repo_name"] = inPayload.Repo.Name + outJson, err := json.Marshal(outMap) + if err != nil { + log.Println("Failed to make json response.") + http.Error(w, "Failed to make json response.", 400) + return + } + + // Now send it. + request, err := http.NewRequest("POST", ctx.postUrl, bytes.NewBuffer(outJson)) + request.Header.Set("Content-Type", "application/json; charset=UTF-8") + client := &http.Client{} + response, err := client.Do(request) + if err != nil { + log.Println("Failed to make request. err:", err) + http.Error(w, "Failed to pass on request.", 400) + return + } + defer response.Body.Close() + + // It worked! + log.Println("Success!") + return +} + +type HandlerContext struct { + postUrl string +} + +func NewHandlerContext(postUrl string) *HandlerContext { + return &HandlerContext{postUrl: postUrl} +} diff --git a/go.mod b/go.mod index c0b629e..b7d1a8f 100644 --- a/go.mod +++ b/go.mod @@ -2,4 +2,7 @@ module git.jpace121.net/public/j7s-gitea-api-wrapper go 1.15 -require code.gitea.io/sdk/gitea v0.15.1 +require ( + code.gitea.io/gitea/modules/structs v0.0.0-20190610152049-835b53fc259c + code.gitea.io/sdk/gitea v0.15.1 +) diff --git a/go.sum b/go.sum index 0918234..ea2da7d 100644 --- a/go.sum +++ b/go.sum @@ -1,4 +1,7 @@ +code.gitea.io/gitea v1.19.0 h1:YoXejoZ+VV2VZ2f+0MgQnkVHSJ4wBXhFfC6+14qwidA= code.gitea.io/gitea-vet v0.2.1/go.mod h1:zcNbT/aJEmivCAhfmkHOlT645KNOf9W2KnkLgFjGGfE= +code.gitea.io/gitea/modules/structs v0.0.0-20190610152049-835b53fc259c h1:WwxK+8qmKYgU2pfcbCeRSqKwEPeHnW/sfmNc6pjLZC8= +code.gitea.io/gitea/modules/structs v0.0.0-20190610152049-835b53fc259c/go.mod h1:e/Ukqo229PbsSEymXfLWmNz4g04hwnFml5lW6U+0Azs= code.gitea.io/sdk v0.11.0 h1:R3VdjBCxObyLKnv4Svd/TM6oGsXzN8JORbzgkEFb83w= code.gitea.io/sdk/gitea v0.15.1 h1:WJreC7YYuxbn0UDaPuWIe/mtiNKTvLN8MLkaw71yx/M= code.gitea.io/sdk/gitea v0.15.1/go.mod h1:klY2LVI3s3NChzIk/MzMn7G1FHrfU7qd63iSMVoHRBA=