|
| 1 | +name: Fix completion snapshots on command |
| 2 | + |
| 3 | +on: |
| 4 | + issue_comment: |
| 5 | + types: [created] |
| 6 | + |
| 7 | +permissions: |
| 8 | + contents: write |
| 9 | + pull-requests: write |
| 10 | + issues: write |
| 11 | + |
| 12 | +jobs: |
| 13 | + fix-completions: |
| 14 | + # Only run on PR comments that start with /fixcompletions or /completions |
| 15 | + if: github.event.issue.pull_request && |
| 16 | + (startsWith(github.event.comment.body, '/fixcompletions') || startsWith(github.event.comment.body, '/completions')) |
| 17 | + runs-on: ubuntu-latest |
| 18 | + |
| 19 | + steps: |
| 20 | + - name: React to comment |
| 21 | + uses: actions/github-script@v7 |
| 22 | + with: |
| 23 | + script: | |
| 24 | + await github.rest.reactions.createForIssueComment({ |
| 25 | + owner: context.repo.owner, |
| 26 | + repo: context.repo.repo, |
| 27 | + comment_id: context.payload.comment.id, |
| 28 | + content: 'eyes' |
| 29 | + }); |
| 30 | +
|
| 31 | + - name: Comment on PR - Started |
| 32 | + uses: actions/github-script@v7 |
| 33 | + with: |
| 34 | + script: | |
| 35 | + const runUrl = `https://github.com/${context.repo.owner}/${context.repo.repo}/actions/runs/${process.env.GITHUB_RUN_ID}`; |
| 36 | + await github.rest.issues.createComment({ |
| 37 | + owner: context.repo.owner, |
| 38 | + repo: context.repo.repo, |
| 39 | + issue_number: context.payload.issue.number, |
| 40 | + body: `▶️ CLI completion snapshot update started. Track progress in [this workflow run](${runUrl}).` |
| 41 | + }); |
| 42 | +
|
| 43 | + - name: Checkout repository |
| 44 | + uses: actions/checkout@v4 |
| 45 | + with: |
| 46 | + token: ${{ secrets.GITHUB_TOKEN }} |
| 47 | + fetch-depth: 0 |
| 48 | + |
| 49 | + - name: Checkout PR branch |
| 50 | + run: | |
| 51 | + gh pr checkout ${{ github.event.issue.number }} |
| 52 | + env: |
| 53 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 54 | + |
| 55 | + - name: Build repository |
| 56 | + id: build |
| 57 | + run: | |
| 58 | + chmod +x ./build.sh |
| 59 | + ./build.sh |
| 60 | + continue-on-error: true |
| 61 | + timeout-minutes: 15 |
| 62 | + |
| 63 | + - name: Run completion tests |
| 64 | + id: test |
| 65 | + if: steps.build.outcome == 'success' |
| 66 | + run: | |
| 67 | + # Use the repo-local dotnet that was built |
| 68 | + ./.dotnet/dotnet test test/dotnet.Tests/dotnet.Tests.csproj --filter "FullyQualifiedName~VerifyCompletions" |
| 69 | + continue-on-error: true |
| 70 | + timeout-minutes: 10 |
| 71 | + |
| 72 | + - name: Compare snapshots |
| 73 | + id: compare |
| 74 | + if: steps.test.outcome != 'skipped' |
| 75 | + run: | |
| 76 | + # Use repo-local dotnet so we only restore the project we need on the runner |
| 77 | + ./.dotnet/dotnet msbuild test/dotnet.Tests/dotnet.Tests.csproj -restore -t:CompareCliSnapshots |
| 78 | + continue-on-error: true |
| 79 | + |
| 80 | + - name: Check for snapshot changes |
| 81 | + id: check-changes |
| 82 | + if: steps.compare.outcome == 'success' |
| 83 | + run: | |
| 84 | + # Detect new received files (ignored by git) and diff-ed verified files |
| 85 | + shopt -s nullglob |
| 86 | + received_files=(test/dotnet.Tests/CompletionTests/snapshots/**/*.received.*) |
| 87 | + shopt -u nullglob |
| 88 | +
|
| 89 | + diff_output=$(git diff --name-only -- test/dotnet.Tests/CompletionTests/snapshots/ | grep -E '\.verified\.' || true) |
| 90 | +
|
| 91 | + if [ ${#received_files[@]} -gt 0 ] || [ -n "$diff_output" ]; then |
| 92 | + echo "changes=true" >> $GITHUB_OUTPUT |
| 93 | + echo "Changed snapshot files:" |
| 94 | + printf '%s\n' "${received_files[@]}" |
| 95 | + if [ -n "$diff_output" ]; then |
| 96 | + echo "$diff_output" |
| 97 | + fi |
| 98 | + else |
| 99 | + echo "changes=false" >> $GITHUB_OUTPUT |
| 100 | + fi |
| 101 | +
|
| 102 | + - name: Update verified snapshots |
| 103 | + id: update |
| 104 | + if: steps.check-changes.outputs.changes == 'true' |
| 105 | + run: | |
| 106 | + # This renames .received.* files to .verified.* |
| 107 | + ./.dotnet/dotnet msbuild test/dotnet.Tests/dotnet.Tests.csproj -restore -t:UpdateCliSnapshots |
| 108 | + continue-on-error: true |
| 109 | + |
| 110 | + - name: Commit and push snapshot changes |
| 111 | + id: commit |
| 112 | + if: steps.update.outcome == 'success' |
| 113 | + run: | |
| 114 | + git config --global user.name 'github-actions[bot]' |
| 115 | + git config --global user.email 'github-actions[bot]@users.noreply.github.com' |
| 116 | +
|
| 117 | + # Add snapshot files |
| 118 | + git add test/dotnet.Tests/CompletionTests/snapshots/ |
| 119 | +
|
| 120 | + # Create commit |
| 121 | + COMMIT_DATE=$(date -u +"%Y-%m-%d") |
| 122 | + git commit -m "Update CLI completion snapshots - $COMMIT_DATE" |
| 123 | +
|
| 124 | + # Push to the PR branch |
| 125 | + git push |
| 126 | + continue-on-error: true |
| 127 | + |
| 128 | + - name: Comment on PR - Success |
| 129 | + if: steps.commit.outcome == 'success' |
| 130 | + uses: actions/github-script@v7 |
| 131 | + with: |
| 132 | + script: | |
| 133 | + const runUrl = `https://github.com/${context.repo.owner}/${context.repo.repo}/actions/runs/${process.env.GITHUB_RUN_ID}`; |
| 134 | + await github.rest.issues.createComment({ |
| 135 | + owner: context.repo.owner, |
| 136 | + repo: context.repo.repo, |
| 137 | + issue_number: context.payload.issue.number, |
| 138 | + body: `✅ CLI completion snapshots have been updated and committed to this PR. See [workflow details](${runUrl}).` |
| 139 | + }); |
| 140 | + await github.rest.reactions.createForIssueComment({ |
| 141 | + owner: context.repo.owner, |
| 142 | + repo: context.repo.repo, |
| 143 | + comment_id: context.payload.comment.id, |
| 144 | + content: '+1' |
| 145 | + }); |
| 146 | +
|
| 147 | + - name: Comment on PR - No changes |
| 148 | + if: steps.compare.outcome == 'success' && steps.check-changes.outputs.changes == 'false' |
| 149 | + uses: actions/github-script@v7 |
| 150 | + with: |
| 151 | + script: | |
| 152 | + const runUrl = `https://github.com/${context.repo.owner}/${context.repo.repo}/actions/runs/${process.env.GITHUB_RUN_ID}`; |
| 153 | + await github.rest.issues.createComment({ |
| 154 | + owner: context.repo.owner, |
| 155 | + repo: context.repo.repo, |
| 156 | + issue_number: context.payload.issue.number, |
| 157 | + body: `ℹ️ No completion snapshot files needed to be updated. Review [the workflow run](${runUrl}).` |
| 158 | + }); |
| 159 | + await github.rest.reactions.createForIssueComment({ |
| 160 | + owner: context.repo.owner, |
| 161 | + repo: context.repo.repo, |
| 162 | + comment_id: context.payload.comment.id, |
| 163 | + content: '+1' |
| 164 | + }); |
| 165 | +
|
| 166 | + - name: Comment on PR - Failure |
| 167 | + if: steps.build.outcome == 'failure' || (steps.test.outcome == 'failure' && steps.compare.outcome != 'success') || (steps.check-changes.outputs.changes == 'true' && (steps.update.outcome == 'failure' || steps.commit.outcome == 'failure')) |
| 168 | + uses: actions/github-script@v7 |
| 169 | + with: |
| 170 | + script: | |
| 171 | + let errorMsg = '❌ Failed to update completion snapshots.'; |
| 172 | + const runUrl = `https://github.com/${context.repo.owner}/${context.repo.repo}/actions/runs/${process.env.GITHUB_RUN_ID}`; |
| 173 | +
|
| 174 | + if ('${{ steps.build.outcome }}' === 'failure') { |
| 175 | + errorMsg += ' The build failed.'; |
| 176 | + } else if ('${{ steps.update.outcome }}' === 'failure') { |
| 177 | + errorMsg += ' Could not update snapshot files.'; |
| 178 | + } else if ('${{ steps.commit.outcome }}' === 'failure') { |
| 179 | + errorMsg += ' Could not commit changes.'; |
| 180 | + } |
| 181 | +
|
| 182 | + await github.rest.issues.createComment({ |
| 183 | + owner: context.repo.owner, |
| 184 | + repo: context.repo.repo, |
| 185 | + issue_number: context.payload.issue.number, |
| 186 | + body: `${errorMsg} Please check [the workflow run](${runUrl}) for details.` |
| 187 | + }); |
| 188 | + await github.rest.reactions.createForIssueComment({ |
| 189 | + owner: context.repo.owner, |
| 190 | + repo: context.repo.repo, |
| 191 | + comment_id: context.payload.comment.id, |
| 192 | + content: 'confused' |
| 193 | + }); |
0 commit comments