Skip to content

Commit d00dca8

Browse files
authored
Merge pull request github#36632 from github/repo-sync
Repo sync
2 parents 4437098 + 3c79099 commit d00dca8

8 files changed

+490
-30
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
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 }}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
name: Codespace review - Down
2+
3+
# **What it does**: When closing or merging a pull request, if there are any associated codespaces, to shut them down.
4+
# **Why we have it**: To conserve resources.
5+
# **Who does it impact**: Contributors who open a pull request.
6+
7+
on:
8+
pull_request:
9+
types:
10+
- closed
11+
workflow_dispatch:
12+
13+
permissions:
14+
contents: read
15+
pull-requests: write
16+
17+
jobs:
18+
codespace-review-down:
19+
runs-on: ubuntu-latest
20+
if: ${{ github.repository == 'github/docs-internal' }}
21+
steps:
22+
- uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
23+
- name: Delete codespace
24+
env:
25+
GH_TOKEN: ${{ secrets.DOCS_BOT_PAT_CODESPACE }}
26+
BRANCH_NAME: ${{ github.head_ref || github.ref_name }}
27+
LOGIN: docs-bot
28+
REPO: github/docs-internal
29+
run: |
30+
echo "Checking if there's codespaces for this PR..."
31+
names=$( \
32+
gh codespace list \
33+
--repo "$REPO" \
34+
--limit 1000 \
35+
--json "name,gitStatus,owner" \
36+
--jq ".[] | select(.owner == \"$LOGIN\" and .gitStatus.ref == \"$BRANCH_NAME\") | .name" \
37+
)
38+
echo "- Names:"
39+
echo "$(echo "$names" | sed 's/^/ /')"
40+
count=$(echo "$names" | sed '/^\s*$/d' | wc -l)
41+
echo "- Count: $count"
42+
43+
if [[ $count -gt 0 ]]
44+
then
45+
echo "Codespaces found for this PR"
46+
else
47+
echo "Codespaces not found, exiting..."
48+
exit 0
49+
fi
50+
51+
echo "Shutting down the codespaces..."
52+
echo "$names" | while read -r name
53+
do
54+
echo "Deleting $name..."
55+
gh codespace delete --codespace "$name"
56+
echo "Deleted $name"
57+
done
58+
echo "Shut down the codespaces"
59+
60+
- name: Find code changes comment
61+
uses: peter-evans/find-comment@3eae4d37986fb5a8592848f6a574fdf654e61f9e
62+
id: findComment
63+
with:
64+
issue-number: ${{ github.event.pull_request.number }}
65+
comment-author: 'github-actions[bot]'
66+
body-includes: '<!-- AUTO_CODESPACE -->'
67+
68+
- name: Update comment
69+
uses: peter-evans/create-or-update-comment@71345be0265236311c031f5c7866368bd1eff043
70+
if: ${{ steps.findComment.outputs.comment-id }} # only update if it exists
71+
with:
72+
comment-id: ${{ steps.findComment.outputs.comment-id }}
73+
issue-number: ${{ github.event.pull_request.number }}
74+
edit-mode: replace
75+
body: |
76+
<!-- AUTO_CODESPACE -->
77+
78+
### Review this PR in a codespace 📦
79+
80+
Your pull request is now merged or closed, so I've removed all automatically created codespaces.
81+
82+
🤖 This comment is [automatically generated][workflow].
83+
84+
[workflow]: ${{ github.server_url }}/${{ github.repository }}/blob/${{ github.sha }}/.github/workflows/codespace-review-down.yml

0 commit comments

Comments
 (0)