Skip to content

Commit 23dcc23

Browse files
authored
Refactor visitor counter update logic in workflow
Refactor commit and push logic for visitor counter updates to streamline handling for both PR and non-PR events.
1 parent 563605e commit 23dcc23

1 file changed

Lines changed: 18 additions & 24 deletions

File tree

.github/workflows/use-visitor-counter.yml

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ jobs:
2121
uses: actions/checkout@v4
2222
with:
2323
fetch-depth: 0
24+
ref: ${{ github.head_ref || github.ref_name }}
2425

2526
- name: Shallow clone visitor counter logic
2627
run: git clone --depth=1 https://github.com/brown9804/github-visitor-counter.git
@@ -57,30 +58,23 @@ jobs:
5758
git config --global user.name "github-actions[bot]"
5859
git config --global user.email "github-actions[bot]@users.noreply.github.com"
5960
60-
# Commit and push logic for PR events (merge, not rebase)
61-
- name: Commit and push changes (PR)
62-
if: github.event_name == 'pull_request'
61+
- name: Commit and merge changes
6362
env:
64-
TOKEN: ${{ secrets.GITHUB_TOKEN }}
63+
PR_BRANCH: ${{ github.head_ref || github.ref_name }}
64+
GIT_AUTHOR_NAME: github-actions[bot]
65+
GIT_AUTHOR_EMAIL: github-actions[bot]@users.noreply.github.com
66+
GIT_COMMITTER_NAME: github-actions[bot]
67+
GIT_COMMITTER_EMAIL: github-actions[bot]@users.noreply.github.com
6568
run: |
66-
git fetch origin
67-
git checkout ${{ github.head_ref }}
68-
git pull origin ${{ github.head_ref }} || echo "No merge needed"
69+
# Ensure we're on the correct branch
70+
git switch -c "$PR_BRANCH" || git switch "$PR_BRANCH"
71+
72+
# Stage and commit changes if any
6973
git add -A
70-
git commit -m "Update visitor count" || echo "No changes to commit"
71-
git remote set-url origin https://x-access-token:${TOKEN}@github.com/${{ github.repository }}
72-
git push origin HEAD:${{ github.head_ref }}
73-
74-
# Commit and push logic for non-PR events (merge, not rebase)
75-
- name: Commit and push changes (non-PR)
76-
if: github.event_name != 'pull_request'
77-
env:
78-
TOKEN: ${{ secrets.GITHUB_TOKEN }}
79-
run: |
80-
git fetch origin
81-
git checkout ${{ github.ref_name }} || git checkout -b ${{ github.ref_name }} origin/${{ github.ref_name }}
82-
git pull origin ${{ github.ref_name }} || echo "No merge needed"
83-
git add -A
84-
git commit -m "Update visitor count" || echo "No changes to commit"
85-
git remote set-url origin https://x-access-token:${TOKEN}@github.com/${{ github.repository }}
86-
git push origin HEAD:${{ github.ref_name }}
74+
git diff --staged --quiet || git commit -m "Update visitor count"
75+
76+
# Pull and merge existing changes
77+
git pull origin "$PR_BRANCH" --no-rebase
78+
79+
# Push all changes
80+
git push origin "$PR_BRANCH"

0 commit comments

Comments
 (0)