|
| 1 | +#/ |
| 2 | +# @license Apache-2.0 |
| 3 | +# |
| 4 | +# Copyright (c) 2024 The Stdlib Authors. |
| 5 | +# |
| 6 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +# you may not use this file except in compliance with the License. |
| 8 | +# You may obtain a copy of the License at |
| 9 | +# |
| 10 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +# |
| 12 | +# Unless required by applicable law or agreed to in writing, software |
| 13 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +# See the License for the specific language governing permissions and |
| 16 | +# limitations under the License. |
| 17 | +#/ |
| 18 | + |
| 19 | +# Workflow name: |
| 20 | +name: cleanup_coverage |
| 21 | + |
| 22 | +# Workflow triggers: |
| 23 | +on: |
| 24 | + |
| 25 | + # Trigger the workflow when a pull request is closed (e.g., merged or closed without merging): |
| 26 | + pull_request: |
| 27 | + types: |
| 28 | + - closed |
| 29 | + |
| 30 | +# Workflow jobs: |
| 31 | +jobs: |
| 32 | + |
| 33 | + # Define a job to perform coverage cleanup... |
| 34 | + cleanup: |
| 35 | + |
| 36 | + # Define a display name: |
| 37 | + name: 'Cleanup coverage' |
| 38 | + |
| 39 | + # Define the type of virtual host machine: |
| 40 | + runs-on: ubuntu-latest |
| 41 | + |
| 42 | + steps: |
| 43 | + # Delete the 'pr-<number>' branch from the 'stdlib-js/www-test-code-coverage' repository: |
| 44 | + - name: 'Delete coverage branch for PR' |
| 45 | + env: |
| 46 | + REPO_GITHUB_TOKEN: ${{ secrets.REPO_GITHUB_TOKEN }} |
| 47 | + PR_NUMBER: ${{ github.event.pull_request.number }} |
| 48 | + run: | |
| 49 | + curl -X DELETE -H "Authorization: token $REPO_GITHUB_TOKEN" \ |
| 50 | + "https://api.github.com/repos/stdlib-js/www-test-code-coverage/git/refs/heads/pr-${PR_NUMBER}" \ |
| 51 | + || echo "Branch pr-${PR_NUMBER} does not exist or could not be deleted." |
| 52 | +
|
| 53 | + # Find and update the '## Coverage Report' comment in the PR |
| 54 | + - name: 'Update coverage comment in PR' |
| 55 | + # Pin action to full length commit SHA |
| 56 | + uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1 |
| 57 | + with: |
| 58 | + github-token: ${{ secrets.CHATBOT_GITHUB_TOKEN }} |
| 59 | + script: | |
| 60 | + const prNumber = context.payload.pull_request.number; |
| 61 | + const { data: comments } = await github.rest.issues.listComments({ |
| 62 | + owner: context.repo.owner, |
| 63 | + repo: context.repo.repo, |
| 64 | + issue_number: prNumber, |
| 65 | + }); |
| 66 | + const coverageComment = comments.find( comment => comment.body && comment.body.includes( '## Coverage Report' ) ); |
| 67 | + if ( coverageComment ) { |
| 68 | + // Replace URLs with plain text in the coverage report comment body: |
| 69 | + const updatedBody = coverageComment.body.replace( /<a href="[^"]+">([^<]+)<\/a>/g, '$1' ); |
| 70 | + await github.rest.issues.updateComment({ |
| 71 | + owner: context.repo.owner, |
| 72 | + repo: context.repo.repo, |
| 73 | + comment_id: coverageComment.id, |
| 74 | + body: updatedBody, |
| 75 | + }); |
| 76 | + } else { |
| 77 | + console.log( 'No Coverage Report comment found.' ); |
| 78 | + } |
0 commit comments