1 name: GitHub Clone Count Update
10 runs-on: ubuntu-latest
11 if: github.repository == 'sap/e-mobility-charging-stations-simulator'
14 - uses: actions/checkout@v4
17 run: echo "${{ secrets.SECRET_TOKEN }}" | gh auth login --with-token
19 - name: parse latest clone count
21 curl --user "${{ github.actor }}:${{ secrets.SECRET_TOKEN }}" \
22 -H "Accept: application/vnd.github.v3+json" \
23 https://api.github.com/repos/${{ github.repository }}/traffic/clones \
26 - name: create gist and download previous count
29 if gh secret list | grep -q "GIST_ID"
32 echo "GIST=${{ secrets.GIST_ID }}" >> $GITHUB_OUTPUT
33 curl https://gist.githubusercontent.com/${{ github.actor }}/${{ secrets.GIST_ID }}/raw/clone.json > clone_before.json
34 if cat clone_before.json | grep '404: Not Found'; then
35 echo "GIST_ID not valid anymore. Creating another gist..."
36 gist_id=$(gh gist create clone.json | awk -F / '{print $NF}')
37 echo $gist_id | gh secret set GIST_ID
38 echo "GIST=${gist_id}" >> $GITHUB_OUTPUT
39 cp clone.json clone_before.json
40 git rm --ignore-unmatch CLONE.md
43 echo "GIST_ID not found. Creating a gist..."
44 gist_id=$(gh gist create clone.json | awk -F / '{print $NF}')
45 echo $gist_id | gh secret set GIST_ID
46 echo "GIST=${gist_id}" >> $GITHUB_OUTPUT
47 cp clone.json clone_before.json
50 - name: update clone.json
52 curl https://raw.githubusercontent.com/MShawon/github-clone-count-badge/master/main.py > main.py
55 - name: update gist with latest count
57 content=$(sed -e 's/\\/\\\\/g' -e 's/\t/\\t/g' -e 's/\"/\\"/g' -e 's/\r//g' "clone.json" | sed -E ':a;N;$!ba;s/\r{0,1}\n/\\n/g')
58 echo '{"description": "${{ github.repository }} clone statistics", "files": {"clone.json": {"content": "'"$content"'"}}}' > post_clone.json
60 --user "${{ github.actor }}:${{ secrets.SECRET_TOKEN }}" \
61 -H "Content-Type: application/json" \
62 -d @post_clone.json https://api.github.com/gists/${{ steps.set-gist.outputs.GIST }} > /dev/null 2>&1
64 if [ ! -f CLONE.md ]; then
65 shields="https://img.shields.io/badge/dynamic/json?color=success&label=Clone&query=count&url="
66 url="https://gist.githubusercontent.com/${{ github.actor }}/${{ steps.set-gist.outputs.GIST }}/raw/clone.json"
67 repo="https://github.com/MShawon/github-clone-count-badge"
72 ```markdown' >> CLONE.md
73 echo "[![GitHub Clones]($shields$url&logo=github)]($repo)" >> CLONE.md
79 echo "<a href='$repo'><img alt='GitHub Clones' src='$shields$url&logo=github'></a>" >> CLONE.md
80 echo '```' >> CLONE.md
82 git config --local user.name "GitHub Action"
83 git config --local user.email "action@github.com"
85 git commit -m "docs: create clone count badge"
89 uses: CasperWA/push-protected@v2
91 token: ${{ secrets.SECRET_TOKEN }}