|
21 | 21 | uses: actions/checkout@v4 |
22 | 22 | with: |
23 | 23 | fetch-depth: 0 |
| 24 | + ref: ${{ github.head_ref || github.ref_name }} |
24 | 25 |
|
25 | 26 | - name: Shallow clone visitor counter logic |
26 | 27 | run: git clone --depth=1 https://github.com/brown9804/github-visitor-counter.git |
@@ -57,38 +58,23 @@ jobs: |
57 | 58 | git config --global user.name "github-actions[bot]" |
58 | 59 | git config --global user.email "github-actions[bot]@users.noreply.github.com" |
59 | 60 |
|
60 | | - - name: Commit and push changes (PR) |
61 | | - if: github.event_name == 'pull_request' |
| 61 | + - name: Commit and merge changes |
62 | 62 | env: |
63 | | - 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 |
64 | 68 | run: | |
65 | | - git fetch origin |
66 | | - git checkout -b ${{ github.event.pull_request.head.ref }} origin/${{ github.event.pull_request.head.ref }} |
67 | | - git add "*.md" metrics.json |
68 | | - git commit -m "Update visitor count" || echo "No changes to commit" |
69 | | - git remote set-url origin https://x-access-token:${TOKEN}@github.com/${{ github.repository }} |
70 | | - git pull --rebase origin ${{ github.event.pull_request.head.ref }} || echo "No rebase needed" |
71 | | - git push origin HEAD:${{ github.event.pull_request.head.ref }} |
72 | | -
|
73 | | - - name: Commit and push changes (non-PR) |
74 | | - if: github.event_name != 'pull_request' |
75 | | - env: |
76 | | - TOKEN: ${{ secrets.GITHUB_TOKEN }} |
77 | | - run: | |
78 | | - git fetch origin |
79 | | - git checkout ${{ github.event.pull_request.head.ref }} || git checkout -b ${{ github.event.pull_request.head.ref }} origin/${{ github.event.pull_request.head.ref }} |
80 | | - git add "*.md" metrics.json |
81 | | - git commit -m "Update visitor count" || echo "No changes to commit" |
82 | | - git remote set-url origin https://x-access-token:${TOKEN}@github.com/${{ github.repository }} |
83 | | - git pull --rebase origin ${{ github.event.pull_request.head.ref }} || echo "No rebase needed" |
84 | | - git push origin HEAD:${{ github.event.pull_request.head.ref }} |
85 | | -
|
86 | | - - name: Create Pull Request (non-PR) |
87 | | - if: github.event_name != 'pull_request' |
88 | | - uses: peter-evans/create-pull-request@v6 |
89 | | - with: |
90 | | - token: ${{ secrets.GITHUB_TOKEN }} |
91 | | - branch: update-visitor-count |
92 | | - title: "Update visitor count" |
93 | | - body: "Automated update of visitor count" |
94 | | - base: main |
| 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 |
| 73 | + git add -A |
| 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