-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmain.go
109 lines (94 loc) · 2.83 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
package main
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"strings"
"text/template"
"github.com/sj14/review-bot/hoster/github"
"github.com/sj14/review-bot/hoster/gitlab"
"github.com/sj14/review-bot/slackermost"
)
func main() {
var (
host = flag.String("host", "", "host address (e.g. github.com, gitlab.com or self-hosted gitlab url)")
token = flag.String("token", "", "host API token")
repo = flag.String("repo", "", "repository (format: 'owner/repo'), or project id (only gitlab)")
reviewersPath = flag.String("reviewers", "examples/reviewers.json", "path to the reviewers file")
templatePath = flag.String("template", "", "path to the template file")
webhook = flag.String("webhook", "", "slack/mattermost webhook URL")
channelOrUser = flag.String("channel", "", "mattermost channel (e.g. MyChannel) or user (e.g. @AnyUser)")
)
flag.Parse()
if *host == "" {
log.Fatalln("missing host")
}
if *repo == "" {
log.Fatalln("missing repository")
}
reviewers := loadReviewers(*reviewersPath)
var tmpl *template.Template
if *templatePath != "" {
tmpl = loadTemplate(*templatePath)
} else if *host == "github.com" {
tmpl = github.DefaultTemplate()
} else {
tmpl = gitlab.DefaultTemplate()
}
var reminder string
if *host == "github.com" {
ownerRespo := strings.SplitN(*repo, "/", 2)
if len(ownerRespo) != 2 {
log.Fatalln("wrong repo format (use 'owner/repo')")
}
repo, reminders := github.AggregateReminder(*token, ownerRespo[0], ownerRespo[1], reviewers)
if len(reminders) == 0 {
// prevent from sending the header only
return
}
reminder = github.ExecTemplate(tmpl, repo, reminders)
} else {
project, reminders := gitlab.AggregateReminder(*host, *token, *repo, reviewers)
if len(reminders) == 0 {
// prevent from sending the header only
return
}
reminder = gitlab.ExecTemplate(tmpl, project, reminders)
}
if reminder == "" {
return
}
fmt.Println(reminder)
if *webhook != "" {
if err := slackermost.Send(*channelOrUser, reminder, *webhook); err != nil {
log.Fatalf("failed sending slackermost message: %v", err)
}
}
}
func loadTemplate(path string) *template.Template {
t, err := template.ParseFiles(path)
if err != nil {
log.Fatalf("failed to read template file: %v", err)
}
return t
}
// load reviewers from given json file
// formatting:
// "GitLab UserID":"Mattermost Username"
// e.g. {"3":"@john.doe","5":"@max"}
// or
// 'Github LoginName': 'Mattermost Username'
// e.g. {"sj14":"@simon","john":"@john"}
func loadReviewers(path string) map[string]string {
b, err := ioutil.ReadFile(path)
if err != nil {
log.Fatalf("failed to read reviewers file: %v", err)
}
reviewers := map[string]string{}
if err := json.Unmarshal(b, &reviewers); err != nil {
log.Fatalf("failed to unmarshal reviewers: %v", err)
}
return reviewers
}