Skip to content

Commit 885427f

Browse files
authored
Add workflow to sync .editorconfig from canonical repo
This workflow synchronizes the .editorconfig file from a canonical repository on push or pull request events. It checks for differences and creates a pull request if the local .editorconfig is outdated or missing.
1 parent ae49470 commit 885427f

1 file changed

Lines changed: 96 additions & 0 deletions

File tree

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
name: Sync .editorconfig from canonical
2+
on:
3+
push:
4+
pull_request:
5+
6+
permissions:
7+
contents: write
8+
9+
env:
10+
# Replace with your canonical repo and branch if different
11+
CANONICAL_REPO: BionicCode/bioniccode-code-style
12+
CANONICAL_BRANCH: main
13+
# Raw URL used if canonical is public (constructed from CANONICAL_REPO/CANONICAL_BRANCH)
14+
CANONICAL_RAW_URL: https://raw.githubusercontent.com/BionicCode/bioniccode-code-style/main/.editorconfig
15+
16+
jobs:
17+
sync-editorconfig:
18+
# Determine the branch to target: for pull_request use the PR head branch, otherwise use ref_name
19+
env:
20+
TARGET_BRANCH: ${{ github.event.pull_request.head.ref || github.ref_name }}
21+
runs-on: ubuntu-latest
22+
23+
steps:
24+
- name: Exit if triggered by Actions bot (avoid loops)
25+
if: ${{ github.actor == 'github-actions[bot]' }}
26+
run: |
27+
echo "Triggered by github-actions[bot], exiting to avoid loop."
28+
exit 0
29+
30+
- name: Checkout repository
31+
uses: actions/checkout@v4
32+
with:
33+
fetch-depth: 0
34+
35+
- name: Fetch canonical .editorconfig (private repo path if token provided)
36+
# If secret CANONICAL_REPO_TOKEN is set in the consumer repo, checkout canonical repo; otherwise fall back to raw URL
37+
run: |
38+
set -e
39+
# Prefer checkout via token if secret is provided (for private canonical repos)
40+
if [ -n "${{ secrets.CANONICAL_REPO_TOKEN }}" ]; then
41+
echo "Using secret to checkout canonical repository"
42+
git clone --depth 1 https://x-access-token:${{ secrets.CANONICAL_REPO_TOKEN }}@github.com/${{ env.CANONICAL_REPO }} canonical-repo || true
43+
if [ -f canonical-repo/.editorconfig ]; then
44+
cp canonical-repo/.editorconfig canonical.editorconfig
45+
else
46+
echo "canonical .editorconfig not found in canonical-repo"
47+
fi
48+
else
49+
echo "No CANONICAL_REPO_TOKEN secret found — attempting to fetch public raw file"
50+
curl -fsSL "${{ env.CANONICAL_RAW_URL }}" -o canonical.editorconfig || true
51+
fi
52+
53+
- name: Ensure we have a canonical file
54+
run: |
55+
if [ ! -s canonical.editorconfig ]; then
56+
echo "Error: canonical.editorconfig not available. Aborting." >&2
57+
exit 1
58+
fi
59+
60+
- name: Compare with repository .editorconfig
61+
id: compare
62+
run: |
63+
if [ -f .editorconfig ] && cmp -s .editorconfig canonical.editorconfig; then
64+
echo "same=true" >> $GITHUB_OUTPUT
65+
else
66+
echo "same=false" >> $GITHUB_OUTPUT
67+
fi
68+
69+
- name: Prepare workspace with canonical .editorconfig
70+
if: steps.compare.outputs.same == 'false'
71+
run: |
72+
# Overwrite or create the repo's .editorconfig with the canonical file
73+
cp canonical.editorconfig .editorconfig
74+
# Show diff for logs
75+
echo "---- .editorconfig (canonical) ----"
76+
sed -n '1,200p' canonical.editorconfig || true
77+
echo "-----------------------------------"
78+
79+
- name: Create sync pull request if .editorconfig differs or missing
80+
if: steps.compare.outputs.same == 'false'
81+
uses: peter-evans/create-pull-request@v5
82+
with:
83+
# new branch name for sync commit
84+
branch: sync-editorconfig/${{ github.run_id }}
85+
# target the branch that triggered the run (developer's branch)
86+
base: ${{ env.TARGET_BRANCH }}
87+
title: chore: sync .editorconfig with canonical
88+
body: |
89+
This automated PR updates or adds .editorconfig from the canonical repository (${{ env.CANONICAL_REPO }}@${{ env.CANONICAL_BRANCH }}).
90+
Please merge or pull this update to ensure your local analyzers and formatting use the canonical rules.
91+
commit-message: chore(sync): update .editorconfig from canonical
92+
labels: automation
93+
# The action will pick up changes in the working directory; specifying file-pattern helps
94+
file-pattern: .editorconfig
95+
# Author set to the GitHub Actions bot to make automated commits identifiable
96+
author: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

0 commit comments

Comments
 (0)