-
Notifications
You must be signed in to change notification settings - Fork 31
114 lines (94 loc) · 4.08 KB
/
pr-artifacts-comment.yml
File metadata and controls
114 lines (94 loc) · 4.08 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
name: PR Artifacts Comment
# This workflow runs after the PR Build workflow completes
# It posts a comment with artifact links to the PR
# This approach avoids permission issues with PRs from forks
on:
workflow_run:
workflows: ["PR Build"]
types: [completed]
permissions:
actions: read
pull-requests: write
jobs:
comment:
# Only run for successful PR builds
if: >
github.event.workflow_run.event == 'pull_request' &&
github.event.workflow_run.conclusion == 'success'
runs-on: ubuntu-latest
steps:
- name: Get PR number
id: pr
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
PR_NUMBER=$(gh api repos/${{ github.repository }}/actions/runs/${{ github.event.workflow_run.id }} \
--jq '.pull_requests[0].number')
if [ -z "$PR_NUMBER" ] || [ "$PR_NUMBER" = "null" ]; then
echo "No PR found for this workflow run"
exit 0
fi
echo "pr_number=$PR_NUMBER" >> "$GITHUB_OUTPUT"
echo "Found PR #$PR_NUMBER"
- name: Get artifacts and post comment
if: steps.pr.outputs.pr_number != ''
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
PR_NUMBER: ${{ steps.pr.outputs.pr_number }}
RUN_ID: ${{ github.event.workflow_run.id }}
run: |
# Get artifact list
ARTIFACTS=$(gh api repos/${{ github.repository }}/actions/runs/$RUN_ID/artifacts \
--jq '.artifacts[] | "- **\(.name)** (\(.size_in_bytes / 1024 / 1024 | floor) MB)"' | sort)
if [ -z "$ARTIFACTS" ]; then
echo "No artifacts found for run $RUN_ID"
exit 0
fi
# Get version info from workflow run name or conclusion
VERSION=$(gh api repos/${{ github.repository }}/actions/runs/$RUN_ID \
--jq '.head_branch // "unknown"')
# Build comment body
MARKER="<!-- mcpproxy-pr-artifacts -->"
RUN_URL="https://github.com/${{ github.repository }}/actions/runs/$RUN_ID"
read -r -d '' COMMENT_BODY << 'ENDOFCOMMENT' || true
<!-- mcpproxy-pr-artifacts -->
## 📦 Build Artifacts
**Workflow Run:** [View Run](RUN_URL_PLACEHOLDER)
**Branch:** `VERSION_PLACEHOLDER`
### Available Artifacts
ARTIFACTS_PLACEHOLDER
### How to Download
**Option 1: GitHub Web UI** (easiest)
1. Go to the workflow run page linked above
2. Scroll to the bottom "Artifacts" section
3. Click on the artifact you want to download
**Option 2: GitHub CLI**
```bash
gh run download RUN_ID_PLACEHOLDER --repo REPO_PLACEHOLDER
```
> **Note:** Artifacts expire in 14 days.
ENDOFCOMMENT
# Replace placeholders
COMMENT_BODY="${COMMENT_BODY//RUN_URL_PLACEHOLDER/$RUN_URL}"
COMMENT_BODY="${COMMENT_BODY//VERSION_PLACEHOLDER/$VERSION}"
COMMENT_BODY="${COMMENT_BODY//ARTIFACTS_PLACEHOLDER/$ARTIFACTS}"
COMMENT_BODY="${COMMENT_BODY//RUN_ID_PLACEHOLDER/$RUN_ID}"
COMMENT_BODY="${COMMENT_BODY//REPO_PLACEHOLDER/${{ github.repository }}}"
# Remove leading spaces from heredoc
COMMENT_BODY=$(echo "$COMMENT_BODY" | sed 's/^ //')
# Check for existing comment
EXISTING_COMMENT=$(gh api repos/${{ github.repository }}/issues/$PR_NUMBER/comments \
--jq ".[] | select(.user.login == \"github-actions[bot]\" and (.body | contains(\"$MARKER\"))) | .id" | head -1)
if [ -n "$EXISTING_COMMENT" ]; then
# Update existing comment
gh api repos/${{ github.repository }}/issues/comments/$EXISTING_COMMENT \
-X PATCH \
-f body="$COMMENT_BODY"
echo "✅ Updated existing artifact comment (ID: $EXISTING_COMMENT)"
else
# Create new comment
gh api repos/${{ github.repository }}/issues/$PR_NUMBER/comments \
-X POST \
-f body="$COMMENT_BODY"
echo "✅ Created new artifact comment"
fi