Skip to content

Update Code Statistics #44

Update Code Statistics

Update Code Statistics #44

name: Update Code Statistics
on:
schedule:
- cron: "0 0 * * 0" # Runs weekly on Sunday at midnight (UTC)
workflow_dispatch: # Allows manual trigger
jobs:
count-lines:
runs-on: ubuntu-latest
steps:
- name: Checkout Code
uses: actions/checkout@v6.0.2
- name: Install Dependencies
run: |
sudo apt-get update
sudo apt-get install -y jq cloc
- name: Count Lines of Code and Update Profile
env:
GH_TOKEN: ${{ secrets.WORKFLOW_SECRET }}
run: |
chmod +x ./scripts/count-loc.sh ./scripts/update-stats.sh
./scripts/count-loc.sh | ./scripts/update-stats.sh
- name: Create or Update Pull Request
env:
GH_TOKEN: ${{ secrets.WORKFLOW_SECRET }}
run: |
git config --global user.name "github-actions[bot]"
git config --global user.email "github-actions[bot]@users.noreply.github.com"
# Check if there are changes
if ! git diff --quiet profile/ABOUT.md || ! git diff --quiet profile/README.md; then
# Check for existing open PR with update-code-stats branch
EXISTING_PR=$(gh pr list --base main --state open --json number,headRefName \
--jq '.[] | select(.headRefName | startswith("update-code-stats-")) | .number' \
| head -n 1)
if [ -n "$EXISTING_PR" ]; then
# Update existing PR branch
BRANCH=$(gh pr view "$EXISTING_PR" --json headRefName --jq '.headRefName')
echo "Updating existing PR #$EXISTING_PR on branch $BRANCH"
# Save the new stats before switching branches
cp profile/ABOUT.md /tmp/ABOUT.md.new
cp profile/README.md /tmp/README.md.new
# Checkout and reset to the remote branch state
git fetch origin "$BRANCH"
git checkout "$BRANCH"
git reset --hard "origin/$BRANCH"
# Restore the new stats
cp /tmp/ABOUT.md.new profile/ABOUT.md
cp /tmp/README.md.new profile/README.md
git add profile/ABOUT.md profile/README.md
git commit -m "chore: update code statistics"
git push origin "$BRANCH"
else
# Create a new branch with timestamp
BRANCH="update-code-stats-$(date +%Y%m%d-%H%M%S)"
git checkout -b "$BRANCH"
# Commit changes
git add profile/ABOUT.md profile/README.md
git commit -m "chore: update code statistics"
# Push to remote
git push origin "$BRANCH"
# Create PR
if ! gh pr create \
--base main \
--head "$BRANCH" \
--title "chore: update code statistics" \
--body "Automated update of code statistics from weekly workflow run."; then
echo "::error::Failed to create PR. Branch $BRANCH has been pushed with the changes."
exit 1
fi
fi
else
echo "No changes to commit"
fi