Skip to content

Weekly clone stats

Weekly clone stats #22

name: Weekly Clone Stats Email
on:
schedule:
- cron: '0 8 * * 1' # Every Monday at 08:00 UTC
pull_request:
branches:
- main
permissions:
contents: read
jobs:
fetch-clone-stats:
runs-on: ubuntu-latest
steps:
- name: Check out code
uses: actions/checkout@v3
- name: Fetch Clone Stats
env:
GITHUB_TOKEN: ${{ secrets.PAT_TOKEN }}
run: |
REPO="${{ github.repository }}"
curl -L \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer $GITHUB_TOKEN" \
-H "X-GitHub-Api-Version: 2022-11-28" \
https://api.github.com/repos/$REPO/traffic/clones > clone_stats.json
- name: Upload Clone Stats
uses: actions/upload-artifact@v4
with:
name: clone-stats
path: clone_stats.json
downloading-and-sending-stats:
needs: fetch-clone-stats
runs-on: ubuntu-latest
steps:
- name: Check out code
uses: actions/checkout@v3
- name: Download Clone Stats
uses: actions/download-artifact@v4
with:
name: clone-stats
path: .
- name: Parse clone stats
id: parse
run: |
COUNT=$(jq '.count' clone_stats.json)
UNIQUES=$(jq '.uniques' clone_stats.json)
echo "count=$COUNT" >> "$GITHUB_OUTPUT"
echo "uniques=$UNIQUES" >> "$GITHUB_OUTPUT"
- name: Send custom email
run: |
ENCODED_AUTH=${{ secrets.SENDGRID_KEY }}
FROM=${{ vars.EMAIL_FROM }}
TO=${{ vars.EMAIL_TO }}
COUNT=${{ steps.parse.outputs.count }}
REPOSITORY=${{ github.repository }}
UNIQUES=${{ steps.parse.outputs.uniques }}
curl -X POST https://api.sendgrid.com/v3/mail/send \
-H "Authorization: Bearer $ENCODED_AUTH" \
-H "Content-Type: application/json" \
-d '{
"personalizations": [{
"to": [{"email": "$TO"}],
"subject": "Weekly GitHub Clone Stats for $REPOSITORY"
}],
"from": {"email": "$FROM"},
"content": [{
"type": "text/plain",
"value": "Hi there! Here are the clone stats for $REPOSITORY this week:\n\n🔁 Total Clones: $COUNT\n👤 Unique Cloners: $UNIQUES\n\n– GitHub Bot"
}]
}'