代码拉取完成,页面将自动刷新
同步操作将从 LongRaindy/webhook 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
package main
import (
"log"
"os"
"os/exec"
"io/ioutil"
"net/url"
"net/http"
"encoding/json"
)
// --------------------------------------------------------------------------------
type WatchItem struct {
Repo string `json:"repo"`
Branch string `json:"branch"`
Script string `json:"script"`
}
type Config struct {
BindHost string `json:"bind"`
Items []WatchItem `json:"items"`
}
// --------------------------------------------------------------------------------
type Repository struct {
Url string `json:"url"` // "https://github.com/qbox/base"
}
type Payload struct {
Ref string `json:"ref"` // "refs/heads/develop"
Repo Repository `json:"repository"`
}
// --------------------------------------------------------------------------------
func runScript(item *WatchItem) (err error) {
script := "./" + item.Script
out, err := exec.Command("bash", "-c", script).Output()
if err != nil {
log.Println("Exec command failed:", err)
}
log.Print("Run ", script, " output:\n", string(out))
return
}
func handleQuery(query url.Values, cfg *Config) (err error) {
payload := query.Get("payload")
b := []byte(payload)
var payloadObj map[string]interface{}
err = json.Unmarshal(b, &payloadObj)
if err != nil {
log.Println("json.Unmarshal payload failed:", err)
return
}
b, _ = json.MarshalIndent(payloadObj, "", " ")
text := string(b)
log.Println(text)
var event Payload
err = json.Unmarshal(b, &event)
if err != nil {
log.Println("json.Unmarshal payload failed:", err)
return
}
for _, item := range cfg.Items {
if event.Repo.Url == item.Repo && event.Ref == "refs/heads/" + item.Branch {
err = runScript(&item)
break
}
}
return
}
// --------------------------------------------------------------------------------
var cfg Config
func handle(w http.ResponseWriter, req *http.Request) {
req.ParseForm()
handleQuery(req.Form, &cfg)
}
func main() {
if len(os.Args) < 2 {
println("Usage: webhook <ConfigFile>\n")
return
}
cfgbuf, err := ioutil.ReadFile(os.Args[1])
if err != nil {
log.Println("Read config file failed:", err)
return
}
err = json.Unmarshal(cfgbuf, &cfg)
if err != nil {
log.Println("Unmarshal config failed:", err)
return
}
http.HandleFunc("/", handle)
log.Fatal(http.ListenAndServe(cfg.BindHost, nil))
}
// --------------------------------------------------------------------------------
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。