-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Refactor webhooks to reduce code duplication #9422
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
Changes from 1 commit
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
3df1cd3
Start webhook refactoring to reduce code duplication
096095e
More webhook refactoring
41b527a
Unify webhook release messages
aebc450
Fix webhook release link
35bb3d7
Remove sql import
611bfe0
More webhook refactoring
17f63e8
More webhook refactoring
4506db9
Webhook tests extended
9cbab66
Merge branch 'master' into refactor_webhooks
lafriks 4281924
Merge branch 'master' into refactor_webhooks
zeripath 48e3ed6
Merge branch 'master' into refactor_webhooks
zeripath 9eecded
Fixed issue opened webhook
97d3704
Merge branch 'master' into refactor_webhooks
techknowlogick 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
package webhook | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"code.gitea.io/gitea/modules/setting" | ||
api "code.gitea.io/gitea/modules/structs" | ||
) | ||
|
||
type linkFormatter = func(string, string) string | ||
|
||
func noneLinkFormatter(url string, text string) string { | ||
return text | ||
} | ||
|
||
func htmlLinkFormatter(url string, text string) string { | ||
return fmt.Sprintf(`<a href="%s">%s</a>`, url, text) | ||
} | ||
|
||
func getIssuesPayloadInfo(p *api.IssuePayload, linkFormatter linkFormatter) (string, string, int) { | ||
senderLink := linkFormatter(setting.AppURL+p.Sender.UserName, p.Sender.UserName) | ||
repoLink := linkFormatter(p.Repository.HTMLURL, p.Repository.FullName) | ||
issueTitle := fmt.Sprintf("#%d %s", p.Index, p.Issue.Title) | ||
titleLink := linkFormatter(p.Issue.URL, issueTitle) | ||
var text string | ||
color := yellowColor | ||
|
||
switch p.Action { | ||
case api.HookIssueOpened: | ||
text = fmt.Sprintf("[%s] Issue opened by %s", repoLink, senderLink) | ||
zeripath marked this conversation as resolved.
Show resolved
Hide resolved
|
||
color = orangeColor | ||
case api.HookIssueClosed: | ||
text = fmt.Sprintf("[%s] Issue closed: %s by %s", repoLink, titleLink, senderLink) | ||
color = redColor | ||
case api.HookIssueReOpened: | ||
text = fmt.Sprintf("[%s] Issue re-opened: %s by %s", repoLink, titleLink, senderLink) | ||
case api.HookIssueEdited: | ||
text = fmt.Sprintf("[%s] Issue edited: %s by %s", repoLink, titleLink, senderLink) | ||
case api.HookIssueAssigned: | ||
text = fmt.Sprintf("[%s] Issue assigned to %s: %s by %s", repoLink, | ||
linkFormatter(setting.AppURL+p.Issue.Assignee.UserName, p.Issue.Assignee.UserName), | ||
titleLink, senderLink) | ||
color = greenColor | ||
case api.HookIssueUnassigned: | ||
text = fmt.Sprintf("[%s] Issue unassigned: %s by %s", repoLink, titleLink, senderLink) | ||
case api.HookIssueLabelUpdated: | ||
text = fmt.Sprintf("[%s] Issue labels updated: %s by %s", repoLink, titleLink, senderLink) | ||
case api.HookIssueLabelCleared: | ||
text = fmt.Sprintf("[%s] Issue labels cleared: %s by %s", repoLink, titleLink, senderLink) | ||
case api.HookIssueSynchronized: | ||
text = fmt.Sprintf("[%s] Issue synchronized: %s by %s", repoLink, titleLink, senderLink) | ||
case api.HookIssueMilestoned: | ||
mileStoneLink := fmt.Sprintf("%s/milestone/%d", p.Repository.HTMLURL, p.Issue.Milestone.ID) | ||
text = fmt.Sprintf("[%s] Issue milestoned to %s: %s by %s", repoLink, | ||
linkFormatter(mileStoneLink, p.Issue.Milestone.Title), titleLink, senderLink) | ||
case api.HookIssueDemilestoned: | ||
text = fmt.Sprintf("[%s] Issue milestone cleared: %s by %s", repoLink, titleLink, senderLink) | ||
} | ||
|
||
return text, issueTitle, color | ||
} | ||
|
||
func getPullRequestPayloadInfo(p *api.PullRequestPayload, linkFormatter linkFormatter) (string, string) { | ||
senderLink := linkFormatter(setting.AppURL+p.Sender.UserName, p.Sender.UserName) | ||
repoLink := linkFormatter(p.Repository.HTMLURL, p.Repository.FullName) | ||
issueTitle := fmt.Sprintf("#%d %s", p.Index, p.PullRequest.Title) | ||
titleLink := linkFormatter(p.PullRequest.URL, issueTitle) | ||
var text string | ||
|
||
switch p.Action { | ||
case api.HookIssueOpened: | ||
text = fmt.Sprintf("[%s] Pull request opened by %s", repoLink, senderLink) | ||
case api.HookIssueClosed: | ||
if p.PullRequest.HasMerged { | ||
text = fmt.Sprintf("[%s] Pull request merged: %s by %s", repoLink, titleLink, senderLink) | ||
} else { | ||
text = fmt.Sprintf("[%s] Pull request closed: %s by %s", repoLink, titleLink, senderLink) | ||
} | ||
case api.HookIssueReOpened: | ||
text = fmt.Sprintf("[%s] Pull request re-opened: %s by %s", repoLink, titleLink, senderLink) | ||
case api.HookIssueEdited: | ||
text = fmt.Sprintf("[%s] Pull request edited: %s by %s", repoLink, titleLink, senderLink) | ||
case api.HookIssueAssigned: | ||
list := make([]string, len(p.PullRequest.Assignees)) | ||
for i, user := range p.PullRequest.Assignees { | ||
list[i] = linkFormatter(setting.AppURL+user.UserName, user.UserName) | ||
} | ||
text = fmt.Sprintf("[%s] Pull request assigned to %s: %s by %s", repoLink, | ||
strings.Join(list, ", "), | ||
titleLink, senderLink) | ||
case api.HookIssueUnassigned: | ||
text = fmt.Sprintf("[%s] Pull request unassigned: %s by %s", repoLink, titleLink, senderLink) | ||
case api.HookIssueLabelUpdated: | ||
text = fmt.Sprintf("[%s] Pull request labels updated: %s by %s", repoLink, titleLink, senderLink) | ||
case api.HookIssueLabelCleared: | ||
text = fmt.Sprintf("[%s] Pull request labels cleared: %s by %s", repoLink, titleLink, senderLink) | ||
case api.HookIssueSynchronized: | ||
text = fmt.Sprintf("[%s] Pull request synchronized: %s by %s", repoLink, titleLink, senderLink) | ||
case api.HookIssueMilestoned: | ||
mileStoneLink := fmt.Sprintf("%s/milestone/%d", p.Repository.HTMLURL, p.PullRequest.Milestone.ID) | ||
text = fmt.Sprintf("[%s] Pull request milestoned to %s: %s by %s", repoLink, | ||
linkFormatter(mileStoneLink, p.PullRequest.Milestone.Title), titleLink, senderLink) | ||
case api.HookIssueDemilestoned: | ||
text = fmt.Sprintf("[%s] Pull request milestone cleared: %s %s", repoLink, titleLink, senderLink) | ||
} | ||
|
||
return text, issueTitle | ||
} |
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
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.