|
| 1 | +name: Weekly Clone Stats Email |
| 2 | + |
| 3 | +on: |
| 4 | + schedule: |
| 5 | + - cron: "0 8 * * 1" # Every Monday at 08:00 UTC |
| 6 | + |
| 7 | +permissions: |
| 8 | + contents: read |
| 9 | + |
| 10 | +jobs: |
| 11 | + fetch-clone-stats: |
| 12 | + runs-on: ubuntu-latest |
| 13 | + steps: |
| 14 | + - name: Check out code |
| 15 | + uses: actions/checkout@v3 |
| 16 | + |
| 17 | + - name: Fetch Clone Stats |
| 18 | + env: |
| 19 | + GITHUB_TOKEN: ${{ secrets.PAT_TOKEN }} |
| 20 | + run: | |
| 21 | + REPO="${{ github.repository }}" |
| 22 | + curl -L \ |
| 23 | + -H "Accept: application/vnd.github+json" \ |
| 24 | + -H "Authorization: Bearer $GITHUB_TOKEN" \ |
| 25 | + -H "X-GitHub-Api-Version: 2022-11-28" \ |
| 26 | + https://api.github.com/repos/$REPO/traffic/clones > clone_stats.json |
| 27 | +
|
| 28 | + - name: Upload Clone Stats |
| 29 | + uses: actions/upload-artifact@v4 |
| 30 | + with: |
| 31 | + name: clone-stats |
| 32 | + path: clone_stats.json |
| 33 | + |
| 34 | + downloading-and-sending-stats: |
| 35 | + needs: fetch-clone-stats |
| 36 | + runs-on: ubuntu-latest |
| 37 | + steps: |
| 38 | + - name: Check out code |
| 39 | + uses: actions/checkout@v3 |
| 40 | + |
| 41 | + - name: Download Clone Stats |
| 42 | + uses: actions/download-artifact@v4 |
| 43 | + with: |
| 44 | + name: clone-stats |
| 45 | + path: . |
| 46 | + |
| 47 | + - name: Parse clone stats |
| 48 | + id: parse |
| 49 | + run: | |
| 50 | + COUNT=$(jq '.count' clone_stats.json) |
| 51 | + UNIQUES=$(jq '.uniques' clone_stats.json) |
| 52 | + echo "count=$COUNT" >> "$GITHUB_OUTPUT" |
| 53 | + echo "uniques=$UNIQUES" >> "$GITHUB_OUTPUT" |
| 54 | +
|
| 55 | + - name: Send custom email |
| 56 | + run: | |
| 57 | + ENCODED_AUTH=${{ secrets.SENDGRID_KEY }} |
| 58 | + FROM=${{ vars.EMAIL_FROM }} |
| 59 | + TO=${{ vars.EMAIL_TO }} |
| 60 | + COUNT=${{ steps.parse.outputs.count }} |
| 61 | + REPOSITORY=${{ github.repository }} |
| 62 | + UNIQUES=${{ steps.parse.outputs.uniques }} |
| 63 | +
|
| 64 | + curl -X POST https://api.sendgrid.com/v3/mail/send \ |
| 65 | + -H "Authorization: Bearer $ENCODED_AUTH" \ |
| 66 | + -H "Content-Type: application/json" \ |
| 67 | + -d "{ |
| 68 | + \"personalizations\": [{ |
| 69 | + \"to\": [{\"email\": \"${TO}\"}], |
| 70 | + \"subject\": \"Weekly GitHub Clone Stats for ${REPOSITORY}\" |
| 71 | + }], |
| 72 | + \"from\": {\"email\": \"${FROM}\"}, |
| 73 | + \"content\": [{ |
| 74 | + \"type\": \"text/plain\", |
| 75 | + \"value\": \"Hi there! Here are the clone stats for ${REPOSITORY} this week:\n\n🔁 Total Clones: ${COUNT}\n👤 Unique Cloners: ${UNIQUES}\" |
| 76 | + }] |
| 77 | + }" |
0 commit comments