diff --git a/httpd/wrobot/webhook.go b/httpd/wrobot/webhook.go index 94f40e5f..8d4600c6 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/adapter.go b/wclient/whapp/adapter.go index 787e861a..5c95d591 100644 --- a/wclient/whapp/adapter.go +++ b/wclient/whapp/adapter.go @@ -28,5 +28,4 @@ func Handler(header http.Header, app string, msg string) string { } return res - } diff --git a/wclient/whapp/gitea/event.go b/wclient/whapp/gitea/event.go deleted file mode 100644 index b5fd88c4..00000000 --- a/wclient/whapp/gitea/event.go +++ /dev/null @@ -1,184 +0,0 @@ -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/events/event.go b/wclient/whapp/gitea/events/event.go new file mode 100644 index 00000000..0cb97feb --- /dev/null +++ b/wclient/whapp/gitea/events/event.go @@ -0,0 +1,778 @@ +package events + +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"` +} + +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 dfd08a40..d7283050 100644 --- a/wclient/whapp/gitea/gitea.go +++ b/wclient/whapp/gitea/gitea.go @@ -1,9 +1,9 @@ package gitea import ( - "encoding/json" - "errors" "fmt" + "github.com/opentdp/wrest-chat/wclient/whapp/gitea/handlers" + "github.com/opentdp/wrest-chat/wclient/whapp/gitea/templates" "net/http" ) @@ -15,25 +15,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 + return fmt.Sprintf(templates.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 00000000..e1ccf402 --- /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/gitea/events" + "github.com/opentdp/wrest-chat/wclient/whapp/gitea/templates" +) + +func CreateEventHandler(msg string) (string, error) { + data := &events.GiteaCreateEvent{} + err := json.Unmarshal([]byte(msg), &data) + + if err != nil { + return "", errors.New("解析Gitea Create事件失败") + } + + switch data.RefType { + case "tag": + 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 new file mode 100644 index 00000000..83513903 --- /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/gitea/events" + "github.com/opentdp/wrest-chat/wclient/whapp/gitea/templates" +) + +func IssueCommentEventHandler(msg string) (string, error) { + data := &events.GiteaIssueCommentEvent{} + + err := jsoniter.UnmarshalFromString(msg, &data) + + if err != nil { + return "", errors.New("解析Gitea IssueComment事件失败") + } + + switch data.Action { + case "created": + 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 new file mode 100644 index 00000000..f8f44649 --- /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/gitea/events" + "github.com/opentdp/wrest-chat/wclient/whapp/gitea/templates" +) + +func IssuesEventHandler(msg string) (string, error) { + data := &events.GiteaIssuesEvent{} + + err := jsoniter.UnmarshalFromString(msg, &data) + if err != nil { + return "", errors.New("解析 Gitea Issues 事件失败") + } + + switch data.Action { + case "opened": + 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 new file mode 100644 index 00000000..5ddfe24b --- /dev/null +++ b/wclient/whapp/gitea/handlers/push.go @@ -0,0 +1,25 @@ +package handlers + +import ( + "encoding/json" + "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) { + data := &events.GiteaPushEvent{} + err := json.Unmarshal([]byte(msg), &data) + + if err != nil { + 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/template.go b/wclient/whapp/gitea/template.go deleted file mode 100644 index 3068b644..00000000 --- a/wclient/whapp/gitea/template.go +++ /dev/null @@ -1,16 +0,0 @@ -package gitea - -var ( - TemplateUnsupport = ` -🔔 来自%s的消息 -⚠️ 暂不支持该类型 -🙈 我们正在努力支持更多类型,敬请期待! -` - TemplatePush = ` -🔔 来自%s的消息 -👤 %s(%s) -📌 向仓库 %s 推送了%d次提交 -🔗 详情查看:%s -📊 提交记录一目了然,快来一探究竟吧! -` -) diff --git a/wclient/whapp/gitea/templates/template.go b/wclient/whapp/gitea/templates/template.go new file mode 100644 index 00000000..f6953669 --- /dev/null +++ b/wclient/whapp/gitea/templates/template.go @@ -0,0 +1,61 @@ +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", `🔔 有人推送代码啦 +📦 仓库:{{ .Repository.FullName }} +📊 提交记录:{{ range $index, $val := .Commits }} + {{inc $index}}. {{ getShortMsg $val.Message }}(by @{{ $val.Author.Name }}){{ end }} +`) + TemplateCreateTag = NewTemplate("GITEA_CREATE_TAG", `🔖 有人创建Tag啦 +📦 {{ .Repository.FullName }} +🏷️ {{ .Ref }} +`) + TemplateOpenIssue = NewTemplate("OPEN_ISSUE", `✨ 有人提Issue啦 +📦 {{ .Repository.FullName }}#{{ .Issue.Number }} +💡 {{ .Issue.Title }} +👤 {{ .Sender.FullName }}({{ .Sender.Email }}) +🏷️ {{ range $index, $val := .Issue.Labels }}{{ $val.Name }} {{ end }} +`) + + TemplateCreateIssueComment = NewTemplate("CREATE_ISSUE_COMMENT", `🗨️ {{ .Repository.Name }}#{{ .Issue.Number }} 有新评论 +📦 {{ .Repository.FullName }}#{{ .Issue.Number }} +👤 {{ .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 +}