-
Notifications
You must be signed in to change notification settings - Fork 20
/
entry.go
280 lines (253 loc) · 6.73 KB
/
entry.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package changelog
import (
"fmt"
"io/ioutil"
"path/filepath"
"sort"
"sync"
"time"
"golang.org/x/sync/errgroup"
"github.com/go-git/go-billy/v5/memfs"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/storage/memory"
)
var TypeValues = []string{
"enhancement",
"improvement",
"feature",
"bug",
"note",
"new-resource",
"new-datasource",
"new-ephemeral",
"new-function",
"deprecation",
"breaking-change",
}
type Entry struct {
Issue string
Body string
Date time.Time
Hash string
}
// EntryList provides thread-safe operations on a list of Entry values
type EntryList struct {
mu sync.RWMutex
es []*Entry
}
type EntryErrorCode string
const (
EntryErrorNotFound EntryErrorCode = "NOT_FOUND"
EntryErrorUnknownTypes EntryErrorCode = "UNKNOWN_TYPES"
)
type EntryValidationError struct {
message string
Code EntryErrorCode
Details map[string]interface{}
}
func (e *EntryValidationError) Error() string {
return e.message
}
// Validates that an Entry body contains properly formatted changelog notes
func (e *Entry) Validate() *EntryValidationError {
notes := NotesFromEntry(*e)
if len(notes) < 1 {
return &EntryValidationError{
message: fmt.Sprintf("no changelog entry found in: %s", string(e.Body)),
Code: EntryErrorNotFound,
}
}
var unknownTypes []string
for _, note := range notes {
if !TypeValid(note.Type) {
unknownTypes = append(unknownTypes, note.Type)
}
}
if len(unknownTypes) > 0 {
return &EntryValidationError{
message: fmt.Sprintf("unknown changelog types %v: please use only the configured changelog entry types: %v", unknownTypes, TypeValues),
Code: EntryErrorUnknownTypes,
Details: map[string]interface{}{
"unknownTypes": unknownTypes,
},
}
}
return nil
}
// NewEntryList returns an EntryList with capacity c
func NewEntryList(c int) *EntryList {
return &EntryList{
es: make([]*Entry, 0, c),
}
}
// Append appends entries to the EntryList
func (el *EntryList) Append(entries ...*Entry) {
el.mu.Lock()
defer el.mu.Unlock()
el.es = append(el.es, entries...)
}
// Get returns the Entry at index i
func (el *EntryList) Get(i int) *Entry {
el.mu.RLock()
defer el.mu.RUnlock()
if i >= len(el.es) || i < 0 {
return nil
}
return el.es[i]
}
// Set sets the Entry at index i. The list will be resized if i is larger than
// the current list capacity.
func (el *EntryList) Set(i int, e *Entry) {
if i < 0 {
panic("invalid slice index")
}
el.mu.Lock()
defer el.mu.Unlock()
if i > (cap(el.es) - 1) {
// resize the slice
newEntries := make([]*Entry, i)
copy(newEntries, el.es)
el.es = newEntries
}
el.es[i] = e
}
// Len returns the number of items in the EntryList
func (el *EntryList) Len() int {
el.mu.RLock()
defer el.mu.RUnlock()
return len(el.es)
}
// SortByIssue does an in-place sort of the entries by their issue number.
func (el *EntryList) SortByIssue() {
el.mu.Lock()
defer el.mu.Unlock()
sort.Slice(el.es, func(i, j int) bool {
return el.es[i].Issue < el.es[j].Issue
})
}
type changelog struct {
content []byte
hash string
date time.Time
}
// Diff returns the slice of Entry values that represent the difference of
// entries in the dir directory within repo from ref1 revision to ref2 revision.
// ref1 and ref2 should be valid git refs as strings and dir should be a valid
// directory path in the repository.
//
// The function calculates the diff by first checking out ref2 and collecting
// the set of all entries in dir. It then checks out ref1 and subtracts the
// entries found in dir. The resulting set of entries is then filtered to
// exclude any entries that came before the commit date of ref1.
//
// Along the way, if any git or filesystem interactions fail, an error is returned.
func Diff(repo, ref1, ref2, dir string) (*EntryList, error) {
r, err := git.Clone(memory.NewStorage(), memfs.New(), &git.CloneOptions{
URL: repo,
})
if err != nil {
return nil, err
}
rev2, err := r.ResolveRevision(plumbing.Revision(ref2))
if err != nil {
return nil, fmt.Errorf("could not resolve revision %s: %w", ref2, err)
}
var rev1 *plumbing.Hash
if ref1 != "-" {
rev1, err = r.ResolveRevision(plumbing.Revision(ref1))
if err != nil {
return nil, fmt.Errorf("could not resolve revision %s: %w", ref1, err)
}
}
wt, err := r.Worktree()
if err != nil {
return nil, err
}
if err := wt.Checkout(&git.CheckoutOptions{
Hash: *rev2,
Force: true,
}); err != nil {
return nil, fmt.Errorf("could not checkout repository at %s: %w", ref2, err)
}
entriesAfterFI, err := wt.Filesystem.ReadDir(dir)
if err != nil {
return nil, fmt.Errorf("could not read repository directory %s: %w", dir, err)
}
// a set of all entries at rev2 (this release); the set of entries at ref1
// will then be subtracted from it to arrive at a set of 'candidate' entries.
entryCandidates := make(map[string]bool, len(entriesAfterFI))
for _, i := range entriesAfterFI {
entryCandidates[i.Name()] = true
}
if rev1 != nil {
err = wt.Checkout(&git.CheckoutOptions{
Hash: *rev1,
Force: true,
})
if err != nil {
return nil, err
}
entriesBeforeFI, err := wt.Filesystem.ReadDir(dir)
if err != nil {
return nil, fmt.Errorf("could not read repository directory %s: %w", dir, err)
}
for _, i := range entriesBeforeFI {
delete(entryCandidates, i.Name())
}
// checkout rev2 so that we can read files later
if err := wt.Checkout(&git.CheckoutOptions{
Hash: *rev2,
Force: true,
}); err != nil {
return nil, fmt.Errorf("could not checkout repository at %s: %w", ref2, err)
}
}
entries := NewEntryList(len(entryCandidates))
errg := new(errgroup.Group)
for name := range entryCandidates {
name := name // https://golang.org/doc/faq#closures_and_goroutines
errg.Go(func() error {
fp := filepath.Join(dir, name)
f, err := wt.Filesystem.Open(fp)
if err != nil {
return fmt.Errorf("error opening file at %s: %w", name, err)
}
contents, err := ioutil.ReadAll(f)
f.Close()
if err != nil {
return fmt.Errorf("error reading file at %s: %w", name, err)
}
log, err := r.Log(&git.LogOptions{FileName: &fp})
if err != nil {
return fmt.Errorf("error fetching git log for %s: %w", name, err)
}
lastChange, err := log.Next()
if err != nil {
return fmt.Errorf("error fetching next git log: %w", err)
}
entries.Append(&Entry{
Issue: name,
Body: string(contents),
Date: lastChange.Author.When,
Hash: lastChange.Hash.String(),
})
return nil
})
}
if err := errg.Wait(); err != nil {
return nil, err
}
entries.SortByIssue()
return entries, nil
}
func TypeValid(Type string) bool {
for _, a := range TypeValues {
if a == Type {
return true
}
}
return false
}