From 623a746612e2a64aec9c9fb0057c00ad3bbb4130 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matthias=20D=C3=B6tsch?= Date: Fri, 4 Aug 2023 12:43:51 +0200 Subject: [PATCH] #451 remove all needed approve reaction on revoked pullrequest --- bot/util/contains.go | 11 +++++++++++ bot/util/contains_test.go | 19 +++++++++++++++++++ command/pullrequest/pull_request.go | 18 +++++++++++++++++- command/pullrequest/pull_request_test.go | 15 +++++++++++---- 4 files changed, 58 insertions(+), 5 deletions(-) create mode 100644 bot/util/contains.go create mode 100644 bot/util/contains_test.go diff --git a/bot/util/contains.go b/bot/util/contains.go new file mode 100644 index 00000000..4645d563 --- /dev/null +++ b/bot/util/contains.go @@ -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 +} diff --git a/bot/util/contains_test.go b/bot/util/contains_test.go new file mode 100644 index 00000000..ef37c0ca --- /dev/null +++ b/bot/util/contains_test.go @@ -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)) +} diff --git a/command/pullrequest/pull_request.go b/command/pullrequest/pull_request.go index bed2a58c..c71b8985 100644 --- a/command/pullrequest/pull_request.go +++ b/command/pullrequest/pull_request.go @@ -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) @@ -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) diff --git a/command/pullrequest/pull_request_test.go b/command/pullrequest/pull_request_test.go index 7689da62..6167d653 100644 --- a/command/pullrequest/pull_request_test.go +++ b/command/pullrequest/pull_request_test.go @@ -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" @@ -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) @@ -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) @@ -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) @@ -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,