From 7f808b49778c4fe8af737aa6600cbcde934c91cd Mon Sep 17 00:00:00 2001 From: dydhyhwu Date: Tue, 23 Apr 2024 22:55:16 +0800 Subject: [PATCH 1/5] feat(webhook): support gitea issue_comment, create tag, open issue event --- wclient/whapp/adapter.go | 28 + wclient/whapp/gitea/event.go | 594 ++++++++++++++++++ wclient/whapp/gitea/gitea.go | 27 +- wclient/whapp/gitea/handlers/create.go | 25 + wclient/whapp/gitea/handlers/issue_comment.go | 25 + wclient/whapp/gitea/handlers/issues.go | 24 + wclient/whapp/gitea/handlers/push.go | 19 + wclient/whapp/gitea/template.go | 34 +- 8 files changed, 750 insertions(+), 26 deletions(-) create mode 100644 wclient/whapp/gitea/handlers/create.go create mode 100644 wclient/whapp/gitea/handlers/issue_comment.go create mode 100644 wclient/whapp/gitea/handlers/issues.go create mode 100644 wclient/whapp/gitea/handlers/push.go diff --git a/wclient/whapp/adapter.go b/wclient/whapp/adapter.go index 787e861..e42e4e4 100644 --- a/wclient/whapp/adapter.go +++ b/wclient/whapp/adapter.go @@ -1,12 +1,28 @@ package whapp import ( + "bytes" "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" + "strings" + "text/template" ) +var funcMap = template.FuncMap{ + "inc": func(i int) int { + return i + 1 + }, + "getShortMsg": func(msg string) string { + msgs := strings.Split(msg, "\n") + if len(msgs) <= 1 { + return strings.ReplaceAll(msg, "\n", "") + } + return strings.ReplaceAll(msgs[0], "\n", "") + }, +} + func Handler(header http.Header, app string, msg string) string { var res string @@ -28,5 +44,17 @@ func Handler(header http.Header, app string, msg string) string { } return res +} +func NewTemplate(name string, content string) *template.Template { + return template.Must(template.New(name).Funcs(funcMap).Parse(content)) +} + +func Render(t *template.Template, data interface{}) (string, error) { + var buf bytes.Buffer + err := t.Execute(&buf, data) + if err != nil { + return "渲染通知模板失败", err + } + return buf.String(), nil } diff --git a/wclient/whapp/gitea/event.go b/wclient/whapp/gitea/event.go index b5fd88c..5ea9e3e 100644 --- a/wclient/whapp/gitea/event.go +++ b/wclient/whapp/gitea/event.go @@ -182,3 +182,597 @@ type GiteaPushEvent struct { Username string `json:"username"` } `json:"sender"` } + +type GiteaCreateEvent struct { + Sha string `json:"sha"` + Ref string `json:"ref"` + RefType string `json:"ref_type"` + 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"` + 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"` +} + +type GiteaIssueCommentEvent struct { + Action string `json:"action"` + Issue struct { + Id int `json:"id"` + Url string `json:"url"` + HtmlUrl string `json:"html_url"` + Number int `json:"number"` + User 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:"user"` + OriginalAuthor string `json:"original_author"` + OriginalAuthorId int `json:"original_author_id"` + Title string `json:"title"` + Body string `json:"body"` + Ref string `json:"ref"` + Assets []interface{} `json:"assets"` + Labels []struct { + Id int `json:"id"` + Name string `json:"name"` + Exclusive bool `json:"exclusive"` + IsArchived bool `json:"is_archived"` + Color string `json:"color"` + Description string `json:"description"` + Url string `json:"url"` + } `json:"labels"` + Milestone interface{} `json:"milestone"` + Assignee 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:"assignee"` + Assignees []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:"assignees"` + State string `json:"state"` + IsLocked bool `json:"is_locked"` + Comments int `json:"comments"` + CreatedAt time.Time `json:"created_at"` + UpdatedAt time.Time `json:"updated_at"` + ClosedAt interface{} `json:"closed_at"` + DueDate interface{} `json:"due_date"` + PullRequest interface{} `json:"pull_request"` + Repository struct { + Id int `json:"id"` + Name string `json:"name"` + Owner string `json:"owner"` + FullName string `json:"full_name"` + } `json:"repository"` + PinOrder int `json:"pin_order"` + } `json:"issue"` + Comment struct { + Id int `json:"id"` + HtmlUrl string `json:"html_url"` + PullRequestUrl string `json:"pull_request_url"` + IssueUrl string `json:"issue_url"` + User 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:"user"` + OriginalAuthor string `json:"original_author"` + OriginalAuthorId int `json:"original_author_id"` + Body string `json:"body"` + Assets []interface{} `json:"assets"` + CreatedAt time.Time `json:"created_at"` + UpdatedAt time.Time `json:"updated_at"` + } `json:"comment"` + 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"` + 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"` + IsPull bool `json:"is_pull"` +} + +type GiteaIssuesEvent struct { + Action string `json:"action"` + Number int `json:"number"` + Issue struct { + Id int `json:"id"` + Url string `json:"url"` + HtmlUrl string `json:"html_url"` + Number int `json:"number"` + User 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:"user"` + OriginalAuthor string `json:"original_author"` + OriginalAuthorId int `json:"original_author_id"` + Title string `json:"title"` + Body string `json:"body"` + Ref string `json:"ref"` + Assets []interface{} `json:"assets"` + Labels []struct { + Id int `json:"id"` + Name string `json:"name"` + Exclusive bool `json:"exclusive"` + IsArchived bool `json:"is_archived"` + Color string `json:"color"` + Description string `json:"description"` + Url string `json:"url"` + } `json:"labels"` + Milestone interface{} `json:"milestone"` + Assignee 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:"assignee"` + Assignees []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:"assignees"` + State string `json:"state"` + IsLocked bool `json:"is_locked"` + Comments int `json:"comments"` + CreatedAt time.Time `json:"created_at"` + UpdatedAt time.Time `json:"updated_at"` + ClosedAt interface{} `json:"closed_at"` + DueDate interface{} `json:"due_date"` + PullRequest interface{} `json:"pull_request"` + Repository struct { + Id int `json:"id"` + Name string `json:"name"` + Owner string `json:"owner"` + FullName string `json:"full_name"` + } `json:"repository"` + PinOrder int `json:"pin_order"` + } `json:"issue"` + 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"` + 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"` + CommitId string `json:"commit_id"` +} diff --git a/wclient/whapp/gitea/gitea.go b/wclient/whapp/gitea/gitea.go index dfd08a4..c903fc6 100644 --- a/wclient/whapp/gitea/gitea.go +++ b/wclient/whapp/gitea/gitea.go @@ -1,9 +1,8 @@ package gitea import ( - "encoding/json" - "errors" "fmt" + "github.com/opentdp/wrest-chat/wclient/whapp/gitea/handlers" "net/http" ) @@ -15,25 +14,15 @@ func HandleWebhook(header http.Header, msg string) (string, error) { hookType := header.Get("X-Gitea-Event") switch hookType { case "push": - return giteaPushEventHandler(msg) + return handlers.PushEventHandler(msg) + case "create": + return handlers.CreateEventHandler(msg) + case "issue_comment": + return handlers.IssueCommentEventHandler(msg) + case "issues": + return handlers.IssuesEventHandler(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/handlers/create.go b/wclient/whapp/gitea/handlers/create.go new file mode 100644 index 0000000..3109506 --- /dev/null +++ b/wclient/whapp/gitea/handlers/create.go @@ -0,0 +1,25 @@ +package handlers + +import ( + "encoding/json" + "errors" + "fmt" + "github.com/opentdp/wrest-chat/wclient/whapp" + "github.com/opentdp/wrest-chat/wclient/whapp/gitea" +) + +func CreateEventHandler(msg string) (string, error) { + data := &gitea.GiteaCreateEvent{} + err := json.Unmarshal([]byte(msg), &data) + + if err != nil { + return "", errors.New("解析Gitea Create事件失败") + } + + switch data.Ref { + case "tag": + return whapp.Render(gitea.TemplateCreateTag, data) + } + + return fmt.Sprintf("暂时不支持的 Create 事件 Ref 类型: %s", data.Ref), nil +} diff --git a/wclient/whapp/gitea/handlers/issue_comment.go b/wclient/whapp/gitea/handlers/issue_comment.go new file mode 100644 index 0000000..59cdbef --- /dev/null +++ b/wclient/whapp/gitea/handlers/issue_comment.go @@ -0,0 +1,25 @@ +package handlers + +import ( + "errors" + jsoniter "github.com/json-iterator/go" + "github.com/opentdp/wrest-chat/wclient/whapp" + "github.com/opentdp/wrest-chat/wclient/whapp/gitea" +) + +func IssueCommentEventHandler(msg string) (string, error) { + data := &gitea.GiteaIssueCommentEvent{} + + err := jsoniter.UnmarshalFromString(msg, &data) + + if err != nil { + return "", errors.New("解析Gitea IssueComment事件失败") + } + + switch data.Action { + case "created": + return whapp.Render(gitea.TemplateCreateIssueComment, data) + } + + return "", errors.New("解析Gitea IssueComment事件失败") +} diff --git a/wclient/whapp/gitea/handlers/issues.go b/wclient/whapp/gitea/handlers/issues.go new file mode 100644 index 0000000..803c31b --- /dev/null +++ b/wclient/whapp/gitea/handlers/issues.go @@ -0,0 +1,24 @@ +package handlers + +import ( + "errors" + jsoniter "github.com/json-iterator/go" + "github.com/opentdp/wrest-chat/wclient/whapp" + "github.com/opentdp/wrest-chat/wclient/whapp/gitea" +) + +func IssuesEventHandler(msg string) (string, error) { + data := &gitea.GiteaIssuesEvent{} + + err := jsoniter.UnmarshalFromString(msg, &data) + if err != nil { + return "", errors.New("解析 Gitea Issues 事件失败") + } + + switch data.Action { + case "opened": + return whapp.Render(gitea.TemplateOpenIssue, data) + } + + return "", errors.New("解析 Gitea Issues 事件失败") +} diff --git a/wclient/whapp/gitea/handlers/push.go b/wclient/whapp/gitea/handlers/push.go new file mode 100644 index 0000000..49be325 --- /dev/null +++ b/wclient/whapp/gitea/handlers/push.go @@ -0,0 +1,19 @@ +package handlers + +import ( + "encoding/json" + "errors" + "github.com/opentdp/wrest-chat/wclient/whapp" + "github.com/opentdp/wrest-chat/wclient/whapp/gitea" +) + +func PushEventHandler(msg string) (string, error) { + data := &gitea.GiteaPushEvent{} + err := json.Unmarshal([]byte(msg), &data) + + if err != nil { + return "", errors.New("解析Gitea Push事件失败") + } + + return whapp.Render(gitea.TemplatePush, data) +} diff --git a/wclient/whapp/gitea/template.go b/wclient/whapp/gitea/template.go index 3068b64..94c6ff3 100644 --- a/wclient/whapp/gitea/template.go +++ b/wclient/whapp/gitea/template.go @@ -1,16 +1,36 @@ package gitea +import ( + "github.com/opentdp/wrest-chat/wclient/whapp" +) + var ( TemplateUnsupport = ` 🔔 来自%s的消息 ⚠️ 暂不支持该类型 🙈 我们正在努力支持更多类型,敬请期待! ` - TemplatePush = ` -🔔 来自%s的消息 -👤 %s(%s) -📌 向仓库 %s 推送了%d次提交 -🔗 详情查看:%s -📊 提交记录一目了然,快来一探究竟吧! -` + TemplatePush = whapp.NewTemplate("GITEA_PUSH", `🔔 来自Gitea的消息 +👤 {{ .Pusher.FullName }}({{ .Pusher.Email }}) +📌 向仓库 {{ .Repository.FullName }} 推送了{{ .TotalCommits }}次提交 +📊 提交记录:{{ range $index, $val := .Commits }} +{{inc $index}}: {{ getShortMsg $val.Message }}(by {{ $val.Author.Name }}){{ end }} +`) + TemplateCreateTag = whapp.NewTemplate("GITEA_CREATE_TAG", `🔖 新Tag +📦 {{ .Repository.FullName }} +🏷️ {{ .Ref }} +👤 {{ .Sender.FullName }}({{ .Sender.Email }}) +`) + TemplateOpenIssue = whapp.NewTemplate("GITEA_CREATE_TAG", `✨ 有人提Issue了 +📦 {{ .Repository.FullName }}#{{ .Issue.Number }} +💡 {{ .Issue.Title }} +👤 {{ .Sender.FullName }}({{ .Sender.Email }}) +🏷️ {{ range _, $val := .Issue.Labels }}{{ $val.Name }} {{ end }} +`) + + TemplateCreateIssueComment = whapp.NewTemplate("GITEA_CREATE_TAG", `🗨️ {{ .Repository.Name }}#{{ .Issue.Number }} 有新评论 +📦 {{ .Repository.FullName }} +🏷️ {{ .Ref }} +👤 {{ .Sender.FullName }}({{ .Sender.Email }}) +`) ) From 9f5f48d90237387a47b8a3caf91e8d6041a0dbc9 Mon Sep 17 00:00:00 2001 From: dydhyhwu Date: Tue, 23 Apr 2024 23:13:27 +0800 Subject: [PATCH 2/5] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E5=BE=AA=E7=8E=AF?= =?UTF-8?q?=E4=BE=9D=E8=B5=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- wclient/whapp/adapter.go | 29 --------- wclient/whapp/gitea/{ => events}/event.go | 2 +- wclient/whapp/gitea/gitea.go | 3 +- wclient/whapp/gitea/handlers/create.go | 7 +- wclient/whapp/gitea/handlers/issue_comment.go | 7 +- wclient/whapp/gitea/handlers/issues.go | 7 +- wclient/whapp/gitea/handlers/push.go | 8 +-- wclient/whapp/gitea/template.go | 36 ----------- wclient/whapp/gitea/templates/template.go | 64 +++++++++++++++++++ 9 files changed, 80 insertions(+), 83 deletions(-) rename wclient/whapp/gitea/{ => events}/event.go (99%) delete mode 100644 wclient/whapp/gitea/template.go create mode 100644 wclient/whapp/gitea/templates/template.go diff --git a/wclient/whapp/adapter.go b/wclient/whapp/adapter.go index e42e4e4..5c95d59 100644 --- a/wclient/whapp/adapter.go +++ b/wclient/whapp/adapter.go @@ -1,28 +1,12 @@ package whapp import ( - "bytes" "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" - "strings" - "text/template" ) -var funcMap = template.FuncMap{ - "inc": func(i int) int { - return i + 1 - }, - "getShortMsg": func(msg string) string { - msgs := strings.Split(msg, "\n") - if len(msgs) <= 1 { - return strings.ReplaceAll(msg, "\n", "") - } - return strings.ReplaceAll(msgs[0], "\n", "") - }, -} - func Handler(header http.Header, app string, msg string) string { var res string @@ -45,16 +29,3 @@ func Handler(header http.Header, app string, msg string) string { return res } - -func NewTemplate(name string, content string) *template.Template { - return template.Must(template.New(name).Funcs(funcMap).Parse(content)) -} - -func Render(t *template.Template, data interface{}) (string, error) { - var buf bytes.Buffer - err := t.Execute(&buf, data) - if err != nil { - return "渲染通知模板失败", err - } - return buf.String(), nil -} diff --git a/wclient/whapp/gitea/event.go b/wclient/whapp/gitea/events/event.go similarity index 99% rename from wclient/whapp/gitea/event.go rename to wclient/whapp/gitea/events/event.go index 5ea9e3e..0cb97fe 100644 --- a/wclient/whapp/gitea/event.go +++ b/wclient/whapp/gitea/events/event.go @@ -1,4 +1,4 @@ -package gitea +package events import "time" diff --git a/wclient/whapp/gitea/gitea.go b/wclient/whapp/gitea/gitea.go index c903fc6..d728305 100644 --- a/wclient/whapp/gitea/gitea.go +++ b/wclient/whapp/gitea/gitea.go @@ -3,6 +3,7 @@ package gitea import ( "fmt" "github.com/opentdp/wrest-chat/wclient/whapp/gitea/handlers" + "github.com/opentdp/wrest-chat/wclient/whapp/gitea/templates" "net/http" ) @@ -23,6 +24,6 @@ func HandleWebhook(header http.Header, msg string) (string, error) { return handlers.IssuesEventHandler(msg) } - return fmt.Sprintf(TemplateUnsupport, name), nil + return fmt.Sprintf(templates.TemplateUnsupport, name), nil } diff --git a/wclient/whapp/gitea/handlers/create.go b/wclient/whapp/gitea/handlers/create.go index 3109506..c9feb97 100644 --- a/wclient/whapp/gitea/handlers/create.go +++ b/wclient/whapp/gitea/handlers/create.go @@ -4,12 +4,11 @@ import ( "encoding/json" "errors" "fmt" - "github.com/opentdp/wrest-chat/wclient/whapp" - "github.com/opentdp/wrest-chat/wclient/whapp/gitea" + "github.com/opentdp/wrest-chat/wclient/whapp/gitea/templates" ) func CreateEventHandler(msg string) (string, error) { - data := &gitea.GiteaCreateEvent{} + data := &events.GiteaCreateEvent{} err := json.Unmarshal([]byte(msg), &data) if err != nil { @@ -18,7 +17,7 @@ func CreateEventHandler(msg string) (string, error) { switch data.Ref { case "tag": - return whapp.Render(gitea.TemplateCreateTag, data) + return templates.Render(templates.TemplateCreateTag, data) } return fmt.Sprintf("暂时不支持的 Create 事件 Ref 类型: %s", data.Ref), nil diff --git a/wclient/whapp/gitea/handlers/issue_comment.go b/wclient/whapp/gitea/handlers/issue_comment.go index 59cdbef..3ec8504 100644 --- a/wclient/whapp/gitea/handlers/issue_comment.go +++ b/wclient/whapp/gitea/handlers/issue_comment.go @@ -3,12 +3,11 @@ package handlers import ( "errors" jsoniter "github.com/json-iterator/go" - "github.com/opentdp/wrest-chat/wclient/whapp" - "github.com/opentdp/wrest-chat/wclient/whapp/gitea" + "github.com/opentdp/wrest-chat/wclient/whapp/gitea/templates" ) func IssueCommentEventHandler(msg string) (string, error) { - data := &gitea.GiteaIssueCommentEvent{} + data := &events.GiteaIssueCommentEvent{} err := jsoniter.UnmarshalFromString(msg, &data) @@ -18,7 +17,7 @@ func IssueCommentEventHandler(msg string) (string, error) { switch data.Action { case "created": - return whapp.Render(gitea.TemplateCreateIssueComment, data) + return templates.Render(templates.TemplateCreateIssueComment, data) } return "", errors.New("解析Gitea IssueComment事件失败") diff --git a/wclient/whapp/gitea/handlers/issues.go b/wclient/whapp/gitea/handlers/issues.go index 803c31b..934aff2 100644 --- a/wclient/whapp/gitea/handlers/issues.go +++ b/wclient/whapp/gitea/handlers/issues.go @@ -3,12 +3,11 @@ package handlers import ( "errors" jsoniter "github.com/json-iterator/go" - "github.com/opentdp/wrest-chat/wclient/whapp" - "github.com/opentdp/wrest-chat/wclient/whapp/gitea" + "github.com/opentdp/wrest-chat/wclient/whapp/gitea/templates" ) func IssuesEventHandler(msg string) (string, error) { - data := &gitea.GiteaIssuesEvent{} + data := &events.GiteaIssuesEvent{} err := jsoniter.UnmarshalFromString(msg, &data) if err != nil { @@ -17,7 +16,7 @@ func IssuesEventHandler(msg string) (string, error) { switch data.Action { case "opened": - return whapp.Render(gitea.TemplateOpenIssue, data) + return templates.Render(templates.TemplateOpenIssue, data) } return "", errors.New("解析 Gitea Issues 事件失败") diff --git a/wclient/whapp/gitea/handlers/push.go b/wclient/whapp/gitea/handlers/push.go index 49be325..d95bee0 100644 --- a/wclient/whapp/gitea/handlers/push.go +++ b/wclient/whapp/gitea/handlers/push.go @@ -3,17 +3,17 @@ package handlers import ( "encoding/json" "errors" - "github.com/opentdp/wrest-chat/wclient/whapp" - "github.com/opentdp/wrest-chat/wclient/whapp/gitea" + "github.com/opentdp/wrest-chat/wclient/whapp/gitea/events" + "github.com/opentdp/wrest-chat/wclient/whapp/gitea/templates" ) func PushEventHandler(msg string) (string, error) { - data := &gitea.GiteaPushEvent{} + data := &events.GiteaPushEvent{} err := json.Unmarshal([]byte(msg), &data) if err != nil { return "", errors.New("解析Gitea Push事件失败") } - return whapp.Render(gitea.TemplatePush, data) + return templates.Render(templates.TemplatePush, data) } diff --git a/wclient/whapp/gitea/template.go b/wclient/whapp/gitea/template.go deleted file mode 100644 index 94c6ff3..0000000 --- a/wclient/whapp/gitea/template.go +++ /dev/null @@ -1,36 +0,0 @@ -package gitea - -import ( - "github.com/opentdp/wrest-chat/wclient/whapp" -) - -var ( - TemplateUnsupport = ` -🔔 来自%s的消息 -⚠️ 暂不支持该类型 -🙈 我们正在努力支持更多类型,敬请期待! -` - TemplatePush = whapp.NewTemplate("GITEA_PUSH", `🔔 来自Gitea的消息 -👤 {{ .Pusher.FullName }}({{ .Pusher.Email }}) -📌 向仓库 {{ .Repository.FullName }} 推送了{{ .TotalCommits }}次提交 -📊 提交记录:{{ range $index, $val := .Commits }} -{{inc $index}}: {{ getShortMsg $val.Message }}(by {{ $val.Author.Name }}){{ end }} -`) - TemplateCreateTag = whapp.NewTemplate("GITEA_CREATE_TAG", `🔖 新Tag -📦 {{ .Repository.FullName }} -🏷️ {{ .Ref }} -👤 {{ .Sender.FullName }}({{ .Sender.Email }}) -`) - TemplateOpenIssue = whapp.NewTemplate("GITEA_CREATE_TAG", `✨ 有人提Issue了 -📦 {{ .Repository.FullName }}#{{ .Issue.Number }} -💡 {{ .Issue.Title }} -👤 {{ .Sender.FullName }}({{ .Sender.Email }}) -🏷️ {{ range _, $val := .Issue.Labels }}{{ $val.Name }} {{ end }} -`) - - TemplateCreateIssueComment = whapp.NewTemplate("GITEA_CREATE_TAG", `🗨️ {{ .Repository.Name }}#{{ .Issue.Number }} 有新评论 -📦 {{ .Repository.FullName }} -🏷️ {{ .Ref }} -👤 {{ .Sender.FullName }}({{ .Sender.Email }}) -`) -) diff --git a/wclient/whapp/gitea/templates/template.go b/wclient/whapp/gitea/templates/template.go new file mode 100644 index 0000000..2fd081e --- /dev/null +++ b/wclient/whapp/gitea/templates/template.go @@ -0,0 +1,64 @@ +package templates + +import ( + "bytes" + "strings" + "text/template" +) + +var funcMap = template.FuncMap{ + "inc": func(i int) int { + return i + 1 + }, + "getShortMsg": func(msg string) string { + msgs := strings.Split(msg, "\n") + if len(msgs) <= 1 { + return strings.ReplaceAll(msg, "\n", "") + } + return strings.ReplaceAll(msgs[0], "\n", "") + }, +} + +var ( + TemplateUnsupport = ` +🔔 来自%s的消息 +⚠️ 暂不支持该类型 +🙈 我们正在努力支持更多类型,敬请期待! +` + TemplatePush = NewTemplate("GITEA_PUSH", `🔔 来自Gitea的消息 +👤 {{ .Pusher.FullName }}({{ .Pusher.Email }}) +📌 向仓库 {{ .Repository.FullName }} 推送了{{ .TotalCommits }}次提交 +📊 提交记录:{{ range $index, $val := .Commits }} +{{inc $index}}: {{ getShortMsg $val.Message }}(by {{ $val.Author.Name }}){{ end }} +`) + TemplateCreateTag = NewTemplate("GITEA_CREATE_TAG", `🔖 新Tag +📦 {{ .Repository.FullName }} +🏷️ {{ .Ref }} +👤 {{ .Sender.FullName }}({{ .Sender.Email }}) +`) + TemplateOpenIssue = NewTemplate("GITEA_CREATE_TAG", `✨ 有人提Issue了 +📦 {{ .Repository.FullName }}#{{ .Issue.Number }} +💡 {{ .Issue.Title }} +👤 {{ .Sender.FullName }}({{ .Sender.Email }}) +🏷️ {{ range _, $val := .Issue.Labels }}{{ $val.Name }} {{ end }} +`) + + TemplateCreateIssueComment = NewTemplate("GITEA_CREATE_TAG", `🗨️ {{ .Repository.Name }}#{{ .Issue.Number }} 有新评论 +📦 {{ .Repository.FullName }} +🏷️ {{ .Ref }} +👤 {{ .Sender.FullName }}({{ .Sender.Email }}) +`) +) + +func NewTemplate(name string, content string) *template.Template { + return template.Must(template.New(name).Funcs(funcMap).Parse(content)) +} + +func Render(t *template.Template, data interface{}) (string, error) { + var buf bytes.Buffer + err := t.Execute(&buf, data) + if err != nil { + return "渲染通知模板失败", err + } + return buf.String(), nil +} From 2035ed0555a9ff3093fb8a15ae64da18a66f877b Mon Sep 17 00:00:00 2001 From: dydhyhwu Date: Tue, 23 Apr 2024 23:30:27 +0800 Subject: [PATCH 3/5] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E6=A8=A1=E6=9D=BF?= =?UTF-8?q?=E6=8A=A5=E9=94=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- wclient/whapp/gitea/handlers/create.go | 1 + wclient/whapp/gitea/handlers/issue_comment.go | 1 + wclient/whapp/gitea/handlers/issues.go | 1 + wclient/whapp/gitea/templates/template.go | 6 +++--- 4 files changed, 6 insertions(+), 3 deletions(-) diff --git a/wclient/whapp/gitea/handlers/create.go b/wclient/whapp/gitea/handlers/create.go index c9feb97..2746fed 100644 --- a/wclient/whapp/gitea/handlers/create.go +++ b/wclient/whapp/gitea/handlers/create.go @@ -4,6 +4,7 @@ import ( "encoding/json" "errors" "fmt" + "github.com/opentdp/wrest-chat/wclient/whapp/gitea/events" "github.com/opentdp/wrest-chat/wclient/whapp/gitea/templates" ) diff --git a/wclient/whapp/gitea/handlers/issue_comment.go b/wclient/whapp/gitea/handlers/issue_comment.go index 3ec8504..8351390 100644 --- a/wclient/whapp/gitea/handlers/issue_comment.go +++ b/wclient/whapp/gitea/handlers/issue_comment.go @@ -3,6 +3,7 @@ package handlers import ( "errors" jsoniter "github.com/json-iterator/go" + "github.com/opentdp/wrest-chat/wclient/whapp/gitea/events" "github.com/opentdp/wrest-chat/wclient/whapp/gitea/templates" ) diff --git a/wclient/whapp/gitea/handlers/issues.go b/wclient/whapp/gitea/handlers/issues.go index 934aff2..f8f4464 100644 --- a/wclient/whapp/gitea/handlers/issues.go +++ b/wclient/whapp/gitea/handlers/issues.go @@ -3,6 +3,7 @@ package handlers import ( "errors" jsoniter "github.com/json-iterator/go" + "github.com/opentdp/wrest-chat/wclient/whapp/gitea/events" "github.com/opentdp/wrest-chat/wclient/whapp/gitea/templates" ) diff --git a/wclient/whapp/gitea/templates/template.go b/wclient/whapp/gitea/templates/template.go index 2fd081e..f0dbcbf 100644 --- a/wclient/whapp/gitea/templates/template.go +++ b/wclient/whapp/gitea/templates/template.go @@ -36,14 +36,14 @@ var ( 🏷️ {{ .Ref }} 👤 {{ .Sender.FullName }}({{ .Sender.Email }}) `) - TemplateOpenIssue = NewTemplate("GITEA_CREATE_TAG", `✨ 有人提Issue了 + TemplateOpenIssue = NewTemplate("OPEN_ISSUE", `✨ 有人提Issue了 📦 {{ .Repository.FullName }}#{{ .Issue.Number }} 💡 {{ .Issue.Title }} 👤 {{ .Sender.FullName }}({{ .Sender.Email }}) -🏷️ {{ range _, $val := .Issue.Labels }}{{ $val.Name }} {{ end }} +🏷️ {{ range $index, $val := .Issue.Labels }}{{ $val.Name }} {{ end }} `) - TemplateCreateIssueComment = NewTemplate("GITEA_CREATE_TAG", `🗨️ {{ .Repository.Name }}#{{ .Issue.Number }} 有新评论 + TemplateCreateIssueComment = NewTemplate("CREATE_ISSUE_COMMENT", `🗨️ {{ .Repository.Name }}#{{ .Issue.Number }} 有新评论 📦 {{ .Repository.FullName }} 🏷️ {{ .Ref }} 👤 {{ .Sender.FullName }}({{ .Sender.Email }}) From d83d16f4785a09e3a3286eab48fbbfbfcae251bb Mon Sep 17 00:00:00 2001 From: dydhyhwu Date: Tue, 23 Apr 2024 23:52:47 +0800 Subject: [PATCH 4/5] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E8=AF=84=E8=AE=BA?= =?UTF-8?q?issue=E8=A7=A3=E6=9E=90=E5=A4=B1=E8=B4=A5=E7=9A=84=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- wclient/whapp/gitea/templates/template.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/wclient/whapp/gitea/templates/template.go b/wclient/whapp/gitea/templates/template.go index f0dbcbf..f093490 100644 --- a/wclient/whapp/gitea/templates/template.go +++ b/wclient/whapp/gitea/templates/template.go @@ -44,8 +44,7 @@ var ( `) TemplateCreateIssueComment = NewTemplate("CREATE_ISSUE_COMMENT", `🗨️ {{ .Repository.Name }}#{{ .Issue.Number }} 有新评论 -📦 {{ .Repository.FullName }} -🏷️ {{ .Ref }} +📦 {{ .Repository.FullName }}#{{ .Issue.Number }} 👤 {{ .Sender.FullName }}({{ .Sender.Email }}) `) ) From caebdef556ffc86d880c253f75f6b9f49ba61fe8 Mon Sep 17 00:00:00 2001 From: dydhyhwu Date: Wed, 24 Apr 2024 21:21:40 +0800 Subject: [PATCH 5/5] =?UTF-8?q?feat(webhook):=20=E4=BC=98=E5=8C=96?= =?UTF-8?q?=E6=8F=90=E7=A4=BA=E4=BF=A1=E6=81=AF=E6=A8=A1=E6=9D=BF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- httpd/wrobot/webhook.go | 7 ++++++- wclient/whapp/gitea/handlers/create.go | 2 +- wclient/whapp/gitea/handlers/push.go | 6 ++++++ wclient/whapp/gitea/templates/template.go | 12 +++++------- 4 files changed, 18 insertions(+), 9 deletions(-) diff --git a/httpd/wrobot/webhook.go b/httpd/wrobot/webhook.go index 94f40e5..8d4600c 100644 --- a/httpd/wrobot/webhook.go +++ b/httpd/wrobot/webhook.go @@ -123,7 +123,12 @@ func (w *Webhook) receive(c *gin.Context) { // 根据app类型不同,调用不同的处理方式,参照handler的注册 msg := whapp.Handler(c.Request.Header, app, string(request)) - res := wc.CmdClient.SendTxt(msg, hook.TargetId, "") + + var res int32 + + if msg != "" { + res = wc.CmdClient.SendTxt(msg, hook.TargetId, "") + } if res == 0 { c.Set("Message", "OK") diff --git a/wclient/whapp/gitea/handlers/create.go b/wclient/whapp/gitea/handlers/create.go index 2746fed..e1ccf40 100644 --- a/wclient/whapp/gitea/handlers/create.go +++ b/wclient/whapp/gitea/handlers/create.go @@ -16,7 +16,7 @@ func CreateEventHandler(msg string) (string, error) { return "", errors.New("解析Gitea Create事件失败") } - switch data.Ref { + switch data.RefType { case "tag": return templates.Render(templates.TemplateCreateTag, data) } diff --git a/wclient/whapp/gitea/handlers/push.go b/wclient/whapp/gitea/handlers/push.go index d95bee0..5ddfe24 100644 --- a/wclient/whapp/gitea/handlers/push.go +++ b/wclient/whapp/gitea/handlers/push.go @@ -5,6 +5,7 @@ import ( "errors" "github.com/opentdp/wrest-chat/wclient/whapp/gitea/events" "github.com/opentdp/wrest-chat/wclient/whapp/gitea/templates" + "strings" ) func PushEventHandler(msg string) (string, error) { @@ -15,5 +16,10 @@ func PushEventHandler(msg string) (string, error) { return "", errors.New("解析Gitea Push事件失败") } + // ignore push tag + if strings.HasPrefix(data.Ref, "refs/tags/") { + return "", nil + } + return templates.Render(templates.TemplatePush, data) } diff --git a/wclient/whapp/gitea/templates/template.go b/wclient/whapp/gitea/templates/template.go index f093490..f695366 100644 --- a/wclient/whapp/gitea/templates/template.go +++ b/wclient/whapp/gitea/templates/template.go @@ -25,18 +25,16 @@ var ( ⚠️ 暂不支持该类型 🙈 我们正在努力支持更多类型,敬请期待! ` - TemplatePush = NewTemplate("GITEA_PUSH", `🔔 来自Gitea的消息 -👤 {{ .Pusher.FullName }}({{ .Pusher.Email }}) -📌 向仓库 {{ .Repository.FullName }} 推送了{{ .TotalCommits }}次提交 + TemplatePush = NewTemplate("GITEA_PUSH", `🔔 有人推送代码啦 +📦 仓库:{{ .Repository.FullName }} 📊 提交记录:{{ range $index, $val := .Commits }} -{{inc $index}}: {{ getShortMsg $val.Message }}(by {{ $val.Author.Name }}){{ end }} + {{inc $index}}. {{ getShortMsg $val.Message }}(by @{{ $val.Author.Name }}){{ end }} `) - TemplateCreateTag = NewTemplate("GITEA_CREATE_TAG", `🔖 新Tag + TemplateCreateTag = NewTemplate("GITEA_CREATE_TAG", `🔖 有人创建Tag啦 📦 {{ .Repository.FullName }} 🏷️ {{ .Ref }} -👤 {{ .Sender.FullName }}({{ .Sender.Email }}) `) - TemplateOpenIssue = NewTemplate("OPEN_ISSUE", `✨ 有人提Issue了 + TemplateOpenIssue = NewTemplate("OPEN_ISSUE", `✨ 有人提Issue啦 📦 {{ .Repository.FullName }}#{{ .Issue.Number }} 💡 {{ .Issue.Title }} 👤 {{ .Sender.FullName }}({{ .Sender.Email }})