diff --git a/wclient/whapp/adapter.go b/wclient/whapp/adapter.go index 078c07e..787e861 100644 --- a/wclient/whapp/adapter.go +++ b/wclient/whapp/adapter.go @@ -1,6 +1,9 @@ package whapp import ( + "github.com/opentdp/wrest-chat/wclient/whapp/gitea" + "github.com/opentdp/wrest-chat/wclient/whapp/github" + "github.com/opentdp/wrest-chat/wclient/whapp/text" "net/http" ) @@ -11,11 +14,11 @@ func Handler(header http.Header, app string, msg string) string { switch app { case "github": - res, err = GithubWebhook(header, msg) + res, err = github.HandleWebhook(header, msg) case "gitea": - res, err = GiteaWebhook(msg) + res, err = gitea.HandleWebhook(header, msg) case "text": - res, err = TextWebhook(msg) + res, err = text.HandleWebhook(msg) default: res = "暂不支持该应用的 webhook" } diff --git a/wclient/whapp/gitea.go b/wclient/whapp/gitea.go deleted file mode 100644 index 1a82b78..0000000 --- a/wclient/whapp/gitea.go +++ /dev/null @@ -1,7 +0,0 @@ -package whapp - -func GiteaWebhook(msg string) (string, error) { - - return "收到来自Gitea的Webhook", nil - -} diff --git a/wclient/whapp/gitea/event.go b/wclient/whapp/gitea/event.go new file mode 100644 index 0000000..b5fd88c --- /dev/null +++ b/wclient/whapp/gitea/event.go @@ -0,0 +1,184 @@ +package gitea + +import "time" + +type GiteaPushEvent 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"` + Verification interface{} `json:"verification"` + Timestamp time.Time `json:"timestamp"` + Added []interface{} `json:"added"` + Removed []interface{} `json:"removed"` + Modified []string `json:"modified"` + } `json:"commits"` + TotalCommits int `json:"total_commits"` + HeadCommit 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"` + Verification interface{} `json:"verification"` + Timestamp time.Time `json:"timestamp"` + Added []interface{} `json:"added"` + Removed []interface{} `json:"removed"` + Modified []string `json:"modified"` + } `json:"head_commit"` + Repository struct { + Id int `json:"id"` + Owner struct { + Id int `json:"id"` + Login string `json:"login"` + LoginName string `json:"login_name"` + FullName string `json:"full_name"` + Email string `json:"email"` + AvatarUrl string `json:"avatar_url"` + Language string `json:"language"` + IsAdmin bool `json:"is_admin"` + LastLogin time.Time `json:"last_login"` + Created time.Time `json:"created"` + Restricted bool `json:"restricted"` + Active bool `json:"active"` + ProhibitLogin bool `json:"prohibit_login"` + Location string `json:"location"` + Website string `json:"website"` + Description string `json:"description"` + Visibility string `json:"visibility"` + FollowersCount int `json:"followers_count"` + FollowingCount int `json:"following_count"` + StarredReposCount int `json:"starred_repos_count"` + Username string `json:"username"` + } `json:"owner"` + Name string `json:"name"` + FullName string `json:"full_name"` + Description string `json:"description"` + Empty bool `json:"empty"` + Private bool `json:"private"` + Fork bool `json:"fork"` + Template bool `json:"template"` + Parent interface{} `json:"parent"` + Mirror bool `json:"mirror"` + Size int `json:"size"` + Language string `json:"language"` + LanguagesUrl string `json:"languages_url"` + HtmlUrl string `json:"html_url"` + Url string `json:"url"` + Link string `json:"link"` + SshUrl string `json:"ssh_url"` + CloneUrl string `json:"clone_url"` + OriginalUrl string `json:"original_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"` + OpenPrCounter int `json:"open_pr_counter"` + ReleaseCounter int `json:"release_counter"` + DefaultBranch string `json:"default_branch"` + Archived bool `json:"archived"` + CreatedAt time.Time `json:"created_at"` + UpdatedAt time.Time `json:"updated_at"` + ArchivedAt time.Time `json:"archived_at"` + Permissions struct { + Admin bool `json:"admin"` + Push bool `json:"push"` + Pull bool `json:"pull"` + } `json:"permissions"` + HasIssues bool `json:"has_issues"` + InternalTracker struct { + EnableTimeTracker bool `json:"enable_time_tracker"` + AllowOnlyContributorsToTrackTime bool `json:"allow_only_contributors_to_track_time"` + EnableIssueDependencies bool `json:"enable_issue_dependencies"` + } `json:"internal_tracker"` + HasWiki bool `json:"has_wiki"` + HasPullRequests bool `json:"has_pull_requests"` + HasProjects bool `json:"has_projects"` + HasReleases bool `json:"has_releases"` + HasPackages bool `json:"has_packages"` + HasActions bool `json:"has_actions"` + IgnoreWhitespaceConflicts bool `json:"ignore_whitespace_conflicts"` + AllowMergeCommits bool `json:"allow_merge_commits"` + AllowRebase bool `json:"allow_rebase"` + AllowRebaseExplicit bool `json:"allow_rebase_explicit"` + AllowSquashMerge bool `json:"allow_squash_merge"` + AllowRebaseUpdate bool `json:"allow_rebase_update"` + DefaultDeleteBranchAfterMerge bool `json:"default_delete_branch_after_merge"` + DefaultMergeStyle string `json:"default_merge_style"` + DefaultAllowMaintainerEdit bool `json:"default_allow_maintainer_edit"` + AvatarUrl string `json:"avatar_url"` + Internal bool `json:"internal"` + MirrorInterval string `json:"mirror_interval"` + MirrorUpdated time.Time `json:"mirror_updated"` + RepoTransfer interface{} `json:"repo_transfer"` + } `json:"repository"` + Pusher struct { + Id int `json:"id"` + Login string `json:"login"` + LoginName string `json:"login_name"` + FullName string `json:"full_name"` + Email string `json:"email"` + AvatarUrl string `json:"avatar_url"` + Language string `json:"language"` + IsAdmin bool `json:"is_admin"` + LastLogin time.Time `json:"last_login"` + Created time.Time `json:"created"` + Restricted bool `json:"restricted"` + Active bool `json:"active"` + ProhibitLogin bool `json:"prohibit_login"` + Location string `json:"location"` + Website string `json:"website"` + Description string `json:"description"` + Visibility string `json:"visibility"` + FollowersCount int `json:"followers_count"` + FollowingCount int `json:"following_count"` + StarredReposCount int `json:"starred_repos_count"` + Username string `json:"username"` + } `json:"pusher"` + Sender struct { + Id int `json:"id"` + Login string `json:"login"` + LoginName string `json:"login_name"` + FullName string `json:"full_name"` + Email string `json:"email"` + AvatarUrl string `json:"avatar_url"` + Language string `json:"language"` + IsAdmin bool `json:"is_admin"` + LastLogin time.Time `json:"last_login"` + Created time.Time `json:"created"` + Restricted bool `json:"restricted"` + Active bool `json:"active"` + ProhibitLogin bool `json:"prohibit_login"` + Location string `json:"location"` + Website string `json:"website"` + Description string `json:"description"` + Visibility string `json:"visibility"` + FollowersCount int `json:"followers_count"` + FollowingCount int `json:"following_count"` + StarredReposCount int `json:"starred_repos_count"` + Username string `json:"username"` + } `json:"sender"` +} diff --git a/wclient/whapp/gitea/gitea.go b/wclient/whapp/gitea/gitea.go new file mode 100644 index 0000000..dfd08a4 --- /dev/null +++ b/wclient/whapp/gitea/gitea.go @@ -0,0 +1,39 @@ +package gitea + +import ( + "encoding/json" + "errors" + "fmt" + "net/http" +) + +var ( + name = "Gitea" +) + +func HandleWebhook(header http.Header, msg string) (string, error) { + hookType := header.Get("X-Gitea-Event") + switch hookType { + case "push": + return giteaPushEventHandler(msg) + } + + return fmt.Sprintf(TemplateUnsupport, name), nil + +} + +func giteaPushEventHandler(msg string) (string, error) { + data := &GiteaPushEvent{} + err := json.Unmarshal([]byte(msg), &data) + + if err != nil { + return "", errors.New("解析Gitea Push事件失败") + } + + return fmt.Sprintf(TemplatePush, + name, + data.Pusher.FullName, data.Pusher.Email, + data.Repository.FullName, data.TotalCommits, + data.CompareUrl, + ), nil +} diff --git a/wclient/whapp/gitea/template.go b/wclient/whapp/gitea/template.go new file mode 100644 index 0000000..3068b64 --- /dev/null +++ b/wclient/whapp/gitea/template.go @@ -0,0 +1,16 @@ +package gitea + +var ( + TemplateUnsupport = ` +🔔 来自%s的消息 +⚠️ 暂不支持该类型 +🙈 我们正在努力支持更多类型,敬请期待! +` + TemplatePush = ` +🔔 来自%s的消息 +👤 %s(%s) +📌 向仓库 %s 推送了%d次提交 +🔗 详情查看:%s +📊 提交记录一目了然,快来一探究竟吧! +` +) diff --git a/wclient/whapp/github.go b/wclient/whapp/github.go deleted file mode 100644 index 5646bda..0000000 --- a/wclient/whapp/github.go +++ /dev/null @@ -1,67 +0,0 @@ -package whapp - -import ( - "encoding/json" - "errors" - "fmt" - "net/http" - - "github.com/opentdp/wrest-chat/wclient/whapp/event" -) - -func GithubWebhook(header http.Header, msg string) (string, error) { - - hookType := header.Get("X-GitHub-Event") - targetType := header.Get("X-GitHub-Hook-Installation-Target-Type") - - switch hookType { - case "push": - return githubPushEventHandler(targetType, msg) - case "ping": - return githubPingEventHandler(targetType, msg) - } - - return fmt.Sprintf("收到来自 Github 的 Webhook,暂不支持的类型: %s", hookType), nil - -} - -func githubPingEventHandler(targetType string, msg string) (string, error) { - - data := &event.GithubPingEvent{} - err := json.Unmarshal([]byte(msg), &data) - - if err != nil { - return "", errors.New("解析Github Ping事件数据失败") - } - - target := getTarget(targetType) - - return fmt.Sprintf("[Github] 收到来自 %s %s 的测试请求", target, data.Repository.Name), nil - -} - -func githubPushEventHandler(targetType string, msg string) (string, error) { - - data := &event.GithubPushEvent{} - err := json.Unmarshal([]byte(msg), &data) - - if err != nil { - return "", errors.New("解析Github Push事件数据失败") - } - - target := getTarget(targetType) - - return fmt.Sprintf("[Github] %s(%s) 向 %s %s 推送了%d次提交\n\n详情查看: %s", data.Pusher.Name, data.Pusher.Email, target, data.Repository.Name, len(data.Commits), data.Compare), nil - -} - -func getTarget(targetType string) string { - - switch targetType { - case "repository": - return "仓库" - } - - return "未知" - -} diff --git a/wclient/whapp/event/github.go b/wclient/whapp/github/event.go similarity index 99% rename from wclient/whapp/event/github.go rename to wclient/whapp/github/event.go index 08e1ea6..d8908ab 100644 --- a/wclient/whapp/event/github.go +++ b/wclient/whapp/github/event.go @@ -1,4 +1,4 @@ -package event +package github import ( "time" diff --git a/wclient/whapp/github/github.go b/wclient/whapp/github/github.go new file mode 100644 index 0000000..d03ecf7 --- /dev/null +++ b/wclient/whapp/github/github.go @@ -0,0 +1,62 @@ +package github + +import ( + "encoding/json" + "errors" + "fmt" + "net/http" +) + +var ( + name = "Github" +) + +func HandleWebhook(header http.Header, msg string) (string, error) { + + hookType := header.Get("X-GitHub-Event") + + switch hookType { + case "push": + return githubPushEventHandler(msg) + case "ping": + return githubPingEventHandler(msg) + } + + return fmt.Sprintf("收到来自 Github 的 Webhook,暂不支持的类型: %s", hookType), nil + +} + +func githubPingEventHandler(msg string) (string, error) { + + data := &GithubPingEvent{} + err := json.Unmarshal([]byte(msg), &data) + + if err != nil { + return "", errors.New("解析Github Ping事件数据失败") + } + + return fmt.Sprintf(TemplatePing, + name, + data.Repository.FullName, + data.Repository.HtmlUrl, + ), nil + +} + +func githubPushEventHandler(msg string) (string, error) { + + data := &GithubPushEvent{} + err := json.Unmarshal([]byte(msg), &data) + + if err != nil { + return "", errors.New("解析Github Push事件数据失败") + } + + return fmt.Sprintf(TemplatePush, + name, + data.Pusher.Name, data.Pusher.Email, + data.Repository.Name, len(data.Commits), + data.Compare, + ), nil + +} diff --git a/wclient/whapp/github/template.go b/wclient/whapp/github/template.go new file mode 100644 index 0000000..c496f1b --- /dev/null +++ b/wclient/whapp/github/template.go @@ -0,0 +1,23 @@ +package github + +var ( + TemplateUnsupport = ` +🔔 来自%s的消息 +⚠️ 暂不支持该类型 +🙈 我们正在努力支持更多类型,敬请期待! +` + TemplatePush = ` +🔔 来自%s的消息 +👤 %s(%s) +📌 向仓库 %s 推送了%d次提交 +🔗 详情查看:%s +📊 提交记录一目了然,快来一探究竟吧! +` + TemplatePing = ` +🔔 来自%s的消息 +📌 仓库 %s +🎉 添加webhook成功 +✅ 操作顺利,一切就绪! +🔗 详情查看:%s +` +) diff --git a/wclient/whapp/text.go b/wclient/whapp/text.go deleted file mode 100644 index 263ca7f..0000000 --- a/wclient/whapp/text.go +++ /dev/null @@ -1,7 +0,0 @@ -package whapp - -func TextWebhook(msg string) (string, error) { - - return msg, nil - -} diff --git a/wclient/whapp/text/text.go b/wclient/whapp/text/text.go new file mode 100644 index 0000000..0c7530d --- /dev/null +++ b/wclient/whapp/text/text.go @@ -0,0 +1,7 @@ +package text + +func HandleWebhook(msg string) (string, error) { + + return msg, nil + +}