Skip to content

Commit

Permalink
Merge pull request #452 from innogames/remove_approve_reaction
Browse files Browse the repository at this point in the history
remove all needed approve reaction on revoked pullrequest
  • Loading branch information
brainexe authored Aug 4, 2023
2 parents ecee2bc + 623a746 commit bbceed1
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 5 deletions.
11 changes: 11 additions & 0 deletions bot/util/contains.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package util

func Contains[T comparable](slice []T, given T) bool {
for _, current := range slice {
if current == given {
return true
}
}

return false
}
19 changes: 19 additions & 0 deletions bot/util/contains_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package util

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestContains(t *testing.T) {
list := []string{"foo", "bar", "baz"}
assert.False(t, Contains(list, ""))
assert.False(t, Contains(list, "XXX"))
assert.True(t, Contains(list, "foo"))

list2 := []int{1, 2, 3}
assert.False(t, Contains(list2, -1))
assert.False(t, Contains(list2, 0))
assert.True(t, Contains(list2, 2))
}
18 changes: 17 additions & 1 deletion command/pullrequest/pull_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,9 @@ func (c command) setPRReactions(pr pullRequest, currentReactions reactionMap, me

hasApproval = true
} else {
c.removeReaction(currentReactions, c.cfg.Reactions.Approved, message)
for _, reaction := range c.getAllApprovedReactions() {
c.removeReaction(currentReactions, reaction, message)
}
}

c.processBuildStatus(pr, currentReactions, message)
Expand Down Expand Up @@ -262,6 +264,20 @@ func (c command) getApproveReactions(approvers []string) reactionMap {
return reactions
}

// we have to get rid of all possivle approve reactions in case someone revoked the approval
func (c *command) getAllApprovedReactions() []util.Reaction {
reactions := make([]util.Reaction, 0)
reactions = append(reactions, c.cfg.Reactions.Approved)

for _, reaction := range c.cfg.CustomApproveReaction {
if !util.Contains(reactions, reaction) {
reactions = append(reactions, reaction)
}
}

return reactions
}

func getPRLinkMessage(prw *pullRequestWatch) string {
if len(prw.PullRequest.Link) > 0 {
return fmt.Sprintf("\nYou can check it here: %s", prw.PullRequest.Link)
Expand Down
15 changes: 11 additions & 4 deletions command/pullrequest/pull_request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/innogames/slack-bot/v2/bot/config"
"github.com/innogames/slack-bot/v2/bot/matcher"
"github.com/innogames/slack-bot/v2/bot/msg"
"github.com/innogames/slack-bot/v2/bot/util"
"github.com/innogames/slack-bot/v2/client"
"github.com/innogames/slack-bot/v2/mocks"
"github.com/pkg/errors"
Expand Down Expand Up @@ -82,7 +83,7 @@ func TestPullRequest(t *testing.T) {
On("GetReactions", msgRef, slack.NewGetReactionsParameters()).Return(nil, nil)

mocks.AssertRemoveReaction(slackClient, "eyes", message)
mocks.AssertReaction(slackClient, "white_check_mark", message)
mocks.AssertReaction(slackClient, "project_approve", message)
mocks.AssertReaction(slackClient, "twisted_rightwards_arrows", message)

actual := commands.Run(message)
Expand All @@ -102,7 +103,7 @@ func TestPullRequest(t *testing.T) {
message.Text = "vcd.example.com/projects/foo/repos/bar/pull-requests/1337"

mocks.AssertRemoveReaction(slackClient, "eyes", message)
mocks.AssertRemoveReaction(slackClient, "white_check_mark", message)
mocks.AssertRemoveReaction(slackClient, "project_approve", message)
mocks.AssertReaction(slackClient, "x", message)

actual := commands.Run(message)
Expand All @@ -117,13 +118,13 @@ func TestPullRequest(t *testing.T) {
fetcher.err = nil
fetcher.pr = pullRequest{
Status: prStatusOpen,
Approvers: []string{"test"},
Approvers: []string{"user1"},
}
message.Text = "vcd.example.com/projects/foo/repos/bar/pull-requests/1337"

mocks.AssertRemoveReaction(slackClient, "eyed", message)
mocks.AssertRemoveReaction(slackClient, "x", message)
mocks.AssertReaction(slackClient, "white_check_mark", message)
mocks.AssertReaction(slackClient, "approve_backend", message)

actual := commands.Run(message)
assert.True(t, actual)
Expand Down Expand Up @@ -156,6 +157,12 @@ func initTest(slackClient client.SlackClient) (bot.Commands, *testFetcher) {
base := bot.BaseCommand{SlackClient: slackClient}

cfg := config.DefaultConfig
cfg.PullRequest.Reactions.Approved = "project_approve"
cfg.PullRequest.CustomApproveReaction = map[string]util.Reaction{
"user1": "approve_backend",
"user2": "approve_backend",
"user3": "approve_frontend",
}

cmd := &command{
base,
Expand Down

0 comments on commit bbceed1

Please sign in to comment.