/
cmd_commit_msg.go
220 lines (172 loc) · 6.11 KB
/
cmd_commit_msg.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
package main
import (
"fmt"
"os"
"path/filepath"
"sort"
"strings"
"github.com/spf13/cobra"
)
// CommitMessageTmpl is a template data structure which is used to generate files with commit message information.
type CommitMessageTmpl struct {
Scopes ScopesTmpl
Types TypesTmpl
}
// TypesTmpl is a template data structure which is used to generate files with commit message types.
type TypesTmpl struct {
List []string
Details []NameDescriptionTmpl
}
// ScopesTmpl is a template data structure which is used to generate files with commit message scopes.
type ScopesTmpl struct {
All []string
Packages []string
Extra []NameDescriptionTmpl
}
// NameDescriptionTmpl is a template item which includes a name, description and list of scopes.
type NameDescriptionTmpl struct {
Name string
Description string
Scopes []string
}
func newCommitLintCmd() *cobra.Command {
cmd := &cobra.Command{
Use: cmdUseCommitLint,
Short: "Generate commit lint files",
RunE: commitLintRunE,
DisableAutoGenTag: true,
}
return cmd
}
var commitScopesExtra = []NameDescriptionTmpl{
{"api", "used for changes that change the openapi specification", nil},
{"cmd", "used for changes to the `%s` top level binaries", nil},
{"web", "used for changes to the React based frontend", nil},
}
var commitTypes = []NameDescriptionTmpl{
{"build", "Changes that affect the build system or external dependencies", []string{"bundler", "deps", "docker", "go", "npm"}},
{"ci", "Changes to our CI configuration files and scripts", []string{"autheliabot", "buildkite", "codecov", "husky", "golangci-lint", "renovate", "reviewdog"}},
{"docs", "Documentation only changes", nil},
{"feat", "A new feature", nil},
{"fix", "A bug fix", nil},
{"i18n", "Updating translations or internationalization settings", nil},
{"perf", "A code change that improves performance", nil},
{"refactor", "A code change that neither fixes a bug nor adds a feature", nil},
{"release", "Releasing a new version of Authelia", nil},
{"test", "Adding missing tests or correcting existing tests", nil},
}
var commitTypesExtra = []string{"revert"}
func getGoPackages(dir string) (pkgs []string, err error) {
var (
entries []os.DirEntry
entriesSub []os.DirEntry
)
if entries, err = os.ReadDir(dir); err != nil {
return nil, fmt.Errorf("failed to detect go packages in directory '%s': %w", dir, err)
}
for _, entry := range entries {
if !entry.IsDir() {
continue
}
if entriesSub, err = os.ReadDir(filepath.Join(dir, entry.Name())); err != nil {
continue
}
for _, entrySub := range entriesSub {
if entrySub.IsDir() {
continue
}
if strings.HasSuffix(entrySub.Name(), ".go") {
pkgs = append(pkgs, entry.Name())
break
}
}
}
return pkgs, nil
}
func commitLintRunE(cmd *cobra.Command, args []string) (err error) {
var root, pathCommitLintConfig, pathDocsCommitMessageGuidelines string
if root, err = cmd.Flags().GetString(cmdFlagRoot); err != nil {
return err
}
if pathCommitLintConfig, err = getPFlagPath(cmd.Flags(), cmdFlagRoot, cmdFlagWeb, cmdFlagFileConfigCommitLint); err != nil {
return err
}
if pathDocsCommitMessageGuidelines, err = cmd.Flags().GetString(cmdFlagFileDocsCommitMsgGuidelines); err != nil {
return err
}
data := &CommitMessageTmpl{
Scopes: ScopesTmpl{
All: []string{},
Packages: []string{},
Extra: []NameDescriptionTmpl{},
},
Types: TypesTmpl{
List: []string{},
Details: []NameDescriptionTmpl{},
},
}
var (
cmds []string
pkgs []string
)
if cmds, err = getGoPackages(filepath.Join(root, subPathCmd)); err != nil {
return err
}
if pkgs, err = getGoPackages(filepath.Join(root, subPathInternal)); err != nil {
return err
}
data.Scopes.All = append(data.Scopes.All, pkgs...)
data.Scopes.Packages = append(data.Scopes.Packages, pkgs...)
for _, scope := range commitScopesExtra {
switch scope.Name {
case subPathCmd:
data.Scopes.Extra = append(data.Scopes.Extra, NameDescriptionTmpl{Name: scope.Name, Description: fmt.Sprintf(scope.Description, strings.Join(cmds, "|"))})
default:
data.Scopes.Extra = append(data.Scopes.Extra, scope)
}
data.Scopes.All = append(data.Scopes.All, scope.Name)
}
for _, cType := range commitTypes {
data.Types.List = append(data.Types.List, cType.Name)
data.Types.Details = append(data.Types.Details, cType)
data.Scopes.All = append(data.Scopes.All, cType.Scopes...)
}
data.Types.List = append(data.Types.List, commitTypesExtra...)
sort.Slice(data.Scopes.All, func(i, j int) bool {
return data.Scopes.All[i] < data.Scopes.All[j]
})
sort.Slice(data.Scopes.Packages, func(i, j int) bool {
return data.Scopes.Packages[i] < data.Scopes.Packages[j]
})
sort.Slice(data.Scopes.Extra, func(i, j int) bool {
return data.Scopes.Extra[i].Name < data.Scopes.Extra[j].Name
})
sort.Slice(data.Types.List, func(i, j int) bool {
return data.Types.List[i] < data.Types.List[j]
})
sort.Slice(data.Types.Details, func(i, j int) bool {
return data.Types.Details[i].Name < data.Types.Details[j].Name
})
var f *os.File
fullPathCommitLintConfig := filepath.Join(root, pathCommitLintConfig)
if f, err = os.Create(fullPathCommitLintConfig); err != nil {
return fmt.Errorf("failed to create output file '%s': %w", fullPathCommitLintConfig, err)
}
if err = tmplDotCommitLintRC.Execute(f, data); err != nil {
return fmt.Errorf("failed to write output file '%s': %w", fullPathCommitLintConfig, err)
}
if err = f.Close(); err != nil {
return fmt.Errorf("failed to close output file '%s': %w", fullPathCommitLintConfig, err)
}
fullPathDocsCommitMessageGuidelines := filepath.Join(root, pathDocsCommitMessageGuidelines)
if f, err = os.Create(fullPathDocsCommitMessageGuidelines); err != nil {
return fmt.Errorf("failed to create output file '%s': %w", fullPathDocsCommitMessageGuidelines, err)
}
if err = tmplDocsCommitMessageGuidelines.Execute(f, data); err != nil {
return fmt.Errorf("failed to write output file '%s': %w", fullPathDocsCommitMessageGuidelines, err)
}
if err = f.Close(); err != nil {
return fmt.Errorf("failed to close output file '%s': %w", fullPathDocsCommitMessageGuidelines, err)
}
return nil
}