-
Notifications
You must be signed in to change notification settings - Fork 653
chore: update PR visualization example to work with latest sdk #214
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
john0isaac
wants to merge
4
commits into
github:main
Choose a base branch
from
john0isaac:fix/pr-visualizer-example
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+101
−78
Open
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
836a4fb
chore: update pr visualization example to latest version of sdk
john0isaac 844493d
docs: update pr visualization documentation to match python example
john0isaac 727b022
fix: apply pr review comments
john0isaac fc22d03
revert log level change
john0isaac File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -27,49 +27,46 @@ pip install copilot-sdk | |
|
|
||
| ```bash | ||
| # Auto-detect from current git repo | ||
| python pr_breakdown.py | ||
| python pr_visualization.py | ||
|
|
||
| # Specify a repo explicitly | ||
| python pr_breakdown.py --repo github/copilot-sdk | ||
| python pr_visualization.py --repo github/copilot-sdk | ||
| ``` | ||
|
|
||
| ## Full example: pr_breakdown.py | ||
| ## Full example: pr_visualization.py | ||
|
|
||
| ```python | ||
| #!/usr/bin/env python3 | ||
|
|
||
| import os | ||
| import re | ||
| import subprocess | ||
| import sys | ||
| import os | ||
| from copilot import CopilotClient | ||
|
|
||
| from copilot import CopilotClient, SessionConfig | ||
| from copilot.generated.session_events import SessionEvent, SessionEventType | ||
| from copilot.types import CopilotClientOptions, MessageOptions, SystemMessageAppendConfig | ||
|
|
||
| # ============================================================================ | ||
| # Git & GitHub Detection | ||
| # ============================================================================ | ||
|
|
||
| def is_git_repo(): | ||
| try: | ||
| subprocess.run( | ||
| ["git", "rev-parse", "--git-dir"], | ||
| check=True, | ||
| capture_output=True | ||
| ) | ||
| subprocess.run(["git", "rev-parse", "--git-dir"], check=True, capture_output=True) | ||
| return True | ||
| except (subprocess.CalledProcessError, FileNotFoundError): | ||
| return False | ||
|
|
||
|
|
||
| def get_github_remote(): | ||
| try: | ||
| result = subprocess.run( | ||
| ["git", "remote", "get-url", "origin"], | ||
| check=True, | ||
| capture_output=True, | ||
| text=True | ||
| ["git", "remote", "get-url", "origin"], check=True, capture_output=True, text=True | ||
| ) | ||
| remote_url = result.stdout.strip() | ||
|
|
||
| # Handle SSH: [email protected]:owner/repo.git | ||
| import re | ||
| ssh_match = re.search(r"git@github\.com:(.+/.+?)(?:\.git)?$", remote_url) | ||
| if ssh_match: | ||
| return ssh_match.group(1) | ||
|
|
@@ -91,14 +88,17 @@ def parse_args(): | |
| return {"repo": args[idx + 1]} | ||
| return {} | ||
|
|
||
|
|
||
| def prompt_for_repo(): | ||
| return input("Enter GitHub repo (owner/repo): ").strip() | ||
|
|
||
|
|
||
| # ============================================================================ | ||
| # Main Application | ||
| # ============================================================================ | ||
|
|
||
| def main(): | ||
|
|
||
| async def main(): | ||
| print("🔍 PR Age Chart Generator\n") | ||
|
|
||
| # Determine the repository | ||
|
|
@@ -126,14 +126,17 @@ def main(): | |
|
|
||
| owner, repo_name = repo.split("/", 1) | ||
|
|
||
| options = CopilotClientOptions( | ||
| log_level="info", | ||
| ) | ||
| # Create Copilot client - no custom tools needed! | ||
| client = CopilotClient(log_level="error") | ||
| client.start() | ||
| client = CopilotClient(options=options) | ||
| await client.start() | ||
|
|
||
| session = client.create_session( | ||
| session_config = SessionConfig( | ||
| model="gpt-5", | ||
| system_message={ | ||
| "content": f""" | ||
| system_message=SystemMessageAppendConfig( | ||
| content=f""" | ||
| <context> | ||
| You are analyzing pull requests for the GitHub repository: {owner}/{repo_name} | ||
| The current working directory is: {os.getcwd()} | ||
|
|
@@ -146,39 +149,43 @@ The current working directory is: {os.getcwd()} | |
| - Be concise in your responses | ||
| </instructions> | ||
| """ | ||
| } | ||
| ), | ||
| ) | ||
|
|
||
| session = await client.create_session(config=session_config) | ||
|
|
||
| # Set up event handling | ||
| def handle_event(event): | ||
| if event["type"] == "assistant.message": | ||
| print(f"\n🤖 {event['data']['content']}\n") | ||
| elif event["type"] == "tool.execution_start": | ||
| print(f" ⚙️ {event['data']['toolName']}") | ||
| def handle_event(event: SessionEvent): | ||
| if event.type == SessionEventType.ASSISTANT_MESSAGE: | ||
| print(f"\n🤖 {event.data.content}\n") | ||
| elif event.type == SessionEventType.TOOL_EXECUTION_START: | ||
| print(f" ⚙️ {event.data.tool_name}") | ||
|
|
||
| session.on(handle_event) | ||
|
|
||
| # Initial prompt - let Copilot figure out the details | ||
| print("\n📊 Starting analysis...\n") | ||
|
|
||
| session.send(prompt=f""" | ||
| message_options = MessageOptions( | ||
| prompt=f""" | ||
| Fetch the open pull requests for {owner}/{repo_name} 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. | ||
| """) | ||
| """, | ||
| ) | ||
john0isaac marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| session.wait_for_idle() | ||
| await session.send_and_wait(options=message_options, timeout=300.0) | ||
|
|
||
| # Interactive loop | ||
| print("\n💡 Ask follow-up questions or type \"exit\" to quit.\n") | ||
| print('\n💡 Ask follow-up questions or type "exit" to quit.\n') | ||
| print("Examples:") | ||
| print(" - \"Expand to the last month\"") | ||
| print(" - \"Show me the 5 oldest PRs\"") | ||
| print(" - \"Generate a pie chart instead\"") | ||
| print(" - \"Group by author instead of age\"") | ||
| print(' - "Expand to the last month"') | ||
| print(' - "Show me the 5 oldest PRs"') | ||
| print(' - "Generate a pie chart instead"') | ||
| print(' - "Group by author instead of age"') | ||
| print() | ||
|
|
||
| while True: | ||
|
|
@@ -189,13 +196,17 @@ The current working directory is: {os.getcwd()} | |
| break | ||
|
|
||
| if user_input: | ||
| session.send(prompt=user_input) | ||
| session.wait_for_idle() | ||
| message_options = MessageOptions(prompt=user_input) | ||
john0isaac marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| await session.send_and_wait(options=message_options, timeout=300.0) | ||
|
|
||
| await session.destroy() | ||
| await client.stop() | ||
|
|
||
| client.stop() | ||
|
|
||
| if __name__ == "__main__": | ||
| main() | ||
| import asyncio | ||
|
|
||
| asyncio.run(main()) | ||
| ``` | ||
|
|
||
| ## How it works | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.