|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/csv" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + |
| 8 | + tea "github.com/charmbracelet/bubbletea" |
| 9 | + "github.com/charmbracelet/lipgloss" |
| 10 | + "github.com/rs/zerolog/log" |
| 11 | + "github.com/spf13/cobra" |
| 12 | +) |
| 13 | + |
| 14 | +// CreateTicketsCmd is the command that runs a Bubble Tea TUI for your CSV data. |
| 15 | +var CreateTicketsCmd = &cobra.Command{ |
| 16 | + Use: "create-tickets", |
| 17 | + Short: "Interactive TUI to confirm Jira tickets from CSV", |
| 18 | + Long: `Reads a CSV file describing flaky tests and displays each proposed |
| 19 | +ticket in a text-based UI. Press 'y' to confirm, 'n' to skip, 'q' to quit.`, |
| 20 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 21 | + csvPath, _ := cmd.Flags().GetString("csv-path") |
| 22 | + if csvPath == "" { |
| 23 | + log.Error().Msg("CSV path is required") |
| 24 | + os.Exit(1) |
| 25 | + } |
| 26 | + |
| 27 | + records, err := readCSV(csvPath) |
| 28 | + if err != nil { |
| 29 | + log.Error().Err(err).Msg("Error reading CSV file") |
| 30 | + os.Exit(1) |
| 31 | + } |
| 32 | + |
| 33 | + // Convert CSV rows to Ticket objects (skipping invalid rows). |
| 34 | + var tickets []Ticket |
| 35 | + for i, row := range records { |
| 36 | + if len(row) < 10 { |
| 37 | + log.Warn().Msgf("Skipping row %d (not enough columns)", i+1) |
| 38 | + continue |
| 39 | + } |
| 40 | + tickets = append(tickets, rowToTicket(row)) |
| 41 | + } |
| 42 | + |
| 43 | + if len(tickets) == 0 { |
| 44 | + log.Warn().Msg("No valid tickets found in CSV.") |
| 45 | + return nil |
| 46 | + } |
| 47 | + |
| 48 | + // Create and run Bubble Tea program |
| 49 | + p := tea.NewProgram(initialModel(tickets)) |
| 50 | + if _, err := p.Run(); err != nil { |
| 51 | + log.Error().Err(err).Msg("Error running Bubble Tea program") |
| 52 | + os.Exit(1) |
| 53 | + } |
| 54 | + return nil |
| 55 | + }, |
| 56 | +} |
| 57 | + |
| 58 | +func init() { |
| 59 | + CreateTicketsCmd.Flags().String("csv-path", "", "Path to the CSV file containing ticket data") |
| 60 | + // Add this command to your root command in your main.go or wherever you define your CLI. |
| 61 | + // Example: rootCmd.AddCommand(BubbleTeaJiraCmd) |
| 62 | +} |
| 63 | + |
| 64 | +// Ticket is a simple struct holding Summary and Description for each row. |
| 65 | +type Ticket struct { |
| 66 | + Summary string |
| 67 | + Description string |
| 68 | +} |
| 69 | + |
| 70 | +// readCSV reads the entire CSV into a [][]string. |
| 71 | +func readCSV(path string) ([][]string, error) { |
| 72 | + f, err := os.Open(path) |
| 73 | + if err != nil { |
| 74 | + return nil, err |
| 75 | + } |
| 76 | + defer f.Close() |
| 77 | + |
| 78 | + r := csv.NewReader(f) |
| 79 | + return r.ReadAll() |
| 80 | +} |
| 81 | + |
| 82 | +// rowToTicket maps one CSV row to a Ticket, embedding your “flaky test” info. |
| 83 | +func rowToTicket(row []string) Ticket { |
| 84 | + // CSV columns (based on your format): |
| 85 | + // 0: Package |
| 86 | + // 1: Test Path |
| 87 | + // 2: Test |
| 88 | + // 3: Runs |
| 89 | + // 4: Successes |
| 90 | + // 5: Failures |
| 91 | + // 6: Skips |
| 92 | + // 7: Overall Flake Rate |
| 93 | + // 8: Code Owners |
| 94 | + // 9: Failed Logs |
| 95 | + |
| 96 | + packageName := row[0] |
| 97 | + testName := row[2] |
| 98 | + flakeRate := row[7] |
| 99 | + codeOwners := row[8] |
| 100 | + failedLogs := row[9] |
| 101 | + |
| 102 | + if codeOwners == "" { |
| 103 | + codeOwners = "N/A" |
| 104 | + } |
| 105 | + if failedLogs == "" { |
| 106 | + failedLogs = "N/A" |
| 107 | + } |
| 108 | + |
| 109 | + summary := fmt.Sprintf("[Flaky Test] %s (Rate: %s%%)", testName, flakeRate) |
| 110 | + |
| 111 | + description := fmt.Sprintf(`Test Details: |
| 112 | +
|
| 113 | +Package: %s |
| 114 | +Test Name: %s |
| 115 | +Flake Rate: %s%% in the last 7 days |
| 116 | +Code Owners: %s |
| 117 | +
|
| 118 | +Test Logs: |
| 119 | +%s |
| 120 | +
|
| 121 | +Action Items: |
| 122 | +
|
| 123 | +Investigate Failed Test Logs: Thoroughly review the provided logs to identify patterns or common error messages that indicate the root cause. |
| 124 | +
|
| 125 | +Fix the Issue: Analyze and address the underlying problem causing the flakiness. |
| 126 | +
|
| 127 | +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. |
| 128 | +
|
| 129 | +Unskip the Test: Once confirmed stable, remove any test skip markers to re-enable the test in the CI pipeline. |
| 130 | +
|
| 131 | +Reference Guidelines: Follow the recommendations in the Flaky Test Guide. |
| 132 | +`, packageName, testName, flakeRate, codeOwners, failedLogs) |
| 133 | + |
| 134 | + return Ticket{ |
| 135 | + Summary: summary, |
| 136 | + Description: description, |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +// ------------------ Bubble Tea Model ------------------ |
| 141 | + |
| 142 | +type model struct { |
| 143 | + tickets []Ticket |
| 144 | + index int // current ticket index |
| 145 | + confirmed int // how many confirmed |
| 146 | + skipped int // how many skipped |
| 147 | + quitting bool // whether we're exiting |
| 148 | +} |
| 149 | + |
| 150 | +// initialModel sets up the Bubble Tea model with the tickets to display. |
| 151 | +func initialModel(tickets []Ticket) model { |
| 152 | + return model{ |
| 153 | + tickets: tickets, |
| 154 | + index: 0, |
| 155 | + confirmed: 0, |
| 156 | + skipped: 0, |
| 157 | + quitting: false, |
| 158 | + } |
| 159 | +} |
| 160 | + |
| 161 | +// Custom messages if we needed them (not strictly required here). |
| 162 | +type confirmMsg struct{} |
| 163 | +type skipMsg struct{} |
| 164 | +type quitMsg struct{} |
| 165 | + |
| 166 | +// Init is called when the program starts (we have no initial I/O to perform). |
| 167 | +func (m model) Init() tea.Cmd { |
| 168 | + return nil |
| 169 | +} |
| 170 | + |
| 171 | +// Update is called on every event (keyboard or otherwise). |
| 172 | +func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { |
| 173 | + switch msg := msg.(type) { |
| 174 | + case tea.KeyMsg: |
| 175 | + switch msg.String() { |
| 176 | + case "y": // confirm |
| 177 | + return updateConfirm(m) |
| 178 | + case "n": // skip |
| 179 | + return updateSkip(m) |
| 180 | + case "q", "ctrl+c", "esc": // quit |
| 181 | + return updateQuit(m) |
| 182 | + } |
| 183 | + } |
| 184 | + return m, nil |
| 185 | +} |
| 186 | + |
| 187 | +func updateConfirm(m model) (tea.Model, tea.Cmd) { |
| 188 | + m.confirmed++ |
| 189 | + m.index++ |
| 190 | + if m.index >= len(m.tickets) { |
| 191 | + m.quitting = true |
| 192 | + } |
| 193 | + return m, nil |
| 194 | +} |
| 195 | + |
| 196 | +func updateSkip(m model) (tea.Model, tea.Cmd) { |
| 197 | + m.skipped++ |
| 198 | + m.index++ |
| 199 | + if m.index >= len(m.tickets) { |
| 200 | + m.quitting = true |
| 201 | + } |
| 202 | + return m, nil |
| 203 | +} |
| 204 | + |
| 205 | +func updateQuit(m model) (tea.Model, tea.Cmd) { |
| 206 | + m.quitting = true |
| 207 | + return m, tea.Quit |
| 208 | +} |
| 209 | + |
| 210 | +// View renders our UI: if done, show summary; otherwise show current ticket. |
| 211 | +func (m model) View() string { |
| 212 | + if m.quitting || m.index >= len(m.tickets) { |
| 213 | + return finalView(m) |
| 214 | + } |
| 215 | + |
| 216 | + t := m.tickets[m.index] |
| 217 | + |
| 218 | + headerStyle := lipgloss.NewStyle(). |
| 219 | + Bold(true). |
| 220 | + Foreground(lipgloss.Color("205")) // pinkish |
| 221 | + summaryStyle := lipgloss.NewStyle(). |
| 222 | + Bold(true). |
| 223 | + Foreground(lipgloss.Color("10")) // green |
| 224 | + descStyle := lipgloss.NewStyle(). |
| 225 | + Foreground(lipgloss.Color("7")) // light gray |
| 226 | + helpStyle := lipgloss.NewStyle(). |
| 227 | + Faint(true) |
| 228 | + |
| 229 | + header := headerStyle.Render( |
| 230 | + fmt.Sprintf("Proposed Ticket #%d of %d", m.index+1, len(m.tickets)), |
| 231 | + ) |
| 232 | + sum := summaryStyle.Render("Summary: ") + t.Summary |
| 233 | + desc := descStyle.Render("Description:\n" + t.Description) |
| 234 | + help := helpStyle.Render("\nPress [y] to confirm, [n] to skip, [q] to quit.") |
| 235 | + |
| 236 | + return fmt.Sprintf("%s\n\n%s\n\n%s\n%s\n", header, sum, desc, help) |
| 237 | +} |
| 238 | + |
| 239 | +func finalView(m model) string { |
| 240 | + doneStyle := lipgloss.NewStyle(). |
| 241 | + Bold(true). |
| 242 | + Foreground(lipgloss.Color("6")) // cyan |
| 243 | + return doneStyle.Render(fmt.Sprintf( |
| 244 | + "Done! Confirmed %d tickets, skipped %d. Exiting...\n", |
| 245 | + m.confirmed, m.skipped, |
| 246 | + )) |
| 247 | +} |
0 commit comments