Skip to content

Commit f468e74

Browse files
committed
Add delete
1 parent dc9ac49 commit f468e74

File tree

2 files changed

+42
-30
lines changed

2 files changed

+42
-30
lines changed

tools/flakeguard/cmd/create_jira_tickets.go

Lines changed: 33 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -350,14 +350,31 @@ func updateNormalMode(m model, msg tea.KeyMsg) (tea.Model, tea.Cmd) {
350350
}
351351
t := m.tickets[m.index]
352352

353-
// Always allow 'q' (quit) and 'e' (enter existing ticket)
353+
// Always allow 'q' (quit), 'e' (enter existing ticket), and 'd' (delete existing ticket)
354354
switch msg.String() {
355355
case "q", "esc", "ctrl+c":
356356
return updateQuit(m)
357357
case "e":
358358
m.mode = "promptExisting"
359359
m.inputValue = ""
360360
return m, nil
361+
case "d":
362+
// Only allow removal if an existing ticket is present
363+
if t.ExistingJiraKey != "" && m.JiraClient != nil {
364+
err := jirautils.DeleteTicketInJira(m.JiraClient, t.ExistingJiraKey)
365+
if err != nil {
366+
// Log error if deletion fails
367+
log.Error().Err(err).Msgf("Failed to delete ticket %s", t.ExistingJiraKey)
368+
} else {
369+
// Clear ticket info on success
370+
t.ExistingJiraKey = ""
371+
t.ExistingTicketSource = ""
372+
m.tickets[m.index] = t
373+
// Update local DB (clearing stored ticket)
374+
m.LocalDB.Set(t.TestPackage, t.TestName, "")
375+
}
376+
}
377+
return m, nil
361378
}
362379

363380
// If ticket is valid, allow create ('c') and skip ('n')
@@ -492,19 +509,16 @@ func (m model) View() string {
492509
faintStyle := lipgloss.NewStyle().Faint(true)
493510
errorStyle := lipgloss.NewStyle().Bold(true).Foreground(lipgloss.Color("9"))
494511
existingStyle := lipgloss.NewStyle().Foreground(lipgloss.Color("11"))
512+
redStyle := lipgloss.NewStyle().Foreground(lipgloss.Color("9"))
495513

496514
t := m.tickets[m.index]
497515

498516
// 1) Header line
499-
header := ""
517+
var header string
500518
if t.Valid {
501-
header = headerStyle.Render(
502-
fmt.Sprintf("Proposed Ticket #%d of %d", m.index+1, len(m.tickets)),
503-
)
519+
header = headerStyle.Render(fmt.Sprintf("Proposed Ticket #%d of %d", m.index+1, len(m.tickets)))
504520
} else {
505-
header = headerStyle.Render(
506-
fmt.Sprintf("Ticket #%d of %d (Invalid)", m.index+1, len(m.tickets)),
507-
)
521+
header = headerStyle.Render(fmt.Sprintf("Ticket #%d of %d (Invalid)", m.index+1, len(m.tickets)))
508522
}
509523

510524
// 2) Summary & Description
@@ -526,27 +540,19 @@ func (m model) View() string {
526540
domain := os.Getenv("JIRA_DOMAIN")
527541
link := t.ExistingJiraKey
528542
if domain != "" {
529-
// Turn "DX-203" into a link
530543
link = fmt.Sprintf("https://%s/browse/%s", domain, t.ExistingJiraKey)
531544
}
532545
existingLine = existingStyle.Render(fmt.Sprintf("\n%s: %s", prefix, link))
533546
}
534547

535-
// 4) If invalid + missing required fields => show that after existing line
536-
// or if there's no existing ticket, show it anyway
548+
// 4) If invalid: show reason
537549
invalidLine := ""
538550
if !t.Valid {
539-
invalidLine = errorStyle.Render(
540-
fmt.Sprintf("\nCannot create ticket: %s", t.InvalidReason),
541-
)
551+
invalidLine = errorStyle.Render(fmt.Sprintf("\nCannot create ticket: %s", t.InvalidReason))
542552
}
543553

544554
// 5) Help line
545-
helpLine := ""
546-
// Cases:
547-
// A) If invalid:
548-
// B) If valid & there's an existing ticket => [n] to next, [e] to update ticket id, [q] to quit.
549-
// C) If valid & no existing => [c] to create ticket, [n] to skip, [e] to enter existing ticket, [q] to quit (with DRY RUN text if needed).
555+
var helpLine string
550556
if !t.Valid {
551557
if t.ExistingJiraKey != "" {
552558
helpLine = faintStyle.Render("\n[n] to next, [e] to update ticket id, [q] to quit.")
@@ -555,30 +561,27 @@ func (m model) View() string {
555561
}
556562
} else {
557563
if t.ExistingJiraKey != "" {
558-
helpLine = faintStyle.Render("\n[n] to next, [e] to update ticket id, [q] to quit.")
564+
// Show the "d" option in red when a ticket is found.
565+
helpLine = fmt.Sprintf("\n[n] to next, [e] to update ticket id, %s to remove ticket, [q] to quit.",
566+
redStyle.Render("[d]"))
559567
} else {
560-
// if no existing ticket, the normal prompt
561568
dryRunLabel := ""
562569
if m.DryRun || m.JiraClient == nil {
563570
dryRunLabel = " (DRY RUN)"
564571
}
565-
helpLine = faintStyle.Render(
566-
fmt.Sprintf("\nPress [c] to create ticket%s, [n] to skip, [e] to enter existing ticket, [q] to quit.",
567-
dryRunLabel),
568-
)
572+
helpLine = faintStyle.Render(fmt.Sprintf("\nPress [c] to create ticket%s, [n] to skip, [e] to enter existing ticket, [q] to quit.", dryRunLabel))
569573
}
570574
}
571575

572-
return fmt.Sprintf(
573-
"%s\n%s\n%s\n%s\n%s%s%s\n%s\n",
576+
return fmt.Sprintf("%s\n%s\n%s\n%s\n%s%s%s\n%s\n",
574577
header,
575578
sum,
576579
sumBody,
577580
descHeader,
578581
descBody,
579-
existingLine, // e.g. "Existing ticket found in jira: https://..."
580-
invalidLine, // e.g. "Cannot create ticket: Missing required..."
581-
helpLine, // e.g. "[n] to next, [e]... or "[c] to create ticket..."
582+
existingLine,
583+
invalidLine,
584+
helpLine,
582585
)
583586
}
584587

tools/flakeguard/jirautils/jira.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,12 @@ func CreateTicketInJira(
5050
}
5151
return newIssue.Key, nil
5252
}
53+
54+
// DeleteTicketInJira deletes a Jira ticket with the given ticket key.
55+
func DeleteTicketInJira(client *jira.Client, ticketKey string) error {
56+
resp, err := client.Issue.DeleteWithContext(context.Background(), ticketKey)
57+
if err != nil {
58+
return fmt.Errorf("error deleting Jira ticket %s: %w (resp: %v)", ticketKey, err, resp)
59+
}
60+
return nil
61+
}

0 commit comments

Comments
 (0)