|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | +package app.testlens.cli; |
| 3 | + |
| 4 | +import static app.testlens.cli.CommandSupport.apiClient; |
| 5 | +import static app.testlens.cli.CommandSupport.filterSuffix; |
| 6 | +import static app.testlens.cli.CommandSupport.isInteractiveTerminal; |
| 7 | +import static app.testlens.cli.CommandSupport.resolveRepo; |
| 8 | +import static app.testlens.cli.util.Console.clearProgress; |
| 9 | +import static app.testlens.cli.util.Console.showProgress; |
| 10 | +import static app.testlens.cli.util.Console.styled; |
| 11 | +import static java.util.Comparator.comparing; |
| 12 | +import static java.util.Objects.requireNonNull; |
| 13 | + |
| 14 | +import app.testlens.cli.TestCommand.Mode; |
| 15 | +import app.testlens.cli.client.ApiException; |
| 16 | +import app.testlens.cli.client.api.TestsApi; |
| 17 | +import app.testlens.cli.client.model.FlakeResponse; |
| 18 | +import app.testlens.cli.client.model.TestExecutionResponse; |
| 19 | +import app.testlens.cli.tui.FlakesTui; |
| 20 | +import app.testlens.cli.tui.controller.FlakesController; |
| 21 | +import app.testlens.cli.tui.model.TestInfo; |
| 22 | +import java.io.PrintWriter; |
| 23 | +import java.util.ArrayList; |
| 24 | +import java.util.List; |
| 25 | +import java.util.Map; |
| 26 | +import java.util.concurrent.Callable; |
| 27 | +import org.jspecify.annotations.Nullable; |
| 28 | +import picocli.CommandLine.Command; |
| 29 | +import picocli.CommandLine.Help.Ansi; |
| 30 | +import picocli.CommandLine.Help.Ansi.Style; |
| 31 | +import picocli.CommandLine.Model.CommandSpec; |
| 32 | +import picocli.CommandLine.Option; |
| 33 | +import picocli.CommandLine.Parameters; |
| 34 | +import picocli.CommandLine.Spec; |
| 35 | + |
| 36 | +/** |
| 37 | + * {@code testlens flakes <id>} shows the flakes of one test: the builds in which it flipped its outcome after retries, |
| 38 | + * each with all its executions in chronological order. Options mirror {@link TestCommand}. |
| 39 | + */ |
| 40 | +@Command( |
| 41 | + name = "flakes", |
| 42 | + description = "Show the flaky builds of a test with all their executions", |
| 43 | + mixinStandardHelpOptions = true |
| 44 | +) |
| 45 | +public class FlakesCommand implements Callable<Integer> { |
| 46 | + |
| 47 | + @Spec |
| 48 | + @Nullable |
| 49 | + CommandSpec spec; |
| 50 | + |
| 51 | + @Parameters(index = "0", description = "Internal test ID") |
| 52 | + long id; |
| 53 | + |
| 54 | + @Option(names = "--branch", description = "Only flakes on this branch; PR builds count toward their target branch") |
| 55 | + @Nullable |
| 56 | + String branch; |
| 57 | + |
| 58 | + @Option(names = { "--task", "--project" }, description = "Only flakes for this task/project") |
| 59 | + @Nullable |
| 60 | + String workUnit; |
| 61 | + |
| 62 | + @Option(names = "--job", description = "Only flakes of this job, e.g. CI / build") |
| 63 | + @Nullable |
| 64 | + String job; |
| 65 | + |
| 66 | + @Option( |
| 67 | + names = "--repo", |
| 68 | + description = "Repository as owner/repo (defaults to the GitHub remote 'origin' of the current directory)" |
| 69 | + ) |
| 70 | + @Nullable |
| 71 | + String repo; |
| 72 | + |
| 73 | + @Option( |
| 74 | + names = "--token", |
| 75 | + description = "GitHub access token (defaults to $GITHUB_TOKEN or $GH_TOKEN)", |
| 76 | + defaultValue = "${env:GITHUB_TOKEN:-${env:GH_TOKEN}}" |
| 77 | + ) |
| 78 | + @Nullable |
| 79 | + String token; |
| 80 | + |
| 81 | + @Option( |
| 82 | + names = "--mode", |
| 83 | + description = "Output mode: ${COMPLETION-CANDIDATES} (default: ${DEFAULT-VALUE}, uses the TUI in an interactive terminal)", |
| 84 | + defaultValue = "auto" |
| 85 | + ) |
| 86 | + Mode mode = Mode.AUTO; |
| 87 | + |
| 88 | + @Option( |
| 89 | + names = "--base-url", |
| 90 | + description = "API base URL", |
| 91 | + defaultValue = "https://api.testlens.app", |
| 92 | + hidden = true |
| 93 | + ) |
| 94 | + @Nullable |
| 95 | + String baseUrl; |
| 96 | + |
| 97 | + @Override |
| 98 | + public Integer call() throws Exception { |
| 99 | + var out = requireNonNull(spec).commandLine().getOut(); |
| 100 | + var err = spec.commandLine().getErr(); |
| 101 | + if (token == null || token.isBlank()) { |
| 102 | + err.println("No GitHub token provided (use --token or set GITHUB_TOKEN)"); |
| 103 | + return 2; |
| 104 | + } |
| 105 | + var ownerAndRepo = resolveRepo(repo); |
| 106 | + if (ownerAndRepo == null || ownerAndRepo.length != 2) { |
| 107 | + err.println("Cannot determine repository (use --repo owner/repo)"); |
| 108 | + return 2; |
| 109 | + } |
| 110 | + var owner = ownerAndRepo[0]; |
| 111 | + var repoName = ownerAndRepo[1]; |
| 112 | + var ansi = spec.commandLine().getColorScheme().ansi(); |
| 113 | + var api = new TestsApi(apiClient(token, requireNonNull(baseUrl))); |
| 114 | + try { |
| 115 | + var tui = mode == Mode.TUI || (mode == Mode.AUTO && isInteractiveTerminal()); |
| 116 | + showProgress(out, ansi, "Fetching test..."); |
| 117 | + var test = TestInfo.of(api.getTest(owner, repoName, id)); |
| 118 | + clearProgress(out, ansi); |
| 119 | + showProgress(out, ansi, "Fetching flakes..."); |
| 120 | + var flakes = fetchFlakes(api, owner, repoName, id, branch, workUnit, job); |
| 121 | + clearProgress(out, ansi); |
| 122 | + if (tui) { |
| 123 | + FlakesTui.run(test, branch, workUnit, job, flakes); |
| 124 | + } else { |
| 125 | + printFlakes(out, ansi, test, branch, workUnit, job, flakes); |
| 126 | + } |
| 127 | + return 0; |
| 128 | + } catch (ApiException e) { |
| 129 | + clearProgress(out, ansi); |
| 130 | + return TestCommand.fail(err, e, "Test not found"); |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + /** Fetches all pages of the flakes of a test, newest first. */ |
| 135 | + public static List<FlakeResponse> fetchFlakes( |
| 136 | + TestsApi api, |
| 137 | + String owner, |
| 138 | + String repo, |
| 139 | + long testId, |
| 140 | + @Nullable String branch, |
| 141 | + @Nullable String workUnit, |
| 142 | + @Nullable String job |
| 143 | + ) throws ApiException { |
| 144 | + var flakes = new ArrayList<FlakeResponse>(); |
| 145 | + for (int page = 1;; page++) { |
| 146 | + var response = |
| 147 | + api.getTestFlakesWithHttpInfo(owner, repo, testId, null, branch, workUnit, job, page, null, Map.of()); |
| 148 | + if (response.getData() != null) { |
| 149 | + flakes.addAll(response.getData()); |
| 150 | + } |
| 151 | + if (!ExecutionsLoader.hasRelNext(response.getHeaders())) { |
| 152 | + return flakes; |
| 153 | + } |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + static void printFlakes( |
| 158 | + PrintWriter out, |
| 159 | + Ansi ansi, |
| 160 | + TestInfo test, |
| 161 | + @Nullable String branch, |
| 162 | + @Nullable String workUnit, |
| 163 | + @Nullable String job, |
| 164 | + List<FlakeResponse> flakes |
| 165 | + ) { |
| 166 | + out.println( |
| 167 | + styled(ansi, test.displayName(), Style.bold) + " (test " + test.id() + ")" |
| 168 | + + filterSuffix(branch, workUnit, job) |
| 169 | + ); |
| 170 | + out.println(" " + styled(ansi, test.uniqueId(), Style.faint)); |
| 171 | + out.println(" " + styled(ansi, "newest flake first, executions in chronological order", Style.faint)); |
| 172 | + if (flakes.isEmpty()) { |
| 173 | + out.println(); |
| 174 | + out.println("No flakes in the last 30 days."); |
| 175 | + return; |
| 176 | + } |
| 177 | + for (int i = 0; i < flakes.size(); i++) { |
| 178 | + var flake = flakes.get(i); |
| 179 | + out.println(); |
| 180 | + out.println( |
| 181 | + " " + (i + 1) + ". " + styled( |
| 182 | + ansi, |
| 183 | + FlakesController.abbreviatedSha(flake.getSha()) + " @ " + flake.getBranch(), |
| 184 | + Style.bold |
| 185 | + ) + " · " + flake.getWorkUnit() + (flake.getJob() == null ? "" : " · " + flake.getJob()) |
| 186 | + ); |
| 187 | + var executions = flake.getExecutions() |
| 188 | + .stream() |
| 189 | + .sorted(comparing(TestExecutionResponse::getStartTime)) |
| 190 | + .toList(); |
| 191 | + for (int j = 0; j < executions.size(); j++) { |
| 192 | + TestCommand.printExecution(out, ansi, " ", j + 1, executions.get(j), false); |
| 193 | + } |
| 194 | + } |
| 195 | + } |
| 196 | +} |