Skip to content

Commit 7218551

Browse files
authored
Enhance sync-editorconfig workflow with debug info
Update the workflow to include debug output and commit logic for .editorconfig.
1 parent a8b1434 commit 7218551

1 file changed

Lines changed: 70 additions & 9 deletions

File tree

‎.github/workflows/sync-editorconfig-on-push.yml‎

Lines changed: 70 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ jobs:
3535
uses: actions/checkout@v4
3636
with:
3737
fetch-depth: 0
38+
persist-credentials: true
3839

3940
- name: Fetch canonical .editorconfig (token clone if available and not a fork, otherwise raw URL)
4041
run: |
@@ -85,26 +86,86 @@ jobs:
8586
run: |
8687
# Overwrite or create the repo's .editorconfig with the canonical file
8788
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 "--------------------------------"
9295
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
9498
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'
95160
uses: peter-evans/create-pull-request@v5
96161
with:
97-
# new branch name for sync commit
98162
branch: sync-editorconfig/${{ github.run_id }}
99-
# target the branch that triggered the run (developer's branch)
100163
base: ${{ env.TARGET_BRANCH }}
101164
title: "chore: sync .editorconfig with canonical"
102165
body: |
103166
This automated PR updates or adds .editorconfig from the canonical repository (${{ env.CANONICAL_REPO }}@${{ env.CANONICAL_BRANCH }}).
104167
Please merge or pull this update to ensure your local analyzers and formatting use the canonical rules.
105168
commit-message: "chore(sync): update .editorconfig from canonical"
106169
labels: automation
107-
# include only .editorconfig in the commit
108170
add-paths: .editorconfig
109-
# Author set to the GitHub Actions bot to make automated commits identifiable
110171
author: "github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>"

0 commit comments

Comments
 (0)