Skip to content

Commit febee56

Browse files
authored
feat(webhook): 添加Gitea支持,优化提示模板 (#40)
* feat(webhook): 支持Gitea,更新通知模板.
1 parent 70cfc2a commit febee56

File tree

11 files changed

+338
-85
lines changed

11 files changed

+338
-85
lines changed

Diff for: wclient/whapp/adapter.go

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package whapp
22

33
import (
4+
"github.com/opentdp/wrest-chat/wclient/whapp/gitea"
5+
"github.com/opentdp/wrest-chat/wclient/whapp/github"
6+
"github.com/opentdp/wrest-chat/wclient/whapp/text"
47
"net/http"
58
)
69

@@ -11,11 +14,11 @@ func Handler(header http.Header, app string, msg string) string {
1114

1215
switch app {
1316
case "github":
14-
res, err = GithubWebhook(header, msg)
17+
res, err = github.HandleWebhook(header, msg)
1518
case "gitea":
16-
res, err = GiteaWebhook(msg)
19+
res, err = gitea.HandleWebhook(header, msg)
1720
case "text":
18-
res, err = TextWebhook(msg)
21+
res, err = text.HandleWebhook(msg)
1922
default:
2023
res = "暂不支持该应用的 webhook"
2124
}

Diff for: wclient/whapp/gitea.go

-7
This file was deleted.

Diff for: wclient/whapp/gitea/event.go

+184
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
package gitea
2+
3+
import "time"
4+
5+
type GiteaPushEvent struct {
6+
Ref string `json:"ref"`
7+
Before string `json:"before"`
8+
After string `json:"after"`
9+
CompareUrl string `json:"compare_url"`
10+
Commits []struct {
11+
Id string `json:"id"`
12+
Message string `json:"message"`
13+
Url string `json:"url"`
14+
Author struct {
15+
Name string `json:"name"`
16+
Email string `json:"email"`
17+
Username string `json:"username"`
18+
} `json:"author"`
19+
Committer struct {
20+
Name string `json:"name"`
21+
Email string `json:"email"`
22+
Username string `json:"username"`
23+
} `json:"committer"`
24+
Verification interface{} `json:"verification"`
25+
Timestamp time.Time `json:"timestamp"`
26+
Added []interface{} `json:"added"`
27+
Removed []interface{} `json:"removed"`
28+
Modified []string `json:"modified"`
29+
} `json:"commits"`
30+
TotalCommits int `json:"total_commits"`
31+
HeadCommit struct {
32+
Id string `json:"id"`
33+
Message string `json:"message"`
34+
Url string `json:"url"`
35+
Author struct {
36+
Name string `json:"name"`
37+
Email string `json:"email"`
38+
Username string `json:"username"`
39+
} `json:"author"`
40+
Committer struct {
41+
Name string `json:"name"`
42+
Email string `json:"email"`
43+
Username string `json:"username"`
44+
} `json:"committer"`
45+
Verification interface{} `json:"verification"`
46+
Timestamp time.Time `json:"timestamp"`
47+
Added []interface{} `json:"added"`
48+
Removed []interface{} `json:"removed"`
49+
Modified []string `json:"modified"`
50+
} `json:"head_commit"`
51+
Repository struct {
52+
Id int `json:"id"`
53+
Owner struct {
54+
Id int `json:"id"`
55+
Login string `json:"login"`
56+
LoginName string `json:"login_name"`
57+
FullName string `json:"full_name"`
58+
Email string `json:"email"`
59+
AvatarUrl string `json:"avatar_url"`
60+
Language string `json:"language"`
61+
IsAdmin bool `json:"is_admin"`
62+
LastLogin time.Time `json:"last_login"`
63+
Created time.Time `json:"created"`
64+
Restricted bool `json:"restricted"`
65+
Active bool `json:"active"`
66+
ProhibitLogin bool `json:"prohibit_login"`
67+
Location string `json:"location"`
68+
Website string `json:"website"`
69+
Description string `json:"description"`
70+
Visibility string `json:"visibility"`
71+
FollowersCount int `json:"followers_count"`
72+
FollowingCount int `json:"following_count"`
73+
StarredReposCount int `json:"starred_repos_count"`
74+
Username string `json:"username"`
75+
} `json:"owner"`
76+
Name string `json:"name"`
77+
FullName string `json:"full_name"`
78+
Description string `json:"description"`
79+
Empty bool `json:"empty"`
80+
Private bool `json:"private"`
81+
Fork bool `json:"fork"`
82+
Template bool `json:"template"`
83+
Parent interface{} `json:"parent"`
84+
Mirror bool `json:"mirror"`
85+
Size int `json:"size"`
86+
Language string `json:"language"`
87+
LanguagesUrl string `json:"languages_url"`
88+
HtmlUrl string `json:"html_url"`
89+
Url string `json:"url"`
90+
Link string `json:"link"`
91+
SshUrl string `json:"ssh_url"`
92+
CloneUrl string `json:"clone_url"`
93+
OriginalUrl string `json:"original_url"`
94+
Website string `json:"website"`
95+
StarsCount int `json:"stars_count"`
96+
ForksCount int `json:"forks_count"`
97+
WatchersCount int `json:"watchers_count"`
98+
OpenIssuesCount int `json:"open_issues_count"`
99+
OpenPrCounter int `json:"open_pr_counter"`
100+
ReleaseCounter int `json:"release_counter"`
101+
DefaultBranch string `json:"default_branch"`
102+
Archived bool `json:"archived"`
103+
CreatedAt time.Time `json:"created_at"`
104+
UpdatedAt time.Time `json:"updated_at"`
105+
ArchivedAt time.Time `json:"archived_at"`
106+
Permissions struct {
107+
Admin bool `json:"admin"`
108+
Push bool `json:"push"`
109+
Pull bool `json:"pull"`
110+
} `json:"permissions"`
111+
HasIssues bool `json:"has_issues"`
112+
InternalTracker struct {
113+
EnableTimeTracker bool `json:"enable_time_tracker"`
114+
AllowOnlyContributorsToTrackTime bool `json:"allow_only_contributors_to_track_time"`
115+
EnableIssueDependencies bool `json:"enable_issue_dependencies"`
116+
} `json:"internal_tracker"`
117+
HasWiki bool `json:"has_wiki"`
118+
HasPullRequests bool `json:"has_pull_requests"`
119+
HasProjects bool `json:"has_projects"`
120+
HasReleases bool `json:"has_releases"`
121+
HasPackages bool `json:"has_packages"`
122+
HasActions bool `json:"has_actions"`
123+
IgnoreWhitespaceConflicts bool `json:"ignore_whitespace_conflicts"`
124+
AllowMergeCommits bool `json:"allow_merge_commits"`
125+
AllowRebase bool `json:"allow_rebase"`
126+
AllowRebaseExplicit bool `json:"allow_rebase_explicit"`
127+
AllowSquashMerge bool `json:"allow_squash_merge"`
128+
AllowRebaseUpdate bool `json:"allow_rebase_update"`
129+
DefaultDeleteBranchAfterMerge bool `json:"default_delete_branch_after_merge"`
130+
DefaultMergeStyle string `json:"default_merge_style"`
131+
DefaultAllowMaintainerEdit bool `json:"default_allow_maintainer_edit"`
132+
AvatarUrl string `json:"avatar_url"`
133+
Internal bool `json:"internal"`
134+
MirrorInterval string `json:"mirror_interval"`
135+
MirrorUpdated time.Time `json:"mirror_updated"`
136+
RepoTransfer interface{} `json:"repo_transfer"`
137+
} `json:"repository"`
138+
Pusher struct {
139+
Id int `json:"id"`
140+
Login string `json:"login"`
141+
LoginName string `json:"login_name"`
142+
FullName string `json:"full_name"`
143+
Email string `json:"email"`
144+
AvatarUrl string `json:"avatar_url"`
145+
Language string `json:"language"`
146+
IsAdmin bool `json:"is_admin"`
147+
LastLogin time.Time `json:"last_login"`
148+
Created time.Time `json:"created"`
149+
Restricted bool `json:"restricted"`
150+
Active bool `json:"active"`
151+
ProhibitLogin bool `json:"prohibit_login"`
152+
Location string `json:"location"`
153+
Website string `json:"website"`
154+
Description string `json:"description"`
155+
Visibility string `json:"visibility"`
156+
FollowersCount int `json:"followers_count"`
157+
FollowingCount int `json:"following_count"`
158+
StarredReposCount int `json:"starred_repos_count"`
159+
Username string `json:"username"`
160+
} `json:"pusher"`
161+
Sender struct {
162+
Id int `json:"id"`
163+
Login string `json:"login"`
164+
LoginName string `json:"login_name"`
165+
FullName string `json:"full_name"`
166+
Email string `json:"email"`
167+
AvatarUrl string `json:"avatar_url"`
168+
Language string `json:"language"`
169+
IsAdmin bool `json:"is_admin"`
170+
LastLogin time.Time `json:"last_login"`
171+
Created time.Time `json:"created"`
172+
Restricted bool `json:"restricted"`
173+
Active bool `json:"active"`
174+
ProhibitLogin bool `json:"prohibit_login"`
175+
Location string `json:"location"`
176+
Website string `json:"website"`
177+
Description string `json:"description"`
178+
Visibility string `json:"visibility"`
179+
FollowersCount int `json:"followers_count"`
180+
FollowingCount int `json:"following_count"`
181+
StarredReposCount int `json:"starred_repos_count"`
182+
Username string `json:"username"`
183+
} `json:"sender"`
184+
}

Diff for: wclient/whapp/gitea/gitea.go

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package gitea
2+
3+
import (
4+
"encoding/json"
5+
"errors"
6+
"fmt"
7+
"net/http"
8+
)
9+
10+
var (
11+
name = "Gitea"
12+
)
13+
14+
func HandleWebhook(header http.Header, msg string) (string, error) {
15+
hookType := header.Get("X-Gitea-Event")
16+
switch hookType {
17+
case "push":
18+
return giteaPushEventHandler(msg)
19+
}
20+
21+
return fmt.Sprintf(TemplateUnsupport, name), nil
22+
23+
}
24+
25+
func giteaPushEventHandler(msg string) (string, error) {
26+
data := &GiteaPushEvent{}
27+
err := json.Unmarshal([]byte(msg), &data)
28+
29+
if err != nil {
30+
return "", errors.New("解析Gitea Push事件失败")
31+
}
32+
33+
return fmt.Sprintf(TemplatePush,
34+
name,
35+
data.Pusher.FullName, data.Pusher.Email,
36+
data.Repository.FullName, data.TotalCommits,
37+
data.CompareUrl,
38+
), nil
39+
}

Diff for: wclient/whapp/gitea/template.go

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package gitea
2+
3+
var (
4+
TemplateUnsupport = `
5+
🔔 来自%s的消息
6+
⚠️ 暂不支持该类型
7+
🙈 我们正在努力支持更多类型,敬请期待!
8+
`
9+
TemplatePush = `
10+
🔔 来自%s的消息
11+
👤 %s(%s)
12+
📌 向仓库 %s 推送了%d次提交
13+
🔗 详情查看:%s
14+
📊 提交记录一目了然,快来一探究竟吧!
15+
`
16+
)

Diff for: wclient/whapp/github.go

-67
This file was deleted.

Diff for: wclient/whapp/event/github.go renamed to wclient/whapp/github/event.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package event
1+
package github
22

33
import (
44
"time"

0 commit comments

Comments
 (0)