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

Add ephemeral message to PR request #29

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
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
54 changes: 51 additions & 3 deletions notifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,12 @@ func (n *SlackNotifier) ChangesRequested(c context.Context, repo *github.Reposit
func (n *SlackNotifier) ReviewRequested(c context.Context, repo *github.Repository, pr *github.PullRequest, reviewer *github.User) error {
text := fmt.Sprintf(
"%s you have been asked by %s to review %s",
buildLinkToUser(reviewer), buildUserName(pr.User),
buildLinkToUser(reviewer),
buildUserName(pr.User),
prLink(pr.HTMLURL, repo, pr),
)

if err := n.notifyChannel(c, notificationChannel, text); err != nil {
if err := n.notifyWithEphemeral(c, notificationChannel, text, pr.User); err != nil {
return err
}

Expand All @@ -82,7 +83,7 @@ func (n *SlackNotifier) ReviewRequested(c context.Context, repo *github.Reposito
}

presenceText := fmt.Sprintf(
"%s seems away and might not able to review %s",
"%s may be away and might not able to review %s",
buildLinkToUser(reviewer),
prLink(pr.HTMLURL, repo, pr),
)
Expand Down Expand Up @@ -125,6 +126,53 @@ func (n *SlackNotifier) notifyUser(c context.Context, userID, text string) error
return n.notifyChannel(c, channel.ID, text)
}

// This function is designed to be called when someone is assigned to review a PR to
// give them the option saying they are looking at it or to ask the requester reassign it.
// This is done via a second ephemeral message that's sent right after the main message.
func (n *SlackNotifier) notifyWithEphemeral(c context.Context, channel, text string, user *github.User) error {
slackUser := findSlackUser(user)
params := slackapi.NewPostMessageParameters()
params.AsUser = true
params.EscapeText = false

var mainMsg, confirmationMsg slackapi.Blocks

// Main message
textBlock := slackapi.NewSectionBlock(
slackapi.NewTextBlockObject(slackapi.MarkdownType, text, false, false),
nil,
nil,
)

// Ephemeral message
buttonBlock := slackapi.NewActionBlock(
"Let me know",
slackapi.NewButtonBlockElement("", "looking", slackapi.NewTextBlockObject("plain_text", "Looking", false, false)),
slackapi.NewButtonBlockElement("", "not_now", slackapi.NewTextBlockObject("plain_text", "Sorry, please reassign", false, false)),
)

mainMsg.BlockSet = append(mainMsg.BlockSet, textBlock)
confirmationMsg.BlockSet = append(confirmationMsg.BlockSet, buttonBlock)

// Send the main message requesting PR review
_, _, err := n.client.PostMessageContext(
c,
channel,
slackapi.MsgOptionBlocks(mainMsg.BlockSet...),
slackapi.MsgOptionPostMessageParameters(params),
)
if err != nil {
fmt.Println(err)
}

_, err = n.client.PostEphemeralContext(c, channel, slackUser.ID, slackapi.MsgOptionBlocks(confirmationMsg.BlockSet...))
if err != nil {
fmt.Println(err)
}

return nil
}

func (n *SlackNotifier) notifyChannel(c context.Context, channel, text string) error {
l := ctx.Logger(c).With("at", "slack.ping-user")

Expand Down