docs: add PR template
[e-mobility-charging-stations-simulator.git] / .github / workflows / clone-count.yml
CommitLineData
241e529d 1name: GitHub Clone Count Update
3848c429
JB
2
3on:
4 schedule:
c41ef1e0 5 - cron: '0 */24 * * *'
3848c429
JB
6 workflow_dispatch:
7
8jobs:
9d9a7a0d 9 clone-count:
3848c429 10 runs-on: ubuntu-latest
8d9bfb08 11 if: github.repository == 'sap/e-mobility-charging-stations-simulator'
3848c429
JB
12
13 steps:
49722c24 14 - uses: actions/checkout@v4
c41ef1e0 15
3848c429
JB
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
9d9a7a0d 27 id: set-gist
3848c429
JB
28 run: |
29 if gh secret list | grep -q "GIST_ID"
30 then
31 echo "GIST_ID found"
35920236 32 echo "GIST=${{ secrets.GIST_ID }}" >> $GITHUB_OUTPUT
3848c429
JB
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
35920236 38 echo "GIST=${gist_id}" >> $GITHUB_OUTPUT
3848c429 39 cp clone.json clone_before.json
940943f4 40 git rm --ignore-unmatch CLONE.md
3848c429
JB
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
35920236 46 echo "GIST=${gist_id}" >> $GITHUB_OUTPUT
3848c429
JB
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
c41ef1e0 55 - name: update gist with latest count
3848c429
JB
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" \
9d9a7a0d 62 -d @post_clone.json https://api.github.com/gists/${{ steps.set-gist.outputs.GIST }} > /dev/null 2>&1
3848c429
JB
63
64 if [ ! -f CLONE.md ]; then
65 shields="https://img.shields.io/badge/dynamic/json?color=success&label=Clone&query=count&url="
9d9a7a0d 66 url="https://gist.githubusercontent.com/${{ github.actor }}/${{ steps.set-gist.outputs.GIST }}/raw/clone.json"
3848c429
JB
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
c41ef1e0 81
169e5973
JB
82 git config --local user.name "GitHub Action"
83 git config --local user.email "action@github.com"
9869fa53
JB
84 git add CLONE.md
85 git commit -m "docs: create clone count badge"
3848c429
JB
86 fi
87
9d9a7a0d 88 - name: push
a43b0b47 89 uses: CasperWA/push-protected@v2
3848c429 90 with:
a43b0b47 91 token: ${{ secrets.SECRET_TOKEN }}