Skip to content

Commit bc82e53

Browse files
committed
Refactor ticket creation logic to improve validation and logging handling
1 parent 08c43fc commit bc82e53

File tree

1 file changed

+81
-61
lines changed

1 file changed

+81
-61
lines changed

tools/flakeguard/cmd/create_jira_tickets.go

Lines changed: 81 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -164,11 +164,11 @@ func init() {
164164
// -------------------------------------------------------------------------------------
165165

166166
type Ticket struct {
167-
RowIndex int // which row in the original CSV (after skipping header)
168-
Confirmed bool // set to true if user pressed 'y'
169-
TestName string
167+
RowIndex int
168+
Confirmed bool
170169
Valid bool
171170
InvalidReason string
171+
TestName string
172172
Summary string
173173
Description string
174174
ExistingJiraKey string
@@ -177,65 +177,40 @@ type Ticket struct {
177177
// rowToTicket builds a Ticket from one CSV row (your columns).
178178
// Required fields: Package(row[0]), TestName(row[2]), FlakeRate(row[7]), Logs(row[9]).
179179
func rowToTicket(row []string) Ticket {
180-
pkg := row[0]
181-
testName := row[2]
182-
flakeRate := row[7]
183-
logs := row[9]
184-
185-
// Check for missing required fields
186-
var missing []string
187-
if pkg == "" {
188-
missing = append(missing, "Package")
189-
}
190-
if testName == "" {
191-
missing = append(missing, "Test Name")
192-
}
193-
if flakeRate == "" {
194-
missing = append(missing, "Flake Rate")
195-
}
196-
if logs == "" {
197-
missing = append(missing, "Test Logs")
198-
}
180+
pkg := strings.TrimSpace(row[0])
181+
testName := strings.TrimSpace(row[2])
182+
flakeRate := strings.TrimSpace(row[7])
183+
logs := strings.TrimSpace(row[9])
199184

200-
if len(missing) > 0 {
201-
reason := fmt.Sprintf("Missing required field(s): %s", strings.Join(missing, ", "))
202-
if testName != "" {
203-
reason += fmt.Sprintf(" [Test Name: %s]", testName)
204-
}
205-
return Ticket{
206-
Valid: false,
207-
InvalidReason: reason,
208-
}
209-
}
185+
// 1) Build a single summary that might contain empty strings
186+
summary := fmt.Sprintf("Fix Flaky Test: %s (%s%% flake rate)", testName, flakeRate)
210187

211-
// Parse logs for multiple links
212-
var logLines []string
213-
logLinks := strings.Split(logs, ",")
214-
runNumber := 1
215-
for _, link := range logLinks {
216-
link = strings.TrimSpace(link)
217-
if link == "" {
218-
continue
188+
// 2) If logs is empty, we’ll show (Logs not available)
189+
var logSection string
190+
if logs == "" {
191+
logSection = "(Logs not available)"
192+
} else {
193+
// Parse logs into bullets
194+
var lines []string
195+
runNumber := 1
196+
for _, link := range strings.Split(logs, ",") {
197+
link = strings.TrimSpace(link)
198+
if link == "" {
199+
continue
200+
}
201+
lines = append(lines,
202+
fmt.Sprintf("- [Run %d](%s)", runNumber, link),
203+
)
204+
runNumber++
219205
}
220-
// Format each log line as a Markdown link: "- [Run 1](http://...)"
221-
logLines = append(logLines,
222-
fmt.Sprintf("- [Run %d](%s)", runNumber, link),
223-
)
224-
runNumber++
225-
}
226-
if len(logLines) == 0 {
227-
return Ticket{
228-
Valid: false,
229-
InvalidReason: "No valid test logs found after parsing",
206+
if len(lines) == 0 {
207+
logSection = "(Logs not available)"
208+
} else {
209+
logSection = strings.Join(lines, "\n")
230210
}
231211
}
232-
// Join them into one string with newlines
233-
testLogSection := strings.Join(logLines, "\n")
234212

235-
// Summary: "Fix Flaky Test: <TestName> (<FlakeRate>% flake rate)"
236-
summary := fmt.Sprintf("Fix Flaky Test: %s (%s%% flake rate)", testName, flakeRate)
237-
238-
// Build the description (Markdown style)
213+
// 3) Always build the same final description, with placeholders if fields are empty
239214
description := fmt.Sprintf(`
240215
## Test Details:
241216
- **Package:** `+"`%s`"+`
@@ -251,14 +226,45 @@ func rowToTicket(row []string) Ticket {
251226
3. **Rerun Tests Locally:** Execute the test and related changes on a local environment to ensure that the fix stabilizes the test, as well as all other tests that may be affected.
252227
4. **Unskip the Test:** Once confirmed stable, remove any test skip markers to re-enable the test in the CI pipeline.
253228
5. **Reference Guidelines:** Follow the recommendations in the [Flaky Test Guide].
254-
`, pkg, testName, flakeRate, testLogSection)
229+
`,
230+
pkg,
231+
testName,
232+
flakeRate,
233+
logSection,
234+
)
255235

256-
return Ticket{
257-
TestName: testName,
258-
Valid: true,
236+
// 4) Mark ticket Valid or Invalid based on required fields
237+
ticket := Ticket{
259238
Summary: summary,
260239
Description: description,
240+
TestName: testName,
241+
Valid: true, // we assume valid unless required fields are missing
242+
}
243+
244+
var missing []string
245+
if pkg == "" {
246+
missing = append(missing, "Package")
247+
}
248+
if testName == "" {
249+
missing = append(missing, "Test Name")
250+
}
251+
if flakeRate == "" {
252+
missing = append(missing, "Flake Rate")
253+
}
254+
if logs == "" || logSection == "(Logs not available)" {
255+
missing = append(missing, "Test Logs")
261256
}
257+
258+
if len(missing) > 0 {
259+
ticket.Valid = false
260+
reason := fmt.Sprintf("Missing required field(s): %s", strings.Join(missing, ", "))
261+
if testName != "" {
262+
reason += fmt.Sprintf(" [Test Name: %s]", testName)
263+
}
264+
ticket.InvalidReason = reason
265+
}
266+
267+
return ticket
262268
}
263269

264270
// -------------------------------------------------------------------------------------
@@ -412,8 +418,22 @@ func (m model) View() string {
412418
fmt.Sprintf("Ticket #%d of %d (Invalid)", m.index+1, len(m.tickets)),
413419
)
414420
errMsg := errorStyle.Render("Cannot create ticket: " + t.InvalidReason)
421+
422+
sumHeader := summaryStyle.Render("Summary:")
423+
sumContent := t.Summary
424+
sum := fmt.Sprintf("%s\n\n%s\n", sumHeader, sumContent)
425+
descHeader := descHeaderStyle.Render("Description:\n")
426+
descBody := descBodyStyle.Render(t.Description)
427+
415428
hint := helpStyle.Render("\nPress any key to skip, or [q] to quit.\n")
416-
return fmt.Sprintf("%s\n\n%s\n%s\n", header, errMsg, hint)
429+
430+
return fmt.Sprintf("%s\n\n%s\n\n%s\n%s\n\n%s\n",
431+
header,
432+
errMsg,
433+
sum,
434+
descHeader+descBody,
435+
hint,
436+
)
417437
}
418438

419439
header := headerStyle.Render(

0 commit comments

Comments
 (0)