Skip to content

Update daily-commit-link.yml #24

Update daily-commit-link.yml

Update daily-commit-link.yml #24

name: Daily Commit Link and POTD Badge Tracker
on:
push:
branches:
- main # Trigger on push to main branch
schedule:
- cron: '0 0 * * *' # This will run daily at midnight UTC
workflow_dispatch: # Allows manual triggering
jobs:
update-link:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Setup Python
uses: actions/setup-python@v5
with:
python-version: 3.8
- name: Install jq
run: sudo apt-get install jq # Install jq to parse JSON responses
- name: Install requests
run: pip install requests
- name: Get latest solution commit
id: latest_commit
run: |
# Fetch the latest commit from the repository
COMMIT_DATA=$(curl -s -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
"https://api.github.com/repos/${{ github.repository }}/commits?sha=main&per_page=1")
# Check if the commit data was retrieved successfully
if [ -z "$COMMIT_DATA" ]; then
echo "Error: No commit data received" >&2
exit 1
fi
# Extract the filename of the solution from the commit message
FILE_NAME=$(echo $COMMIT_DATA | jq -r '.[0].commit.message' | grep -oP '\d{2}\(.*\).md')
# Check if a valid filename was extracted
if [ -z "$FILE_NAME" ]; then
echo "Error: No filename found in commit message" >&2
exit 1
fi
# Set an output to pass to the next step
echo "FILE_NAME=$FILE_NAME" >> $GITHUB_ENV
- name: Update README with the Latest Commit
run: python src/update_commit.py ${{ github.repository }} ${{ secrets.GITHUB_TOKEN }} README.md ${{ env.FILE_NAME }}
- name: Commit and Push Changes
run: |
git config --local user.email "github-actions[bot]@users.noreply.github.com"
git config --local user.name "github-actions[bot]"
git add README.md
git diff-index --quiet HEAD || git commit -m "Update latest commit and POTD badge"
git push