-
-
Notifications
You must be signed in to change notification settings - Fork 177
Fix: Add missing media attachments checker workflow #670
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
Closed
Closed
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
b7e3440
Fix: Add missing media attachments checker workflow
Aaqilyousuf 812cbab
Merge branch 'main' into fix/media-attachment
Aaqilyousuf a0acc16
Merge branch 'main' into fix/media-attachment
Aaqilyousuf 77300b6
chore: requested changes are made
Aaqilyousuf 477980e
enforce screenshot requirement on HTML changes
Aaqilyousuf a2c5a6c
enforce screenshot requirement on HTML changes
Aaqilyousuf 4416eb2
enforce screenshot requirement on HTML changes
Aaqilyousuf ebeda09
Merge branch 'main' into fix/media-attachment
Aaqilyousuf 67f7b7a
enforce screenshot requirement on HTML changes
Aaqilyousuf 503ec6f
enforce screenshot requirement on HTML changes
Aaqilyousuf f43167f
enforce screenshot requirement on HTML changes
Aaqilyousuf 6154dbc
Merge branch 'main' into fix/media-attachment
A1L13N 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
Some comments aren't visible on the classic Files Changed page.
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 |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| name: Check PR for Screenshots when HTML changes | ||
|
|
||
| on: | ||
| pull_request: | ||
| types: [opened, synchronize, reopened] | ||
| paths: | ||
| - '**/*.html' | ||
|
|
||
| jobs: | ||
| check-for-screenshots: | ||
| runs-on: ubuntu-latest | ||
| name: Check for PR screenshots | ||
| permissions: | ||
| contents: read | ||
| pull-requests: read | ||
| issues: write | ||
| steps: | ||
|
Aaqilyousuf marked this conversation as resolved.
|
||
| - name: Checkout code | ||
| uses: actions/checkout@v4 | ||
|
|
||
|
Aaqilyousuf marked this conversation as resolved.
|
||
| - name: Check for screenshots in PR description or comments | ||
| uses: actions/github-script@v7 | ||
| with: | ||
| github-token: ${{ secrets.GITHUB_TOKEN }} | ||
| script: | | ||
| const { owner, repo } = context.repo; | ||
| const prNumber = context.payload.pull_request.number; | ||
| const botCommentIdentifier = '<!-- media-check-bot -->'; | ||
|
|
||
| console.log(`Checking PR #${prNumber} for screenshots because HTML files were changed.`); | ||
|
|
||
| // --- 1. Get PR body + comments --- | ||
| const prBody = context.payload.pull_request.body || ''; | ||
|
|
||
| const comments = await github.paginate( | ||
| github.rest.issues.listComments, | ||
| { owner, repo, issue_number: prNumber } | ||
| ); | ||
|
|
||
| const allCommentsText = comments.map(comment => comment.body || '').join('\n'); | ||
| const combinedText = prBody + '\n' + allCommentsText; | ||
|
|
||
| // --- 2. Look for actual media (image/video uploads) --- | ||
|
|
||
| // Extract URLs from Markdown image syntax () | ||
| const mdImageUrls = [...combinedText.matchAll(/!\[[^\]]*\]\(([^)]+)\)/gi)].map(m => m[1]); | ||
|
|
||
| // Extract all plain URLs in the text | ||
| const allUrls = [...combinedText.matchAll(/\bhttps?:\/\/[^\s)>"']+/gi)].map(m => m[0]); | ||
|
|
||
| // Merge and deduplicate all URLs | ||
| const candidates = [...new Set([...mdImageUrls, ...allUrls])]; | ||
|
|
||
| // Define file extensions that count as media | ||
| const mediaExt = /\.(?:png|jpe?g|gif|webp|svg|apng|mp4|mov|webm)$/i; | ||
|
|
||
| // Define known GitHub hosts for attachments (including new user-attachments paths) | ||
| const ghAssetsHost = /(?:^|\/\/)(?:user-images\.githubusercontent\.com|private-user-images\.githubusercontent\.com|github\.com\/user-attachments\/assets|github-production-user-asset-[^/]+\.s3\.amazonaws\.com)\b/i; | ||
|
|
||
| // Filter candidate URLs: either matches media extensions OR GitHub attachment hosts | ||
| const mediaUrls = candidates.filter(u => mediaExt.test(u) || ghAssetsHost.test(u)); | ||
|
|
||
| // Final flag: true if at least one media URL found | ||
| const mediaFound = mediaUrls.length > 0; | ||
|
|
||
| // --- 3. Find previous bot comment --- | ||
| const botComment = comments.find(comment => | ||
| comment.user?.login === 'github-actions[bot]' && | ||
| comment.body?.includes(botCommentIdentifier) | ||
| ); | ||
|
|
||
|
Aaqilyousuf marked this conversation as resolved.
|
||
| if (mediaFound) { | ||
| console.log('✅ Screenshot or media found in the PR.'); | ||
| if (botComment) { | ||
| try { | ||
| await github.rest.issues.deleteComment({ | ||
| owner, | ||
| repo, | ||
| comment_id: botComment.id, | ||
| }); | ||
| console.log('Removed previous warning comment.'); | ||
| } catch (e) { | ||
| console.warn(`Could not delete previous comment (likely fork perms): ${e.status || ''}`); | ||
| } | ||
| } | ||
| return; | ||
| } | ||
|
|
||
| // --- 4. Post or update warning comment --- | ||
| const commentBody = `${botCommentIdentifier} | ||
|
|
||
| ## Screenshot Missing | ||
|
|
||
| You've modified **HTML files**, but no screenshots or recordings were found in the PR description or comments. | ||
|
|
||
| *Please attach a screenshot or GIF demonstrating the change.*`; | ||
|
|
||
|
Aaqilyousuf marked this conversation as resolved.
|
||
| if (botComment) { | ||
| await github.rest.issues.updateComment({ | ||
| owner, | ||
| repo, | ||
| comment_id: botComment.id, | ||
| body: commentBody, | ||
| }); | ||
| console.log('Updated existing warning comment.'); | ||
| } else { | ||
| await github.rest.issues.createComment({ | ||
| owner, | ||
| repo, | ||
| issue_number: prNumber, | ||
| body: commentBody, | ||
| }); | ||
| console.log('Created a new warning comment.'); | ||
| } | ||
|
|
||
| // --- 5. Fail workflow to block merging --- | ||
| core.setFailed('Missing screenshots for HTML changes. Please add one to continue.'); | ||
|
|
||
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.