-
Notifications
You must be signed in to change notification settings - Fork 6
93 lines (84 loc) · 3.11 KB
/
use-visitor-counter.yml
File metadata and controls
93 lines (84 loc) · 3.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
name: Use Visitor Counter Logic
on:
pull_request:
branches:
- main
workflow_dispatch: # Allows manual triggering
# schedule:
# - cron: '0 0 * * *' # Runs daily at midnight
permissions:
contents: write
pull-requests: write
jobs:
update-visitor-count:
runs-on: ubuntu-latest
steps:
- name: Checkout current repository
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: '20'
- name: Run visitor counter logic (updates markdown badges and metrics.json)
run: node .github/scripts/update_repo_views_counter.js
env:
TRAFFIC_TOKEN: ${{ secrets.TRAFFIC_TOKEN }}
REPO: ${{ github.repository }}
- name: Configure Git author
run: |
git config --global user.name "github-actions[bot]"
git config --global user.email "github-actions[bot]@users.noreply.github.com"
# Commit and push logic for PR events (merge, not rebase)
- name: Commit and push changes (PR)
if: github.event_name == 'pull_request'
env:
TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
BRANCH="${{ github.head_ref }}"
git remote set-url origin https://x-access-token:${TOKEN}@github.com/${{ github.repository }}
HAS_CHANGES=false
if ! git diff --quiet || ! git diff --cached --quiet || [ -n "$(git ls-files --others --exclude-standard)" ]; then
HAS_CHANGES=true
git stash push --include-untracked --message visitor-counter
fi
git fetch origin "$BRANCH"
git checkout -B "$BRANCH" "origin/$BRANCH"
if [ "$HAS_CHANGES" = true ]; then
git stash pop
fi
git add -A
if git diff --cached --quiet; then
echo "No changes to commit"
exit 0
fi
git commit -m "Update visitor count"
git pull --rebase origin "$BRANCH"
git push origin HEAD:"$BRANCH"
# Commit and push logic for non-PR events (merge, not rebase)
- name: Commit and push changes (non-PR)
if: github.event_name != 'pull_request'
env:
TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
BRANCH="${{ github.ref_name }}"
git remote set-url origin https://x-access-token:${TOKEN}@github.com/${{ github.repository }}
HAS_CHANGES=false
if ! git diff --quiet || ! git diff --cached --quiet || [ -n "$(git ls-files --others --exclude-standard)" ]; then
HAS_CHANGES=true
git stash push --include-untracked --message visitor-counter
fi
git fetch origin "$BRANCH"
git checkout -B "$BRANCH" "origin/$BRANCH"
if [ "$HAS_CHANGES" = true ]; then
git stash pop
fi
git add -A
if git diff --cached --quiet; then
echo "No changes to commit"
exit 0
fi
git commit -m "Update visitor count"
git pull --rebase origin "$BRANCH"
git push origin HEAD:"$BRANCH"