initial
This commit is contained in:
commit
a78cd5bc3a
3
go.mod
Normal file
3
go.mod
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
module git.lolli.tech/lollipopkit/gogs-webhook
|
||||||
|
|
||||||
|
go 1.18
|
BIN
gogs-webhook
Executable file
BIN
gogs-webhook
Executable file
Binary file not shown.
59
main.go
Normal file
59
main.go
Normal file
@ -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)
|
||||||
|
}
|
67
model.go
Normal file
67
model.go
Normal file
@ -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"`
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user