Skip to content

Commit fba69d1

Browse files
authored
Update GitHub Actions workflow for editorconfig sync
1 parent c112874 commit fba69d1

1 file changed

Lines changed: 48 additions & 29 deletions

File tree

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

Lines changed: 48 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ name: Sync .editorconfig from canonical (PR-only)
22

33
on:
44
push:
5+
# Avoid loops: ignore the bot-created sync branches.
56
branches-ignore:
6-
- "chore/sync-editorconfig" # bot PR branch
7+
- "chore/sync-editorconfig/**"
78
pull_request:
89
workflow_dispatch:
9-
# Optional polling safety net (recommended for dormant repos):
10+
# Optional: enable schedule if you want "silent repos" to still get updates.
1011
# schedule:
1112
# - cron: "17 * * * *" # hourly at :17
1213

@@ -15,15 +16,17 @@ permissions:
1516
pull-requests: write
1617

1718
env:
19+
# Your canonical raw URL (as provided)
1820
CANONICAL_RAW_URL: https://raw.githubusercontent.com/BionicCode/bioniccode-code-style/refs/heads/main/BionicCode.CodeStyle/BionicCode.CodeStyle/.editorconfig
19-
SYNC_BRANCH: chore/sync-editorconfig
21+
SYNC_BRANCH_PREFIX: chore/sync-editorconfig
2022

21-
# Concurrency scoped per PR or per ref (prevents cross-branch cancellations)
23+
# Avoid cross-branch cancellations; scope concurrency per PR number or per ref.
2224
concurrency:
2325
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
2426
cancel-in-progress: true
2527

2628
jobs:
29+
# 1) PR guard: fail early if the PR branch is out of sync with canonical.
2730
verify-on-pr:
2831
if: github.event_name == 'pull_request'
2932
runs-on: ubuntu-latest
@@ -32,23 +35,24 @@ jobs:
3235
uses: actions/checkout@v4
3336
with:
3437
ref: ${{ github.head_ref }}
38+
fetch-depth: 1
3539

3640
- name: Download canonical .editorconfig
3741
run: |
3842
set -euo pipefail
3943
curl -fsSL "${CANONICAL_RAW_URL}" -o canonical.editorconfig
4044
41-
- name: Ensure exactly one tracked .editorconfig (strict)
45+
- name: Ensure exactly one tracked .editorconfig (repo root)
4246
run: |
4347
set -euo pipefail
44-
count=$(git ls-files '**/.editorconfig' | wc -l | tr -d ' ')
45-
if [ "$count" -ne 1 ]; then
46-
echo "::error::Expected exactly one tracked .editorconfig (repo root). Found: $count"
48+
count="$(git ls-files '**/.editorconfig' | wc -l | tr -d ' ')"
49+
if [ "${count}" -ne 1 ]; then
50+
echo "::error::Expected exactly one tracked .editorconfig (repo root). Found: ${count}"
4751
git ls-files '**/.editorconfig' || true
4852
exit 1
4953
fi
5054
51-
- name: Fail if branch .editorconfig is out of sync
55+
- name: Fail if .editorconfig is out of sync
5256
run: |
5357
set -euo pipefail
5458
if cmp -s .editorconfig canonical.editorconfig; then
@@ -58,61 +62,76 @@ jobs:
5862
exit 1
5963
fi
6064
65+
# 2) PR creator/updater: on push/workflow_dispatch, create or update a sync PR into the *current branch*.
6166
open-or-update-sync-pr:
62-
if: github.actor != 'github-actions[bot]'
67+
if: >
68+
github.event_name != 'pull_request' &&
69+
startsWith(github.ref, 'refs/heads/') &&
70+
github.actor != 'github-actions[bot]'
6371
runs-on: ubuntu-latest
6472
steps:
6573
- name: Determine base branch
6674
id: base
6775
shell: bash
6876
run: |
6977
set -euo pipefail
70-
if [ "${{ github.event_name }}" = "pull_request" ]; then
71-
echo "name=${{ github.head_ref }}" >> "$GITHUB_OUTPUT"
72-
else
73-
echo "name=${{ github.ref_name }}" >> "$GITHUB_OUTPUT"
74-
fi
75-
78+
# For push & workflow_dispatch on a branch, github.ref_name is the branch name.
79+
echo "name=${{ github.ref_name }}" >> "$GITHUB_OUTPUT"
80+
7681
- name: Checkout base branch
7782
uses: actions/checkout@v4
7883
with:
7984
ref: ${{ steps.base.outputs.name }}
80-
85+
fetch-depth: 0
86+
8187
- name: Download canonical .editorconfig
8288
run: |
8389
set -euo pipefail
84-
curl -fsSL "https://raw.githubusercontent.com/BionicCode/bioniccode-code-style/main/.editorconfig" \
85-
-o canonical.editorconfig
86-
87-
- name: Apply if changed
90+
curl -fsSL "${CANONICAL_RAW_URL}" -o canonical.editorconfig
91+
92+
- name: Apply canonical .editorconfig if changed
8893
id: diff
8994
shell: bash
9095
run: |
9196
set -euo pipefail
97+
9298
if [ -f .editorconfig ] && cmp -s .editorconfig canonical.editorconfig; then
9399
echo "changed=false" >> "$GITHUB_OUTPUT"
94-
else
95-
mv canonical.editorconfig .editorconfig
96-
echo "changed=true" >> "$GITHUB_OUTPUT"
100+
exit 0
97101
fi
98-
99-
- name: Compute sync branch name (per-base)
102+
103+
mv canonical.editorconfig .editorconfig
104+
echo "changed=true" >> "$GITHUB_OUTPUT"
105+
106+
- name: Compute sync branch name (per base branch)
100107
if: steps.diff.outputs.changed == 'true'
101108
id: syncbranch
102109
shell: bash
103110
run: |
104111
set -euo pipefail
105112
base="${{ steps.base.outputs.name }}"
106113
safe="$(echo "$base" | tr '/ ' '--')"
107-
echo "name=chore/sync-editorconfig/$safe" >> "$GITHUB_OUTPUT"
108-
114+
echo "name=${SYNC_BRANCH_PREFIX}/${safe}" >> "$GITHUB_OUTPUT"
115+
109116
- name: Create or update PR into the base branch
110117
if: steps.diff.outputs.changed == 'true'
111-
uses: peter-evans/create-pull-request@v7
118+
id: cpr
119+
uses: peter-evans/create-pull-request@v8
112120
with:
113121
branch: ${{ steps.syncbranch.outputs.name }}
114122
base: ${{ steps.base.outputs.name }}
115123
title: "chore: sync .editorconfig from canonical"
116124
commit-message: "chore(sync): update .editorconfig from canonical"
125+
body: |
126+
Automated sync of `.editorconfig` from the canonical repository.
127+
128+
Source: BionicCode/bioniccode-code-style (main)
117129
add-paths: |
118130
.editorconfig
131+
labels: automation
132+
133+
- name: Report PR result
134+
if: steps.diff.outputs.changed == 'true'
135+
run: |
136+
echo "PR number: ${{ steps.cpr.outputs.pull-request-number }}"
137+
echo "PR URL: ${{ steps.cpr.outputs.pull-request-url }}"

0 commit comments

Comments
 (0)