Skip to content

Commit 8c1d188

Browse files
authored
Refactor paint workflow for issue handling
1 parent f488040 commit 8c1d188

1 file changed

Lines changed: 71 additions & 57 deletions

File tree

.github/workflows/paint.yml

Lines changed: 71 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: Canvas Paint Engine (8x8)
1+
name: Paint Pixel
22

33
on:
44
issues:
@@ -13,73 +13,87 @@ jobs:
1313
runs-on: ubuntu-latest
1414

1515
steps:
16-
- name: Checkout repo
17-
uses: actions/checkout@v4
16+
- uses: actions/checkout@v4
1817

19-
- name: Parse Issue Title
20-
id: parse
21-
run: |
22-
TITLE="${{ github.event.issue.title }}"
23-
echo "Title: $TITLE"
24-
25-
# 8x8 VALIDATION
26-
if [[ "$TITLE" =~ ^Paint\ \[([A-H][1-8])\]\ \#([A-Fa-f0-9]{6})$ ]]; then
27-
COORD=$(echo "$TITLE" | sed -E 's/^Paint \[([A-H][1-8])\].*/\1/')
28-
COLOR=$(echo "$TITLE" | sed -E 's/.*(#([A-Fa-f0-9]{6})).*/\1/')
29-
echo "coord=$COORD" >> $GITHUB_OUTPUT
30-
echo "color=$COLOR" >> $GITHUB_OUTPUT
31-
else
32-
echo "invalid=true" >> $GITHUB_OUTPUT
33-
fi
18+
- name: Install jq
19+
run: sudo apt-get install jq
3420

35-
- name: Handle Invalid Format
36-
if: steps.parse.outputs.invalid == 'true'
21+
- name: Process Issue
3722
env:
38-
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
23+
ISSUE_BODY: ${{ github.event.issue.title }}
24+
USERNAME: ${{ github.event.issue.user.login }}
25+
ISSUE_NUMBER: ${{ github.event.issue.number }}
26+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
3927
run: |
40-
gh issue comment ${{ github.event.issue.number }} \
41-
--body $'❌ Invalid format.\n\nUse exactly:\nPaint [A5] #FF5733\n\nValid coordinates: A1–H8'
28+
NOW=$(date +%s)
4229
43-
gh issue edit ${{ github.event.issue.number }} --add-label "Invalid"
30+
echo "Processing: $ISSUE_BODY"
4431
45-
- name: Update SVG
46-
if: steps.parse.outputs.invalid != 'true'
47-
run: |
48-
COORD="${{ steps.parse.outputs.coord }}"
49-
COLOR="${{ steps.parse.outputs.color }}"
32+
# Validate format
33+
if [[ ! "$ISSUE_BODY" =~ ^Paint\ \[([A-H][1-8])\]\ \#([A-Fa-f0-9]{6})$ ]]; then
34+
gh issue comment $ISSUE_NUMBER --body "❌ Invalid format. Use: Paint [A5] #FF5733"
35+
gh issue edit $ISSUE_NUMBER --add-label "Invalid"
36+
exit 0
37+
fi
5038
51-
echo "Painting $COORD with $COLOR"
39+
COORD=$(echo "$ISSUE_BODY" | sed -E 's/^Paint \[([A-H][1-8])\] .*/\1/')
40+
COLOR=$(echo "$ISSUE_BODY" | sed -E 's/^Paint \[[A-H][1-8]\] (#.*)/\1/')
5241
53-
sed -i.bak "s/\(id=\"$COORD\"[^>]*fill=\)\"#[A-Fa-f0-9]\{6\}\"/\1\"$COLOR\"/" map.svg
54-
rm map.svg.bak
42+
# Load state
43+
STATE=$(cat data/state.json)
5544
56-
- name: Append History
57-
if: steps.parse.outputs.invalid != 'true'
58-
run: |
59-
echo "$(date -u +"%Y-%m-%dT%H:%M:%SZ"),${{ github.actor }},${{ steps.parse.outputs.coord }},${{ steps.parse.outputs.color }}" >> history.log
45+
USER_COOLDOWN=$(echo $STATE | jq -r ".users[\"$USERNAME\"] // 0")
46+
TILE_LOCK=$(echo $STATE | jq -r ".tiles[\"$COORD\"].lock_until // 0")
6047
61-
- name: Update Cache Buster
62-
if: steps.parse.outputs.invalid != 'true'
63-
run: |
64-
TS=$(date +%s)
65-
sed -i.bak "s/map\.svg?ts=[0-9]*/map.svg?ts=$TS/" README.md
66-
rm README.md.bak
48+
# Check user cooldown
49+
if [ "$NOW" -lt "$USER_COOLDOWN" ]; then
50+
REMAIN=$(( ($USER_COOLDOWN - $NOW) / 3600 ))
51+
gh issue comment $ISSUE_NUMBER --body "⏳ You may paint again in ${REMAIN}h."
52+
exit 0
53+
fi
6754
68-
- name: Commit Changes
69-
if: steps.parse.outputs.invalid != 'true'
70-
run: |
71-
git config user.name "canvas-bot"
55+
# Check tile lock
56+
if [ "$NOW" -lt "$TILE_LOCK" ]; then
57+
gh issue comment $ISSUE_NUMBER --body "🔒 Tile $COORD is locked for another hour."
58+
exit 0
59+
fi
60+
61+
# Update SVG
62+
sed -i "s/id=\"$COORD\" fill=\"[^\"]*\"/id=\"$COORD\" fill=\"$COLOR\"/" map.svg
63+
64+
# Update state
65+
NEW_USER_COOLDOWN=$((NOW + 86400))
66+
NEW_TILE_LOCK=$((NOW + 3600))
67+
68+
UPDATED_STATE=$(echo $STATE | jq \
69+
--arg user "$USERNAME" \
70+
--arg coord "$COORD" \
71+
--arg color "$COLOR" \
72+
--argjson now $NOW \
73+
--argjson user_cd $NEW_USER_COOLDOWN \
74+
--argjson tile_lock $NEW_TILE_LOCK \
75+
'
76+
.users[$user] = $user_cd |
77+
.tiles[$coord].last_user = $user |
78+
.tiles[$coord].last_painted = $now |
79+
.tiles[$coord].paint_count = (.tiles[$coord].paint_count // 0) + 1 |
80+
.tiles[$coord].lock_until = $tile_lock |
81+
.history += [{
82+
user: $user,
83+
coord: $coord,
84+
color: $color,
85+
timestamp: $now
86+
}]
87+
')
88+
89+
echo "$UPDATED_STATE" > data/state.json
90+
91+
git config user.name "commit-art-bot"
7292
git config user.email "actions@github.com"
73-
git add map.svg history.log README.md
74-
git commit -m "Paint ${{ steps.parse.outputs.coord }} ${{ steps.parse.outputs.color }}" || echo "No changes to commit"
75-
git push
7693
77-
- name: Comment Success
78-
if: steps.parse.outputs.invalid != 'true'
79-
env:
80-
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
81-
run: |
82-
gh issue comment ${{ github.event.issue.number }} \
83-
--body "🎨 Successfully painted **${{ steps.parse.outputs.coord }}** with **${{ steps.parse.outputs.color }}**."
94+
git add map.svg data/state.json
95+
git commit -m "🎨 $USERNAME painted $COORD $COLOR"
96+
git push
8497
85-
gh issue edit ${{ github.event.issue.number }} --add-label "Completed"
98+
gh issue comment $ISSUE_NUMBER --body "✅ Painted $COORD $COLOR"
99+
gh issue edit $ISSUE_NUMBER --add-label "Completed"

0 commit comments

Comments
 (0)