Skip to content

Commit f146677

Browse files
committed
fix: address review feedback on sync-preview workflow
- Pass PREVIEW_VERSION via env var instead of string interpolation in node -e scripts (safer against special chars) - Make git add of package-lock.json conditional on file existence to match the earlier -f guard - Replace loose title search for dedup with headRefName prefix filter to avoid false positives from unrelated PRs - Clarify why package.json/package-lock.json are special-cased (preview carries a different version string that needs preserving)
1 parent 41595ab commit f146677

1 file changed

Lines changed: 13 additions & 10 deletions

File tree

.github/workflows/sync-preview.yml

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,9 @@ jobs:
6565
if git merge origin/main --no-edit -m "chore: merge main into preview"; then
6666
echo "status=clean" >> $GITHUB_OUTPUT
6767
else
68-
# If package.json or package-lock.json conflict, accept main's content
69-
# (we'll fix the version back afterwards)
68+
# preview carries a higher version string than main (e.g. 1.0.0-preview.X vs 0.13.X).
69+
# This means package.json/package-lock.json almost always conflict on the version field.
70+
# Accept main's content here; the version is restored in the next step.
7071
for f in package.json package-lock.json; do
7172
if git diff --name-only --diff-filter=U | grep -qx "$f"; then
7273
git checkout --theirs "$f"
@@ -91,24 +92,25 @@ jobs:
9192
CURRENT_VERSION=$(node -p "require('./package.json').version")
9293
9394
if [[ "$CURRENT_VERSION" != "$PREVIEW_VERSION" ]]; then
94-
node -e "
95+
PREVIEW_VERSION="$PREVIEW_VERSION" node -e "
9596
const fs = require('fs');
9697
const pkg = JSON.parse(fs.readFileSync('package.json', 'utf8'));
97-
pkg.version = '$PREVIEW_VERSION';
98+
pkg.version = process.env.PREVIEW_VERSION;
9899
fs.writeFileSync('package.json', JSON.stringify(pkg, null, 2) + '\n');
99100
"
100101
if [[ -f package-lock.json ]]; then
101-
node -e "
102+
PREVIEW_VERSION="$PREVIEW_VERSION" node -e "
102103
const fs = require('fs');
103104
const lock = JSON.parse(fs.readFileSync('package-lock.json', 'utf8'));
104-
lock.version = '$PREVIEW_VERSION';
105+
lock.version = process.env.PREVIEW_VERSION;
105106
if (lock.packages && lock.packages['']) {
106-
lock.packages[''].version = '$PREVIEW_VERSION';
107+
lock.packages[''].version = process.env.PREVIEW_VERSION;
107108
}
108109
fs.writeFileSync('package-lock.json', JSON.stringify(lock, null, 2) + '\n');
109110
"
110111
fi
111-
git add package.json package-lock.json
112+
git add package.json
113+
[[ -f package-lock.json ]] && git add package-lock.json
112114
git commit -m "chore: restore preview version ($PREVIEW_VERSION)"
113115
fi
114116
@@ -120,8 +122,9 @@ jobs:
120122
env:
121123
GH_TOKEN: ${{ steps.app-token.outputs.token }}
122124
run: |
123-
# Check if there's already an open sync PR
124-
COUNT=$(gh pr list --base preview --search "sync-preview:" --state open --json number --jq 'length')
125+
# Check if there's already an open sync PR (match by branch prefix, not title search)
126+
COUNT=$(gh pr list --base preview --state open --json headRefName \
127+
--jq '[.[] | select(.headRefName | startswith("sync-preview/"))] | length')
125128
if [[ "$COUNT" != "0" ]]; then
126129
echo "ℹ️ Sync PR already open — skipping duplicate."
127130
exit 0

0 commit comments

Comments
 (0)