Skip to content

Commit

Permalink
Update daily-commit-link.yml
Browse files Browse the repository at this point in the history
  • Loading branch information
Hunterdii authored Nov 6, 2024
1 parent 0c0d253 commit 68ec746
Showing 1 changed file with 43 additions and 60 deletions.
103 changes: 43 additions & 60 deletions .github/workflows/daily-commit-link.yml
Original file line number Diff line number Diff line change
@@ -1,69 +1,52 @@
name: Daily Commit Link and POTD Badge Tracker
name: Fetch Latest Commit and Extract Filename

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
- main

jobs:
update-link:
extract-filename:
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")
# Debugging: output the full commit data
echo "Commit Data: $COMMIT_DATA"
# 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')
# Debugging: output the extracted filename
echo "Extracted Filename: $FILE_NAME"
# 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
- name: Checkout repository
uses: actions/checkout@v3

- name: Fetch the latest commit from the repository
run: |
# Fetch the latest commit data with the changed files
COMMIT_DATA=$(curl -s -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
"https://api.github.com/repos/${{ github.repository }}/commits?sha=main&per_page=1")
# Debugging: output the full commit data
echo "Commit Data: $COMMIT_DATA"
# 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 filenames of changed files from the commit
CHANGED_FILES=$(echo $COMMIT_DATA | jq -r '.[0].files[].filename')
# Find the .md file in the list of changed files
FILE_NAME=$(echo "$CHANGED_FILES" | grep -oP '\d{2}\(.*\).md' | head -n 1)
# Debugging: output the extracted filename
echo "Extracted Filename: $FILE_NAME"
# Check if a valid filename was extracted
if [ -z "$FILE_NAME" ]; then
echo "Error: No filename found in changed files" >&2
exit 1
fi
# Set an output to pass to the next step
echo "FILE_NAME=$FILE_NAME" >> $GITHUB_ENV
- name: Use extracted filename in next step
run: |
# Access the extracted filename
echo "The extracted filename is: $FILE_NAME"

0 comments on commit 68ec746

Please sign in to comment.