Skip to content

Commit

Permalink
queue/pr overview: add option to pin the recent message
Browse files Browse the repository at this point in the history
  • Loading branch information
brainexe committed Jul 8, 2024
1 parent 1eab179 commit 422245d
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 23 deletions.
18 changes: 8 additions & 10 deletions bot/matcher/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,11 @@ func (m optionMatcher) Match(message msg.Message) (Runner, Result) {
return nil, nil
}

fmt.Println(message.Text)
fmt.Println(m.command)
fmt.Println(optionsString)
options := parseOptions(optionsString)

for option := range options {
if !slices.Contains(m.whiteList, option) {
return func(match Result, message msg.Message) {
return func(_ Result, message msg.Message) {
m.slackClient.AddReaction("❌", message)
m.slackClient.ReplyError(
message,
Expand All @@ -59,18 +56,19 @@ func (m optionMatcher) Match(message msg.Message) (Runner, Result) {
return m.run, options
}

// parseOptions parses a string like "option1=value1 --option2='value 2' option3="value 3""
func parseOptions(given string) Result {
re := regexp.MustCompile(`(\w+)=('([^']*)'|"([^"]*)"|(\S+))|(\w+)`)
re := regexp.MustCompile(`(--)?([\w\-]+)=('([^']*)'|"([^"]*)"|(\S+))|(\w+)`)
matches := re.FindAllStringSubmatch(given, -1)

options := make(Result)
for _, match := range matches {
if match[1] != "" {
key := match[1]
value := match[3] + match[4] + match[5]
if match[2] != "" {
key := match[2]
value := match[4] + match[5] + match[6]
options[key] = value
} else if match[6] != "" {
key := match[6]
} else if match[7] != "" {
key := match[7]
options[key] = "true"
}
}
Expand Down
4 changes: 4 additions & 0 deletions bot/matcher/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ func TestOptions(t *testing.T) {
input: `foo=bar test=true`,
expected: Result{"foo": "bar", "test": "true"},
},
{
input: `--foo=bar --test=true`,
expected: Result{"foo": "bar", "test": "true"},
},
}

for _, test := range tests {
Expand Down
35 changes: 24 additions & 11 deletions command/queue/list_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ import (
"github.com/slack-go/slack"
)

const processingReaction = "eyes"
const (
processingReaction = "eyes"
defaultName = "queued commands"
)

type listCommand struct {
bot.BaseCommand
Expand All @@ -32,39 +35,44 @@ func NewListCommand(base bot.BaseCommand) bot.Command {
func (c *listCommand) GetMatcher() matcher.Matcher {
return matcher.NewGroupMatcher(
matcher.NewTextMatcher("list queue", c.listAll),
matcher.NewOptionMatcher("list queue in channel", []string{"pin"}, c.listChannel, c.SlackClient),
matcher.NewOptionMatcher("list queue in channel", []string{"pin", "hide-empty", "element-name"}, c.listChannel, c.SlackClient),
)
}

// ListAll shows a list of all queued commands
func (c *listCommand) listAll(match matcher.Result, message msg.Message) {
c.sendList(message, func(_ msg.Message) bool {
c.sendList(message, match, func(_ msg.Message) bool {
return true
})
}

// ListChannel shows a list of all queued commands within the current channel
func (c *listCommand) listChannel(match matcher.Result, message msg.Message) {
c.sendList(message, func(queuedEvent msg.Message) bool {
c.sendList(message, match, func(queuedEvent msg.Message) bool {
return message.GetChannel() == queuedEvent.GetChannel()
})

if match.Has("pin") {
err := c.PinMessage(message)
c.ReplyError(message, err)
}
}

// format a block-based message with all matching commands
func (c *listCommand) sendList(message msg.Message, filter filterFunc) {
func (c *listCommand) sendList(message msg.Message, options matcher.Result, filter filterFunc) {
// add :eyes: temporary because loading the list might take some seconds
c.AddReaction(processingReaction, message)
defer c.RemoveReaction(processingReaction, message)

count, queueBlocks := c.getQueueAsBlocks(message, filter)

if count == 0 && options.Has("hide-empty") {
// hide empty list
return
}

elementName := defaultName
if options.Has("element-name") {
elementName = options.GetString("element-name")
}

blocks := []slack.Block{
client.GetTextBlock(fmt.Sprintf("*%d queued commands*", count)),
client.GetTextBlock(fmt.Sprintf("*%d %s*", count, elementName)),
}
blocks = append(blocks, queueBlocks...)

Expand All @@ -75,6 +83,11 @@ func (c *listCommand) sendList(message msg.Message, filter filterFunc) {
}

c.SendBlockMessage(message, blocks, msgOptions...)

if options.Has("pin") {
err := c.PinMessage(message)
c.ReplyError(message, err)
}
}

// loads all matching queue entries and format them into slack.Block
Expand Down
17 changes: 15 additions & 2 deletions command/queue/queue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,11 @@ func TestQueue(t *testing.T) {
assert.True(t, actual)

// list queue for other channel
message.Text = "list queue in channel"
message.Text = "list queue in channel element-name=pull-requests"
message.Channel = "C1212121"
mocks.AssertReaction(slackClient, processingReaction, message)
mocks.AssertRemoveReaction(slackClient, processingReaction, message)
mocks.AssertContainsSlackBlocks(t, slackClient, message, client.GetTextBlock("*0 queued commands*"))
mocks.AssertContainsSlackBlocks(t, slackClient, message, client.GetTextBlock("*0 pull-requests*"))

actual = command.Run(message)
assert.True(t, actual)
Expand Down Expand Up @@ -162,6 +162,19 @@ func TestQueue(t *testing.T) {
actual := command.Run(message)
assert.True(t, actual)
})

t.Run("Test refresh queue command", func(t *testing.T) {
message.Text = "list queue in channel hide-empty=true pin=true"
message.UpdatedMessage = true

assert.Empty(t, client.InternalMessages)

mocks.AssertReaction(slackClient, processingReaction, message)
mocks.AssertRemoveReaction(slackClient, processingReaction, message)

actual := command.Run(message)
assert.True(t, actual)
})
}

func TestFallbackQueue(t *testing.T) {
Expand Down

0 comments on commit 422245d

Please sign in to comment.