-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
117 lines (96 loc) · 2.53 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
113
114
115
116
117
package main
import (
"flag"
"fmt"
"log"
"os"
"strings"
"github.com/pkg/errors"
"github.com/spf13/viper"
)
var (
repo string
fromCommit, toCommit string
configPath string
)
func init() {
defaultConfigPath := fmt.Sprintf("%s/.prlog.yaml", os.Getenv("HOME"))
flag.StringVar(&repo, "repo", "", "'organization/name' of repository")
flag.StringVar(&fromCommit, "from", "", "first commit of the changelog")
flag.StringVar(&toCommit, "to", "", "last commit of the changelog")
flag.StringVar(&configPath, "config", defaultConfigPath, "path to the config file")
viper.SetConfigType("yaml")
}
func main() {
flag.Parse()
viper.SetDefault("git-token", os.Getenv("PRLOG_GIT_TOKEN"))
viper.SetDefault("local-only", false)
if configPath != "" {
viper.SetConfigFile(configPath)
}
// Read configuration file
if err := viper.ReadInConfig(); err != nil {
log.Fatalf("Failed to read configuration: %v", err)
}
if repo == "" || fromCommit == "" || toCommit == "" {
fmt.Println("Parameters 'repo', 'from' and 'to' are required:")
flag.PrintDefaults()
os.Exit(1)
}
gitPRs, err := mergedPrs()
if err != nil {
fmt.Printf("Failed to list merged PRs from git history: %v\n", err)
os.Exit(1)
}
ghPRs, err := githubPRs(repo)
if err != nil {
fmt.Printf("Failed to get PR details from GitHub: %s\n", err)
os.Exit(1)
}
prlabels := viper.GetStringMapStringSlice("pr-labels")
prGroups := make(map[string][]PullRequest)
for _, gitPRs := range gitPRs {
pr, err := pullRequestDesc(gitPRs, ghPRs)
if err != nil {
continue
}
assignToGroup(pr, prlabels, prGroups)
}
for grName, prs := range prGroups {
printPRs(grName, prs)
}
}
func printPRs(title string, prs []PullRequest) {
if len(prs) == 0 {
return
}
fmt.Printf("%s:\n", strings.Title(title))
for _, pr := range prs {
fmt.Printf(" - %s (#%d) [%s]\n", pr.Title, pr.Number, pr.Author.Login)
}
fmt.Println()
}
func pullRequestDesc(no int, all []PullRequest) (PullRequest, error) {
for _, pr := range all {
if pr.Number == no {
return pr, nil
}
}
return PullRequest{}, errors.Errorf("failed to find pull request #%d", no)
}
func assignToGroup(pr PullRequest, prlabels map[string][]string, prGroups map[string][]PullRequest) {
assigned := false
for _, lbl := range pr.LabelNames() {
for grName, grLbls := range prlabels {
for _, grLbl := range grLbls {
if grLbl == lbl {
prGroups[grName] = append(prGroups[grName], pr)
assigned = true
}
}
}
}
if !assigned {
prGroups["other"] = append(prGroups["other"], pr)
}
}