Skip to content

Commit

Permalink
allow granular selection of github notification method via destination
Browse files Browse the repository at this point in the history
Signed-off-by: Tony Li <[email protected]>
  • Loading branch information
tonglil committed Jun 8, 2024
1 parent 0802cd4 commit f81d5ce
Showing 1 changed file with 27 additions and 7 deletions.
34 changes: 27 additions & 7 deletions pkg/services/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@ import (
"github.com/argoproj/notifications-engine/pkg/util/text"
)

var (
gitSuffix = regexp.MustCompile(`\.git$`)
)
var gitSuffix = regexp.MustCompile(`\.git$`)

type GitHubOptions struct {
AppID interface{} `json:"appID"`
Expand Down Expand Up @@ -317,7 +315,7 @@ func fullNameByRepoURL(rawURL string) string {
return path
}

func (g gitHubService) Send(notification Notification, _ Destination) error {
func (g gitHubService) Send(notification Notification, dest Destination) error {
if notification.GitHub == nil {
return fmt.Errorf("config is empty")
}
Expand All @@ -326,7 +324,29 @@ func (g gitHubService) Send(notification Notification, _ Destination) error {
if len(u) < 2 {
return fmt.Errorf("GitHub.repoURL (%s) does not have a `/`", notification.GitHub.repoURL)
}
if notification.GitHub.Status != nil {

var (
sendStatus bool
sendDeployment bool
sendComment bool
)

switch dest.Recipient {
case "status":
sendStatus = true
case "deployment":
sendDeployment = true
case "comment":
sendComment = true
default:
// match previous behavior: send all notifications if a destination is
// not specified
sendStatus = true
sendDeployment = true
sendComment = true
}

if notification.GitHub.Status != nil && sendStatus {
// maximum is 140 characters
description := trunc(notification.Message, 140)
_, _, err := g.client.Repositories.CreateStatus(
Expand All @@ -346,7 +366,7 @@ func (g gitHubService) Send(notification Notification, _ Destination) error {
}
}

if notification.GitHub.Deployment != nil {
if notification.GitHub.Deployment != nil && sendDeployment {
// maximum is 140 characters
description := trunc(notification.Message, 140)
deployments, _, err := g.client.Repositories.ListDeployments(
Expand Down Expand Up @@ -406,7 +426,7 @@ func (g gitHubService) Send(notification Notification, _ Destination) error {
}
}

if notification.GitHub.PullRequestComment != nil {
if notification.GitHub.PullRequestComment != nil && sendComment {
// maximum is 65536 characters
body := trunc(notification.GitHub.PullRequestComment.Content, 65536)
comment := &github.IssueComment{
Expand Down

0 comments on commit f81d5ce

Please sign in to comment.