-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
config.go
311 lines (277 loc) · 7.72 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
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
package tagpr
import (
"os"
"strconv"
"strings"
"github.com/Songmu/gitconfig"
"github.com/google/go-github/v57/github"
)
const (
defaultConfigFile = ".tagpr"
defaultConfigContent = `# config file for the tagpr in git config format
# The tagpr generates the initial configuration, which you can rewrite to suit your environment.
# CONFIGURATIONS:
# tagpr.releaseBranch
# Generally, it is "main." It is the branch for releases. The tagpr tracks this branch,
# creates or updates a pull request as a release candidate, or tags when they are merged.
#
# tagpr.versionFile
# Versioning file containing the semantic version needed to be updated at release.
# It will be synchronized with the "git tag".
# Often this is a meta-information file such as gemspec, setup.cfg, package.json, etc.
# Sometimes the source code file, such as version.go or Bar.pm, is used.
# If you do not want to use versioning files but only git tags, specify the "-" string here.
# You can specify multiple version files by comma separated strings.
#
# tagpr.vPrefix
# Flag whether or not v-prefix is added to semver when git tagging. (e.g. v1.2.3 if true)
# This is only a tagging convention, not how it is described in the version file.
#
# tagpr.changelog (Optional)
# Flag whether or not changelog is added or changed during the release.
#
# tagpr.command (Optional)
# Command to change files just before release.
#
# tagpr.template (Optional)
# Pull request template in go template format
#
# tagpr.release (Optional)
# GitHub Release creation behavior after tagging [true, draft, false]
# If this value is not set, the release is to be created.
#
# tagpr.majorLabels (Optional)
# Label of major update targets. Default is [major]
#
# tagpr.minorLabels (Optional)
# Label of minor update targets. Default is [minor]
#
[tagpr]
`
defaultMajorLabels = "major"
defaultMinorLabels = "minor"
envReleaseBranch = "TAGPR_RELEASE_BRANCH"
envVersionFile = "TAGPR_VERSION_FILE"
envVPrefix = "TAGPR_VPREFIX"
envChangelog = "TAGPR_CHANGELOG"
envCommand = "TAGPR_COMMAND"
envTemplate = "TAGPR_TEMPLATE"
envRelease = "TAGPR_RELEASE"
envMajorLabels = "TAGPR_MAJOR_LABELS"
envMinorLabels = "TAGPR_MAINOR_LABELS"
configReleaseBranch = "tagpr.releaseBranch"
configVersionFile = "tagpr.versionFile"
configVPrefix = "tagpr.vPrefix"
configChangelog = "tagpr.changelog"
configCommand = "tagpr.command"
configTemplate = "tagpr.template"
configRelease = "tagpr.release"
configMajorLabels = "tagpr.majorLabels"
configMinorLabels = "tagpr.minorLabels"
)
type config struct {
releaseBranch *string
versionFile *string
command *string
template *string
release *string
vPrefix *bool
changelog *bool
majorLabels *string
minorLabels *string
conf string
gitconfig *gitconfig.Config
}
func newConfig(gitPath string) (*config, error) {
cfg := &config{
conf: defaultConfigFile,
gitconfig: &gitconfig.Config{GitPath: gitPath, File: defaultConfigFile},
}
err := cfg.Reload()
return cfg, err
}
func (cfg *config) Reload() error {
if rb := os.Getenv(envReleaseBranch); rb != "" {
cfg.releaseBranch = github.String(rb)
} else {
out, err := cfg.gitconfig.Get(configReleaseBranch)
if err == nil {
cfg.releaseBranch = github.String(out)
}
}
if rb := os.Getenv(envVersionFile); rb != "" {
cfg.versionFile = github.String(rb)
} else {
out, err := cfg.gitconfig.Get(configVersionFile)
if err == nil {
cfg.versionFile = github.String(out)
}
}
if vPrefix := os.Getenv(envVPrefix); vPrefix != "" {
b, err := strconv.ParseBool(vPrefix)
if err != nil {
return err
}
cfg.vPrefix = github.Bool(b)
} else {
b, err := cfg.gitconfig.Bool(configVPrefix)
if err == nil {
cfg.vPrefix = github.Bool(b)
}
}
if changelog := os.Getenv(envChangelog); changelog != "" {
b, err := strconv.ParseBool(changelog)
if err != nil {
return err
}
cfg.changelog = github.Bool(b)
} else {
b, err := cfg.gitconfig.Bool(configChangelog)
if err == nil {
cfg.changelog = github.Bool(b)
}
}
if command := os.Getenv(envCommand); command != "" {
cfg.command = github.String(command)
} else {
command, err := cfg.gitconfig.Get(configCommand)
if err == nil {
cfg.command = github.String(command)
}
}
if tmpl := os.Getenv(envTemplate); tmpl != "" {
cfg.template = github.String(tmpl)
} else {
tmpl, err := cfg.gitconfig.Get(configTemplate)
if err == nil {
cfg.template = github.String(tmpl)
}
}
if rel := os.Getenv(envRelease); rel != "" {
cfg.release = github.String(rel)
} else {
rel, err := cfg.gitconfig.Get(configRelease)
if err == nil {
cfg.release = github.String(rel)
}
}
if major := os.Getenv(envMajorLabels); major != "" {
cfg.majorLabels = github.String(major)
} else {
major, err := cfg.gitconfig.Get(configMajorLabels)
if err == nil {
cfg.majorLabels = github.String(major)
} else {
cfg.majorLabels = github.String(defaultMajorLabels)
}
}
if minor := os.Getenv(envMinorLabels); minor != "" {
cfg.minorLabels = github.String(minor)
} else {
minor, err := cfg.gitconfig.Get(configMinorLabels)
if err == nil {
cfg.minorLabels = github.String(minor)
} else {
cfg.minorLabels = github.String(defaultMinorLabels)
}
}
return nil
}
func (cfg *config) set(key, value string) error {
if !exists(cfg.conf) {
if err := cfg.initializeFile(); err != nil {
return err
}
}
if value == "" {
value = "-" // value "-" represents null (really?)
}
_, err := cfg.gitconfig.Do(key, value)
if err != nil {
// in this case, config file might be invalid or broken, so retry once.
if err = cfg.initializeFile(); err != nil {
return err
}
_, err = cfg.gitconfig.Do(key, value)
}
return err
}
func (cfg *config) initializeFile() error {
if err := os.RemoveAll(cfg.conf); err != nil {
return err
}
if err := os.WriteFile(cfg.conf, []byte(defaultConfigContent), 0644); err != nil {
return err
}
return nil
}
func (cfg *config) SetRelaseBranch(br string) error {
if err := cfg.set(configReleaseBranch, br); err != nil {
return err
}
cfg.releaseBranch = github.String(br)
return nil
}
func (cfg *config) SetVersionFile(fpath string) error {
if err := cfg.set(configVersionFile, fpath); err != nil {
return err
}
cfg.versionFile = github.String(fpath)
return nil
}
func (cfg *config) SetVPrefix(vPrefix bool) error {
if err := cfg.set(configVPrefix, strconv.FormatBool(vPrefix)); err != nil {
return err
}
cfg.vPrefix = github.Bool(vPrefix)
return nil
}
func stringify(pstr *string) string {
if pstr == nil || *pstr == "-" {
return ""
}
return *pstr
}
func (cfg *config) ReleaseBranch() string {
return stringify(cfg.releaseBranch)
}
func (cfg *config) VersionFile() string {
if cfg.versionFile == nil {
return ""
}
return *cfg.versionFile
}
func (cfg *config) Command() string {
return stringify(cfg.command)
}
func (cfg *config) Template() string {
return stringify(cfg.template)
}
func (cfg *config) Release() bool {
rel := strings.ToLower(stringify(cfg.release))
if rel == "draft" || rel == "" {
return true
}
b, err := strconv.ParseBool(rel)
if err != nil {
return true
}
return b
}
func (cfg *config) ReleaseDraft() bool {
return strings.ToLower(stringify(cfg.release)) == "draft"
}
func (cfg *config) MajorLabels() []string {
labels := strings.Split(stringify(cfg.majorLabels), ",")
for i, v := range labels {
labels[i] = strings.TrimSpace(v)
}
return labels
}
func (cfg *config) MinorLabels() []string {
labels := strings.Split(stringify(cfg.minorLabels), ",")
for i, v := range labels {
labels[i] = strings.TrimSpace(v)
}
return labels
}