/
issues.go
163 lines (139 loc) · 3.83 KB
/
issues.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package cmd
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
import (
"fmt"
"os"
"regexp"
"sort"
"strings"
"sync"
"time"
"github.com/rayburgemeestre/jirahours/util"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"gopkg.in/src-d/go-git.v4"
"gopkg.in/src-d/go-git.v4/plumbing/object"
)
type Entry struct {
t time.Time
jirakey string
message string
repo string
}
type EntrySet struct {
entries []Entry
repo string
}
var (
wg sync.WaitGroup
channel chan EntrySet
dates string
output string
)
func init() {
issuesCmd.Flags().StringVarP(&dates, "file", "f", "dates.txt", "file to read dates from (e.g. dates.txt)")
issuesCmd.Flags().StringVarP(&output, "out", "o", "issues.txt", "file to write commit entries to (e.g. issues.txt)")
rootCmd.AddCommand(issuesCmd)
}
var issuesCmd = &cobra.Command{
Use: "issues",
Short: "Read in a dates file and gather all relevant git commit messages for the min and max date found in this file.",
Long: `This will write the result to an output file so it can be manually edited before proceding to the generate step.`,
Run: func(cmd *cobra.Command, args []string) {
min, max := util.GetMinMaxDatefile(dates)
repos := viper.GetStringSlice("repositories")
fmt.Println("Reading", len(repos), "repositories.")
var issuesAll []Entry
channel = make(chan EntrySet)
wg = sync.WaitGroup{}
seen := map[string]bool{}
wg.Add(1)
go func() {
for range repos {
msg1 := <-channel
fmt.Println("Received", len(msg1.entries), "from repo", msg1.repo)
if len(msg1.entries) > 0 {
for _, m := range msg1.entries {
if _, seen := seen[m.message]; seen {
continue
}
seen[m.message] = true
issuesAll = append(issuesAll, m)
}
}
}
wg.Done()
}()
for _, path := range repos {
wg.Add(1)
go process(path, min, max)
}
wg.Wait()
sort.Slice(issuesAll, func(i, j int) bool {
return issuesAll[i].t.Before(issuesAll[j].t.Local())
})
{
f, err := os.Create(output)
util.CheckIfError(err)
defer f.Close()
for _, issue := range issuesAll {
_, err := f.WriteString(fmt.Sprintln(issue.t.String(), "***", issue.jirakey, "***", issue.message, "***", issue.repo))
util.CheckIfError(err)
}
f.Sync()
}
fmt.Println("Generated", output)
},
}
func process(path string, from time.Time, until time.Time) {
r, err := git.PlainOpen(path)
if err != nil {
fmt.Println("Error with repository:", path)
}
util.CheckIfError(err)
ref, err := r.Head()
util.CheckIfError(err)
cIter, err := r.Log(&git.LogOptions{From: ref.Hash()})
util.CheckIfError(err)
var issues []Entry
chunks := strings.Split(path, "/")
reponame := chunks[len(chunks)-1]
u := viper.GetString("regexes.user")
u = strings.TrimRight(u, "\n\r \t")
reUser := regexp.MustCompile(u)
c := viper.GetString("regexes.commits")
c = strings.TrimRight(c, "\n\r \t")
reCommit := regexp.MustCompile(c)
err = cIter.ForEach(func(c *object.Commit) error {
if reUser.MatchString(c.Author.Name) {
if c.Author.When.After(from) && c.Author.When.Before(until) {
commitMessageLines := strings.Split(c.Message, "\n")
for _, line := range commitMessageLines {
result := make(map[string]string)
match := reCommit.FindStringSubmatch(line)
if len(match) <= 2 {
continue
}
for i, name := range reCommit.SubexpNames() {
if i != 0 && name != "" {
result[name] = match[i]
}
}
e := Entry{
t: c.Author.When.Local(),
jirakey: result["key"],
message: result["message"],
repo: reponame,
}
issues = append(issues, e)
}
}
}
return nil
})
util.CheckIfError(err)
channel <- EntrySet{issues, reponame}
wg.Done()
}