Skip to content
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

Webhook for Pull Request approval/rejection #5027

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
17f9a87
started work on allowing pr approval/rejection... Untested though
adelowo Oct 6, 2018
1d478c9
whoops
adelowo Oct 6, 2018
b5564d6
fix pr#Issue being nil
adelowo Oct 6, 2018
e92bef5
updated hook name
adelowo Oct 6, 2018
b549837
properly format webhook for slack
adelowo Oct 6, 2018
d2cfc93
remove redundant newline
adelowo Oct 6, 2018
8969baf
remove redundant comment
adelowo Oct 6, 2018
5ed0dbc
fix make lint
adelowo Oct 6, 2018
59f4f5a
fix build
adelowo Oct 6, 2018
1923bd9
add webhook formatting for discord
adelowo Oct 6, 2018
50f4846
add webhook formatting for dingtalk
adelowo Oct 6, 2018
ce77646
try to fix naming
adelowo Oct 6, 2018
d517038
Merge branch 'master' of https://github.com/go-gitea/gitea into webho…
adelowo Oct 6, 2018
73fc34e
Merge remote-tracking branch 'origin/master' into webhook_for_pr_appr…
adelowo Oct 9, 2018
d1c234c
add error handling
adelowo Oct 9, 2018
67259f0
fix style guide
adelowo Oct 9, 2018
0770f77
add support for pull request comment webhooks
adelowo Oct 23, 2018
745c619
Merge branch 'master' of https://github.com/go-gitea/gitea into webho…
adelowo Oct 23, 2018
0f698bb
fix naming
adelowo Oct 23, 2018
d2577fc
remove redundant comment and update translations
adelowo Oct 24, 2018
50e0ae1
fix merge conflicts
adelowo Dec 11, 2018
c03bd6f
Merge branch 'master' into webhook_for_pr_approval_rejection
techknowlogick Dec 27, 2018
25f4aac
Merge branch 'master' into webhook_for_pr_approval_rejection
techknowlogick Dec 27, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
add webhook formatting for discord
  • Loading branch information
adelowo committed Oct 6, 2018
commit 1923bd9093ecdc498b99a8e1b0bee4e23900088f
46 changes: 46 additions & 0 deletions models/webhook_discord.go
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,40 @@ func getDiscordPullRequestPayload(p *api.PullRequestPayload, meta *DiscordMeta)
}, nil
}

func getDiscordPullRequestApprovalPayload(p *api.PullRequestPayload, meta *DiscordMeta, event HookEventType) (*DiscordPayload, error) {
var text, title string
var color int
switch p.Action {
case api.HookIssueSynchronized:
action, err := parseActionText(event)
if err != nil {
return nil, err
}

title = fmt.Sprintf("[%s] Review on pull request %s: #%d %s", p.Repository.FullName, action, p.Index, p.PullRequest.Title)
text = p.PullRequest.Body
color = warnColor
}

return &DiscordPayload{
Username: meta.Username,
AvatarURL: meta.IconURL,
Embeds: []DiscordEmbed{
{
Title: title,
Description: text,
URL: p.PullRequest.HTMLURL,
Color: color,
Author: DiscordEmbedAuthor{
Name: p.Sender.UserName,
URL: setting.AppURL + p.Sender.UserName,
IconURL: p.Sender.AvatarURL,
},
},
},
}, nil
}

func getDiscordRepositoryPayload(p *api.RepositoryPayload, meta *DiscordMeta) (*DiscordPayload, error) {
var title, url string
var color int
Expand Down Expand Up @@ -492,6 +526,8 @@ func GetDiscordPayload(p api.Payloader, event HookEventType, meta string) (*Disc
return getDiscordPushPayload(p.(*api.PushPayload), discord)
case HookEventPullRequest:
return getDiscordPullRequestPayload(p.(*api.PullRequestPayload), discord)
case HookEventPullRequestRejected, HookEventPullRequestApproved:
return getDiscordPullRequestApprovalPayload(p.(*api.PullRequestPayload), discord, event)
case HookEventRepository:
return getDiscordRepositoryPayload(p.(*api.RepositoryPayload), discord)
case HookEventRelease:
Expand All @@ -500,3 +536,13 @@ func GetDiscordPayload(p api.Payloader, event HookEventType, meta string) (*Disc

return s, nil
}

func parseActionText(event HookEventType) (string, error) {
if event == HookEventPullRequestApproved {
return "approved", nil
} else if event == HookEventPullRequestRejected {
return "rejected", nil
}

return "", errors.New("unknown event type")
}
10 changes: 3 additions & 7 deletions models/webhook_slack.go
Original file line number Diff line number Diff line change
Expand Up @@ -335,13 +335,9 @@ func getSlackPullRequestApprovalPayload(p *api.PullRequestPayload, slack *SlackM
var text, title, attachmentText string
switch p.Action {
case api.HookIssueSynchronized:
var action string
if event == HookEventPullRequestRejected {
action = "rejected"
} else if event == HookEventPullRequestApproved {
action = "approved"
} else {
return nil, errors.New("unknown event type")
action, err := parseActionText(event)
if err != nil {
return nil, err
}

text = fmt.Sprintf("[%s] Review on pull request %s : %s by %s", p.Repository.FullName, action, titleLink, senderLink)
Expand Down