Move f5 played by @thieleju #282
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Chess Workflow on Issue | |
on: | |
issues: | |
types: [opened] | |
jobs: | |
Make-a-move: | |
runs-on: ubuntu-latest | |
# Only allow one job to run at a time | |
concurrency: | |
group: chess | |
cancel-in-progress: true | |
steps: | |
- name: Checkout repository | |
uses: actions/checkout@v2 | |
- name: Get chess move notation from issue | |
run: | | |
echo "ISSUE_TITLE=${{ github.event.issue.title }}" >> $GITHUB_ENV | |
- name: Set up Python | |
uses: actions/setup-python@v2 | |
with: | |
python-version: "3.x" | |
cache: "pip" | |
- name: Generate Chessboard Image and update readme | |
run: | | |
# Install dependencies | |
pip install -r scripts/requirements.txt | |
# generate new chessboard image and export .env variables | |
# parameters: issue_title, author | |
python scripts/export_chessboard.py "$ISSUE_TITLE" "${{ github.event.issue.user.login }}" | |
# read environment variables from .env file | |
while IFS= read -r line; do | |
echo "$line" >> $GITHUB_ENV | |
done < <(grep -v '^#' .env) | |
# if MOVE_STATUS is "invalid" skip rest of the steps | |
MOVE_STATUS=$(grep MOVE_STATUS .env | cut -d '=' -f2) | |
if [ "$MOVE_STATUS" == "invalid" ]; then | |
echo "Move was invalid, skipping ..." | |
exit 0 | |
fi | |
# Get game number from .env | |
GAME_NUMBER=$(grep GAME_NUMBER .env | cut -d '=' -f2) | |
# Remove all other chessboard images from game directory | |
rm -f games/game${GAME_NUMBER}/chessboard*.png | |
# rename file to chessboard.png + timestamp | |
TIMESTAMP=-$(date +%s) | |
mv -f chessboard.png games/game${GAME_NUMBER}/chessboard$TIMESTAMP.png | |
# add Filename to github_env | |
echo "CHESSBOARD_FILENAME=games/game${GAME_NUMBER}/chessboard$TIMESTAMP.png" >> $GITHUB_ENV | |
- name: Commit changes | |
if: ${{ env.MOVE_STATUS == 'valid' }} | |
run: | | |
# parameters: moves, image_url, link_url | |
python scripts/update_readme.py "${{ env.GAME_HISTORY }}" "https://raw.githubusercontent.com/thieleju/thieleju/main/${{ env.CHESSBOARD_FILENAME }}" "${{ env.VALID_MOVES }}" | |
# commit changes | |
git config --global user.email "[email protected]" | |
git config --global user.name "GitHub Actions" | |
git add . | |
git commit -m "π Update chessboard with new move ${{ env.ISSUE_TITLE }} by @${{ github.event.issue.user.login }}" | |
git push origin HEAD | |
- name: Reply to issue (Game in Progress) | |
if: ${{ env.GAME_STATUS == 'in_progress' }} | |
uses: actions/github-script@v7 | |
with: | |
script: | | |
const commenter = context.payload.issue.user.login; | |
const ISSUE_TITLE = "${{ env.ISSUE_TITLE }}"; | |
let replyBody; | |
let newIssueTitle = `Move ${ISSUE_TITLE} played by @${{ github.event.issue.user.login }}`; | |
if (process.env.MOVE_STATUS === 'valid') { | |
replyBody = `Your move was played and the chessboard has been updated. Thank you for playing, @${commenter}! π\n\nCheck the updated chessboard [here](https://github.com/thieleju)`; | |
} else if (process.env.MOVE_STATUS === 'invalid') { | |
replyBody = `Your move was invalid, please don't edit the title of the issue. Moves are in Standard Algebraic Notation ([SAN](https://www.chess.com/terms/chess-notation)) (e4, Bf4, exd5, Qxf7+, ...).\n\nCheck the updated chessboard [here](https://github.com/thieleju)`; | |
newIssueTitle += ' (Invalid Move)'; | |
await github.rest.issues.addLabels({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
issue_number: context.issue.number, | |
labels: ['invalid move'] | |
}) | |
} else { | |
newIssueTitle += ' (Invalid Move)'; | |
replyBody = "Something went wrong. :confused: Please try again."; | |
await github.rest.issues.addLabels({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
issue_number: context.issue.number, | |
labels: ['invalid move'] | |
}) | |
} | |
await github.rest.issues.createComment({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
issue_number: context.issue.number, | |
body: replyBody | |
}); | |
await github.rest.issues.update({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
issue_number: context.issue.number, | |
state: 'closed', | |
title: newIssueTitle | |
}); | |
- name: Reply to issue (Game Ended) | |
if: ${{ env.GAME_STATUS != 'in_progress' }} | |
uses: actions/github-script@v7 | |
with: | |
script: | | |
const moveHistory = "${{ env.GAME_HISTORY }}".replace(/\|/g, "<br>"); | |
const ISSUE_TITLE = "${{ env.ISSUE_TITLE }}"; | |
const gifUrl = `https://raw.githubusercontent.com/thieleju/thieleju/main/games/game${{ env.GAME_NUMBER }}/game.gif`; | |
let gameResult = ""; | |
let gameResultLabel = ""; | |
const gameStatus = "${{ env.GAME_STATUS }}"; | |
switch (gameStatus) { | |
case "white_wins": | |
gameResult = ":sparkles: White Won :sparkles:"; | |
gameResultLabel = "white"; | |
break; | |
case "black_wins": | |
gameResult = ":sparkles: Black Won :sparkles:"; | |
gameResultLabel = "black"; | |
break; | |
case "stalemate": | |
case "insufficient_material": | |
case "threefold_repetition": | |
case "draw": | |
gameResult = ":sparkles: Game ended in a draw (${gameStatus})"; | |
gameResultLabel = "draw"; | |
break; | |
default: | |
gameResult = "Invalid result :("; | |
} | |
const replyBody = ` | |
## ${gameResult} | |
<div> | |
<img width="500" src="${gifUrl}" \> | |
</div> | |
### Game History: | |
<p>${moveHistory}</p> | |
Thank you all for playing! :rocket: | |
--- | |
:chess_pawn: New game started! Click [here](https://github.com/thieleju) go to the new game. | |
`; | |
await github.rest.issues.createComment({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
issue_number: context.issue.number, | |
body: replyBody | |
}); | |
await github.rest.issues.addLabels({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
issue_number: context.issue.number, | |
labels: [gameResultLabel] | |
}) | |
await github.rest.issues.update({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
issue_number: context.issue.number, | |
state: 'closed', | |
title: `${gameResult} - Move ${ISSUE_TITLE} played by @${{ github.event.issue.user.login }}` | |
}); | |
- name: Start new Game | |
if: ${{ env.GAME_STATUS != 'in_progress' }} | |
run: | | |
GAME_NUMBER=$((GAME_NUMBER + 1)) | |
echo "Starting new game with GAME_NUMBER $GAME_NUMBER" | |
python scripts/export_chessboard.py $GAME_NUMBER | |
# read environment variables from .env file | |
while IFS= read -r line; do | |
echo "$line" >> $GITHUB_ENV | |
done < <(grep -v '^#' .env) | |
TIMESTAMP=-$(date +%s) | |
CHESSBOARD_FILENAME=games/game${GAME_NUMBER}/chessboard$TIMESTAMP.png | |
mv -f chessboard.png $CHESSBOARD_FILENAME | |
VALID_MOVES=$(grep VALID_MOVES .env | cut -d '=' -f2) | |
# parameters: moves, image_url, link_url | |
python scripts/update_readme.py "" "https://raw.githubusercontent.com/thieleju/thieleju/main/${CHESSBOARD_FILENAME}" "$VALID_MOVES" | |
# commit changes | |
git config --global user.email "[email protected]" | |
git config --global user.name "GitHub Actions" | |
git add . | |
git commit -m "π Started new game #${GAME_NUMBER}" | |
git push origin HEAD |