-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
82 lines (68 loc) · 1.93 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
package main
import (
"context"
"flag"
"fmt"
"os"
"github.com/google/go-github/github"
"github.com/nlopes/slack"
"github.com/xeonx/timeago"
"golang.org/x/oauth2"
)
// Main function
func main() {
// Required
githubToken := GetEnv("GITHUB_TOKEN", "")
slackToken := GetEnv("SLACK_TOKEN", "")
// Optional
githubSearchQuery := GetEnv("GITHUB_QUERY", "is:open is:pr")
slackChannel := GetEnv("SLACK_CHANNEL", "engineering")
slackIconEmoji := GetEnv("SLACK_ICON_EMOJI", ":tada:")
slackUsername := GetEnv("SLACK_USERNAME", "GitHub Reminder")
flag.Parse()
text := ""
prs := GetPullRequests(githubToken, githubSearchQuery)
if len(prs) > 0 {
for _, pr := range prs {
ta := timeago.English.Format(*pr.CreatedAt)
text = text + "— " + pr.GetTitle()
text = text + " (" + ta + ") "
text = text + " [" + pr.GetHTMLURL() + "] "
text = text + "\n"
}
SendToSlack(slackToken, slackChannel, text, slackIconEmoji, slackUsername)
}
}
// Get the pull requests list
func GetPullRequests(token string, query string) []github.Issue {
ctx := context.Background()
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: token},
)
tc := oauth2.NewClient(ctx, ts)
client := github.NewClient(tc)
options := &github.SearchOptions{}
result, _, _ := client.Search.Issues(ctx, query, options)
return result.Issues
}
// Send text to a Slack chan
func SendToSlack(token string, channel string, text string, iconEmoji string, username string) {
api := slack.New(token)
params := slack.PostMessageParameters{}
params.IconEmoji = iconEmoji
params.Username = username
channelID, timestamp, err := api.PostMessage(channel, text, params)
if err != nil {
fmt.Printf("%s\n", err)
return
}
fmt.Printf("Message successfully sent to channel %s at %s", channelID, timestamp)
}
// Get environment var with a fallback
func GetEnv(key string, fallback string) string {
value := os.Getenv(key)
if len(value) == 0 {
return fallback
}
return value
}