GitHub Clone Count Update Everyday #475
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: GitHub Clone Count Update Everyday | |
| on: | |
| # run automatically every 24 hours | |
| schedule: | |
| - cron: "0 */24 * * *" | |
| # allows to manually run the job at any time | |
| workflow_dispatch: | |
| # run on every push on the master branch | |
| push: | |
| branches: | |
| - main | |
| jobs: | |
| build: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - uses: actions/checkout@v2 | |
| - name: gh login | |
| run: echo "${{ secrets.SECRET_TOKEN }}" | gh auth login --with-token | |
| - name: parse latest clone count | |
| run: | | |
| curl --user "${{ github.actor }}:${{ secrets.SECRET_TOKEN }}" \ | |
| -H "Accept: application/vnd.github.v3+json" \ | |
| https://api.github.com/repos/${{ github.repository }}/traffic/clones \ | |
| > XMU-CS-exam_clone.json | |
| - name: create gist and download previous count | |
| id: set_id | |
| run: | | |
| if gh secret list | grep -q "GIST_ID" | |
| then | |
| echo "GIST_ID found" | |
| echo ::set-output name=GIST::${{ secrets.GIST_ID }} | |
| curl https://gist.githubusercontent.com/${{ github.actor }}/${{ secrets.GIST_ID }}/raw/XMU-CS-exam_clone.json > XMU-CS-exam_clone_before.json | |
| if cat XMU-CS-exam_clone_before.json | grep '404: Not Found'; then | |
| echo "GIST_ID not valid anymore. Creating another gist..." | |
| gist_id=$(gh gist create XMU-CS-exam_clone.json | awk -F / '{print $NF}') | |
| echo $gist_id | gh secret set GIST_ID | |
| echo ::set-output name=GIST::$gist_id | |
| cp XMU-CS-exam_clone.json XMU-CS-exam_clone_before.json | |
| git rm --ignore-unmatch Git_State/CLONE.md | |
| fi | |
| else | |
| echo "GIST_ID not found. Creating a gist..." | |
| gist_id=$(gh gist create XMU-CS-exam_clone.json | awk -F / '{print $NF}') | |
| echo $gist_id | gh secret set GIST_ID | |
| echo ::set-output name=GIST::$gist_id | |
| cp XMU-CS-exam_clone.json XMU-CS-exam_clone_before.json | |
| fi | |
| - name: update XMU-CS-exam_clone.json | |
| run: | | |
| curl https://raw.githubusercontent.com/Misaka-xxw/XMU-CS-exam/main/Git_State/clone.py > clone.py | |
| python3 clone.py | |
| - name: Update gist with latest count | |
| run: | | |
| content=$(sed -e 's/\\/\\\\/g' -e 's/\t/\\t/g' -e 's/\"/\\"/g' -e 's/\r//g' "XMU-CS-exam_clone.json" | sed -E ':a;N;$!ba;s/\r{0,1}\n/\\n/g') | |
| echo '{"description": "${{ github.repository }} clone statistics", "files": {"XMU-CS-exam_clone.json": {"content": "'"$content"'"}}}' > XMU-CS-exam_post_clone.json | |
| curl -s -X PATCH \ | |
| --user "${{ github.actor }}:${{ secrets.SECRET_TOKEN }}" \ | |
| -H "Content-Type: application/json" \ | |
| -d @XMU-CS-exam_post_clone.json https://api.github.com/gists/${{ steps.set_id.outputs.GIST }} > /dev/null 2>&1 | |
| if [ ! -f Git_State/CLONE.md ]; then | |
| shields="https://img.shields.io/badge/dynamic/json?color=success&label=Clone&query=count_total&url=" | |
| url="https://gist.githubusercontent.com/${{ github.actor }}/${{ steps.set_id.outputs.GIST }}/raw/XMU-CS-exam_clone.json" | |
| repo="https://github.com/Misaka-xxw/XMU-CS-exam" | |
| echo ''> Git_State/CLONE.md | |
| echo ' | |
| **Markdown** | |
| ```markdown' >> Git_State/CLONE.md | |
| echo "[]($repo)" >> Git_State/CLONE.md | |
| echo ' | |
| ``` | |
| **HTML** | |
| ```html' >> Git_State/CLONE.md | |
| echo "<a href='$repo'><img alt='GitHub Clones' src='$shields$url&logo=github'></a>" >> Git_State/CLONE.md | |
| echo '```' >> Git_State/CLONE.md | |
| git add Git_State/CLONE.md | |
| git config --global user.name "GitHub Action" | |
| git config --global user.email "[email protected]" | |
| git commit -m "create clone count badge" | |
| fi | |
| - name: Push | |
| uses: ad-m/github-push-action@master | |
| with: | |
| github_token: ${{ secrets.GITHUB_TOKEN }} |