|
| 1 | +name: Codespace review - Check |
| 2 | + |
| 3 | +# **What it does**: Check on a regular basis for if a codespace is about to shut down, and comment on the pull request. |
| 4 | +# **Why we have it**: We want to notify contributors when their codespace is about to shut down. |
| 5 | +# **Who does it impact**: Contributors who open a pull request. |
| 6 | + |
| 7 | +on: |
| 8 | + schedule: |
| 9 | + - cron: '20,35,50,5 * * * *' # Check every 15 minutes, without hitting the top of the hour |
| 10 | + pull_request: |
| 11 | + paths: |
| 12 | + - '.github/workflows/codespace-review-check.yml' |
| 13 | + workflow_dispatch: |
| 14 | + |
| 15 | +permissions: |
| 16 | + contents: read |
| 17 | + pull-requests: write |
| 18 | + |
| 19 | +jobs: |
| 20 | + codespace-review-check-find: |
| 21 | + runs-on: ubuntu-latest |
| 22 | + if: ${{ github.repository == 'github/docs-internal' }} |
| 23 | + outputs: |
| 24 | + matrix: ${{ steps.set-matrix.outputs.matrix }} |
| 25 | + steps: |
| 26 | + - uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1 |
| 27 | + |
| 28 | + - name: Check codespaces |
| 29 | + id: set-matrix |
| 30 | + env: |
| 31 | + GH_TOKEN: ${{ secrets.DOCS_BOT_PAT_CODESPACE }} |
| 32 | + LOGIN: docs-bot |
| 33 | + REPO: github/docs-internal |
| 34 | + run: | |
| 35 | + ago=$(date -d '225 minutes ago' -Iseconds) |
| 36 | + echo "- Ago: $ago" |
| 37 | + # on mac: date -v-225M -Iseconds |
| 38 | + # -v-225M means 225 minutes ago, 4 * 60 - 15 = 225 |
| 39 | + # -Iseconds means ISO 8601 format, to seconds |
| 40 | + branches=$( |
| 41 | + gh codespace list \ |
| 42 | + --repo "$REPO" \ |
| 43 | + --limit 1000 \ |
| 44 | + --json name,owner,lastUsedAt,gitStatus \ |
| 45 | + --jq ".[] | select(.owner == \"$LOGIN\" and .lastUsedAt < \"$ago\") | .gitStatus.ref" \ |
| 46 | + ) |
| 47 | + echo "- Branches:" |
| 48 | + echo "$(echo "$branches" | sed 's/^/ /')" |
| 49 | + count=$(echo "$branches" | sed '/^\s*$/d' | wc -l) |
| 50 | + echo "- Count: $count" |
| 51 | +
|
| 52 | + if [[ $count -gt 0 ]] |
| 53 | + then |
| 54 | + echo "Codespaces found that are idle or soon to idle" |
| 55 | + else |
| 56 | + echo "Codespaces not found, exiting..." |
| 57 | + exit 0 |
| 58 | + fi |
| 59 | +
|
| 60 | + # https://stackoverflow.com/a/70716837 |
| 61 | + # This might not need `| sed 's/"/\\"/g'` |
| 62 | + matrix=$(echo "$branches" | jq -scR 'split("\n") | map(select(. != ""))' | sed 's/"/\\"/g') |
| 63 | + echo "- Matrix: $matrix" |
| 64 | + echo "matrix=$matrix" >> $GITHUB_OUTPUT |
| 65 | +
|
| 66 | + - uses: ./.github/actions/slack-alert |
| 67 | + if: ${{ failure() && github.event_name != 'workflow_dispatch' }} |
| 68 | + with: |
| 69 | + slack_channel_id: ${{ secrets.DOCS_ALERTS_SLACK_CHANNEL_ID }} |
| 70 | + slack_token: ${{ secrets.SLACK_DOCS_BOT_TOKEN }} |
| 71 | + |
| 72 | + codespace-review-check-comment: |
| 73 | + needs: |
| 74 | + - codespace-review-check-find |
| 75 | + strategy: |
| 76 | + matrix: |
| 77 | + value: ${{ fromJSON(needs.codespace-review-check-find.outputs.matrix) }} |
| 78 | + runs-on: ubuntu-latest |
| 79 | + if: ${{ github.repository == 'github/docs-internal' }} |
| 80 | + env: |
| 81 | + repo: github/docs-internal |
| 82 | + steps: |
| 83 | + - uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1 |
| 84 | + |
| 85 | + - name: Find the pull request |
| 86 | + id: findPr |
| 87 | + run: | |
| 88 | + echo "Looking up pull request" |
| 89 | + echo "- Branch: ${{ matrix.value }}" |
| 90 | + number=$(gh pr view "${{ matrix.value }}" --json number --jq '.number') |
| 91 | + echo "- Number: $number" |
| 92 | + echo "pr-number=$number" >> $GITHUB_OUTPUT |
| 93 | +
|
| 94 | + - name: Find code changes comment |
| 95 | + uses: peter-evans/find-comment@3eae4d37986fb5a8592848f6a574fdf654e61f9e |
| 96 | + id: findComment |
| 97 | + with: |
| 98 | + issue-number: ${{ steps.findPr.outputs.pr-number }} |
| 99 | + comment-author: 'github-actions[bot]' |
| 100 | + body-includes: '<!-- AUTO_CODESPACE -->' |
| 101 | + |
| 102 | + - name: Update comment |
| 103 | + uses: peter-evans/create-or-update-comment@71345be0265236311c031f5c7866368bd1eff043 |
| 104 | + with: |
| 105 | + comment-id: ${{ steps.findComment.outputs.comment-id }} |
| 106 | + issue-number: ${{ steps.findPr.outputs.pr-number }} |
| 107 | + edit-mode: replace |
| 108 | + body: | |
| 109 | + <!-- AUTO_CODESPACE --> |
| 110 | +
|
| 111 | + ### Review this PR in a codespace 📦 |
| 112 | +
|
| 113 | + Your codespace is no longer active. |
| 114 | + You’ve reached the 4 hour limit. |
| 115 | + In order to reactivate your codespace, please update your pull request by adding the https://github.com/${{ env.REPO }}/labels/extend-codespace label. |
| 116 | + If the label is already applied, you can remove and reapply the label to reactivate your codespace. |
| 117 | +
|
| 118 | + 🤖 This comment is [automatically generated][workflow]. |
| 119 | +
|
| 120 | + [workflow]: ${{ github.server_url }}/${{ github.repository }}/blob/${{ github.sha }}/.github/workflows/codespace-review-check.yml |
| 121 | +
|
| 122 | + - uses: ./.github/actions/slack-alert |
| 123 | + if: ${{ failure() && github.event_name != 'workflow_dispatch' }} |
| 124 | + with: |
| 125 | + slack_channel_id: ${{ secrets.DOCS_ALERTS_SLACK_CHANNEL_ID }} |
| 126 | + slack_token: ${{ secrets.SLACK_DOCS_BOT_TOKEN }} |
0 commit comments