Skip to content

Commit 2d456b8

Browse files
authored
ci: update update-year.yml
1 parent 52946e7 commit 2d456b8

File tree

1 file changed

+125
-49
lines changed

1 file changed

+125
-49
lines changed

.github/workflows/update-year.yml

Lines changed: 125 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -6,73 +6,149 @@ on:
66
description: 'Advent of Code year'
77
required: true
88
default: 'yyyy'
9+
env:
10+
AOC_YEAR: ${{ github.event.inputs.aoc_year }}
911
jobs:
1012
verify:
1113
runs-on: ubuntu-latest
12-
steps:
14+
steps:
15+
16+
# Forbid workflow execution on template repository
1317
- name: Check Repository
1418
run: |
1519
if [ "${{ github.repository }}" == "Flashky/advent-of-code-yyyy" ]; then
1620
echo -e "\e[91mError: This workflow cannot be run from the template repository.\e[0m"
1721
exit 1
1822
fi
19-
- name: Check for Token Secret
23+
24+
# Verify for existing token secret set
25+
- name: Check Personal Access Token
26+
env:
27+
PAT_TOKEN: ${{ secrets.PAT_TOKEN }}
2028
run: |
21-
if [ -z "${{ secrets.PAT_TOKEN }}" ]; then
22-
echo -e "\e[91mError: PAT_TOKEN secret is not set.\e[0m"
23-
echo -e "\e[93mTo generate a personal access token (PAT) for GitHub Actions:\e[0m"
24-
echo -e "\e[93m1. Go to \e[94mhttps://github.com/settings/tokens\e[0m"
25-
echo -e "\e[93m2. Click on \e[92mGenerate new token\e[0m\e[93m and then on \e[92mGenerate new token (classic) \e[0m\e[93m\e[0m"
26-
echo -e "\e[93m3. Give your token a name, select the required scopes (e.g., repo), and click on \e[92mGenerate token\e[93m\e[0m"
27-
echo -e "\e[93m4. Copy the generated token"
28-
echo -e "\e[93m5. Go to your repository on GitHub -> Settings -> Secrets -> New repository secret"
29-
echo -e "\e[93m6. Name the secret \e[94mPAT_TOKEN\e[93m and paste the copied token as the value"
29+
echo "Checking personal access token."
30+
SECRETS_URL="${{ github.server_url }}/${{ github.repository }}/settings/secrets/actions"
31+
32+
log_pat_error() {
33+
echo -e "\e[93mTo generate a personal access token (PAT) for GitHub Actions:\e[0m" >&2
34+
echo -e "\e[93m1. Go to \e[94mhttps://github.com/settings/tokens\e[0m" >&2
35+
echo -e "\e[93m2. Click on \e[92mGenerate new token\e[0m\e[93m and then on \e[92mGenerate new token (classic) \e[0m\e[93m\e[0m" >&2
36+
echo -e "\e[93m3. Give your token a name, select the required scopes (e.g., repo), and click on \e[92mGenerate token\e[93m\e[0m" >&2
37+
echo -e "\e[93m4. Copy the generated token" >&2
38+
echo -e "\e[93m5. Go to your repository action secret settings: \e[94m$SECRETS_URL\e[93m" >&2
39+
echo -e "\e[93m6. Name the secret \e[94mPAT_TOKEN\e[93m and paste the copied token as the value" >&2
40+
}
41+
42+
if [ -z "$PAT_TOKEN" ]; then
43+
echo -e "::group::\e[91m❌ Error: PAT_TOKEN secret is not set.\e[0m" >&2
44+
log_pat_error
45+
echo "::error title=Missing PAT_TOKEN::PAT_TOKEN secret is not set."
46+
echo "::endgroup::"
47+
exit 1
48+
fi
49+
RESPONSE=$(curl -s -D - -H "Authorization: token $PAT_TOKEN" https://api.github.com/user)
50+
51+
HTTP_CODE=$(echo "$RESPONSE" | head -n 1 | awk '{print $2}')
52+
SCOPES=$(echo "$RESPONSE" | grep -i "X-OAuth-Scopes" | awk '{print $2}' | tr -d '\r')
53+
54+
# 1. Verify 401 and 403 errors
55+
if [ "$HTTP_CODE" -eq 401 ] || [ "$HTTP_CODE" -eq 403 ]; then
56+
echo -e "::group::\e[91m❌ Error: PAT_TOKEN secret is invalid or has expired.\e[0m" >&2
57+
log_pat_error
58+
echo "::error title=The PAT_TOKEN is invalid or has expired::Please, refresh the PAT_TOKEN."
59+
echo "::endgroup::"
60+
exit 1
61+
fi
62+
63+
# Verify other errors
64+
if [ "$HTTP_CODE" -ge 400 ]; then
65+
echo -e "::group::\e[91m❌ Error: Unexpected HTTP status code ($HTTP_CODE).\e[0m" >&2
66+
log_pat_error
67+
echo "::error title=Unexpected HTTP Status Code::Found: $HTTPCODE."
68+
echo "::endgroup::"
69+
exit 1
70+
fi
71+
72+
if [[ "$SCOPES" != *"repo"* ]]; then
73+
echo "::group::\e[91m❌ Error: missing scope 'repo' on the PAT.\e[0m" >&2
74+
log_pat_error
75+
echo "::error title=Missing permissions at PAT_TOKEN::Please, refresh the PAT_TOKEN with 'repo' scope."
76+
echo "::endgroup::"
3077
exit 1
3178
fi
79+
80+
# Verify for a valid year input
3281
- name: Check Year Format
3382
run: |
34-
if echo "${{ github.event.inputs.aoc_year }}" | grep -E -q '^[0-9]{4}$'; then
35-
echo "Year '${{ github.event.inputs.aoc_year }}' format is valid."
83+
if echo "${{ env.AOC_YEAR }}" | grep -E -q '^[0-9]{4}$'; then
84+
echo "Year '${{ env.AOC_YEAR }}' format is valid."
3685
else
37-
echo -e "\e[91mError: Invalid year format ('${{ github.event.inputs.aoc_year }}'). Please provide a valid 4-digit year.\e[0m"
86+
echo -e "\e[91mError: Invalid input year: '${{ env.AOC_YEAR }}'.\e[0m"
87+
echo "::error title=Invalid input year: '${{ env.AOC_YEAR }}'::Please provide a valid 4-digit year (example: 2025)."
3888
exit 1
3989
fi
90+
4091
update:
4192
needs: verify
4293
runs-on: ubuntu-latest
4394
steps:
44-
- uses: actions/checkout@v5
45-
- name: Update Repository Description
46-
run: |
47-
token="${{ secrets.PAT_TOKEN }}"
48-
url=https://api.github.com/repos/${{ github.repository }}
49-
description='{"description": "Advent of Code ${{ github.event.inputs.aoc_year }}"}'
50-
curl -X PATCH -H "Authorization: Bearer $token" -H "Accept: application/vnd.github.v3+json" -d "$description" $url
51-
- name: Update Repository Topics
52-
run: |
53-
token="${{ secrets.PAT_TOKEN }}"
54-
url="https://api.github.com/repos/${{ github.repository }}/topics"
55-
topics='{"names": ["java", "advent-of-code", "advent-of-code-${{ github.event.inputs.aoc_year }}", "advent-of-code-${{ github.event.inputs.aoc_year }}-java"]}'
56-
curl -X PUT -H "Authorization: Bearer $token" -H "Accept: application/vnd.github.mercy-preview+json" -d "$topics" $url
57-
- name: Update README.md
58-
run: |
59-
find . -name 'README.md' | xargs sed -i 's/{year}/'${{ github.event.inputs.aoc_year }}'/g'
60-
- name: Update pom.xml
61-
run: |
62-
find . -name 'pom.xml' | xargs sed -i 's/yyyy/'${{ github.event.inputs.aoc_year }}'/g'
63-
- name: Open Pull Request
64-
uses: peter-evans/create-pull-request@271a8d0340265f705b14b6d32b9829c1cb33d45e
65-
id: pull-request
66-
with:
67-
token: ${{ secrets.GITHUB_TOKEN }}
68-
commit-message: "Update year to ${{ github.event.inputs.aoc_year }}"
69-
title: "Update year to ${{ github.event.inputs.aoc_year }}"
70-
body: |
71-
This PR is auto-generated by [create-pull-request](https://github.com/peter-evans/create-pull-request).
72-
- name: Check PR information
73-
if: ${{ steps.pull-request.outputs.pull-request-number }}
74-
run: |
75-
echo "Pull Request Number - ${{ steps.pull-request.outputs.pull-request-number }}"
76-
echo "Pull Request URL - ${{ steps.pull-request.outputs.pull-request-url }}"
77-
78-
95+
- uses: actions/checkout@v5
96+
97+
# Update repository description
98+
- name: Update Repository Description
99+
env:
100+
GH_TOKEN: ${{ secrets.PAT_TOKEN }}
101+
run: gh repo edit -d "Advent of Code ${{ env.AOC_YEAR }}"
102+
103+
# Update repository topics
104+
- name: Update Repository Topics
105+
env:
106+
GH_TOKEN: ${{ secrets.PAT_TOKEN }}
107+
run: |
108+
gh repo edit \
109+
--add-topic "java" \
110+
--add-topic "advent-of-code" \
111+
--add-topic "advent-of-code-${{ env.AOC_YEAR }}" \
112+
--add-topic "advent-of-code-${{ env.AOC_YEAR }}-java" \
113+
--add-topic "advent-of-code-flashky"
114+
115+
# Clean up folders and README if AoC event is 2025 or newer.
116+
- name: Clean Up (2025+)
117+
if: ${{ env.AOC_YEAR >= 2025 }}
118+
run: |
119+
echo "Year ${{ env.AOC_YEAR }} is 2025 or greater. Performing cleanup..."
120+
121+
echo "Removing day13-day25 source and test folders."
122+
rm -rf src/{main,test}/java/com/adventofcode/flashk/day{13..25}
123+
124+
echo "Removing day 13 to 25 links from README.md."
125+
sed -i '/^- \[Day \(1[3-9]\|2[0-5]\)/d' README.md
126+
127+
# Update README.md
128+
- name: Update README.md
129+
run: |
130+
find . -name 'README.md' | xargs sed -i 's/{year}/'${{ env.AOC_YEAR }}'/g'
131+
132+
# Update pom.xml
133+
- name: Update pom.xml
134+
run: |
135+
find . -name 'pom.xml' | xargs sed -i 's/yyyy/'${{ env.AOC_YEAR }}'/g'
136+
137+
# Open Pull Request
138+
- name: Open Pull Request
139+
uses: peter-evans/create-pull-request@271a8d0340265f705b14b6d32b9829c1cb33d45e # v7.0.8
140+
id: pull-request
141+
with:
142+
token: ${{ secrets.PAT_TOKEN }}
143+
commit-message: "Update year to ${{ env.AOC_YEAR}}"
144+
title: "Update year to ${{ env.AOC_YEAR}}"
145+
body: |
146+
This PR is auto-generated by [create-pull-request](https://github.com/peter-evans/create-pull-request).
147+
148+
# Check PR information
149+
- name: Check PR information
150+
if: ${{ steps.pull-request.outputs.pull-request-number }}
151+
run: |
152+
echo "Pull Request Number - ${{ steps.pull-request.outputs.pull-request-number }}"
153+
echo "Pull Request URL - ${{ steps.pull-request.outputs.pull-request-url }}"
154+

0 commit comments

Comments
 (0)