-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Add feishu webhook support #10229
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Add feishu webhook support #10229
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,201 @@ | ||
// Copyright 2020 The Gitea Authors. All rights reserved. | ||
// Use of this source code is governed by a MIT-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package webhook | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"strings" | ||
|
||
"code.gitea.io/gitea/models" | ||
"code.gitea.io/gitea/modules/git" | ||
api "code.gitea.io/gitea/modules/structs" | ||
) | ||
|
||
type ( | ||
// FeishuPayload represents | ||
FeishuPayload struct { | ||
Title string `json:"title"` | ||
Text string `json:"text"` | ||
} | ||
) | ||
|
||
// SetSecret sets the Feishu secret | ||
func (p *FeishuPayload) SetSecret(_ string) {} | ||
|
||
// JSONPayload Marshals the FeishuPayload to json | ||
func (p *FeishuPayload) JSONPayload() ([]byte, error) { | ||
data, err := json.MarshalIndent(p, "", " ") | ||
if err != nil { | ||
return []byte{}, err | ||
} | ||
return data, nil | ||
} | ||
|
||
func getFeishuCreatePayload(p *api.CreatePayload) (*FeishuPayload, error) { | ||
// created tag/branch | ||
refName := git.RefEndName(p.Ref) | ||
title := fmt.Sprintf("[%s] %s %s created", p.Repo.FullName, p.RefType, refName) | ||
|
||
return &FeishuPayload{ | ||
Text: title, | ||
Title: title, | ||
}, nil | ||
} | ||
|
||
func getFeishuDeletePayload(p *api.DeletePayload) (*FeishuPayload, error) { | ||
// created tag/branch | ||
refName := git.RefEndName(p.Ref) | ||
title := fmt.Sprintf("[%s] %s %s deleted", p.Repo.FullName, p.RefType, refName) | ||
|
||
return &FeishuPayload{ | ||
Text: title, | ||
Title: title, | ||
}, nil | ||
} | ||
|
||
func getFeishuForkPayload(p *api.ForkPayload) (*FeishuPayload, error) { | ||
title := fmt.Sprintf("%s is forked to %s", p.Forkee.FullName, p.Repo.FullName) | ||
|
||
return &FeishuPayload{ | ||
Text: title, | ||
Title: title, | ||
}, nil | ||
} | ||
|
||
func getFeishuPushPayload(p *api.PushPayload) (*FeishuPayload, error) { | ||
var ( | ||
branchName = git.RefEndName(p.Ref) | ||
commitDesc string | ||
) | ||
|
||
title := fmt.Sprintf("[%s:%s] %s", p.Repo.FullName, branchName, commitDesc) | ||
|
||
var text string | ||
// for each commit, generate attachment text | ||
for i, commit := range p.Commits { | ||
var authorName string | ||
if commit.Author != nil { | ||
authorName = " - " + commit.Author.Name | ||
} | ||
text += fmt.Sprintf("[%s](%s) %s", commit.ID[:7], commit.URL, | ||
strings.TrimRight(commit.Message, "\r\n")) + authorName | ||
// add linebreak to each commit but the last | ||
if i < len(p.Commits)-1 { | ||
text += "\n" | ||
} | ||
} | ||
|
||
return &FeishuPayload{ | ||
Text: text, | ||
Title: title, | ||
}, nil | ||
} | ||
|
||
func getFeishuIssuesPayload(p *api.IssuePayload) (*FeishuPayload, error) { | ||
text, issueTitle, attachmentText, _ := getIssuesPayloadInfo(p, noneLinkFormatter, true) | ||
|
||
return &FeishuPayload{ | ||
Text: text + "\r\n\r\n" + attachmentText, | ||
Title: issueTitle, | ||
}, nil | ||
} | ||
|
||
func getFeishuIssueCommentPayload(p *api.IssueCommentPayload) (*FeishuPayload, error) { | ||
text, issueTitle, _ := getIssueCommentPayloadInfo(p, noneLinkFormatter, true) | ||
|
||
return &FeishuPayload{ | ||
Text: text + "\r\n\r\n" + p.Comment.Body, | ||
Title: issueTitle, | ||
}, nil | ||
} | ||
|
||
func getFeishuPullRequestPayload(p *api.PullRequestPayload) (*FeishuPayload, error) { | ||
text, issueTitle, attachmentText, _ := getPullRequestPayloadInfo(p, noneLinkFormatter, true) | ||
|
||
return &FeishuPayload{ | ||
Text: text + "\r\n\r\n" + attachmentText, | ||
Title: issueTitle, | ||
}, nil | ||
} | ||
|
||
func getFeishuPullRequestApprovalPayload(p *api.PullRequestPayload, event models.HookEventType) (*FeishuPayload, error) { | ||
var text, title string | ||
switch p.Action { | ||
case api.HookIssueSynchronized: | ||
action, err := parseHookPullRequestEventType(event) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
title = fmt.Sprintf("[%s] Pull request review %s : #%d %s", p.Repository.FullName, action, p.Index, p.PullRequest.Title) | ||
text = p.Review.Content | ||
|
||
} | ||
|
||
return &FeishuPayload{ | ||
Text: title + "\r\n\r\n" + text, | ||
Title: title, | ||
}, nil | ||
} | ||
|
||
func getFeishuRepositoryPayload(p *api.RepositoryPayload) (*FeishuPayload, error) { | ||
var title string | ||
switch p.Action { | ||
case api.HookRepoCreated: | ||
title = fmt.Sprintf("[%s] Repository created", p.Repository.FullName) | ||
return &FeishuPayload{ | ||
Text: title, | ||
Title: title, | ||
}, nil | ||
case api.HookRepoDeleted: | ||
title = fmt.Sprintf("[%s] Repository deleted", p.Repository.FullName) | ||
return &FeishuPayload{ | ||
Title: title, | ||
Text: title, | ||
}, nil | ||
} | ||
|
||
return nil, nil | ||
} | ||
|
||
func getFeishuReleasePayload(p *api.ReleasePayload) (*FeishuPayload, error) { | ||
text, _ := getReleasePayloadInfo(p, noneLinkFormatter, true) | ||
|
||
return &FeishuPayload{ | ||
Text: text, | ||
Title: text, | ||
}, nil | ||
} | ||
|
||
// GetFeishuPayload converts a ding talk webhook into a FeishuPayload | ||
func GetFeishuPayload(p api.Payloader, event models.HookEventType, meta string) (*FeishuPayload, error) { | ||
s := new(FeishuPayload) | ||
|
||
switch event { | ||
case models.HookEventCreate: | ||
return getFeishuCreatePayload(p.(*api.CreatePayload)) | ||
case models.HookEventDelete: | ||
return getFeishuDeletePayload(p.(*api.DeletePayload)) | ||
case models.HookEventFork: | ||
return getFeishuForkPayload(p.(*api.ForkPayload)) | ||
case models.HookEventIssues: | ||
return getFeishuIssuesPayload(p.(*api.IssuePayload)) | ||
case models.HookEventIssueComment: | ||
return getFeishuIssueCommentPayload(p.(*api.IssueCommentPayload)) | ||
case models.HookEventPush: | ||
return getFeishuPushPayload(p.(*api.PushPayload)) | ||
case models.HookEventPullRequest: | ||
return getFeishuPullRequestPayload(p.(*api.PullRequestPayload)) | ||
case models.HookEventPullRequestApproved, models.HookEventPullRequestRejected, models.HookEventPullRequestComment: | ||
return getFeishuPullRequestApprovalPayload(p.(*api.PullRequestPayload), event) | ||
case models.HookEventRepository: | ||
return getFeishuRepositoryPayload(p.(*api.RepositoryPayload)) | ||
case models.HookEventRelease: | ||
return getFeishuReleasePayload(p.(*api.ReleasePayload)) | ||
} | ||
|
||
return s, nil | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.