Skip to content

Clean up .editorconfig by removing analyzer settings #14

Clean up .editorconfig by removing analyzer settings

Clean up .editorconfig by removing analyzer settings #14

name: Sync .editorconfig from canonical
on:
push:
pull_request:
permissions:
contents: write
pull-requests: write
env:
# Replace with your canonical repo and branch if different
CANONICAL_REPO: BionicCode/bioniccode-code-style
CANONICAL_BRANCH: main
jobs:
sync-editorconfig:
# Determine the branch to target: for pull_request use the PR head branch, otherwise use ref_name
env:
TARGET_BRANCH: ${{ github.event.pull_request.head.ref || github.ref_name }}
# true when this run is a pull_request from a fork; used to avoid using secrets for forked PRs
IS_FORK: ${{ github.event.pull_request != null && github.event.pull_request.head.repo.fork == true }}
runs-on: ubuntu-latest
concurrency:
group: sync-editorconfig-${{ github.ref_name }}
cancel-in-progress: true
steps:
- name: Exit if triggered by Actions bot (avoid loops)
if: ${{ github.actor == 'github-actions[bot]' }}
run: |
echo "Triggered by github-actions[bot], exiting to avoid loop."
exit 0
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0
persist-credentials: true
- name: Fetch canonical .editorconfig (token clone if available and not a fork, otherwise raw URL)
run: |
set -e
CANONICAL_PATH="BionicCode.CodeStyle/BionicCode.CodeStyle/.editorconfig"
RAW_URL="https://raw.githubusercontent.com/${{ env.CANONICAL_REPO }}/${{ env.CANONICAL_BRANCH }}/${CANONICAL_PATH}"
echo "Canonical repo: ${{ env.CANONICAL_REPO }} (branch: ${{ env.CANONICAL_BRANCH }})"
echo "Using raw URL: $RAW_URL"
# Only attempt token clone if a token is present AND this is not a forked PR
if [ -n "${{ secrets.CANONICAL_REPO_TOKEN }}" ] && [ "${{ env.IS_FORK }}" != "true" ]; then
echo "Attempting authenticated clone of canonical repository"
git clone --depth 1 https://x-access-token:${{ secrets.CANONICAL_REPO_TOKEN }}@github.com/${{ env.CANONICAL_REPO }} canonical-repo || true
if [ -f canonical-repo/.editorconfig ]; then
cp canonical-repo/.editorconfig canonical.editorconfig
echo "Fetched canonical .editorconfig via authenticated clone."
else
echo "Authenticated clone succeeded but canonical .editorconfig not found in canonical-repo"
fi
else
echo "No usable token (or forked PR). Falling back to public raw URL"
curl -fsSL "$RAW_URL" -o canonical.editorconfig || true
if [ -s canonical.editorconfig ]; then
echo "Fetched canonical .editorconfig via public raw URL."
else
echo "Failed to fetch canonical.editorconfig from raw URL"
fi
fi
- name: Ensure we have a canonical file
run: |
if [ ! -s canonical.editorconfig ]; then
echo "Error: canonical.editorconfig not available. Aborting." >&2
exit 1
fi
- name: Compare with repository .editorconfig
id: compare
run: |
if [ -f .editorconfig ] && cmp -s .editorconfig canonical.editorconfig; then
echo "same=true" >> $GITHUB_OUTPUT
else
echo "same=false" >> $GITHUB_OUTPUT
fi
- name: Prepare workspace with canonical .editorconfig
if: steps.compare.outputs.same == 'false'
run: |
# Overwrite or create the repo's .editorconfig with the canonical file
cp canonical.editorconfig .editorconfig
# Debug: show file summary to make sure the copy happened
echo "Workspace .editorconfig:"
ls -la .editorconfig || true
echo "---- head of .editorconfig ----"
sed -n '1,40p' .editorconfig || true
echo "--------------------------------"
- name: Commit and attempt to push .editorconfig to target branch (sets push_succeeded)
id: commit_push
if: steps.compare.outputs.same == 'false'
run: |
# We'll always write an output push_succeeded (true/false). The step must succeed
# so the workflow can run a fallback PR when push fails.
set -o pipefail
PUSH_SUCCEEDED=false
echo "Preparing to commit .editorconfig to branch '${{ env.TARGET_BRANCH }}'"
# Configure git for the automated commit
git config user.name "github-actions[bot]"
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
# Only attempt push when this run is allowed to push (push events, or PRs from the same repo)
if [ "${{ github.event_name }}" = "push" ] || \
( [ "${{ github.event_name }}" = "pull_request" ] && [ "${{ github.event.pull_request.head.repo.full_name }}" = "${{ github.repository }}" ] ); then
echo "Push permitted for this run; attempting to commit and push."
# Ensure we have an up-to-date view of the remote target branch
git fetch origin "${{ env.TARGET_BRANCH }}" || true
# Checkout the target branch (create it locally if it doesn't exist)
if git rev-parse --verify "${{ env.TARGET_BRANCH }}" >/dev/null 2>&1; then
git checkout "${{ env.TARGET_BRANCH }}"
else
git checkout -b "${{ env.TARGET_BRANCH }}"
fi
# Add and commit .editorconfig
git add .editorconfig
if git diff --staged --quiet; then
echo "No staged changes to commit. Nothing to push."
PUSH_SUCCEEDED=true
else
git commit -m "chore(sync): update .editorconfig from canonical" --author="github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>" || true
# Push and capture exit code instead of failing the step
echo "Pushing to origin/${{ env.TARGET_BRANCH }}..."
if git push origin "${{ env.TARGET_BRANCH }}"; then
echo "Push successful."
PUSH_SUCCEEDED=true
else
echo "Push failed (remote rejected or permission error). Will fall back to PR."
PUSH_SUCCEEDED=false
fi
fi
else
echo "This run is not permitted to push (e.g., forked PR). Skipping direct push and will fall back to PR."
PUSH_SUCCEEDED=false
fi
# Emit the result for later steps
if [ "$PUSH_SUCCEEDED" = "true" ]; then
echo "push_succeeded=true" >> $GITHUB_OUTPUT
else
echo "push_succeeded=false" >> $GITHUB_OUTPUT
fi
- name: "Fallback: open PR if direct push was not possible/failed"
if: steps.compare.outputs.same == 'false' && steps.commit_push.outputs.push_succeeded != 'true'
uses: peter-evans/create-pull-request@v5
with:
branch: sync-editorconfig/${{ github.run_id }}
base: ${{ env.TARGET_BRANCH }}
title: "chore: sync .editorconfig with canonical"
body: |
This automated PR updates or adds .editorconfig from the canonical repository (${{ env.CANONICAL_REPO }}@${{ env.CANONICAL_BRANCH }}).
Please merge or pull this update to ensure your local analyzers and formatting use the canonical rules.
commit-message: "chore(sync): update .editorconfig from canonical"
labels: automation
add-paths: .editorconfig
author: "github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>"