Skip to content
Draft
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
192 changes: 192 additions & 0 deletions .github/workflows/upmerge-auto-comment.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
name: Post Auto Upmerge Results

on:
workflow_run:
workflows: ["Auto Upmerge"]
types: [completed]

jobs:
post-upmerge-results:
runs-on: ubuntu-latest
if: always()
permissions:
contents: read
pull-requests: write
actions: read
steps:
- name: "Download Auto Upmerge Results"
uses: actions/download-artifact@v4
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
run-id: ${{ github.event.workflow_run.id }}
pattern: "auto-upmerge-results-pr-*"
merge-multiple: true
path: artifacts/

- name: "Setup Python"
uses: actions/setup-python@v5
with:
python-version: "3.8"

- name: "Post Results Comment"
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GITHUB_REPOSITORY: ${{ github.repository }}
WORKFLOW_RUN_ID: ${{ github.event.workflow_run.id }}
WORKFLOW_CONCLUSION: ${{ github.event.workflow_run.conclusion }}
shell: python
run: |
import os
import json
import sys
import subprocess
from pathlib import Path

def find_results_file():
artifacts_path = Path('artifacts')

if not artifacts_path.exists():
print('Artifacts directory not found')
return None

files = list(artifacts_path.iterdir())
print(f'Available files: {[f.name for f in files]}')

for file in files:
if file.name.endswith('auto-upmerge-results.json'):
return file

return None

def load_results(results_file):
try:
with open(results_file, 'r') as f:
return json.load(f)
except Exception as e:
print(f'Error loading results file: {e}')
return None

def build_comment_body(results, workflow_conclusion):
pr_info = results.get('source_pr', {})
pr_number = pr_info.get('number', 'unknown')
source_branch = results.get('source_branch', 'unknown')

successful = results.get('successful_upmerges', [])
failed = results.get('failed_upmerges', [])
attempted = results.get('attempted_branches', [])

comment_body = "## 🔄 Auto Upmerge Results\n\n"

if workflow_conclusion == 'success' and successful and not failed:
comment_body += "**Status**: ✅ All upmerges completed successfully\n"
elif successful and failed:
comment_body += "**Status**: ⚠️ Partial success - some upmerges failed\n"
elif failed and not successful:
comment_body += "**Status**: ❌ All upmerges failed\n"
else:
comment_body += "**Status**: ❌ Auto upmerge workflow failed\n"

comment_body += f"**Source PR**: #{pr_number} - {pr_info.get('title', 'Unknown')}\n"
comment_body += f"**Source Branch**: `{source_branch}`\n"
comment_body += f"**Timestamp**: {results.get('timestamp', 'unknown')}\n\n"

if successful:
comment_body += f"### ✅ Successful Upmerges ({len(successful)})\n\n"
for branch in successful:
branch_info = next((b for b in attempted if b['branch'] == branch), {})
version = branch_info.get('version', 'unknown')
comment_body += f"- `{branch}` (version: {version}) - PR created for manual review\n"
comment_body += "\n"

if failed:
comment_body += f"### ❌ Failed Upmerges ({len(failed)})\n\n"
for branch in failed:
branch_info = next((b for b in attempted if b['branch'] == branch), {})
version = branch_info.get('version', 'unknown')
comment_body += f"- `{branch}` (version: {version}) - Manual intervention required\n"
comment_body += "\n**How To Resolve**: Check the auto upmerge workflow logs for specific error details. You may need to resolve conflicts manually.\n\n"

if not successful and not failed:
comment_body += "No upmerge attempts were made. This could be due to:\n"
comment_body += "- No branches were marked as successful in the upmerge test\n"
comment_body += "- The auto upmerge workflow failed before attempting any upmerges\n\n"

comment_body += "*This comment was automatically generated by the auto upmerge workflow.*"

return comment_body

def post_github_comment(pr_number, comment_body):
if not pr_number or pr_number == 'unknown':
print('No valid PR number found - cannot post comment')
return False

comments_url = f'https://api.github.com/repos/{os.environ["GITHUB_REPOSITORY"]}/issues/{pr_number}/comments'
comment_data = json.dumps({"body": comment_body})

result = subprocess.run([
'curl', '-X', 'POST', comments_url,
'-H', 'Content-Type: application/json',
'-H', f'Authorization: token {os.environ["GITHUB_TOKEN"]}',
'--data', comment_data
], capture_output=True, text=True)

if result.returncode == 0:
print('Successfully posted upmerge results comment')
return True
else:
print(f'Failed to post comment: {result.stderr}')
return False

def post_fallback_comment(workflow_conclusion):
fallback_comment = (
"## 🔄 Auto Upmerge Results\n\n"
"**Status**: ❌ Auto upmerge workflow failed\n"
"**How To Resolve**: Check the auto upmerge workflow logs for error details. "
"The workflow may have failed before generating detailed results.\n\n"
f"**Workflow Status**: {workflow_conclusion}\n\n"
"*This comment was automatically generated by the auto upmerge workflow.*"
)

# Try to get PR number from the original upmerge test artifacts or workflow
# This is a fallback, so we might not have all the context
print('Posting fallback comment - detailed results not available')
return fallback_comment

try:
workflow_conclusion = os.environ.get('WORKFLOW_CONCLUSION', 'unknown')

results_file = find_results_file()

if not results_file:
print('No auto upmerge results found - posting fallback comment')
fallback_comment = post_fallback_comment(workflow_conclusion)
# We don't know the PR number in this case, so we can't post a comment
print('Cannot determine PR number for fallback comment')
sys.exit(0)

print(f'Found results file: {results_file.name}')

results = load_results(results_file)
if not results:
print('Could not load results - cannot post comment')
sys.exit(1)

print(f'Loaded results: {json.dumps(results, indent=2)}')

pr_number = results.get('source_pr', {}).get('number')
if not pr_number:
print('No PR number in results - cannot post comment')
sys.exit(1)

comment_body = build_comment_body(results, workflow_conclusion)
success = post_github_comment(pr_number, comment_body)

if not success:
print('Failed to post comment')
sys.exit(1)

print('Successfully processed auto upmerge results')

except Exception as e:
print(f'Error processing auto upmerge results: {e}')
sys.exit(1)
Loading