RGRTA edits and additions #1046
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: Comment on PRs that need DMFR formatting changes | |
| on: | |
| pull_request_target: | |
| paths: | |
| - 'feeds/**' | |
| permissions: | |
| issues: write | |
| pull-requests: write | |
| contents: read | |
| jobs: | |
| format: | |
| name: Check DMFR Formatting | |
| runs-on: ubuntu-latest | |
| steps: | |
| - uses: actions/checkout@v6 | |
| with: | |
| ref: ${{ github.event.pull_request.head.sha }} | |
| fetch-depth: 0 | |
| # Only needed for fork PRs - security check | |
| - name: Security check for fork PRs | |
| if: ${{ github.event.pull_request.head.repo.full_name != github.repository }} | |
| run: | | |
| # Check if PR modifies .github or scripts directories | |
| if git diff --name-only ${{ github.event.pull_request.head.sha }} ${{ github.event.pull_request.base.sha }} | grep -q -E "^\.github/|^scripts/"; then | |
| echo "Error: PR contains changes to .github or scripts directories, which is not allowed for security reasons" | |
| exit 1 | |
| fi | |
| - name: Install transitland-lib | |
| id: install | |
| run: scripts/install-transitland-lib.sh | |
| continue-on-error: false | |
| - name: Format DMFR files | |
| id: format | |
| run: | | |
| set -euo pipefail # Fail on any error | |
| find ./feeds -type f -name "*.dmfr.json" -exec transitland dmfr format --save {} \; | |
| - name: Check for formatting changes | |
| id: git-diff | |
| run: | | |
| set -euo pipefail # Fail on any error | |
| git_status_output="$(git status --porcelain)" || { | |
| echo "Error running git status: $?" | |
| exit 1 | |
| } | |
| if [ -z "$git_status_output" ]; then | |
| echo "No formatting changes needed" | |
| else | |
| echo "Formatting changes required" | |
| echo "Git status:" | |
| echo "$git_status_output" | |
| echo "Git diff:" | |
| git diff | |
| echo "git_diff<<EOF" >> "$GITHUB_OUTPUT" | |
| git diff >> "$GITHUB_OUTPUT" | |
| echo "EOF" >> "$GITHUB_OUTPUT" | |
| fi | |
| - name: Comment on PR | |
| if: ${{ steps.git-diff.outputs.git_diff != '' }} | |
| uses: actions/github-script@v7 | |
| env: | |
| DIFF: ${{ steps.git-diff.outputs.git_diff }} | |
| with: | |
| script: | | |
| const diff = process.env.DIFF; | |
| // Get existing bot comments to avoid duplicates | |
| const botComments = await github.paginate(github.rest.issues.listComments, { | |
| issue_number: context.issue.number, | |
| owner: context.repo.owner, | |
| repo: context.repo.repo | |
| }); | |
| const botComment = botComments.find(comment => | |
| comment.user.type === 'Bot' && | |
| comment.body.includes('DMFR Format Check Results') | |
| ); | |
| const commentBody = `## DMFR Format Check Results | |
| This PR includes files that need formatting adjustments to follow the "opinionated" DMFR format. | |
| ### Suggested Changes: | |
| \`\`\`diff | |
| ${diff} | |
| \`\`\` | |
| You can apply these changes by: | |
| 1. Using a code editor | |
| 2. Running [transitland-lib](https://github.com/interline-io/transitland-lib/blob/main/dmfr-command.md) locally | |
| 3. Waiting for a maintainer to help | |
| Thank you for contributing to Transitland Atlas! 🚀`; | |
| if (botComment) { | |
| // Update existing comment | |
| await github.rest.issues.updateComment({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| comment_id: botComment.id, | |
| body: commentBody | |
| }); | |
| } else { | |
| // Create new comment | |
| await github.rest.issues.createComment({ | |
| issue_number: context.issue.number, | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| body: commentBody | |
| }); | |
| } |