4d68216c2347bc4763228ce2813020d140e05c49
[e-mobility-charging-stations-simulator.git] / .github / workflows / clone-count.yml
1 name: GitHub Clone Count Update
2
3 on:
4 schedule:
5 - cron: '0 */24 * * *'
6 workflow_dispatch:
7
8 jobs:
9 clone-count:
10 runs-on: ubuntu-latest
11 if: github.repository == 'sap/e-mobility-charging-stations-simulator'
12
13 steps:
14 - uses: actions/checkout@v4
15
16 - name: gh login
17 run: echo "${{ secrets.SECRET_TOKEN }}" | gh auth login --with-token
18
19 - name: parse latest clone count
20 run: |
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 \
24 > clone.json
25
26 - name: create gist and download previous count
27 id: set-gist
28 run: |
29 if gh secret list | grep -q "GIST_ID"
30 then
31 echo "GIST_ID found"
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
41 fi
42 else
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
48 fi
49
50 - name: update clone.json
51 run: |
52 curl https://raw.githubusercontent.com/MShawon/github-clone-count-badge/master/main.py > main.py
53 python3 main.py
54
55 - name: update gist with latest count
56 run: |
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
59 curl -s -X PATCH \
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
63
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"
68 echo ''> CLONE.md
69 echo '
70 **Markdown**
71
72 ```markdown' >> CLONE.md
73 echo "[![GitHub Clones]($shields$url&logo=github)]($repo)" >> CLONE.md
74 echo '
75 ```
76
77 **HTML**
78 ```html' >> CLONE.md
79 echo "<a href='$repo'><img alt='GitHub Clones' src='$shields$url&logo=github'></a>" >> CLONE.md
80 echo '```' >> CLONE.md
81
82 git config --local user.name "GitHub Action"
83 git config --local user.email "action@github.com"
84 git add CLONE.md
85 git commit -m "docs: create clone count badge"
86 fi
87
88 - name: push
89 uses: CasperWA/push-protected@v2
90 with:
91 token: ${{ secrets.SECRET_TOKEN }}