-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathpr-visualization.html
More file actions
446 lines (409 loc) · 20.5 KB
/
Copy pathpr-visualization.html
File metadata and controls
446 lines (409 loc) · 20.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
<!DOCTYPE html>
<!--
| Generated by Apache Maven Doxia Site Renderer 2.0.0 from target/filtered-site/markdown/cookbook/pr-visualization.md at 2026-04-26
| Rendered using Apache Maven Fluido Skin 2.1.0
-->
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="generator" content="Apache Maven Doxia Site Renderer 2.0.0" />
<title>Generating PR Age Charts – GitHub Copilot SDK :: Java</title>
<link rel="stylesheet" href="../css/apache-maven-fluido-2.1.0.min.css" />
<link rel="stylesheet" href="../css/site.css" />
<link rel="stylesheet" href="../css/print.css" media="print" />
<script src="../js/apache-maven-fluido-2.1.0.min.js"></script>
<style>.github-fork-ribbon:before { background-color: gray; }</style>
</head>
<body class="topBarEnabled">
<a class="github-fork-ribbon right-top" href="https://github.com/github/copilot-sdk-java" data-ribbon="Fork me on GitHub">Fork me on GitHub</a>
<header id="topbar" class="navbar navbar-fixed-top ">
<div class="navbar-inner">
<div class="container-fluid">
<a data-target=".nav-collapse" data-toggle="collapse" class="btn btn-navbar">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</a>
<a class="brand" href="../index.html">GitHub Copilot SDK for Java
</a>
<ul class="nav">
<li class="dropdown">
<a class="dropdown-toggle" data-toggle="dropdown">Overview <b class="caret"></b></a>
<ul class="dropdown-menu">
<li><a href="../index.html">Introduction</a></li>
<li><a href="../getting-started.html">Getting Started</a></li>
<li><a href="../setup.html">Setup</a></li>
<li><a href="../documentation.html">Documentation</a></li>
<li><a href="../mcp.html">MCP Servers</a></li>
<li><a href="../hooks.html">Session Hooks</a></li>
<li><a href="../advanced.html">Advanced Usage</a></li>
<li><a href="../apidocs/index.html">API Javadoc</a></li>
</ul>
</li>
<li class="dropdown">
<a class="dropdown-toggle" data-toggle="dropdown">Guides <b class="caret"></b></a>
<ul class="dropdown-menu">
<li><a href="../documentation.html#Basic_Usage">Basic Usage</a></li>
<li><a href="../documentation.html#Handling_Responses">Handling Responses</a></li>
<li><a href="../documentation.html#Streaming_Responses">Streaming</a></li>
<li><a href="../documentation.html#Choosing_a_Model">Choosing a Model</a></li>
<li><a href="../documentation.html#Session_Management">Session Management</a></li>
</ul>
</li>
<li class="dropdown">
<a class="dropdown-toggle" data-toggle="dropdown">Advanced <b class="caret"></b></a>
<ul class="dropdown-menu">
<li><a href="../advanced.html#Custom_Tools">Custom Tools</a></li>
<li><a href="../advanced.html#System_Messages">System Messages</a></li>
<li><a href="../advanced.html#File_Attachments">File Attachments</a></li>
<li><a href="../advanced.html#Bring_Your_Own_Key_.28BYOK.29">BYOK</a></li>
<li><a href="../advanced.html#Infinite_Sessions">Infinite Sessions</a></li>
<li><a href="../advanced.html#MCP_Servers">MCP Servers</a></li>
<li><a href="../advanced.html#Slash_Commands">Slash Commands</a></li>
<li><a href="../advanced.html#Elicitation_UI_Dialogs">Elicitation</a></li>
<li><a href="../advanced.html#Getting_Session_Metadata_by_ID">Session Metadata</a></li>
<li><a href="../hooks.html">Session Hooks</a></li>
</ul>
</li>
<li class="dropdown">
<a class="dropdown-toggle" data-toggle="dropdown">Cookbook <b class="caret"></b></a>
<ul class="dropdown-menu">
<li><a href="../cookbook/README.html">Overview</a></li>
<li><a href="../cookbook/error-handling.html">Error Handling</a></li>
<li><a href="../cookbook/multiple-sessions.html">Multiple Sessions</a></li>
<li><a href="../cookbook/managing-local-files.html">Managing Local Files</a></li>
<li><a>PR Visualization</a></li>
<li><a href="../cookbook/persisting-sessions.html">Persisting Sessions</a></li>
</ul>
</li>
<li class="dropdown">
<a class="dropdown-toggle" data-toggle="dropdown">Project Documentation <b class="caret"></b></a>
<ul class="dropdown-menu">
<li class="dropdown-submenu">
<a href="../project-info.html">Project Information</a>
<ul class="dropdown-menu">
<li><a href="../dependencies.html">Dependencies</a></li>
<li><a href="../dependency-info.html">Maven Coordinates</a></li>
<li><a href="../distribution-management.html">Distribution Management</a></li>
<li><a href="../index.html">About</a></li>
<li><a href="../licenses.html">Licenses</a></li>
<li><a href="../plugin-management.html">Plugin Management</a></li>
<li><a href="../plugins.html">Plugins</a></li>
<li><a href="../scm.html">Source Code Management</a></li>
<li><a href="../summary.html">Summary</a></li>
<li><a href="../team.html">Team</a></li>
</ul>
</li>
<li class="dropdown-submenu">
<a href="../project-reports.html">Project Reports</a>
<ul class="dropdown-menu">
<li><a href="../apidocs/index.html">Javadoc</a></li>
<li><a href="../surefire.html">Surefire</a></li>
<li><a href="../spotbugs.html">SpotBugs</a></li>
<li><a href="../taglist.html">Tag List</a></li>
<li><a href="../dependency-analysis.html">Dependency Analysis</a></li>
</ul>
</li>
</ul>
</li>
</ul>
</div>
</div>
</header>
<div class="container-fluid container-fluid-top">
<header>
<div id="banner">
<div class="pull-left"></div>
<div class="pull-right"></div>
<div class="clear"><hr/></div>
</div>
<div id="breadcrumbs">
<ul class="breadcrumb">
<li id="publishDate">Last Published: 2026-04-26<span class="divider">|</span>
</li>
<li id="projectVersion">Version: 0.3.0-java.2<span class="divider">|</span></li>
<li><a href="../index.html">Home</a><span class="divider">/</span></li>
<li class="active">Generating PR Age Charts</li>
</ul>
</div>
</header>
<div class="row-fluid">
<header id="leftColumn" class="span2">
<nav class="well sidebar-nav">
<ul class="nav nav-list">
<li class="nav-header">Overview</li>
<li><a href="../index.html">Introduction</a></li>
<li><a href="../getting-started.html">Getting Started</a></li>
<li><a href="../setup.html">Setup</a></li>
<li><a href="../documentation.html">Documentation</a></li>
<li><a href="../mcp.html">MCP Servers</a></li>
<li><a href="../hooks.html">Session Hooks</a></li>
<li><a href="../advanced.html">Advanced Usage</a></li>
<li><a href="../apidocs/index.html">API Javadoc</a></li>
<li class="nav-header">Guides</li>
<li><a href="../documentation.html#Basic_Usage">Basic Usage</a></li>
<li><a href="../documentation.html#Handling_Responses">Handling Responses</a></li>
<li><a href="../documentation.html#Streaming_Responses">Streaming</a></li>
<li><a href="../documentation.html#Choosing_a_Model">Choosing a Model</a></li>
<li><a href="../documentation.html#Session_Management">Session Management</a></li>
<li class="nav-header">Advanced</li>
<li><a href="../advanced.html#Custom_Tools">Custom Tools</a></li>
<li><a href="../advanced.html#System_Messages">System Messages</a></li>
<li><a href="../advanced.html#File_Attachments">File Attachments</a></li>
<li><a href="../advanced.html#Bring_Your_Own_Key_.28BYOK.29">BYOK</a></li>
<li><a href="../advanced.html#Infinite_Sessions">Infinite Sessions</a></li>
<li><a href="../advanced.html#MCP_Servers">MCP Servers</a></li>
<li><a href="../advanced.html#Slash_Commands">Slash Commands</a></li>
<li><a href="../advanced.html#Elicitation_UI_Dialogs">Elicitation</a></li>
<li><a href="../advanced.html#Getting_Session_Metadata_by_ID">Session Metadata</a></li>
<li><a href="../hooks.html">Session Hooks</a></li>
<li class="nav-header">Cookbook</li>
<li><a href="../cookbook/README.html">Overview</a></li>
<li><a href="../cookbook/error-handling.html">Error Handling</a></li>
<li><a href="../cookbook/multiple-sessions.html">Multiple Sessions</a></li>
<li><a href="../cookbook/managing-local-files.html">Managing Local Files</a></li>
<li class="active"><a>PR Visualization</a></li>
<li><a href="../cookbook/persisting-sessions.html">Persisting Sessions</a></li>
<li class="nav-header">Project Documentation</li>
<li><a href="../project-info.html"><span class="icon-chevron-right"></span>Project Information</a></li>
<li><a href="../project-reports.html"><span class="icon-chevron-right"></span>Project Reports</a></li>
</ul>
</nav>
<div class="well sidebar-nav">
<div id="poweredBy">
<div class="clear"></div>
<div class="clear"></div>
<a href="https://maven.apache.org/" class="builtBy" target="_blank"><img class="builtBy" alt="Built by Maven" src="../images/logos/maven-feather.png" /></a>
</div>
</div>
</header>
<main id="bodyColumn" class="span10">
<section><a id="Generating_PR_Age_Charts"></a>
<h1>Generating PR Age Charts</h1>
<p>Build an interactive CLI tool that visualizes pull request age distribution for a GitHub repository using Copilot's built-in capabilities.</p><section><a id="Prerequisites"></a>
<h2>Prerequisites</h2>
<p>Install <a href="https://www.jbang.dev/" class="externalLink">JBang</a> to run this example:</p>
<pre class="prettyprint"><code class="language-bash"># macOS (using Homebrew)
brew install jbangdev/tap/jbang
# Linux/macOS (using curl)
curl -Ls https://sh.jbang.dev | bash -s - app setup
# Windows (using Scoop)
scoop install jbang
</code></pre></section><section><a id="Example_scenario"></a>
<h2>Example scenario</h2>
<p>You want to understand how long PRs have been open in a repository. This tool detects the current Git repo or accepts a repo as input, then lets Copilot fetch PR data via the GitHub MCP Server and generate a chart image.</p></section><section><a id="Usage"></a>
<h2>Usage</h2>
<pre class="prettyprint"><code class="language-bash"># Auto-detect from current git repo
jbang PRVisualization.java
# Specify a repo explicitly
jbang PRVisualization.java github/copilot-sdk
</code></pre></section><section><a id="Full_example.3A_PRVisualization.java"></a>
<h2>Full example: PRVisualization.java</h2>
<pre class="prettyprint"><code class="language-java">//DEPS com.github:copilot-sdk-java:0.3.0-java.2
import com.github.copilot.sdk.CopilotClient;
import com.github.copilot.sdk.generated.AssistantMessageEvent;
import com.github.copilot.sdk.generated.ToolExecutionStartEvent;
import com.github.copilot.sdk.json.MessageOptions;
import com.github.copilot.sdk.json.PermissionHandler;
import com.github.copilot.sdk.json.SessionConfig;
import com.github.copilot.sdk.json.SystemMessageConfig;
import java.io.BufferedReader;
import java.io.EOFException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.regex.Pattern;
public class PRVisualization {
public static void main(String[] args) throws Exception {
System.out.println("🔍 PR Age Chart Generator\n");
// Determine the repository
String repo;
if (args.length > 0) {
repo = args[0];
System.out.println("📦 Using specified repo: " + repo);
} else if (isGitRepo()) {
String detected = getGitHubRemote();
if (detected != null && !detected.isEmpty()) {
repo = detected;
System.out.println("📦 Detected GitHub repo: " + repo);
} else {
System.out.println("⚠️ Git repo found but no GitHub remote detected.");
repo = promptForRepo();
}
} else {
System.out.println("📁 Not in a git repository.");
repo = promptForRepo();
}
if (repo == null || !repo.contains("/")) {
System.err.println("❌ Invalid repo format. Expected: owner/repo");
System.exit(1);
}
String[] parts = repo.split("/", 2);
String owner = parts[0];
String repoName = parts[1];
// Create Copilot client
try (var client = new CopilotClient()) {
client.start().get();
String cwd = System.getProperty("user.dir");
var systemMessage = String.format("""
<context>
You are analyzing pull requests for the GitHub repository: %s/%s
The current working directory is: %s
</context>
<instructions>
- Use the GitHub MCP Server tools to fetch PR data
- Use your file and code execution tools to generate charts
- Save any generated images to the current working directory
- Be concise in your responses
</instructions>
""", owner, repoName, cwd);
var session = client.createSession(
new SessionConfig().setOnPermissionRequest(PermissionHandler.APPROVE_ALL)
.setModel("gpt-5")
.setSystemMessage(new SystemMessageConfig().setContent(systemMessage))
).get();
// Set up event handling
session.on(AssistantMessageEvent.class, msg ->
System.out.println("\n🤖 " + msg.getData().content() + "\n")
);
session.on(ToolExecutionStartEvent.class, evt ->
System.out.println(" ⚙️ " + evt.getData().toolName())
);
// Initial prompt - let Copilot figure out the details
System.out.println("\n📊 Starting analysis...\n");
String prompt = String.format("""
Fetch the open pull requests for %s/%s from the last week.
Calculate the age of each PR in days.
Then generate a bar chart image showing the distribution of PR ages
(group them into sensible buckets like <1 day, 1-3 days, etc.).
Save the chart as "pr-age-chart.png" in the current directory.
Finally, summarize the PR health - average age, oldest PR, and how many might be considered stale.
""", owner, repoName);
session.send(new MessageOptions().setPrompt(prompt));
// Wait a bit for initial processing
Thread.sleep(10000);
// Interactive loop
System.out.println("\n💡 Ask follow-up questions or type \"exit\" to quit.\n");
System.out.println("Examples:");
System.out.println(" - \"Expand to the last month\"");
System.out.println(" - \"Show me the 5 oldest PRs\"");
System.out.println(" - \"Generate a pie chart instead\"");
System.out.println(" - \"Group by author instead of age\"");
System.out.println();
try (var reader = new BufferedReader(new InputStreamReader(System.in))) {
while (true) {
System.out.print("You: ");
String input = reader.readLine();
if (input == null) break;
input = input.trim();
if (input.isEmpty()) continue;
if (input.equalsIgnoreCase("exit") || input.equalsIgnoreCase("quit")) {
System.out.println("👋 Goodbye!");
break;
}
session.send(new MessageOptions().setPrompt(input));
Thread.sleep(2000); // Give time for response
}
}
session.close();
}
}
// ============================================================================
// Git & GitHub Detection
// ============================================================================
private static boolean isGitRepo() {
try {
Process proc = Runtime.getRuntime().exec(new String[]{"git", "rev-parse", "--git-dir"});
return proc.waitFor() == 0;
} catch (Exception e) {
return false;
}
}
private static String getGitHubRemote() {
try {
Process proc = Runtime.getRuntime().exec(new String[]{"git", "remote", "get-url", "origin"});
try (BufferedReader reader = new BufferedReader(new InputStreamReader(proc.getInputStream()))) {
String remoteURL = reader.readLine();
if (remoteURL == null) return null;
remoteURL = remoteURL.trim();
// Handle SSH: git@github.com:owner/repo.git
var sshPattern = Pattern.compile("git@github\\.com:(.+/.+?)(?:\\.git)?$");
var sshMatcher = sshPattern.matcher(remoteURL);
if (sshMatcher.find()) {
return sshMatcher.group(1);
}
// Handle HTTPS: https://github.com/owner/repo.git
var httpsPattern = Pattern.compile("https://github\\.com/(.+/.+?)(?:\\.git)?$");
var httpsMatcher = httpsPattern.matcher(remoteURL);
if (httpsMatcher.find()) {
return httpsMatcher.group(1);
}
}
} catch (Exception e) {
// Ignore
}
return null;
}
private static String promptForRepo() throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter GitHub repo (owner/repo): ");
String line = reader.readLine();
if (line == null) {
throw new EOFException("End of input while reading repository name");
}
return line.trim();
}
}
</code></pre></section><section><a id="How_it_works"></a>
<h2>How it works</h2>
<ol style="list-style-type: decimal;">
<li><strong>Repository detection</strong>: Checks command-line argument → git remote → prompts user</li>
<li><strong>No custom tools</strong>: Relies entirely on Copilot CLI's built-in capabilities:
<ul>
<li><strong>GitHub MCP Server</strong> - Fetches PR data from GitHub</li>
<li><strong>File tools</strong> - Saves generated chart images</li>
<li><strong>Code execution</strong> - Generates charts using Python/matplotlib or other methods</li>
</ul></li>
<li><strong>Interactive session</strong>: After initial analysis, user can ask for adjustments</li>
</ol></section><section><a id="Why_this_approach.3F"></a>
<h2>Why this approach?</h2>
<table class="table table-striped">
<thead>
<tr class="a">
<th>Aspect</th>
<th>Custom Tools</th>
<th>Built-in Copilot</th></tr></thead><tbody>
<tr class="b">
<td>Code complexity</td>
<td>High</td>
<td><strong>Minimal</strong></td></tr>
<tr class="a">
<td>Maintenance</td>
<td>You maintain</td>
<td><strong>Copilot maintains</strong></td></tr>
<tr class="b">
<td>Flexibility</td>
<td>Fixed logic</td>
<td><strong>AI decides best approach</strong></td></tr>
<tr class="a">
<td>Chart types</td>
<td>What you coded</td>
<td><strong>Any type Copilot can generate</strong></td></tr>
<tr class="b">
<td>Data grouping</td>
<td>Hardcoded buckets</td>
<td><strong>Intelligent grouping</strong></td></tr></tbody>
</table></section></section> </main>
</div>
</div>
<hr/>
<footer>
<div class="container-fluid">
<div class="row-fluid">
<p>© 2026
</p>
</div>
</div>
</footer>
</body>
</html>