commit a78cd5bc3a3c7425540a6ef478244c4178403abe Author: LollipopKit Date: Wed Jun 1 16:34:03 2022 +0800 initial diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..3fabd75 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module git.lolli.tech/lollipopkit/gogs-webhook + +go 1.18 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..e69de29 diff --git a/gogs-webhook b/gogs-webhook new file mode 100755 index 0000000..54ac12d Binary files /dev/null and b/gogs-webhook differ diff --git a/main.go b/main.go new file mode 100644 index 0000000..87b1353 --- /dev/null +++ b/main.go @@ -0,0 +1,59 @@ +package main + +import ( + "encoding/json" + "fmt" + "io/ioutil" + "log" + "net/http" + "os/exec" + "strings" +) + +const ( + path = "/" +) + +func main() { + http.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) { + reqBody, err := ioutil.ReadAll(r.Body) + if err != nil { + log.Fatalf("Error reading request body: %v\n", err) + w.Write([]byte(fmt.Sprintf("Read body error: %s", err))) + return + } + + var payload GogsPayload + err = json.Unmarshal(reqBody, &payload) + if err != nil { + log.Fatalf("Error unmarshalling request body: %v\n", err) + w.Write([]byte(fmt.Sprintf("Unmarshal body error: %s", err))) + return + } + + log.Printf("Received: [Name] %s [Sender] %s", payload.Repository.FullName, payload.Sender.FullName) + + for _, commit := range payload.Commits { + if strings.Contains(commit.Message, "[DEPLOY]") { + log.Printf("Commit: [SHA] %s [Message] %s\n", commit.ID, commit.Message) + log.Println("Ready to deploy") + + cmd := exec.Command("python3", fmt.Sprintf("~/pro/%s/deploy.py", payload.Repository.Name)) + err = cmd.Run() + if err != nil { + log.Fatalf("Error running deploy script: %v\n", err) + w.Write([]byte(fmt.Sprintf("Deploy script error: %s", err))) + } + + return + } + } + + log.Println("No deploy commit found") + w.Write([]byte("No deploy commit found. Skip.")) + }) + + log.Println("Webhook service started.") + + http.ListenAndServe(":3001", nil) +} \ No newline at end of file diff --git a/model.go b/model.go new file mode 100644 index 0000000..8deedc3 --- /dev/null +++ b/model.go @@ -0,0 +1,67 @@ +package main + +type GogsPayload struct { + Ref string `json:"ref"` + Before string `json:"before"` + After string `json:"after"` + CompareURL string `json:"compare_url"` + Commits []struct { + ID string `json:"id"` + Message string `json:"message"` + URL string `json:"url"` + Author struct { + Name string `json:"name"` + Email string `json:"email"` + Username string `json:"username"` + } `json:"author"` + Committer struct { + Name string `json:"name"` + Email string `json:"email"` + Username string `json:"username"` + } `json:"committer"` + Timestamp string `json:"timestamp"` + } `json:"commits"` + Repository struct { + ID int `json:"id"` + Owner struct { + ID int `json:"id"` + Login string `json:"login"` + FullName string `json:"full_name"` + Email string `json:"email"` + AvatarURL string `json:"avatar_url"` + Username string `json:"username"` + } `json:"owner"` + Name string `json:"name"` + FullName string `json:"full_name"` + Description string `json:"description"` + Private bool `json:"private"` + Fork bool `json:"fork"` + HTMLURL string `json:"html_url"` + SSHURL string `json:"ssh_url"` + CloneURL string `json:"clone_url"` + Website string `json:"website"` + StarsCount int `json:"stars_count"` + ForksCount int `json:"forks_count"` + WatchersCount int `json:"watchers_count"` + OpenIssuesCount int `json:"open_issues_count"` + DefaultBranch string `json:"default_branch"` + CreatedAt string `json:"created_at"` + UpdatedAt string `json:"updated_at"` + } `json:"repository"` + Pusher struct { + ID int `json:"id"` + Login string `json:"login"` + FullName string `json:"full_name"` + Email string `json:"email"` + AvatarURL string `json:"avatar_url"` + Username string `json:"username"` + } `json:"pusher"` + Sender struct { + ID int `json:"id"` + Login string `json:"login"` + FullName string `json:"full_name"` + Email string `json:"email"` + AvatarURL string `json:"avatar_url"` + Username string `json:"username"` + } `json:"sender"` +} \ No newline at end of file