forked from argoproj/argo-workflows
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontents.go
More file actions
201 lines (170 loc) · 5.37 KB
/
contents.go
File metadata and controls
201 lines (170 loc) · 5.37 KB
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
package main
import (
"fmt"
"regexp"
"strings"
"time"
)
// timeNow is a variable that can be replaced in tests to mock time.Now()
var timeNow = time.Now
type feature struct {
Component string
Description string
Author string
Issues []string
Details string
}
// Metadata field definitions
var (
metadataFields = []string{"Component", "Issues", "Description", "Author"}
)
func getMetadataPattern(field string) *regexp.Regexp {
return regexp.MustCompile(field + `:\s*(.*?)(?:\n|$)`)
}
func validateHeaders(content string) (bool, string) {
re := regexp.MustCompile(`(?m)^(#+)\s+(.+)$`)
for _, match := range re.FindAllStringSubmatch(content, -1) {
if len(match) != 3 {
continue
}
level := len(match[1])
// Require level 3 or higher for all headers
if level < 3 {
return false, fmt.Sprintf("Header '%s' must be at least level 3 (###)", match[0])
}
}
return true, ""
}
func validateMetadataOrder(content string) (bool, string) {
lines := strings.Split(content, "\n")
// Find the first line that's not a metadata field
metadataEnd := 0
for i := 0; i < len(lines); i++ {
line := lines[i]
isMetadata := false
for _, field := range metadataFields {
if strings.HasPrefix(line, field+":") {
isMetadata = true
break
}
}
if !isMetadata && line != "" {
metadataEnd = i
break
}
}
// Check if any metadata fields appear after this point
for i := metadataEnd; i < len(lines); i++ {
line := lines[i]
for _, field := range metadataFields {
if strings.HasPrefix(line, field+":") {
return false, fmt.Sprintf("Metadata field '%s' must appear before any other content", line)
}
}
}
return true, ""
}
func parseContent(source string, content string) (bool, feature, error) {
// Check required sections
isValid := true
for _, field := range metadataFields {
if !strings.Contains(content, field+":") {
fmt.Printf("Error: Missing required section '%s:' in %s\n", field, source)
isValid = false
}
}
if headerValid, errMsg := validateHeaders(content); !headerValid {
fmt.Printf("Error: %s in %s\n", errMsg, source)
isValid = false
}
if orderValid, errMsg := validateMetadataOrder(content); !orderValid {
fmt.Printf("Error: %s in %s\n", errMsg, source)
isValid = false
}
// Extract metadata fields
component := ""
if matches := getMetadataPattern("Component").FindStringSubmatch(content); len(matches) > 1 {
component = strings.TrimSpace(matches[1])
if !isValidComponent(component) {
fmt.Printf("Error: Invalid component '%s' in %s. Valid components are: %s. Add more in hack/featuregen/components.go\n", component, source, listValidComponents())
isValid = false
}
}
issuesSection := ""
if matches := getMetadataPattern("Issues").FindStringSubmatch(content); len(matches) > 1 {
issuesSection = matches[1]
}
issues := regexp.MustCompile(`(\d+)`).FindAllStringSubmatch(issuesSection, -1)
issueNumbers := make([]string, len(issues))
for i, issue := range issues {
issueNumbers[i] = issue[1]
}
if len(issueNumbers) == 0 {
fmt.Printf("Error: At least one issue number must be present in %s\n", source)
isValid = false
}
description := ""
if matches := getMetadataPattern("Description").FindStringSubmatch(content); len(matches) > 1 {
description = strings.TrimSpace(matches[1])
}
author := ""
if matches := getMetadataPattern("Author").FindStringSubmatch(content); len(matches) > 1 {
author = strings.TrimSpace(matches[1])
}
// Extract details (everything after metadata)
details := ""
pattern := `(?s)(?:` + strings.Join(metadataFields, ":|") + `:).*?\n\n(.*)`
if detailsMatch := regexp.MustCompile(pattern).FindStringSubmatch(content); len(detailsMatch) > 1 {
details = strings.TrimSpace(detailsMatch[1])
}
return isValid, feature{
Component: component,
Description: description,
Author: author,
Issues: issueNumbers,
Details: details,
}, nil
}
func format(version string, features []feature) string {
var output strings.Builder
// Format new content
versionHeader := "Unreleased"
if version != "" {
versionHeader = version
}
currentDate := timeNow().Format("2006-01-02")
output.WriteString(fmt.Sprintf("# New features in %s (%s)\n\nThis is a concise list of new features.\n\n", versionHeader, currentDate))
// Group features by component
featuresByComponent := make(map[string][]feature)
for _, f := range features {
featuresByComponent[f.Component] = append(featuresByComponent[f.Component], f)
}
// Output features in order of validComponents
for _, component := range validComponents {
componentFeatures := featuresByComponent[component]
if len(componentFeatures) == 0 {
continue
}
output.WriteString(fmt.Sprintf("## %s\n\n", component))
for _, feature := range componentFeatures {
issuesStr := ""
if len(feature.Issues) > 0 {
issues := make([]string, len(feature.Issues))
for i, issue := range feature.Issues {
issues[i] = fmt.Sprintf("[#%s](https://github.com/argoproj/argo-workflows/issues/%s)", issue, issue)
}
issuesStr = fmt.Sprintf("(%s)", strings.Join(issues, ", "))
}
output.WriteString(fmt.Sprintf("- %s by %s %s\n", feature.Description, feature.Author, issuesStr))
if feature.Details != "" {
for line := range strings.SplitSeq(feature.Details, "\n") {
if line != "" {
output.WriteString(fmt.Sprintf(" %s\n", line))
}
}
}
output.WriteString("\n")
}
}
return output.String()
}