Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions .github/workflows/issue-triage.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: AI Issue Triage

on:
issues:
types: [opened]

jobs:
triage:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.10'

- name: Install dependencies
run: |
pip install google-generativeai PyGithub

- name: Run AI Bug Triager
env:
GEMINI_API_KEY: ${{ secrets.GEMINI_API_KEY }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
python scripts/ai_bug_triager.py
--repo "${{ github.repository }}"
--issue "${{ github.event.issue.number }}"
--github-token "$GITHUB_TOKEN"
--gemini-api-key "$GEMINI_API_KEY"
Comment on lines +9 to +32

Check warning

Code scanning / CodeQL

Workflow does not contain permissions Medium

Actions job or workflow does not limit the permissions of the GITHUB_TOKEN. Consider setting an explicit permissions block, using the following as a minimal starting point: {contents: read}

Copilot Autofix

AI 2 months ago

In general, this issue is fixed by adding an explicit permissions block to the workflow (either at the root level or within the specific job) that grants only the minimal scopes the job needs. For an issue‑triage workflow, the likely needs are read access to repository contents plus the ability to read/write issues (e.g., comment, label, close). That suggests contents: read and issues: write as a good least‑privilege baseline instead of inheriting broad default permissions.

The best fix here, without changing existing functionality, is to add a permissions block to the triage job (or at the workflow root). Since we only see a single job, putting it at the job level keeps the change tightly scoped. Based on the workflow’s purpose (β€œAI Issue Triage”) and the fact that it passes GITHUB_TOKEN to a script that likely manipulates issues, we will explicitly grant contents: read and issues: write. This both satisfies CodeQL (by constraining the token) and documents the workflow’s expected permissions. The specific change is to insert, in .github/workflows/issue-triage.yml, a permissions: section under triage: before runs-on: ubuntu-latest. No additional imports, methods, or external definitions are needed, as this is purely a YAML configuration change for GitHub Actions.

Suggested changeset 1
.github/workflows/issue-triage.yml

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/.github/workflows/issue-triage.yml b/.github/workflows/issue-triage.yml
--- a/.github/workflows/issue-triage.yml
+++ b/.github/workflows/issue-triage.yml
@@ -6,6 +6,9 @@
 
 jobs:
   triage:
+    permissions:
+      contents: read
+      issues: write
     runs-on: ubuntu-latest
     steps:
     - name: Checkout repository
EOF
@@ -6,6 +6,9 @@

jobs:
triage:
permissions:
contents: read
issues: write
runs-on: ubuntu-latest
steps:
- name: Checkout repository
Copilot is powered by AI and may make mistakes. Always verify output.
Unable to commit as this autofix suggestion is now outdated
120 changes: 119 additions & 1 deletion .github/workflows/pr-review.yml
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,122 @@ jobs:
owner: context.repo.owner,
repo: context.repo.repo,
body: comment
});
});

ai-code-review:
runs-on: ubuntu-latest
needs: quality-and-safety
if: github.event.pull_request.draft == false

steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.10'

- name: Install dependencies
run: |
pip install google-generativeai PyGithub

- name: Run AI Reviewer
env:
GEMINI_API_KEY: ${{ secrets.GEMINI_API_KEY }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
python scripts/ai_pr_reviewer.py \
--repo "${{ github.repository }}" \
--pr "${{ github.event.pull_request.number }}" \
--github-token "$GITHUB_TOKEN" \
--gemini-api-key "$GEMINI_API_KEY"

trading-risk-guard:
runs-on: ubuntu-latest
needs: quality-and-safety
if: github.event.pull_request.draft == false

steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.10'

- name: Run Trading Risk Guard
run: |
# Get list of changed files
CHANGED_FILES=$(git diff --name-only origin/main...HEAD | tr '\n' ' ')
if [ ! -z "$CHANGED_FILES" ]; then
python scripts/trading_risk_guard.py $CHANGED_FILES
else
echo "No files changed, skipping scan."
fi

automated-backtest:
runs-on: ubuntu-latest
needs: quality-and-safety
if: github.event.pull_request.draft == false

steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.10'

- name: Install dependencies
run: |
pip install google-generativeai PyGithub pandas upstox-python-sdk

- name: Run Automated Backtest
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
UPSTOX_ACCESS_TOKEN: ${{ secrets.UPSTOX_ACCESS_TOKEN }}
run: |
# Run the backtest script for changed strategies
python scripts/automated_backtest.py \
--repo "${{ github.repository }}" \
--pr "${{ github.event.pull_request.number }}" \
--github-token "$GITHUB_TOKEN" \
--upstox-token "$UPSTOX_ACCESS_TOKEN"

docs-and-changelog:
runs-on: ubuntu-latest
needs: quality-and-safety
if: github.event.pull_request.draft == false

steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.10'

- name: Install dependencies
run: |
pip install google-generativeai PyGithub

- name: Run AI Docs Agent
env:
GEMINI_API_KEY: ${{ secrets.GEMINI_API_KEY }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
python scripts/ai_docs_agent.py \
--repo "${{ github.repository }}" \
--pr "${{ github.event.pull_request.number }}" \
--github-token "$GITHUB_TOKEN" \
--gemini-api-key "$GEMINI_API_KEY"
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ gast==0.6.0
google-auth==2.23.4
google-auth-httplib2==0.2.0
google-auth-oauthlib==1.1.0
google-generativeai==0.8.3
PyGithub==2.5.0
google-pasta==0.2.0
greenlet==3.1.1
grpcio==1.71.0
Expand Down
96 changes: 96 additions & 0 deletions scripts/ai_bug_triager.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import os
import sys
import argparse
import google.generativeai as genai
from github import Github

def triage_issue(repo_name, issue_number, github_token, gemini_api_key):
"""
AI Bug Triager Agent.
Analyzes new issues and suggests root causes and fixes.
"""
# 1. Setup
g = Github(github_token)
repo = g.get_repo(repo_name)
issue = repo.get_issue(int(issue_number))

genai.configure(api_key=gemini_api_key)
model = genai.GenerativeModel('gemini-1.5-pro')

# 2. Analyze Issue Content
print(f"🐞 Triaging Issue #{issue_number}: {issue.title}")

# Get a list of key files to help the AI map the issue
# We'll provide a simplified directory structure
file_structure = """
- app.py (Main Entry)
- services/ (Trading, Auth, Data Services)
- strategies/ (Trading Strategies)
- ui/ (Frontend React Code)
- database/ (Models and Repositories)
- core/ (Config, Security, WebSockets)
"""

prompt = f"""
You are a Senior Debugging Engineer for an Algorithmic Trading Platform.
Analyze the following GitHub Issue and suggest a root cause and fix.

Issue Title: {issue.title}
Issue Body: {issue.body}

Codebase Overview:
{file_structure}

Tasks:
1. Identify the likely files involved.
2. If there's a stack trace, explain what the error means.
3. Suggest a specific fix or investigation steps.
4. Assign a priority (Low, Medium, High, Critical).

Format your response as:
### πŸ•΅οΈ AI Diagnosis
- **Likely Files:** [e.g., services/upstox_service.py]
- **Root Cause:** [Explain what's happening]
- **Priority:** [Priority Level]

### πŸ› οΈ Suggested Fix
```python
# [Your suggested code or investigation steps]
```
"""

response = model.generate_content(prompt)
ai_output = response.text.strip()

# 3. Post to GitHub
comment = f"## πŸ€– AI Bug Triager Response

{ai_output}

*This is an automated response to help speed up resolution.*"
issue.create_comment(comment)

# 4. Add labels based on priority (if AI suggests)
if "Priority: Critical" in ai_output:
issue.add_to_labels("critical", "bug")
elif "Priority: High" in ai_output:
issue.add_to_labels("high-priority", "bug")
else:
issue.add_to_labels("bug")

print("βœ… Issue triage completed.")

if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--repo", required=True)
parser.add_argument("--issue", required=True)
parser.add_argument("--github-token", required=True)
parser.add_argument("--gemini-api-key", required=True)

args = parser.parse_args()

try:
triage_issue(args.repo, args.issue, args.github_token, args.gemini_api_key)
except Exception as e:
print(f"❌ Error in Bug Triager: {e}")
sys.exit(1)
82 changes: 82 additions & 0 deletions scripts/ai_docs_agent.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import os
import sys
import argparse
import google.generativeai as genai
from github import Github
from datetime import datetime

def generate_docs_and_changelog(repo_name, pr_number, github_token, gemini_api_key):
"""
AI Documentation & Changelog Agent.
Analyzes the PR and generates a semantic changelog and doc updates.
"""
# 1. Setup
g = Github(github_token)
repo = g.get_repo(repo_name)
pr = repo.get_pull(int(pr_number))

genai.configure(api_key=gemini_api_key)
model = genai.GenerativeModel('gemini-1.5-pro')

# 2. Analyze PR Diff for Documentation Needs
print(f"πŸ“„ Analyzing PR #{pr_number} for documentation impact...")
files = pr.get_files()
file_list = [f.filename for f in files]

# Combined diff for better context (limiting size for LLM)
combined_diff = "
".join([f.patch for f in files if f.patch])[:10000]

prompt = f"""
You are a Technical Writer and Senior Developer. Review the following Pull Request details and diff.
PR Title: {pr.title}
PR Body: {pr.body}
Changed Files: {', '.join(file_list)}

Tasks:
1. Generate a "Semantic Changelog" entry (What changed? Why? Impact?).
2. Identify if any NEW services, strategies, or APIs were added that require new documentation files.
3. Suggest updates to the main README.md or existing docs/ files if necessary.

Diff:
{combined_diff}

Format your response as:
### πŸ“ Semantic Changelog
[A human-readable summary of the changes]

### πŸ“š Documentation Impact
- [Impact 1: e.g., Update README to include new Upstox endpoint]
- [Impact 2: e.g., New strategy 'X' added to docs/strategies/]
"""

response = model.generate_content(prompt)
ai_output = response.text.strip()

# 3. Post to GitHub
comment = f"## πŸ€– AI Documentation & Changelog Agent

{ai_output}

"
pr.create_issue_comment(comment)
print("βœ… Documentation & Changelog suggestions posted.")

# 4. (Optional) Auto-update CHANGELOG.md if in a specific branch/environment
# For now, we'll just suggest the update to keep it safe.

if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--repo", required=True)
parser.add_argument("--pr", required=True)
parser.add_argument("--github-token", required=True)
parser.add_argument("--gemini-api-key", required=True)

args = parser.parse_args()

try:
generate_docs_and_changelog(args.repo, args.pr, args.github_token, args.gemini_api_key)
print("βœ… Docs Agent run completed.")
except Exception as e:
print(f"❌ Error in Docs Agent: {e}")
sys.exit(1)
Loading
Loading