-
Notifications
You must be signed in to change notification settings - Fork 4
/
config.go
259 lines (215 loc) · 7.36 KB
/
config.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
package github
import (
// Stdlib
"fmt"
"strings"
// Internal
"github.com/salsaflow/salsaflow/config/loader"
"github.com/salsaflow/salsaflow/errs"
"github.com/salsaflow/salsaflow/github"
"github.com/salsaflow/salsaflow/prompt"
)
// Configuration ===============================================================
type moduleConfig struct {
// GitHub repository.
GitHubOwner string
GitHubRepository string
// GitHub API authentication.
UserToken string
// Story labels.
StoryLabels []string
// State labels.
ApprovedLabel string
BeingImplementedLabel string
ImplementedLabel string
ReviewedLabel string
SkipReviewLabel string
PassedTestingLabel string
FailedTestingLabel string
SkipTestingLabel string
StagedLabel string
RejectedLabel string
SkipCheckLabels []string
}
func loadConfig() (*moduleConfig, error) {
task := fmt.Sprintf("Load config for module '%v'", ModuleId)
// Load the config.
spec := newConfigSpec()
if err := loader.LoadConfig(spec); err != nil {
return nil, errs.NewError(task, err)
}
// Parse the main repo upstream URL.
owner, repo, err := github.ParseUpstreamURL()
if err != nil {
return nil, errs.NewError(task, err)
}
// Assemble the config object.
var (
local = spec.local
global = spec.global
)
return &moduleConfig{
GitHubOwner: owner,
GitHubRepository: repo,
UserToken: global.UserToken,
StoryLabels: local.StoryLabels,
ApprovedLabel: local.StateLabels.ApprovedLabel,
BeingImplementedLabel: local.StateLabels.BeingImplementedLabel,
ImplementedLabel: local.StateLabels.ImplementedLabel,
ReviewedLabel: local.StateLabels.ReviewedLabel,
SkipReviewLabel: local.StateLabels.SkipReviewLabel,
PassedTestingLabel: local.StateLabels.PassedTestingLabel,
FailedTestingLabel: local.StateLabels.FailedTestingLabel,
SkipTestingLabel: local.StateLabels.SkipTestingLabel,
StagedLabel: local.StateLabels.StagedLabel,
RejectedLabel: local.StateLabels.RejectedLabel,
SkipCheckLabels: local.SkipCheckLabels,
}, nil
}
// Configuration spec ----------------------------------------------------------
type configSpec struct {
global *GlobalConfig
local *LocalConfig
}
func newConfigSpec() *configSpec {
return &configSpec{}
}
// ConfigKey is a part of loader.ConfigSpec
func (spec *configSpec) ConfigKey() string {
return ModuleId
}
// ModuleKind is a part of loader.ModuleConfigSpec
func (spec *configSpec) ModuleKind() loader.ModuleKind {
return ModuleKind
}
// GlobalConfig is a part of loader.ConfigSpec
func (spec *configSpec) GlobalConfig() loader.ConfigContainer {
spec.global = &GlobalConfig{}
return spec.global
}
// LocalConfig is a part of loader.ConfigSpec
func (spec *configSpec) LocalConfig() loader.ConfigContainer {
spec.local = &LocalConfig{spec: spec}
return spec.local
}
// Global configuration --------------------------------------------------------
// GlobalConfig implements loader.ConfigContainer
type GlobalConfig struct {
UserToken string `prompt:"GitHub token to be used when calling GitHub API" secret:"true" json:"token"`
}
// PromptUserForConfig is a part of loader.ConfigContainer
func (global *GlobalConfig) PromptUserForConfig() error {
var c GlobalConfig
if err := prompt.Dialog(&c, "Insert the"); err != nil {
return err
}
*global = c
return nil
}
// Local configuration ---------------------------------------------------------
var DefaultStoryLabels = []string{"enhancement", "bug"}
const (
DefaultApprovedLabel = "approved"
DefaultBeingImplementedLabel = "being implemented"
DefaultImplementedLabel = "implemented"
DefaultReviewedLabel = "reviewed"
DefaultSkipReviewLabel = "no review"
DefaultPassedTestingLabel = "qa+"
DefaultFailedTestingLabel = "qa-"
DefaultSkipTestingLabel = "no qa"
DefaultStagedLabel = "staged"
DefaultRejectedLabel = "rejected"
)
var ImplicitSkipCheckLabels = []string{"duplicate", "invalid"}
// LocalConfig implements loader.ConfigContainer interface.
type LocalConfig struct {
spec *configSpec
StoryLabels []string `json:"story_labels"`
StateLabels struct {
ApprovedLabel string `prompt:"'approved' label" default:"approved" json:"approved"`
BeingImplementedLabel string `prompt:"'being implemented' label" default:"being implemented" json:"being_implemented"`
ImplementedLabel string `prompt:"'implemented' label" default:"implemented" json:"implemented"`
ReviewedLabel string `prompt:"'reviewed' label" default:"reviewed" json:"reviewed"`
SkipReviewLabel string `prompt:"'no review' label" default:"no review" json:"skip_review"`
PassedTestingLabel string `prompt:"'passed testing' label" default:"qa+" json:"passed_testing"`
FailedTestingLabel string `prompt:"'failed testing' label" default:"qa-" json:"failed_testing"`
SkipTestingLabel string `prompt:"'skip testing' label" default:"no qa" json:"skip_testing"`
StagedLabel string `prompt:"'staged' label" default:"staged" json:"staged_for_acceptance"`
RejectedLabel string `prompt:"'rejected' label" default:"rejected" json:"client_rejected"`
} `json:"state_labels"`
SkipCheckLabels []string `json:"skip_release_check_labels"`
}
// PromptUserForConfig is a part of loader.ConfigContainer interface.
func (local *LocalConfig) PromptUserForConfig() error {
c := LocalConfig{spec: local.spec}
// Prompt for the state labels.
if err := prompt.Dialog(&c, "Insert the"); err != nil {
return err
}
// Prompt for the story labels.
storyLabels, err := promptForLabelList("Insert the story labels", DefaultStoryLabels, nil)
fmt.Println()
if err != nil {
return err
}
c.StoryLabels = storyLabels
// Prompt for the release skip check labels.
skipCheckLabels, err := promptForLabelList(
"Insert the skip release check labels", nil, ImplicitSkipCheckLabels)
if err != nil {
return err
}
c.SkipCheckLabels = skipCheckLabels
// Success!
*local = c
return nil
}
func promptForLabelList(msg string, defaultItems, implicitItems []string) ([]string, error) {
var (
lenDefault = len(defaultItems)
lenImplicit = len(implicitItems)
)
// Prompt for the value.
fmt.Printf("%v, comma-separated.\n", msg)
if lenDefault != 0 {
fmt.Printf(" (default values: %v)\n", strings.Join(defaultItems, ", "))
}
if lenImplicit != 0 {
fmt.Printf(" (always included: %v)\n", strings.Join(implicitItems, ", "))
}
fmt.Println()
input, err := prompt.Prompt("Your choice: ")
if err != nil {
if err == prompt.ErrCanceled {
return append(defaultItems, implicitItems...), nil
}
return nil, err
}
// Append the new labels to the default ones.
// Make sure there are no duplicates and empty strings.
var (
insertedLabels = strings.Split(input, ",")
lenInserted = len(insertedLabels)
)
// Save a few allocations.
labels := make([]string, lenImplicit, lenImplicit+lenInserted)
copy(labels, implicitItems)
LabelLoop:
for _, insertedLabel := range insertedLabels {
// Trim spaces.
insertedLabel = strings.TrimSpace(insertedLabel)
// Skip empty strings.
if insertedLabel == "" {
continue
}
// Make sure there are no duplicates.
for _, existingLabel := range labels {
if insertedLabel == existingLabel {
continue LabelLoop
}
}
// Append the label.
labels = append(labels, insertedLabel)
}
return labels, nil
}