Skip to content

Commit eba8ca3

Browse files
committed
attempt at fixing commenter
1 parent 597e89b commit eba8ca3

File tree

1 file changed

+218
-53
lines changed

1 file changed

+218
-53
lines changed

.github/workflows/pr_artifact_url_commenter.yml

Lines changed: 218 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -11,79 +11,244 @@ jobs:
1111
runs-on: ubuntu-latest
1212
if: ${{ github.event.workflow_run.conclusion == 'success' }}
1313
steps:
14-
- name: Set up GitHub CLI
15-
uses: actions/setup-gh@v2
16-
with:
17-
version: latest
18-
19-
- name: Auth GitHub CLI
20-
run: gh auth login --with-token <<< "${{ secrets.GITHUB_TOKEN }}"
21-
2214
- name: Get Artifact and Pull request info
2315
env:
2416
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
2517
WORKFLOW_RUN_EVENT_OBJ: ${{ toJSON(github.event.workflow_run) }}
2618
OWNER: ${{ github.repository_owner }}
2719
REPO: ${{ github.event.repository.name }}
2820
run: |
21+
# Extract workflow run info
2922
PREVIOUS_JOB_ID=$(jq -r '.id' <<< "$WORKFLOW_RUN_EVENT_OBJ")
3023
echo "PREVIOUS_JOB_ID=$PREVIOUS_JOB_ID" >> "$GITHUB_ENV"
3124
32-
SUITE_ID=$(jq -r '.check_suite_id' <<< "$WORKFLOW_RUN_EVENT_OBJ")
33-
echo "SUITE_ID=$SUITE_ID" >> "$GITHUB_ENV"
34-
35-
# Retry loop to wait for artifact availability
36-
for i in {1..5}; do
37-
ARTIFACT_ID=$(gh api "/repos/$OWNER/$REPO/actions/artifacts" \
38-
--jq ".artifacts[] | select(.workflow_run.id==$PREVIOUS_JOB_ID and .expired==false and .name==\"detailedEvolutionPlot.png\") | .id" | head -n1)
39-
if [[ -n "$ARTIFACT_ID" ]]; then break; fi
40-
echo "Artifact not found yet. Retrying in 10s..."
41-
sleep 10
42-
done
43-
echo "ARTIFACT_ID=$ARTIFACT_ID" >> "$GITHUB_ENV"
25+
HEAD_SHA="${{ github.event.workflow_run.head_sha }}"
26+
echo "HEAD_SHA=$HEAD_SHA" >> "$GITHUB_ENV"
4427
45-
PR_NUMBER=$(gh api "/repos/$OWNER/$REPO/commits/${{ github.event.workflow_run.head_sha }}/pulls" --jq '.[0].number')
46-
echo "PR_NUMBER=${PR_NUMBER:-null}" >> "$GITHUB_ENV"
28+
# Get PR number from the workflow run
29+
echo "Looking for PR associated with commit $HEAD_SHA..."
30+
PR_NUMBER=$(gh api "/repos/$OWNER/$REPO/commits/$HEAD_SHA/pulls" \
31+
--jq '.[0].number // empty' 2>/dev/null || echo "")
4732
48-
HEAD_SHA=${{ github.event.workflow_run.head_sha }}
49-
echo "HEAD_SHA=$HEAD_SHA" >> "$GITHUB_ENV"
33+
if [[ -n "$PR_NUMBER" && "$PR_NUMBER" != "null" ]]; then
34+
echo "Found PR #$PR_NUMBER"
35+
echo "PR_NUMBER=$PR_NUMBER" >> "$GITHUB_ENV"
36+
37+
# Look for the specific artifact
38+
ARTIFACT_NAME="detailedEvolutionPlot.png"
39+
echo "Searching for artifact: $ARTIFACT_NAME from workflow run $PREVIOUS_JOB_ID..."
40+
41+
# Retry loop with better error handling
42+
ARTIFACT_ID=""
43+
for i in {1..8}; do
44+
echo "Attempt $i/8: Looking for artifact..."
45+
46+
# Use more robust jq filter
47+
ARTIFACT_ID=$(gh api "/repos/$OWNER/$REPO/actions/artifacts" \
48+
--jq --arg workflow_id "$PREVIOUS_JOB_ID" --arg name "$ARTIFACT_NAME" \
49+
'.artifacts[] | select(.workflow_run.id == ($workflow_id | tonumber) and .expired == false and .name == $name) | .id' \
50+
2>/dev/null | head -n1 || echo "")
51+
52+
if [[ -n "$ARTIFACT_ID" ]]; then
53+
echo "Found artifact with ID: $ARTIFACT_ID"
54+
break
55+
fi
56+
57+
echo "Artifact not available yet, waiting 12s before retry..."
58+
sleep 12
59+
done
60+
61+
if [[ -z "$ARTIFACT_ID" ]]; then
62+
echo "Warning: Could not find artifact '$ARTIFACT_NAME' after 8 attempts"
63+
echo "This might be expected if the artifact generation failed or is still processing."
64+
fi
65+
66+
echo "ARTIFACT_ID=${ARTIFACT_ID:-}" >> "$GITHUB_ENV"
67+
else
68+
echo "No PR found for this workflow run (this is normal for direct pushes to main/dev)"
69+
echo "PR_NUMBER=" >> "$GITHUB_ENV"
70+
echo "ARTIFACT_ID=" >> "$GITHUB_ENV"
71+
fi
5072
5173
- name: Download artifact
52-
if: env.PR_NUMBER != 'null' && env.ARTIFACT_ID != ''
74+
if: env.PR_NUMBER != '' && env.ARTIFACT_ID != ''
5375
run: |
54-
gh api /repos/${{ github.repository }}/actions/artifacts/${{ env.ARTIFACT_ID }}/zip --output artifact.zip
55-
unzip artifact.zip
56-
rm artifact.zip
76+
echo "Downloading artifact ${{ env.ARTIFACT_ID }}..."
77+
78+
# Download with better error handling
79+
if gh api "/repos/${{ github.repository }}/actions/artifacts/${{ env.ARTIFACT_ID }}/zip" --output artifact.zip; then
80+
echo "Successfully downloaded artifact.zip"
81+
82+
# Extract and verify
83+
if unzip -q artifact.zip; then
84+
rm artifact.zip
85+
86+
if [[ -f "detailedEvolutionPlot.png" ]]; then
87+
echo "Found detailedEvolutionPlot.png"
88+
echo "File size: $(du -h detailedEvolutionPlot.png | cut -f1)"
89+
# Verify it's actually a PNG file
90+
if file detailedEvolutionPlot.png | grep -q "PNG image"; then
91+
echo "Verified as valid PNG image"
92+
else
93+
echo "Warning: File may not be a valid PNG"
94+
fi
95+
else
96+
echo "Error: detailedEvolutionPlot.png not found in extracted files"
97+
echo "Available files:"
98+
ls -la
99+
exit 1
100+
fi
101+
else
102+
echo "Error: Failed to extract artifact.zip"
103+
exit 1
104+
fi
105+
else
106+
echo "Error: Failed to download artifact"
107+
exit 1
108+
fi
57109
58-
- name: Update Comment
59-
if: env.PR_NUMBER != 'null' && env.ARTIFACT_ID != ''
110+
- name: Create success comment with artifact
111+
if: env.PR_NUMBER != '' && env.ARTIFACT_ID != ''
60112
env:
61113
JOB_PATH: "${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ env.PREVIOUS_JOB_ID }}"
62-
ARTIFACT_DL: "${{ github.server_url }}/${{ github.repository }}/actions/artifacts/${{ env.ARTIFACT_ID }}"
63-
uses: actions/github-script@v6
114+
ARTIFACT_DL: "${{ github.server_url }}/${{ github.repository }}/suites/${{ github.event.workflow_run.check_suite_id }}/artifacts/${{ env.ARTIFACT_ID }}"
115+
uses: actions/github-script@v7
64116
with:
65117
github-token: ${{secrets.GITHUB_TOKEN}}
66118
script: |
67119
const fs = require('fs');
68-
const imageData = fs.readFileSync('detailedEvolutionPlot.png', {encoding: 'base64'});
69-
github.rest.issues.createComment({
70-
issue_number: process.env.PR_NUMBER,
120+
121+
try {
122+
// Read and validate the image
123+
if (!fs.existsSync('detailedEvolutionPlot.png')) {
124+
throw new Error('detailedEvolutionPlot.png file not found');
125+
}
126+
127+
const stats = fs.statSync('detailedEvolutionPlot.png');
128+
console.log(`Image file size: ${(stats.size / 1024).toFixed(1)} KB`);
129+
130+
if (stats.size === 0) {
131+
throw new Error('detailedEvolutionPlot.png is empty');
132+
}
133+
134+
// GitHub has a ~65KB limit for image embeds, warn if approaching
135+
if (stats.size > 50000) {
136+
console.log('Warning: Image is quite large, may not display properly in GitHub');
137+
}
138+
139+
const imageData = fs.readFileSync('detailedEvolutionPlot.png', {encoding: 'base64'});
140+
const shortSha = process.env.HEAD_SHA.substring(0, 7);
141+
142+
const commentBody = \`![badge]
143+
144+
## Build Successful!
145+
146+
Your COMPAS compilation and test suite completed successfully!
147+
148+
| Item | Value |
149+
|------|-------|
150+
| **Commit** | [\\\`\${shortSha}\\\`](https://github.com/\${context.repo.owner}/\${context.repo.repo}/commit/\${process.env.HEAD_SHA}) |
151+
| **Logs** | [View workflow logs](\${process.env.JOB_PATH}) |
152+
| **Artifact** | [Download evolution plot](\${process.env.ARTIFACT_DL}) |
153+
154+
### Detailed Evolution Plot
155+
156+
<details>
157+
<summary>Click to view/hide evolution plot</summary>
158+
159+
![Detailed Evolution Plot](data:image/png;base64,\${imageData})
160+
161+
</details>
162+
163+
---
164+
<sub>Generated by COMPAS CI • [View workflow run](\${process.env.JOB_PATH})</sub>
165+
166+
[badge]: https://img.shields.io/badge/Build_Success-28a745?style=for-the-badge&logo=github-actions&logoColor=white\`;
167+
168+
await github.rest.issues.createComment({
169+
issue_number: parseInt(process.env.PR_NUMBER),
170+
owner: context.repo.owner,
171+
repo: context.repo.repo,
172+
body: commentBody
173+
});
174+
175+
console.log(`Successfully commented on PR #${process.env.PR_NUMBER} with artifact`);
176+
177+
} catch (error) {
178+
console.error('Error creating comment with artifact:', error);
179+
180+
// Fallback: create comment without image
181+
const fallbackBody = \`![badge]
182+
183+
## Build Successful!
184+
185+
Your COMPAS compilation completed successfully, but there was an issue displaying the evolution plot.
186+
187+
| Item | Value |
188+
|------|-------|
189+
| **Commit** | [\\\`\${process.env.HEAD_SHA.substring(0, 7)}\\\`](https://github.com/\${context.repo.owner}/\${context.repo.repo}/commit/\${process.env.HEAD_SHA}) |
190+
| **Logs** | [View workflow logs](\${process.env.JOB_PATH}) |
191+
| **Artifact** | [Download evolution plot](\${process.env.ARTIFACT_DL}) |
192+
193+
**Note:** Evolution plot could not be embedded (\${error.message})
194+
195+
---
196+
<sub>Generated by COMPAS CI • [View workflow run](\${process.env.JOB_PATH})</sub>
197+
198+
[badge]: https://img.shields.io/badge/Build_Success-28a745?style=for-the-badge&logo=github-actions&logoColor=white\`;
199+
200+
await github.rest.issues.createComment({
201+
issue_number: parseInt(process.env.PR_NUMBER),
202+
owner: context.repo.owner,
203+
repo: context.repo.repo,
204+
body: fallbackBody
205+
});
206+
207+
console.log(`Created fallback comment on PR #${process.env.PR_NUMBER}`);
208+
}
209+
210+
- name: Create success comment without artifact
211+
if: env.PR_NUMBER != '' && env.ARTIFACT_ID == ''
212+
env:
213+
JOB_PATH: "${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ env.PREVIOUS_JOB_ID }}"
214+
uses: actions/github-script@v7
215+
with:
216+
github-token: ${{secrets.GITHUB_TOKEN}}
217+
script: |
218+
const shortSha = process.env.HEAD_SHA.substring(0, 7);
219+
220+
const commentBody = \`![badge]
221+
222+
## Build Successful!
223+
224+
Your COMPAS compilation and test suite completed successfully!
225+
226+
| Item | Value |
227+
|------|-------|
228+
| **Commit** | [\\\`\${shortSha}\\\`](https://github.com/\${context.repo.owner}/\${context.repo.repo}/commit/\${process.env.HEAD_SHA}) |
229+
| **Logs** | [View workflow logs](\${process.env.JOB_PATH}) |
230+
231+
**Note:** The detailed evolution plot artifact was not found or is not yet available. This might be expected if the plot generation step was skipped or failed.
232+
233+
---
234+
<sub>Generated by COMPAS CI • [View workflow run](\${process.env.JOB_PATH})</sub>
235+
236+
[badge]: https://img.shields.io/badge/Build_Success-28a745?style=for-the-badge&logo=github-actions&logoColor=white\`;
237+
238+
await github.rest.issues.createComment({
239+
issue_number: parseInt(process.env.PR_NUMBER),
71240
owner: context.repo.owner,
72-
repo: context.repo.name,
73-
body: `
74-
![badge]
75-
76-
Build Successful! You can find a link to the downloadable artifact below.
77-
78-
| Name | Link |
79-
| -------- | ----------------------- |
80-
| Commit | ${process.env.HEAD_SHA} |
81-
| Logs | ${process.env.JOB_PATH} |
82-
| Download | ${process.env.ARTIFACT_DL} |
83-
84-
### Detailed Evolution Plot
85-
![Detailed Evolution Plot](data:image/png;base64,${imageData})
86-
87-
[badge]: https://img.shields.io/badge/Build_Success!-0d1117?style=for-the-badge&labelColor=3fb950
88-
`
89-
})
241+
repo: context.repo.repo,
242+
body: commentBody
243+
});
244+
245+
console.log(`Successfully commented on PR #${process.env.PR_NUMBER} (no artifact)`);
246+
247+
- name: Debug info for non-PR runs
248+
if: env.PR_NUMBER == ''
249+
run: |
250+
echo "This workflow run was not associated with a pull request"
251+
echo "Head SHA: ${{ github.event.workflow_run.head_sha }}"
252+
echo "Event: ${{ github.event.workflow_run.event }}"
253+
echo "Branch: ${{ github.event.workflow_run.head_branch }}"
254+
echo "This is normal for direct pushes to main/dev branches"

0 commit comments

Comments
 (0)