MASTG v1->v2 MASTG-TEST-0037: Testing WebViews Cleanup (android) #1130
Workflow file for this run
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
| name: Check Duplicate File IDs | |
| on: | |
| pull_request: | |
| paths: | |
| - 'apps/**' | |
| - 'best-practices/**' | |
| - 'demos/**' | |
| - 'tests-beta/**' | |
| - 'tools/**' | |
| - 'techniques/**' | |
| jobs: | |
| check-duplicates: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v4 | |
| with: | |
| fetch-depth: 1 | |
| - name: Set up Python | |
| uses: actions/setup-python@v4 | |
| with: | |
| python-version: '3.10' | |
| - name: Get new files in PR | |
| id: get-new-files | |
| run: | | |
| # Get the base branch ref from the PR event | |
| BASE_REF="${{ github.event.pull_request.base.ref }}" | |
| echo "Base branch is: $BASE_REF" | |
| # Fetch the base branch to ensure it's available locally | |
| git fetch origin $BASE_REF | |
| # Create a file with the list of new files in this PR | |
| git diff --name-status --diff-filter=A origin/$BASE_REF..HEAD | grep -E "^A\s+(apps/|best-practices/|demos/|tests-beta/|tools/|techniques/)" | cut -f2 > new_files_in_pr.txt || echo "No new files matching the pattern" | |
| echo "New files in PR:" | |
| cat new_files_in_pr.txt | |
| # Ensure the file exists even if empty | |
| touch new_files_in_pr.txt | |
| - name: Check for duplicate file IDs | |
| id: check-duplicates | |
| run: | | |
| python .github/scripts/check_duplicate_ids.py | |
| - name: Delete previous comments and comment on PR if duplicates found | |
| if: always() | |
| uses: actions/github-script@v6 | |
| with: | |
| github-token: ${{ secrets.GITHUB_TOKEN }} | |
| script: | | |
| const fs = require('fs'); | |
| // Helper function to format a notification message | |
| const formatDuplicateMessage = (duplicate, isReviewComment = true) => { | |
| // Extract the ID prefix for a more specific message | |
| let idType = duplicate.file_id.split('-').slice(0, 2).join('-'); | |
| // Choose the appropriate header style based on comment type | |
| const headerPrefix = isReviewComment ? '###' : '##'; | |
| return ` | |
| ${headerPrefix} ⚠️ Duplicate \`${idType}\` ID Detected | |
| ${isReviewComment ? 'This file' : `File \`${duplicate.file_path}\``} has the ID \`${duplicate.file_id}\` which already exists in \`${duplicate.existing_path}\`. | |
| **IMPORTANT:** Please use the next available ID: \`${duplicate.suggested_id}\` | |
| `; | |
| }; | |
| // Step 1: Try to clean up existing comments | |
| try { | |
| const comments = await github.rest.pulls.listReviewComments({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| pull_number: context.issue.number | |
| }); | |
| // Filter for comments from this action | |
| const ourComments = comments.data.filter(comment => | |
| comment.body.includes('⚠️ Duplicate') && | |
| comment.body.includes('ID Detected') | |
| ); | |
| console.log(`Found ${ourComments.length} previous comments from this action.`); | |
| // Delete the comments | |
| for (const comment of ourComments) { | |
| try { | |
| await github.rest.pulls.deleteReviewComment({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| comment_id: comment.id | |
| }); | |
| console.log(`Deleted comment ID ${comment.id}`); | |
| } catch (error) { | |
| console.log(`Error deleting comment ID ${comment.id}: ${error.message}`); | |
| } | |
| } | |
| } catch (error) { | |
| console.log(`Error handling existing comments: ${error.message}`); | |
| // Continue with the workflow regardless of errors with existing comments | |
| } | |
| // Step 2: Check if we need to post new notifications | |
| const hasDuplicatesOutput = '${{ steps.check-duplicates.outputs.has_duplicates }}'; | |
| if (hasDuplicatesOutput !== 'true' || !fs.existsSync('duplicate_files.json')) { | |
| console.log('No duplicates to report.'); | |
| return; | |
| } | |
| // Step 3: Post notifications for each duplicate | |
| const duplicatesData = JSON.parse(fs.readFileSync('duplicate_files.json', 'utf8')); | |
| for (const duplicate of duplicatesData) { | |
| // First try posting a review comment (preferred method) | |
| try { | |
| await github.rest.pulls.createReviewComment({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| pull_number: context.issue.number, | |
| commit_id: context.payload.pull_request.head.sha, | |
| path: duplicate.file_path, | |
| body: formatDuplicateMessage(duplicate, true), | |
| line: 1, | |
| side: 'RIGHT' | |
| }); | |
| console.log(`Successfully posted review comment for ${duplicate.file_path}`); | |
| } catch (error) { | |
| console.log(`Error posting review comment: ${error.message}`); | |
| // Fallback: post a comment on the PR thread instead of as a review comment | |
| try { | |
| await github.rest.issues.createComment({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| issue_number: context.issue.number, | |
| body: formatDuplicateMessage(duplicate, false) | |
| }); | |
| console.log(`Posted fallback PR thread comment for ${duplicate.file_path}`); | |
| } catch (fallbackError) { | |
| console.log(`Error posting fallback PR thread comment: ${fallbackError.message}`); | |
| } | |
| } | |
| } |