-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
112 lines (104 loc) · 2.95 KB
/
main.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package main
import (
"fmt"
"log"
"os"
"runtime"
"github.com/google/go-github/v32/github"
cli "github.com/urfave/cli/v2"
)
func main() {
app := &cli.App{}
app.Name = "notify-issues-to-slack"
app.Usage = "The CLI tool to notify Github issues and pull requests to Slack with color."
app.Version = fmt.Sprintf("%s (rev: %s/%s)", version, revision, runtime.Version())
app.UsageText = "notify-issues-to-slack -github-token=... -slack-webhook-url=... -query=... [-danger-filter=...] [-warning-filter=...] [-channel=...] [-text=...] [-username=...] [-icon-emoji=...] [-github-api-url=...]"
cli.AppHelpTemplate = fmt.Sprintf(`%s
SEE ALSO:
Please see https://github.com/shibayu36/notify-issues-to-slack for detailed usage.
`, cli.AppHelpTemplate)
app.Flags = []cli.Flag{
&cli.StringFlag{
Name: "github-token",
Usage: "Github token",
},
&cli.StringFlag{
Name: "slack-webhook-url",
Usage: "Slack webhook URL",
},
&cli.StringFlag{
Name: "query",
Usage: "Query to search Github issues",
},
&cli.StringFlag{
Name: "danger-filter",
Usage: "Colorize the issue's attachment danger. You can use Github search queries",
},
&cli.StringFlag{
Name: "warning-filter",
Usage: "Colorize the issue's attachment warning. You can use Github search queries",
},
&cli.StringFlag{
Name: "channel",
Usage: "Slack channel to be posted",
},
&cli.StringFlag{
Name: "text",
Usage: "text to post with issues",
},
&cli.StringFlag{
Name: "issue-text-format",
Usage: "Text format of each issues used for message attachment",
},
&cli.StringFlag{
Name: "username",
Usage: "Slack username to post",
},
&cli.StringFlag{
Name: "icon-emoji",
Usage: "Slack icon emoji to post",
},
&cli.StringFlag{
Name: "github-api-url",
Usage: "Github API base URL",
},
}
app.Action = func(c *cli.Context) error {
query := convertRelativeTimeQuery(c.String("query"))
gc := &githubClient{
apiURL: c.String("github-api-url"),
token: c.String("github-token"),
}
issues, err := gc.searchGithubIssues(query)
if err != nil {
return err
}
warningIssues := []*github.Issue{}
if wf := c.String("warning-filter"); wf != "" {
warningIssues, err = gc.searchGithubIssues(query + " " + convertRelativeTimeQuery(wf))
if err != nil {
return err
}
}
dangerIssues := []*github.Issue{}
if df := c.String("danger-filter"); df != "" {
dangerIssues, err = gc.searchGithubIssues(query + " " + convertRelativeTimeQuery(df))
if err != nil {
return err
}
}
sc := &slackClient{webhookURL: c.String("slack-webhook-url")}
sc.postIssuesToSlack(issues, warningIssues, dangerIssues, &slackPostOptions{
Text: c.String("text"),
IssueTextFormat: c.String("issue-text-format"),
Channel: c.String("channel"),
Username: c.String("username"),
IconEmoji: c.String("icon-emoji"),
})
return nil
}
err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
}