-
Notifications
You must be signed in to change notification settings - Fork 4
/
release_running.go
285 lines (247 loc) · 7.25 KB
/
release_running.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
281
282
283
284
285
package github
import (
// Stdlib
"bytes"
"fmt"
"io"
"text/tabwriter"
// Internal
"github.com/salsaflow/salsaflow/action"
"github.com/salsaflow/salsaflow/errs"
"github.com/salsaflow/salsaflow/log"
"github.com/salsaflow/salsaflow/modules/common"
"github.com/salsaflow/salsaflow/version"
// Other
"github.com/google/go-github/github"
)
type runningRelease struct {
tracker *issueTracker
version *version.Version
issues []*github.Issue
}
func newRunningRelease(
tracker *issueTracker,
releaseVersion *version.Version,
) (*runningRelease, error) {
return &runningRelease{
tracker: tracker,
version: releaseVersion,
}, nil
}
// Version is a part of common.RunningRelease interface.
func (release *runningRelease) Version() *version.Version {
return release.version
}
// Stories is a part of common.RunningRelease interface.
func (release *runningRelease) Stories() ([]common.Story, error) {
issues, err := release.loadIssues()
if err != nil {
return nil, err
}
return toCommonStories(issues, release.tracker), nil
}
// EnsureStageable is a part of common.RunningRelease interface.
func (release *runningRelease) EnsureStageable() error {
task := "Make sure the issues can be staged"
log.Run(task)
// Load the assigned issues.
issues, err := release.loadIssues()
if err != nil {
return errs.NewError(task, err)
}
// Check the states.
var details bytes.Buffer
tw := tabwriter.NewWriter(&details, 0, 8, 4, '\t', 0)
io.WriteString(tw, "\n")
io.WriteString(tw, "Issue URL\tError\n")
io.WriteString(tw, "=========\t=====\n")
skipLabels := release.tracker.config.SkipCheckLabels
shouldBeSkipped := func(issue *github.Issue) bool {
for _, skipLabel := range skipLabels {
if labeled(issue, skipLabel) {
return true
}
}
return false
}
for _, issue := range issues {
// Skip the story in case it is labeled with a skip label.
if shouldBeSkipped(issue) {
continue
}
// Check the abstract state.
var pwned bool
state := abstractState(issue, release.tracker.config)
switch state {
case common.StoryStateNew:
pwned = true
case common.StoryStateApproved:
pwned = true
case common.StoryStateBeingImplemented:
pwned = true
case common.StoryStateImplemented:
pwned = true
case common.StoryStateReviewed:
pwned = true
case common.StoryStateBeingTested:
pwned = true
case common.StoryStateTested:
// OK
case common.StoryStateStaged:
// OK
case common.StoryStateAccepted:
// OK
case common.StoryStateRejected:
pwned = true
case common.StoryStateClosed:
// OK
case common.StoryStateInvalid:
pwned = true
default:
panic("unknown abstract issues state")
}
if pwned {
fmt.Fprintf(tw, "%v\tinvalid state: %v\n", *issue.HTMLURL, state)
err = common.ErrNotStageable
}
}
if err != nil {
io.WriteString(tw, "\n")
tw.Flush()
return errs.NewErrorWithHint(task, err, details.String())
}
return nil
}
// Stage is a part of common.RunningRelease interface.
func (release *runningRelease) Stage() (action.Action, error) {
stageTask := fmt.Sprintf("Mark relevant GitHub issues as %v", common.StoryStateStaged)
log.Run(stageTask)
// Load the assigned issues.
issues, err := release.loadIssues()
if err != nil {
return nil, errs.NewError(stageTask, err)
}
// Pick only the stories that need staging.
issues = filterIssues(issues, func(issue *github.Issue) bool {
return abstractState(issue, release.tracker.config) == common.StoryStateTested
})
// Update the issues.
toLabelStrings := func(labels []github.Label) []string {
ls := make([]string, 0, len(labels))
for _, label := range labels {
ls = append(ls, *label.Name)
}
return ls
}
var (
remainingLabels = make(map[int][]string, len(issues))
prunedLabels = make(map[int][]string, len(issues))
)
for _, issue := range issues {
remaining, pruned := pruneStateLabels(release.tracker.config, issue.Labels)
remainingLabels[*issue.Number] = toLabelStrings(remaining)
prunedLabels[*issue.Number] = toLabelStrings(pruned)
}
// updateFunc sets the workflow label to express the staged abstract state.
updateFunc := func(
client *github.Client,
owner string,
repo string,
issue *github.Issue,
) (*github.Issue, error) {
issueNum := *issue.Number
labels := remainingLabels[*issue.Number]
labels = append(labels, release.tracker.config.StagedLabel)
updatedLabels, _, err := client.Issues.ReplaceLabelsForIssue(owner, repo, issueNum, labels)
if err != nil {
return nil, err
}
i := *issue
i.Labels = updatedLabels
return &i, nil
}
// rollbackFunc resets the labels to the original value.
rollbackFunc := func(
client *github.Client,
owner string,
repo string,
issue *github.Issue,
) (*github.Issue, error) {
issueNum := *issue.Number
labels := append(remainingLabels[issueNum], prunedLabels[issueNum]...)
updatedLabels, _, err := client.Issues.ReplaceLabelsForIssue(owner, repo, issueNum, labels)
if err != nil {
return nil, err
}
i := *issue
i.Labels = updatedLabels
return &i, nil
}
// Update the issues concurrently.
updatedStories, act, err := release.tracker.updateIssues(issues, updateFunc, rollbackFunc)
if err != nil {
return nil, errs.NewError(stageTask, err)
}
release.issues = updatedStories
// Return the rollback function.
return action.ActionFunc(func() error {
release.issues = nil
return act.Rollback()
}), nil
}
// EnsureReleasable is a part of common.RunningRelease interface.
func (release *runningRelease) EnsureReleasable() error {
var (
config = release.tracker.config
vString = release.version.BaseString()
)
task := fmt.Sprintf(
"Make sure that the issues associated with release %v can be released", vString)
log.Run(task)
// Make sure the issues are loaded.
issues, err := release.loadIssues()
if err != nil {
return errs.NewError(task, err)
}
// Make sure all relevant issues are accepted.
// This includes the issues with SkipCheckLabels.
notAccepted := filterIssues(issues, func(issue *github.Issue) bool {
return abstractState(issue, config) != common.StoryStateAccepted
})
// In case there are no issues in a wrong state, we are done.
if len(notAccepted) == 0 {
return nil
}
// Generate the error hint.
var hint bytes.Buffer
tw := tabwriter.NewWriter(&hint, 0, 8, 2, '\t', 0)
fmt.Fprintf(tw, "\nThe following issues are blocking the release:\n\n")
fmt.Fprintf(tw, "Issue URL\tState\n")
fmt.Fprintf(tw, "=========\t=====\n")
for _, issue := range notAccepted {
fmt.Fprintf(tw, "%v\t%v\n", *issue.HTMLURL, abstractState(issue, config))
}
fmt.Fprintf(tw, "\n")
tw.Flush()
return errs.NewErrorWithHint(task, common.ErrNotReleasable, hint.String())
}
// Release is a part of common.RunningRelease interface.
func (release *runningRelease) Release() (action.Action, error) {
_, act, err := release.tracker.closeMilestone(release.version)
return act, err
}
func (release *runningRelease) loadIssues() ([]*github.Issue, error) {
// Fetch the issues unless cached.
if release.issues == nil {
task := fmt.Sprintf(
"Fetch GitHub issues associated with release %v", release.version.BaseString())
log.Run(task)
issues, err := release.tracker.issuesByRelease(release.version)
if err != nil {
return nil, errs.NewError(task, err)
}
release.issues = issues
}
// Return the cached issues.
return release.issues, nil
}