|
35 | 35 | uses: actions/checkout@v4 |
36 | 36 | with: |
37 | 37 | fetch-depth: 0 |
| 38 | + persist-credentials: true |
38 | 39 |
|
39 | 40 | - name: Fetch canonical .editorconfig (token clone if available and not a fork, otherwise raw URL) |
40 | 41 | run: | |
@@ -85,26 +86,86 @@ jobs: |
85 | 86 | run: | |
86 | 87 | # Overwrite or create the repo's .editorconfig with the canonical file |
87 | 88 | cp canonical.editorconfig .editorconfig |
88 | | - # Show diff for logs (first 200 lines) |
89 | | - echo "---- .editorconfig (canonical) ----" |
90 | | - sed -n '1,200p' canonical.editorconfig || true |
91 | | - echo "-----------------------------------" |
| 89 | + # Debug: show file summary to make sure the copy happened |
| 90 | + echo "Workspace .editorconfig:" |
| 91 | + ls -la .editorconfig || true |
| 92 | + echo "---- head of .editorconfig ----" |
| 93 | + sed -n '1,40p' .editorconfig || true |
| 94 | + echo "--------------------------------" |
92 | 95 |
|
93 | | - - name: Create sync pull request if .editorconfig differs or missing |
| 96 | + - name: Commit and attempt to push .editorconfig to target branch (sets push_succeeded) |
| 97 | + id: commit_push |
94 | 98 | if: steps.compare.outputs.same == 'false' |
| 99 | + run: | |
| 100 | + # We'll always write an output push_succeeded (true/false). The step must succeed |
| 101 | + # so the workflow can run a fallback PR when push fails. |
| 102 | + set -o pipefail |
| 103 | + PUSH_SUCCEEDED=false |
| 104 | +
|
| 105 | + echo "Preparing to commit .editorconfig to branch '${{ env.TARGET_BRANCH }}'" |
| 106 | +
|
| 107 | + # Configure git for the automated commit |
| 108 | + git config user.name "github-actions[bot]" |
| 109 | + git config user.email "41898282+github-actions[bot]@users.noreply.github.com" |
| 110 | +
|
| 111 | + # Only attempt push when this run is allowed to push (push events, or PRs from the same repo) |
| 112 | + if [ "${{ github.event_name }}" = "push" ] || \ |
| 113 | + ( [ "${{ github.event_name }}" = "pull_request" ] && [ "${{ github.event.pull_request.head.repo.full_name }}" = "${{ github.repository }}" ] ); then |
| 114 | +
|
| 115 | + echo "Push permitted for this run; attempting to commit and push." |
| 116 | +
|
| 117 | + # Ensure we have an up-to-date view of the remote target branch |
| 118 | + git fetch origin "${{ env.TARGET_BRANCH }}" || true |
| 119 | +
|
| 120 | + # Checkout the target branch (create it locally if it doesn't exist) |
| 121 | + if git rev-parse --verify "${{ env.TARGET_BRANCH }}" >/dev/null 2>&1; then |
| 122 | + git checkout "${{ env.TARGET_BRANCH }}" |
| 123 | + else |
| 124 | + git checkout -b "${{ env.TARGET_BRANCH }}" |
| 125 | + fi |
| 126 | +
|
| 127 | + # Add and commit .editorconfig |
| 128 | + git add .editorconfig |
| 129 | +
|
| 130 | + if git diff --staged --quiet; then |
| 131 | + echo "No staged changes to commit. Nothing to push." |
| 132 | + PUSH_SUCCEEDED=true |
| 133 | + else |
| 134 | + git commit -m "chore(sync): update .editorconfig from canonical" --author="github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>" || true |
| 135 | +
|
| 136 | + # Push and capture exit code instead of failing the step |
| 137 | + echo "Pushing to origin/${{ env.TARGET_BRANCH }}..." |
| 138 | + if git push origin "${{ env.TARGET_BRANCH }}"; then |
| 139 | + echo "Push successful." |
| 140 | + PUSH_SUCCEEDED=true |
| 141 | + else |
| 142 | + echo "Push failed (remote rejected or permission error). Will fall back to PR." |
| 143 | + PUSH_SUCCEEDED=false |
| 144 | + fi |
| 145 | + fi |
| 146 | + else |
| 147 | + echo "This run is not permitted to push (e.g., forked PR). Skipping direct push and will fall back to PR." |
| 148 | + PUSH_SUCCEEDED=false |
| 149 | + fi |
| 150 | +
|
| 151 | + # Emit the result for later steps |
| 152 | + if [ "$PUSH_SUCCEEDED" = "true" ]; then |
| 153 | + echo "push_succeeded=true" >> $GITHUB_OUTPUT |
| 154 | + else |
| 155 | + echo "push_succeeded=false" >> $GITHUB_OUTPUT |
| 156 | + fi |
| 157 | +
|
| 158 | + - name: Fallback: open PR if direct push was not possible/failed |
| 159 | + if: steps.compare.outputs.same == 'false' && steps.commit_push.outputs.push_succeeded != 'true' |
95 | 160 | uses: peter-evans/create-pull-request@v5 |
96 | 161 | with: |
97 | | - # new branch name for sync commit |
98 | 162 | branch: sync-editorconfig/${{ github.run_id }} |
99 | | - # target the branch that triggered the run (developer's branch) |
100 | 163 | base: ${{ env.TARGET_BRANCH }} |
101 | 164 | title: "chore: sync .editorconfig with canonical" |
102 | 165 | body: | |
103 | 166 | This automated PR updates or adds .editorconfig from the canonical repository (${{ env.CANONICAL_REPO }}@${{ env.CANONICAL_BRANCH }}). |
104 | 167 | Please merge or pull this update to ensure your local analyzers and formatting use the canonical rules. |
105 | 168 | commit-message: "chore(sync): update .editorconfig from canonical" |
106 | 169 | labels: automation |
107 | | - # include only .editorconfig in the commit |
108 | 170 | add-paths: .editorconfig |
109 | | - # Author set to the GitHub Actions bot to make automated commits identifiable |
110 | 171 | author: "github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>" |
0 commit comments