-
Notifications
You must be signed in to change notification settings - Fork 0
/
command_issue.go
94 lines (74 loc) · 1.92 KB
/
command_issue.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package github
import (
"context"
"fmt"
"strings"
"time"
"github.com/google/go-github/github"
"github.com/seabird-chat/seabird-go/pb"
)
func parseTag(line string) (string, string, bool) {
line = strings.TrimPrefix(line, "#")
split := strings.SplitN(line, " ", 2)
if len(split) != 2 {
// TODO: useful error here
return "", "", false
}
tag := strings.TrimSpace(split[0])
line = strings.TrimSpace(split[1])
return tag, line, true
}
func parseUser(line string) (string, string, bool) {
line = strings.TrimPrefix(line, "@")
split := strings.SplitN(line, " ", 2)
if len(split) != 2 {
// TODO: useful error here
return "", "", false
}
user := strings.TrimSpace(split[0])
line = strings.TrimSpace(split[1])
return user, line, true
}
func (c *Client) issueCallback(source *pb.ChannelSource, cmd *pb.CommandEvent) {
arg := strings.TrimSpace(cmd.Arg)
var ok bool
tag := "default"
user := ""
if strings.HasPrefix(arg, "#") {
if tag, arg, ok = parseTag(arg); !ok {
_ = c.replyf(source, "Failed to parse repo tag")
return
}
}
if strings.HasPrefix(arg, "@") {
if user, arg, ok = parseUser(arg); !ok {
_ = c.replyf(source, "Failed to parse user")
return
}
}
target, ok := c.repos[tag]
if !ok {
_ = c.replyf(source, "Unknown repo for tag %s", tag)
return
}
body := fmt.Sprintf("Filed by %s in %s", source.User.DisplayName, source.ChannelId)
req := &github.IssueRequest{
Title: &arg,
Body: &body,
}
if user != "" {
req.Assignee = &user
}
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
defer cancel()
issue, _, err := c.api.Issues.Create(ctx, target.Owner, target.Name, req)
if err != nil {
_ = c.replyf(source, "Got error while creating issue: %s", err)
return
}
if issue.Number == nil || issue.URL == nil {
_ = c.replyf(source, "Got invalid issue response")
return
}
_ = c.replyf(source, "Created issue #%d: %s", *issue.Number, *issue.HTMLURL)
}