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

remove all needed approve reaction on revoked pullrequest #452

Merged
merged 1 commit into from
Aug 4, 2023
Merged
Show file tree
Hide file tree
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
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"

Check failure on line 6 in bot/util/contains_test.go

View workflow job for this annotation

GitHub Actions / Lint

import 'github.com/stretchr/testify/assert' is not allowed from list 'Main' (depguard)
)

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 @@
"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"

Check failure on line 11 in command/pullrequest/pull_request_test.go

View workflow job for this annotation

GitHub Actions / Lint

import 'github.com/innogames/slack-bot/v2/bot/util' is not allowed from list 'Main' (depguard)
"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 @@
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 @@
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 @@
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 @@
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
Loading