Skip to content

Commit a9080fa

Browse files
committed
Fix incompleted commit status events
1 parent 2d7e6e9 commit a9080fa

File tree

18 files changed

+110
-9
lines changed

18 files changed

+110
-9
lines changed

modules/structs/hook.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -509,14 +509,14 @@ type CommitStatusPayload struct {
509509
Commit *PayloadCommit `json:"commit"`
510510
Context string `json:"context"`
511511
// swagger:strfmt date-time
512-
CreatedAt time.Time `json:"created_at"`
513-
Description string `json:"description"`
514-
ID int64 `json:"id"`
515-
Repo *Repository `json:"repository"`
516-
Sender *User `json:"sender"`
517-
SHA string `json:"sha"`
518-
State string `json:"state"`
519-
TargetURL string `json:"target_url"`
512+
CreatedAt time.Time `json:"created_at"`
513+
Description string `json:"description"`
514+
ID int64 `json:"id"`
515+
Repo *Repository `json:"repository"`
516+
Sender *User `json:"sender"`
517+
SHA string `json:"sha"`
518+
State CommitStatusState `json:"state"`
519+
TargetURL string `json:"target_url"`
520520
// swagger:strfmt date-time
521521
UpdatedAt *time.Time `json:"updated_at"`
522522
}

modules/webhook/structs.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ type HookEvents struct {
2626
Repository bool `json:"repository"`
2727
Release bool `json:"release"`
2828
Package bool `json:"package"`
29+
CommitStatus bool `json:"commit_status"`
2930
}
3031

3132
// HookEvent represents events that will delivery hook.

options/locale/locale_en-US.ini

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2362,6 +2362,8 @@ settings.event_pull_request_approvals = Pull Request Approvals
23622362
settings.event_pull_request_merge = Pull Request Merge
23632363
settings.event_package = Package
23642364
settings.event_package_desc = Package created or deleted in a repository.
2365+
settings.event_commitstatus = Commit Status
2366+
settings.event_commitstatus_desc = Commit status updated.
23652367
settings.branch_filter = Branch filter
23662368
settings.branch_filter_desc = Branch whitelist for push, branch creation and branch deletion events, specified as glob pattern. If empty or <code>*</code>, events for all branches are reported. See <a href="%[1]s">%[2]s</a> documentation for syntax. Examples: <code>master</code>, <code>{master,release*}</code>.
23672369
settings.authorization_header = Authorization Header

routers/web/repo/setting/webhook.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ func ParseHookEvent(form forms.WebhookForm) *webhook_module.HookEvent {
184184
Wiki: form.Wiki,
185185
Repository: form.Repository,
186186
Package: form.Package,
187+
CommitStatus: form.CommitStatus,
187188
},
188189
BranchFilter: form.BranchFilter,
189190
}

services/forms/repo_form.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,7 @@ type WebhookForm struct {
263263
Wiki bool
264264
Repository bool
265265
Package bool
266+
CommitStatus bool
266267
Active bool
267268
BranchFilter string `binding:"GlobPattern"`
268269
AuthorizationHeader string

services/webhook/dingtalk.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,12 @@ func (dc dingtalkConvertor) Package(p *api.PackagePayload) (DingtalkPayload, err
170170
return createDingtalkPayload(text, text, "view package", p.Package.HTMLURL), nil
171171
}
172172

173+
func (dc dingtalkConvertor) CommitStatus(p *api.CommitStatusPayload) (DingtalkPayload, error) {
174+
text, _ := getCommitStatusPayloadInfo(p, noneLinkFormatter, true)
175+
176+
return createDingtalkPayload(text, text, "view commit status", p.TargetURL), nil
177+
}
178+
173179
func createDingtalkPayload(title, text, singleTitle, singleURL string) DingtalkPayload {
174180
return DingtalkPayload{
175181
MsgType: "actionCard",

services/webhook/discord.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,12 @@ func (d discordConvertor) Package(p *api.PackagePayload) (DiscordPayload, error)
265265
return d.createPayload(p.Sender, text, "", p.Package.HTMLURL, color), nil
266266
}
267267

268+
func (d discordConvertor) CommitStatus(p *api.CommitStatusPayload) (DiscordPayload, error) {
269+
text, color := getCommitStatusPayloadInfo(p, noneLinkFormatter, true)
270+
271+
return d.createPayload(p.Sender, "view commit status", text, p.TargetURL, color), nil
272+
}
273+
268274
func newDiscordRequest(_ context.Context, w *webhook_model.Webhook, t *webhook_model.HookTask) (*http.Request, []byte, error) {
269275
meta := &DiscordMeta{}
270276
if err := json.Unmarshal([]byte(w.Meta), meta); err != nil {

services/webhook/feishu.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,12 @@ func (fc feishuConvertor) Package(p *api.PackagePayload) (FeishuPayload, error)
166166
return newFeishuTextPayload(text), nil
167167
}
168168

169+
func (fc feishuConvertor) CommitStatus(p *api.CommitStatusPayload) (FeishuPayload, error) {
170+
text, _ := getCommitStatusPayloadInfo(p, markdownFormatter, true)
171+
172+
return newFeishuTextPayload(text), nil
173+
}
174+
169175
func newFeishuRequest(_ context.Context, w *webhook_model.Webhook, t *webhook_model.HookTask) (*http.Request, []byte, error) {
170176
var pc payloadConvertor[FeishuPayload] = feishuConvertor{}
171177
return newJSONRequest(pc, w, t, true)

services/webhook/general.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ func htmlLinkFormatter(url, text string) string {
2828
return fmt.Sprintf(`<a href="%s">%s</a>`, html.EscapeString(url), html.EscapeString(text))
2929
}
3030

31+
func markdownFormatter(url, text string) string {
32+
return fmt.Sprintf("[%s](%s)", text, url)
33+
}
34+
3135
// getPullRequestInfo gets the information for a pull request
3236
func getPullRequestInfo(p *api.PullRequestPayload) (title, link, by, operator, operateResult, assignees string) {
3337
title = fmt.Sprintf("[PullRequest-%s #%d]: %s\n%s", p.Repository.FullName, p.PullRequest.Index, p.Action, p.PullRequest.Title)
@@ -307,6 +311,24 @@ func getPackagePayloadInfo(p *api.PackagePayload, linkFormatter linkFormatter, w
307311
return text, color
308312
}
309313

314+
func getCommitStatusPayloadInfo(p *api.CommitStatusPayload, linkFormatter linkFormatter, _ bool) (string, int) {
315+
refLink := linkFormatter(p.Commit.URL, p.Commit.ID[:10])
316+
var color int
317+
switch p.State {
318+
case api.CommitStatusSuccess:
319+
color = greenColor
320+
case api.CommitStatusPending:
321+
color = greyColor
322+
case api.CommitStatusWarning:
323+
color = yellowColor
324+
default:
325+
color = redColor
326+
}
327+
328+
repoLink := linkFormatter(p.Repo.HTMLURL, p.Repo.FullName)
329+
return fmt.Sprintf("%s Commit %s status changed to %s", repoLink, refLink, p.State.String()), color
330+
}
331+
310332
// ToHook convert models.Webhook to api.Hook
311333
// This function is not part of the convert package to prevent an import cycle
312334
func ToHook(repoLink string, w *webhook_model.Webhook) (*api.Hook, error) {

services/webhook/matrix.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,12 @@ func (m matrixConvertor) Package(p *api.PackagePayload) (MatrixPayload, error) {
240240
return m.newPayload(text)
241241
}
242242

243+
func (m matrixConvertor) CommitStatus(p *api.CommitStatusPayload) (MatrixPayload, error) {
244+
text, _ := getCommitStatusPayloadInfo(p, noneLinkFormatter, true)
245+
246+
return m.newPayload(text)
247+
}
248+
243249
var urlRegex = regexp.MustCompile(`<a [^>]*?href="([^">]*?)">(.*?)</a>`)
244250

245251
func getMessageBody(htmlText string) string {

0 commit comments

Comments
 (0)