|
|
|
@ -3,6 +3,7 @@ package main
@@ -3,6 +3,7 @@ package main
|
|
|
|
|
import ( |
|
|
|
|
"encoding/json" |
|
|
|
|
"fmt" |
|
|
|
|
"io" |
|
|
|
|
"io/ioutil" |
|
|
|
|
"math/rand" |
|
|
|
|
"net/http" |
|
|
|
@ -24,27 +25,52 @@ type handle struct {
@@ -24,27 +25,52 @@ type handle struct {
|
|
|
|
|
Root string |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (h handle) ServeHTTP(w http.ResponseWriter, r *http.Request) { |
|
|
|
|
m := new(WebHook) |
|
|
|
|
|
|
|
|
|
b, err := ioutil.ReadAll(r.Body) |
|
|
|
|
func readJson(r io.Reader, res interface{}) (err error) { |
|
|
|
|
b, err := ioutil.ReadAll(r) |
|
|
|
|
if err != nil { |
|
|
|
|
log.Error.Println(errors.ShowStack(errors.NewErr(err))) |
|
|
|
|
return |
|
|
|
|
} |
|
|
|
|
if err = json.Unmarshal(b, m); err != nil { |
|
|
|
|
if err = json.Unmarshal(b, res); err != nil { |
|
|
|
|
log.Error.Println(errors.ShowStack(errors.NewErr(err))) |
|
|
|
|
return |
|
|
|
|
} |
|
|
|
|
return |
|
|
|
|
} |
|
|
|
|
func (h handle) ServeHTTP(w http.ResponseWriter, r *http.Request) { |
|
|
|
|
|
|
|
|
|
s, err := json.MarshalIndent(m, "", " ") |
|
|
|
|
if err != nil { |
|
|
|
|
log.Error.Println(errors.ShowStack(errors.NewErr(err))) |
|
|
|
|
return |
|
|
|
|
hook := new(Hook) |
|
|
|
|
|
|
|
|
|
switch r.URL.Path { |
|
|
|
|
case "/gitea": |
|
|
|
|
m := new(GiteaHook) |
|
|
|
|
readJson(r.Body, m) |
|
|
|
|
hook = m.ToHook() |
|
|
|
|
|
|
|
|
|
s, err := json.MarshalIndent(m, "", " ") |
|
|
|
|
if err != nil { |
|
|
|
|
log.Error.Println(errors.ShowStack(errors.NewErr(err))) |
|
|
|
|
return |
|
|
|
|
} |
|
|
|
|
log.Debug.Printf("%s\n", s) |
|
|
|
|
|
|
|
|
|
case "/hook": |
|
|
|
|
readJson(r.Body, hook) |
|
|
|
|
hook.Type = "hook" |
|
|
|
|
|
|
|
|
|
s, err := json.MarshalIndent(hook, "", " ") |
|
|
|
|
if err != nil { |
|
|
|
|
log.Error.Println(errors.ShowStack(errors.NewErr(err))) |
|
|
|
|
return |
|
|
|
|
} |
|
|
|
|
log.Debug.Printf("%s\n", s) |
|
|
|
|
|
|
|
|
|
default: |
|
|
|
|
w.WriteHeader(501) |
|
|
|
|
w.Write([]byte(fmt.Sprintf("Can not solve request %s", r.RequestURI))) |
|
|
|
|
} |
|
|
|
|
log.Debug.Printf("%s\n", s) |
|
|
|
|
|
|
|
|
|
m.handleGiteaWebHook(path.Join(h.Root, "repos", m.Repository.FullName)) |
|
|
|
|
go hook.handleGiteaWebHook(path.Join(h.Root, "repos", hook.RepoName)) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
var nocolor = color.New() |
|
|
|
@ -52,6 +78,8 @@ var nocolor = color.New()
@@ -52,6 +78,8 @@ var nocolor = color.New()
|
|
|
|
|
func main() { |
|
|
|
|
pre := "" |
|
|
|
|
root := file.AbsPath() |
|
|
|
|
address := "0.0.0.0" |
|
|
|
|
port := 42179 |
|
|
|
|
|
|
|
|
|
gToken := 0 |
|
|
|
|
for _, arg := range os.Args { |
|
|
|
@ -66,6 +94,12 @@ func main() {
@@ -66,6 +94,12 @@ func main() {
|
|
|
|
|
if l, err := strconv.ParseInt(arg, 10, 32); err == nil { |
|
|
|
|
gToken = int(l) |
|
|
|
|
} |
|
|
|
|
case "-a": |
|
|
|
|
address = arg |
|
|
|
|
case "-p": |
|
|
|
|
if l, err := strconv.ParseInt(arg, 10, 32); err == nil { |
|
|
|
|
port = int(l) |
|
|
|
|
} |
|
|
|
|
default: |
|
|
|
|
switch arg { |
|
|
|
|
case "-t": |
|
|
|
@ -90,7 +124,7 @@ func main() {
@@ -90,7 +124,7 @@ func main() {
|
|
|
|
|
resetLog(log.Debug, f, "[Debug] ") |
|
|
|
|
resetLog(log.Error, f, "[Error] ") |
|
|
|
|
|
|
|
|
|
const addr = "0.0.0.0:9999" |
|
|
|
|
addr := fmt.Sprintf("%s:%d", address, port) |
|
|
|
|
fmt.Printf("Action works on %s\nFolder: %s\n", addr, root) |
|
|
|
|
log.Info.Printf("Action works on %s\nFolder: %s\n", addr, root) |
|
|
|
|
http.ListenAndServe(addr, handle{ |
|
|
|
@ -118,7 +152,7 @@ func run(dir string, env []string, command string, args ...string) (string, erro
@@ -118,7 +152,7 @@ func run(dir string, env []string, command string, args ...string) (string, erro
|
|
|
|
|
return string(res), err |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
type WebHook struct { |
|
|
|
|
type GiteaHook struct { |
|
|
|
|
After string `json:"after"` |
|
|
|
|
Before string `json:"before"` |
|
|
|
|
Commits []struct { |
|
|
|
@ -233,23 +267,64 @@ type WebHook struct {
@@ -233,23 +267,64 @@ type WebHook struct {
|
|
|
|
|
} `json:"sender"` |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (hook *WebHook) handleGiteaWebHook(dirPath string) { |
|
|
|
|
func (hook *GiteaHook) ToHook() *Hook { |
|
|
|
|
return &Hook{ |
|
|
|
|
Type: "gitea", |
|
|
|
|
Token: hook.Secret, |
|
|
|
|
Commit: hook.After, |
|
|
|
|
Branch: hook.Ref, |
|
|
|
|
Pusher: hook.Pusher.Username, |
|
|
|
|
Repo: hook.Repository.FullName, |
|
|
|
|
RepoOwner: hook.Repository.Owner.Username, |
|
|
|
|
RepoName: hook.Repository.Name, |
|
|
|
|
RepoGit: hook.Repository.CloneURL, |
|
|
|
|
RepoPage: hook.Repository.HTMLURL, |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
type Hook struct { |
|
|
|
|
Type string `json:"type"` |
|
|
|
|
Trigger string `json:"trigger"` |
|
|
|
|
Token string `json:"token"` |
|
|
|
|
Commit string `json:"commit"` |
|
|
|
|
Branch string `json:"branch"` |
|
|
|
|
Pusher string `json:"pusher"` |
|
|
|
|
Repo string `json:"repo"` |
|
|
|
|
RepoOwner string `json:"repo_owner"` |
|
|
|
|
RepoName string `json:"repo_name"` |
|
|
|
|
RepoGit string `json:"repo_git"` |
|
|
|
|
RepoPage string `json:"page"` |
|
|
|
|
Arg1 string `json:"arg1"` |
|
|
|
|
Arg2 string `json:"arg2"` |
|
|
|
|
Arg3 string `json:"arg3"` |
|
|
|
|
Arg4 string `json:"arg4"` |
|
|
|
|
Arg5 string `json:"arg5"` |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (hook *Hook) handleGiteaWebHook(dirPath string) { |
|
|
|
|
log.Debug.Println(dirPath) |
|
|
|
|
if file.IsExist(dirPath) { |
|
|
|
|
// 该文件需要处理
|
|
|
|
|
screenName := strings.ReplaceAll(hook.Repository.FullName, "/", "-") |
|
|
|
|
screenName := strings.ReplaceAll(hook.Repo, "/", "-") |
|
|
|
|
|
|
|
|
|
// 环境变量
|
|
|
|
|
env := []string{ |
|
|
|
|
fmt.Sprintf("GITEA_ACTION_TOKEN=%s", hook.Secret), |
|
|
|
|
fmt.Sprintf("GITEA_ACTION_BRANCH=%s", hook.Ref), |
|
|
|
|
fmt.Sprintf("GITEA_ACTION_COMMIT=%s", hook.After), |
|
|
|
|
fmt.Sprintf("GITEA_ACTION_PUSHER=%s", hook.Pusher.Username), |
|
|
|
|
fmt.Sprintf("GITEA_ACTION_REPO=%s", hook.Repository.FullName), |
|
|
|
|
fmt.Sprintf("GITEA_ACTION_REPO_OWNER=%s", hook.Repository.Owner.Username), |
|
|
|
|
fmt.Sprintf("GITEA_ACTION_REPO_NAME=%s", hook.Repository.Name), |
|
|
|
|
fmt.Sprintf("GITEA_ACTION_REPO_GIT=%s", hook.Repository.CloneURL), |
|
|
|
|
fmt.Sprintf("GITEA_ACTION_REPO_PAGE=%s", hook.Repository.HTMLURL), |
|
|
|
|
fmt.Sprintf("OHYEE_ACTION_TYPE=%s", hook.Type), |
|
|
|
|
fmt.Sprintf("OHYEE_ACTION_TRIGGER=%s", hook.Trigger), |
|
|
|
|
fmt.Sprintf("OHYEE_ACTION_TOKEN=%s", hook.Token), |
|
|
|
|
fmt.Sprintf("OHYEE_ACTION_BRANCH=%s", hook.Branch), |
|
|
|
|
fmt.Sprintf("OHYEE_ACTION_COMMIT=%s", hook.Commit), |
|
|
|
|
fmt.Sprintf("OHYEE_ACTION_PUSHER=%s", hook.Pusher), |
|
|
|
|
fmt.Sprintf("OHYEE_ACTION_REPO=%s", hook.Repo), |
|
|
|
|
fmt.Sprintf("OHYEE_ACTION_REPO_OWNER=%s", hook.RepoOwner), |
|
|
|
|
fmt.Sprintf("OHYEE_ACTION_REPO_NAME=%s", hook.RepoName), |
|
|
|
|
fmt.Sprintf("OHYEE_ACTION_REPO_GIT=%s", hook.RepoGit), |
|
|
|
|
fmt.Sprintf("OHYEE_ACTION_REPO_PAGE=%s", hook.RepoPage), |
|
|
|
|
fmt.Sprintf("OHYEE_ACTION_ARG1=%s", hook.Arg1), |
|
|
|
|
fmt.Sprintf("OHYEE_ACTION_ARG2=%s", hook.Arg2), |
|
|
|
|
fmt.Sprintf("OHYEE_ACTION_ARG3=%s", hook.Arg3), |
|
|
|
|
fmt.Sprintf("OHYEE_ACTION_ARG4=%s", hook.Arg4), |
|
|
|
|
fmt.Sprintf("OHYEE_ACTION_ARG5=%s", hook.Arg5), |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// 结束旧的的自动部署命令
|
|
|
|
|