-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added listtasks command to list open Phabricator tasks of a user
Added `listtasks` command to list open tasks for a Phabricator or Slack user. Usage: `listtasks <username>`
- Loading branch information
Showing
3 changed files
with
160 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
package core | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
|
||
"github.com/etcinit/gonduit/constants" | ||
"github.com/etcinit/gonduit/requests" | ||
"github.com/etcinit/phabulous/app/interfaces" | ||
"github.com/etcinit/phabulous/app/messages" | ||
) | ||
|
||
// TaskCommand lists your tasks | ||
type TaskCommand struct{} | ||
|
||
// GetUsage . | ||
func (c *TaskCommand) GetUsage() string { | ||
return "listtasks <username>" | ||
} | ||
|
||
// GetDescription . | ||
func (c *TaskCommand) GetDescription() string { | ||
return "Lists currently-open tasks for a given Phabricator user. If no username is provided, Slack username is used." | ||
} | ||
|
||
// GetMatchers . | ||
func (c *TaskCommand) GetMatchers() []string { | ||
return []string{ | ||
"^listtasks\\s*(.*)$", | ||
} | ||
} | ||
|
||
// GetIMMatchers . | ||
func (c *TaskCommand) GetIMMatchers() []string { | ||
return []string{ | ||
"^listtasks\\s*(.*)$", | ||
} | ||
} | ||
|
||
// GetMentionMatchers returns the channel mention matchers for this command. | ||
func (c *TaskCommand) GetMentionMatchers() []string { | ||
return []string{ | ||
"^listtasks\\s*(.*)$", | ||
} | ||
} | ||
|
||
// GetHandler -- returns a handler that mostly ignores what was | ||
// matched and just returns the user's list of open tasks. | ||
func (c *TaskCommand) GetHandler() interfaces.Handler { | ||
return func(s interfaces.Bot, m interfaces.Message, matches []string) { | ||
s.StartTyping(m.GetChannel()) | ||
|
||
conn, err := s.GetGonduit() | ||
if err != nil { | ||
s.Excuse(m, err) | ||
return | ||
} | ||
|
||
username := matches[1] | ||
if len(username) == 0 { | ||
username, err = s.GetUsername(m.GetUserID()) | ||
if err != nil || username == "" { | ||
s.Excuse(m, err) | ||
return | ||
} | ||
} | ||
nameres, err := conn.UserQuery( | ||
requests.UserQueryRequest{Usernames: []string{username}}, | ||
) | ||
if err != nil || nameres == nil || len(*nameres) == 0 { | ||
message := fmt.Sprintf(` | ||
Couldn't find Phabricator user for Slack username *@%s*. Typically | ||
this means your Slack username doesn't match your Phabricator | ||
username. | ||
`, | ||
username, | ||
) | ||
s.Post( | ||
m.GetChannel(), | ||
message, | ||
messages.IconDefault, | ||
true, | ||
) | ||
return | ||
} | ||
ownerPHID := (*nameres)[0].PHID | ||
|
||
res, err := conn.ManiphestQuery(requests.ManiphestQueryRequest{ | ||
OwnerPHIDs: []string{ownerPHID}, | ||
Status: constants.ManiphestTaskStatusOpen, | ||
Order: constants.ManiphestQueryOrderPriority, | ||
}) | ||
|
||
if err != nil { | ||
s.Excuse(m, err) | ||
return | ||
} | ||
|
||
if res == nil { | ||
s.Post( | ||
m.GetChannel(), | ||
fmt.Sprintf("<@%s> doesn't appear to have any open tasks", username), | ||
messages.IconDefault, | ||
true, | ||
) | ||
return | ||
} | ||
|
||
var buffer bytes.Buffer | ||
for _, value := range *res { | ||
buffer.WriteString( | ||
fmt.Sprintf("*<%s|T%s>* -- %v\n", value.URI, value.ID, value.Title), | ||
) | ||
} | ||
s.Post( | ||
m.GetChannel(), | ||
buffer.String(), | ||
messages.IconTasks, | ||
true, | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package core | ||
|
||
import ( | ||
"testing" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func Test_getUsage(t *testing.T) { | ||
task_command := TaskCommand{} | ||
assert.Equal(t, task_command.GetUsage(), "listtasks <username>", "they should be equal") | ||
} | ||
|
||
func Test_getDescriptionNil(t *testing.T) { | ||
task_command := TaskCommand{} | ||
assert.NotEmpty(t, task_command.GetDescription()) | ||
} | ||
|
||
func Test_getMatchersNil(t *testing.T) { | ||
task_command := TaskCommand{} | ||
assert.NotEmpty(t, task_command.GetMatchers()) | ||
assert.Contains(t, task_command.GetMatchers(), "^listtasks\\s*(.*)$") | ||
} | ||
|
||
func Test_getIMMatchersNil(t *testing.T) { | ||
task_command := TaskCommand{} | ||
assert.NotEmpty(t, task_command.GetIMMatchers()) | ||
} | ||
|
||
func Test_getMentionMatchersNil(t *testing.T) { | ||
task_command := TaskCommand{} | ||
assert.NotEmpty(t, task_command.GetMentionMatchers()) | ||
} | ||
|
||
func Test_getHandlerNil(t *testing.T) { | ||
task_command := TaskCommand{} | ||
assert.NotEmpty(t, task_command.GetHandler()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters