GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — feat: add command for inspecting flakes (#79) · testlens-app/cli@21907da

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • src
    • main/java/app/testlens/cli
      • FlakesCommand.java
      • Testlens.java
      • tui
        • FlakesTui.java
        • controller
          • FlakesController.java
        • model
          • PrTuiModel.java
        • view
          • ExecutionTabsPane.java
          • FlakeDetailsView.java
          • FlakesView.java
          • PrDetailsView.java
          • PrTestsTableView.java
          • PrTuiView.java
    • test/java/app/testlens/cli
      • FlakesCommandTest.java
      • tui
        • FlakesViewTest.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎src/main/java/app/testlens/cli/FlakesCommand.java‎

Copy file name to clipboard
+196Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
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+
}
Collapse file

‎src/main/java/app/testlens/cli/Testlens.java‎

Copy file name to clipboardExpand all lines: src/main/java/app/testlens/cli/Testlens.java
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
@Command(
1111
name = "testlens",
1212
mixinStandardHelpOptions = true,
13-
subcommands = { PrCommand.class, TestCommand.class, CompletionCommand.class, },
13+
subcommands = { PrCommand.class, TestCommand.class, FlakesCommand.class, CompletionCommand.class, },
1414
versionProvider = Testlens.VersionResourceReader.class
1515
)
1616
public class Testlens {
Collapse file

‎src/main/java/app/testlens/cli/tui/FlakesTui.java‎

Copy file name to clipboard
+41Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
package app.testlens.cli.tui;
3+
4+
import app.testlens.cli.client.model.FlakeResponse;
5+
import app.testlens.cli.tui.controller.FlakesController;
6+
import app.testlens.cli.tui.model.TestInfo;
7+
import app.testlens.cli.tui.view.FlakesView;
8+
import dev.tamboui.toolkit.app.ToolkitRunner;
9+
import dev.tamboui.toolkit.event.EventResult;
10+
import dev.tamboui.tui.event.KeyEvent;
11+
import java.util.List;
12+
import org.jspecify.annotations.Nullable;
13+
14+
/** The flakes screen of the TUI, entered directly via {@code testlens flakes <id>}. */
15+
public final class FlakesTui {
16+
17+
private FlakesTui() {}
18+
19+
public static void run(
20+
TestInfo test,
21+
@Nullable String branch,
22+
@Nullable String workUnit,
23+
@Nullable String job,
24+
List<FlakeResponse> flakes
25+
) throws Exception {
26+
try (var runner = ToolkitRunner.create()) {
27+
var controller = new FlakesController(runner.focusManager(), test, flakes);
28+
var view = new FlakesView(controller, test, branch, workUnit, job);
29+
// the modal "no flakes" dialog consumes all keys before unfocused-element routing,
30+
// so quit has to be handled globally, ahead of the dialog
31+
runner.eventRouter().addGlobalHandler(event -> {
32+
if (event instanceof KeyEvent keyEvent && keyEvent.isQuit()) {
33+
runner.quit();
34+
return EventResult.HANDLED;
35+
}
36+
return EventResult.UNHANDLED;
37+
});
38+
runner.run(view::render);
39+
}
40+
}
41+
}

0 commit comments

Comments
0 (0)
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.