Skip to content

Commit b60a22f

Browse files
authored
Refactor update-clones-badge workflow for token usage
Updated the GitHub Actions workflow to use a token from the environment variable for authorization and modified the condition to check for changes before committing.
1 parent 9e355e7 commit b60a22f

1 file changed

Lines changed: 22 additions & 7 deletions

File tree

.github/workflows/update-clones-badge.yml

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ name: Update clone stats
22

33
on:
44
schedule:
5-
- cron: "0 */12 * * *"
6-
workflow_dispatch:
5+
- cron: "0 */12 * * *"
6+
workflow_dispatch:
77

88
permissions:
99
contents: write
@@ -16,18 +16,33 @@ jobs:
1616
uses: actions/checkout@v4
1717

1818
- name: Get clone stats
19+
env:
20+
GH_TOKEN: ${{ secrets.CLONE_STATS_TOKEN }}
1921
run: |
2022
OWNER=${GITHUB_REPOSITORY%/*}
2123
REPO=${GITHUB_REPOSITORY#*/}
22-
curl -s \
24+
25+
RESPONSE=$(curl -s \
2326
-H "Accept: application/vnd.github+json" \
24-
-H "Authorization: Bearer ${{ secrets.CLONE_STATS_TOKEN }}" \
25-
https://api.github.com/repos/$OWNER/$REPO/traffic/clones \
26-
> clones.json
27+
-H "Authorization: Bearer $GH_TOKEN" \
28+
"https://api.github.com/repos/$OWNER/$REPO/traffic/clones")
29+
30+
export RESPONSE
31+
32+
COUNT=$(python - << 'PY'
33+
import json, os
34+
data = json.loads(os.environ["RESPONSE"])
35+
print(data.get("count", 0))
36+
PY
37+
)
38+
39+
cat > clones.json <<EOF
40+
{"schemaVersion": 1, "label": "clones", "message": "$COUNT"}
41+
EOF
2742

2843
- name: Commit changes
2944
run: |
30-
if git diff --quiet; then
45+
if [ -z "$(git status --porcelain)" ]; then
3146
echo "No changes."
3247
exit 0
3348
fi

0 commit comments

Comments
 (0)