Browse Source

initial

master
OhYee 2 years ago
commit
15fe13f7cc
Signed by: OhYee
GPG Key ID: 5A9E1F63ED274FBB
  1. 1
      .gitignore
  2. 8
      go.mod
  3. 9
      go.sum
  4. 191
      main.go

1
.gitignore vendored

@ -0,0 +1 @@ @@ -0,0 +1 @@
action

8
go.mod

@ -0,0 +1,8 @@ @@ -0,0 +1,8 @@
module git.oyohyee.com/OhYee/action
go 1.15
require (
github.com/OhYee/goutils v1.1.3
github.com/OhYee/rainbow v1.0.8
)

9
go.sum

@ -0,0 +1,9 @@ @@ -0,0 +1,9 @@
github.com/OhYee/goutils v1.1.0/go.mod h1:RqaQ1bD+sQAq7zQ/1vAtaojWoFznhAFdWDsMZr7SgkA=
github.com/OhYee/goutils v1.1.3 h1:Sbnat3PEheRBeIbmZbXqhwEo2m0WK7FDBr1TaweBZ4k=
github.com/OhYee/goutils v1.1.3/go.mod h1:a0LN8uy8eh26x+65ArEr6MKYCPl5ndy+yrEecBH3Pz8=
github.com/OhYee/rainbow v1.0.8 h1:qoCl/phGpddIrEyGQXR1AmVkXRY+8jBY+lpkkA/8HLU=
github.com/OhYee/rainbow v1.0.8/go.mod h1:9If6oueKS6Tz2i7iBeCBgXJbNOvYIkMMlmxxcmobaPE=
github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWoS4=
github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ=
golang.org/x/sys v0.0.0-20191005200804-aed5e4c7ecf9 h1:L2auWcuQIvxz9xSEqzESnV/QN/gNRXNApHi3fYwl2w0=
golang.org/x/sys v0.0.0-20191005200804-aed5e4c7ecf9/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=

191
main.go

@ -0,0 +1,191 @@ @@ -0,0 +1,191 @@
package main
import (
"encoding/json"
"io/ioutil"
"net/http"
"os/exec"
"path"
"time"
"github.com/OhYee/goutils/file"
"github.com/OhYee/rainbow/errors"
"github.com/OhYee/rainbow/log"
)
type handle struct {
Root string
}
func (h handle) ServeHTTP(w http.ResponseWriter, r *http.Request) {
m := new(WebHook)
b, err := ioutil.ReadAll(r.Body)
if err != nil {
log.Error.Println(errors.ShowStack(errors.NewErr(err)))
return
}
if err = json.Unmarshal(b, m); err != nil {
log.Error.Println(errors.ShowStack(errors.NewErr(err)))
return
}
s, err := json.MarshalIndent(m, "", " ")
if err != nil {
log.Error.Println(errors.ShowStack(errors.NewErr(err)))
return
}
log.Debug.Println("%s\n", s)
dirPath := path.Join(h.Root, m.Repository.FullName)
if file.IsExist(dirPath) {
// 该文件需要处理
if _, ok := runWrapper(dirPath, "git", "pull"); !ok {
return
}
if _, ok := runWrapper(dirPath, "screen", "-S", m.Repository.FullName, "-X", "quit"); !ok {
return
}
if _, ok := runWrapper(dirPath, "screen", "-dmS", m.Repository.FullName, "-s", "bash ./.ohyee/action.bash"); !ok {
return
}
}
}
func main() {
http.ListenAndServe("0.0.0.0:9999", handle{
Root: file.AbsPath(),
})
}
func runWrapper(dir string, command string, args ...string) (string, bool) {
res, err := run(dir, "git", "pull")
if err != nil {
log.Info.Println(err)
log.Info.Println(res)
return res, false
}
return res, true
}
func run(dir string, command string, args ...string) (string, error) {
cmd := exec.Command(command, args...)
cmd.Dir = ""
res, err := cmd.CombinedOutput()
return string(res), err
}
type WebHook struct {
After string `json:"after"`
Before string `json:"before"`
Commits []struct {
Added []string `json:"added"`
Author struct {
Email string `json:"email"`
Name string `json:"name"`
Username string `json:"username"`
} `json:"author"`
Committer struct {
Email string `json:"email"`
Name string `json:"name"`
Username string `json:"username"`
} `json:"committer"`
ID string `json:"id"`
Message string `json:"message"`
Modified []interface{} `json:"modified"`
Removed []interface{} `json:"removed"`
Timestamp time.Time `json:"timestamp"`
URL string `json:"url"`
Verification interface{} `json:"verification"`
} `json:"commits"`
CompareURL string `json:"compare_url"`
HeadCommit interface{} `json:"head_commit"`
Pusher struct {
AvatarURL string `json:"avatar_url"`
Created time.Time `json:"created"`
Email string `json:"email"`
FullName string `json:"full_name"`
ID int `json:"id"`
IsAdmin bool `json:"is_admin"`
Language string `json:"language"`
LastLogin time.Time `json:"last_login"`
Login string `json:"login"`
Username string `json:"username"`
} `json:"pusher"`
Ref string `json:"ref"`
Repository struct {
AllowMergeCommits bool `json:"allow_merge_commits"`
AllowRebase bool `json:"allow_rebase"`
AllowRebaseExplicit bool `json:"allow_rebase_explicit"`
AllowSquashMerge bool `json:"allow_squash_merge"`
Archived bool `json:"archived"`
AvatarURL string `json:"avatar_url"`
CloneURL string `json:"clone_url"`
CreatedAt time.Time `json:"created_at"`
DefaultBranch string `json:"default_branch"`
Description string `json:"description"`
Empty bool `json:"empty"`
Fork bool `json:"fork"`
ForksCount int `json:"forks_count"`
FullName string `json:"full_name"`
HasIssues bool `json:"has_issues"`
HasProjects bool `json:"has_projects"`
HasPullRequests bool `json:"has_pull_requests"`
HasWiki bool `json:"has_wiki"`
HTMLURL string `json:"html_url"`
ID int `json:"id"`
IgnoreWhitespaceConflicts bool `json:"ignore_whitespace_conflicts"`
Internal bool `json:"internal"`
InternalTracker struct {
AllowOnlyContributorsToTrackTime bool `json:"allow_only_contributors_to_track_time"`
EnableIssueDependencies bool `json:"enable_issue_dependencies"`
EnableTimeTracker bool `json:"enable_time_tracker"`
} `json:"internal_tracker"`
Mirror bool `json:"mirror"`
MirrorInterval string `json:"mirror_interval"`
Name string `json:"name"`
OpenIssuesCount int `json:"open_issues_count"`
OpenPrCounter int `json:"open_pr_counter"`
OriginalURL string `json:"original_url"`
Owner struct {
AvatarURL string `json:"avatar_url"`
Created time.Time `json:"created"`
Email string `json:"email"`
FullName string `json:"full_name"`
ID int `json:"id"`
IsAdmin bool `json:"is_admin"`
Language string `json:"language"`
LastLogin time.Time `json:"last_login"`
Login string `json:"login"`
Username string `json:"username"`
} `json:"owner"`
Parent interface{} `json:"parent"`
Permissions struct {
Admin bool `json:"admin"`
Pull bool `json:"pull"`
Push bool `json:"push"`
} `json:"permissions"`
Private bool `json:"private"`
ReleaseCounter int `json:"release_counter"`
Size int `json:"size"`
SSHURL string `json:"ssh_url"`
StarsCount int `json:"stars_count"`
Template bool `json:"template"`
UpdatedAt time.Time `json:"updated_at"`
WatchersCount int `json:"watchers_count"`
Website string `json:"website"`
} `json:"repository"`
Secret string `json:"secret"`
Sender struct {
AvatarURL string `json:"avatar_url"`
Created time.Time `json:"created"`
Email string `json:"email"`
FullName string `json:"full_name"`
ID int `json:"id"`
IsAdmin bool `json:"is_admin"`
Language string `json:"language"`
LastLogin time.Time `json:"last_login"`
Login string `json:"login"`
Username string `json:"username"`
} `json:"sender"`
}
Loading…
Cancel
Save