-
Notifications
You must be signed in to change notification settings - Fork 16
/
jira_create.go
235 lines (203 loc) · 5.68 KB
/
jira_create.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
package cmd
import (
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"regexp"
"strings"
"unicode"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"k8s.io/apimachinery/pkg/util/sets"
"github.com/openshift-eng/ci-test-mapping/pkg/registry"
)
type CreateFlags struct {
JiraURL string
}
func NewCreateFlags() *CreateFlags {
return &CreateFlags{
JiraURL: "https://issues.redhat.com/rest/api/2/issue/createmeta/OCPBUGS/issuetypes/1",
}
}
var createCmd = &cobra.Command{
Use: "jira-create",
Short: "Create mapping components for missing Jira components",
Run: func(cmd *cobra.Command, args []string) {
f := NewCreateFlags()
bearerToken := os.Getenv("JIRA_TOKEN")
if bearerToken == "" {
cmd.Usage() // nolint:errcheck
logrus.Fatal("jira token required")
}
components, err := getJiraComponents(f.JiraURL, bearerToken)
if err != nil {
logrus.WithError(err).Fatal("could not fetch jira components")
}
reg := registry.NewComponentRegistry()
knownJiraComponents := sets.New[string]()
for _, c := range reg.Components {
knownJiraComponents.Insert(c.JiraComponents()...)
}
for _, component := range components {
if !knownJiraComponents.Has(component) {
logrus.WithFields(logrus.Fields{
"path": "pkg/" + getPackagePath(component),
"package": getPackageName(component),
}).Infof("no mapping for jira component %q, creating...", component)
if err := copyTemplate(component); err != nil {
logrus.WithError(err).Fatal("couldn't copy template")
}
}
}
},
}
func getJiraComponents(url, bearerToken string) ([]string, error) {
req, err := http.NewRequest("GET", url, nil)
if err != nil {
logrus.WithError(err).Fatal("could not create GET client")
}
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", bearerToken))
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Accept", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
logrus.WithError(err).Fatal("error while reading response")
}
var jiraComponents struct {
Values []struct {
FieldID string `json:"fieldId"`
AllowedValues []struct {
Name string `json:"name"`
} `json:"allowedValues"`
} `json:"values"`
}
if err := json.Unmarshal(body, &jiraComponents); err != nil {
return nil, err
}
var components []string
for _, value := range jiraComponents.Values {
if value.FieldID == "components" {
for _, allowedValue := range value.AllowedValues {
if strings.Contains(allowedValue.Name, "Documentation") {
continue
}
components = append(components, allowedValue.Name)
}
}
}
return components, nil
}
func getPackagePath(input string) string {
parts := strings.Split(input, "/")
for i := range parts {
parts[i] = strings.ToLower(getComponentName(parts[i]))
}
return strings.Join(parts, "/")
}
func getPackageName(input string) string {
re := regexp.MustCompile(`\([^)]*\)`)
input = re.ReplaceAllString(input, "")
input = strings.Map(func(r rune) rune {
if unicode.IsPunct(r) || unicode.IsSpace(r) {
return -1
}
return r
}, input)
return strings.ToLower(input)
}
func getComponentName(input string) string {
// Remove everything inside parentheses using a regular expression
re := regexp.MustCompile(`\([^)]*\)`)
input = re.ReplaceAllString(input, "")
// Replace spaces and dashes with newlines
input = strings.ReplaceAll(input, " ", "\n")
input = strings.ReplaceAll(input, "-", "\n")
// Convert the first letter of each word to uppercase
lines := strings.Split(input, "\n")
for i, line := range lines {
if line == "" {
continue
}
lines[i] = strings.ToUpper(string(line[0])) + line[1:]
}
input = strings.Join(lines, "")
// Remove trailing space
input = strings.TrimRight(input, " ")
return input
}
func copyTemplate(component string) error {
destPath := "./pkg/components/" + getPackagePath(component)
srcPath := "./pkg/components/example"
files, err := os.ReadDir(srcPath)
if err != nil {
return err
}
name := getComponentName(component)
parts := strings.Split(name, "/")
if len(parts) > 1 {
name = parts[len(parts)-1]
}
for _, f := range files {
src := srcPath + "/" + f.Name()
dest := destPath + "/" + f.Name()
dir := filepath.Dir(dest)
if err := os.MkdirAll(dir, 0o755); err != nil {
return err
}
// Read the source file
content, err := os.ReadFile(src)
if err != nil {
return err
}
newContent := strings.ReplaceAll(string(content),
"ExampleComponent",
fmt.Sprintf("%sComponent", name))
newContent = strings.ReplaceAll(newContent,
"package example",
fmt.Sprintf("package %s", getPackageName(component)))
newContent = strings.ReplaceAll(newContent,
"Example",
component)
err = os.WriteFile(dest, []byte(newContent), 0o644) //nolint:gosec
if err != nil {
return err
}
}
// Read the source file
regFile, err := os.ReadFile("./pkg/registry/registry.go")
if err != nil {
return err
}
prepend := "// New components go here"
importString := fmt.Sprintf(`"github.com/openshift-eng/ci-test-mapping/pkg/components/%s"`,
getPackagePath(component))
registerCmd :=
fmt.Sprintf(`r.Register("%s", &%s.%s)`,
component,
getPackageName(component),
fmt.Sprintf("%sComponent", name),
)
newContent := strings.ReplaceAll(string(regFile),
prepend,
fmt.Sprintf("%s\n\t%s", registerCmd, prepend))
newContent = strings.ReplaceAll(newContent,
"import (",
fmt.Sprintf("import (\n\t%s", importString))
err = os.WriteFile("./pkg/registry/registry.go", []byte(newContent), 0o644) //nolint:gosec
if err != nil {
return err
}
return nil
}
func init() {
rootCmd.AddCommand(createCmd)
}