Move fxe5 played by @thieleju #216
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 $MOVE_STATUS was invalid, skipping ..." | |
exit 0 | |
fi | |
# Get game number from .env | |
GAME_NUMBER=$(grep GAME_NUMBER .env | cut -d '=' -f2) | |
# 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 | |
# Check if game has ended and initialize next game | |
GAME_STATUS=$(grep GAME_STATUS .env | cut -d '=' -f2) | |
if [ "$GAME_STATUS" != "in_progress" ]; then | |
GAME_NUMBER=$((GAME_NUMBER + 1)) | |
echo "Starting new game with GAME_NUMBER $GAME_NUMBER" | |
python scripts/export_chessboard.py $GAME_NUMBER | |
fi | |
- 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 image, readme and pgn" | |
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(/\.\s/g, ".  ").replace(/\|/g, "<br>"); | |
const ISSUE_TITLE = "${{ env.ISSUE_TITLE }}"; | |
const gameNumberLast = Number(${{ env.GAME_NUMBER }}) - 1; | |
const gifUrl = `https://raw.githubusercontent.com/thieleju/thieleju/main/games/game${gameNumberLast}/game.gif`; | |
const gameResult = ""; | |
const gameResultLabel = ""; | |
switch (${{ env.GAME_STATUS }}) { | |
case "white_wins": | |
gameResult = "White Won"; | |
gameResultLabel = "white"; | |
break; | |
case "black_wins": | |
gameResult = "Black Won"; | |
gameResultLabel = "black"; | |
break; | |
case "stalemate": | |
case "insufficient_material": | |
case "threefold_repetition": | |
case "draw": | |
gameResult = "Game ended in a draw"; | |
gameResultLabel = "draw"; | |
break; | |
default: | |
gameResult = "Invalid result :("; | |
} | |
const replyBody = ` | |
## ${gameResult} | |
### Move History | |
<div> | |
<img width="350" src="${gifUrl}" \> | |
</div> | |
${moveHistory} | |
Thank you all for playing! :rocket: | |
`; | |
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 }}` | |
}); |