Skip to content

Update Clone Count

Update Clone Count #36

name: Update Clone Count
on:
schedule:
- cron: '0 0 * * *' # Runs daily at midnight
workflow_dispatch: # Allows manual triggering
jobs:
update-clone-count:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v2
- name: Fetch clone count
id: fetch_count
run: |
response=$(curl -s -H "Authorization: token ${{ secrets.PAT }}" \
"https://api.github.com/repos/${{ github.repository }}/traffic/clones")
echo "API Response: $response"
yesterday=$(date -u -d "2 hours ago" +"%Y-%m-%d") # account for potential delay in start time
clone_count=$(echo "$response" | jq --arg yesterday "$yesterday" '.clones[] | select(.timestamp | startswith($yesterday)) | .count')
if [ -z "$clone_count" ]; then
echo "CLONE_COUNT=0" >> $GITHUB_ENV # Default to 0 if no clones yesterday
else
echo "CLONE_COUNT=$clone_count-1" >> $GITHUB_ENV # -1 to account for herein checkout
fi
echo "API-retrieved clones count for the day ($yesterday): $clone_count"
- name: Update Gist
env:
GIST_ID: 6dc3699f407bffa35cd6670ad08dd307
run: |
gist_content=$(curl -s -H "Authorization: token ${{ secrets.PAT }}" \
"https://api.github.com/gists/$GIST_ID")
#echo "Gist Response: $gist_content"
current_count=$(echo "$gist_content" | jq -r '.files."clone_count.json".content' | jq '.count' | tr -d '"')
echo "Current clones count from Gist: $current_count"
new_count=$((current_count + CLONE_COUNT))
echo "New count: $new_count"
payload=$(jq -n \
--arg content "{\"count\": \"$new_count\"}" \
'{"files": {"clone_count.json": {"content": $content}}}')
echo "Constructed payload: $payload"
curl -X PATCH -H "Authorization: token ${{ secrets.PAT }}" \
-H "Accept: application/vnd.github.v3+json" \
-d "$payload" \
"https://api.github.com/gists/$GIST_ID"