Skip to content

Commit 39b162f

Browse files
Rewrite retarget workflow: fix all logic bugs, validate locally
- Fix critical bug: diff after reset --hard produced empty patch (now saves upstream 7.6 before reset, diffs against saved copy) - Fix critical bug: --force-with-lease rejected after reset --hard (changed to --force) - Remove gh pr comment step (GITHUB_TOKEN can't cross orgs) - Move Python script to temp file (heredoc inside YAML |block broke PyYAML linters) - Remove dead code: whats-new-76 patch was saved but never used - Remove unused pull-requests:write permission - Add alternate section header variants for What's New matching - Validated: YAML parses clean, patch applies to real files, Python script tested against mock What's New, all edge cases verified under set -euo pipefail
1 parent 3d0b04a commit 39b162f

File tree

1 file changed

+73
-97
lines changed

1 file changed

+73
-97
lines changed

.github/workflows/retarget-7.7.yml

Lines changed: 73 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,11 @@ name: Retarget New-Guid docs to 7.7
22

33
on:
44
schedule:
5-
# Check daily at 08:00 UTC
65
- cron: '0 8 * * *'
7-
workflow_dispatch: # Allow manual trigger
6+
workflow_dispatch:
87

98
permissions:
109
contents: write
11-
pull-requests: write
1210

1311
jobs:
1412
check-and-retarget:
@@ -18,10 +16,8 @@ jobs:
1816
- name: Check if 7.7 folder exists upstream
1917
id: check
2018
run: |
21-
# Public repo — no auth needed (avoids cross-org token scope issues)
2219
STATUS=$(curl -s -o /dev/null -w "%{http_code}" \
2320
"https://api.github.com/repos/MicrosoftDocs/PowerShell-Docs/contents/reference/7.7/Microsoft.PowerShell.Utility")
24-
echo "status=$STATUS" >> "$GITHUB_OUTPUT"
2521
if [ "$STATUS" = "200" ]; then
2622
echo "exists=true" >> "$GITHUB_OUTPUT"
2723
else
@@ -36,15 +32,11 @@ jobs:
3632
ref: doc/new-guid-uuid-v7
3733
fetch-depth: 0
3834

39-
- name: Configure git identity
35+
- name: Configure git
4036
if: steps.check.outputs.exists == 'true'
4137
run: |
4238
git config user.name "github-actions[bot]"
4339
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
44-
45-
- name: Add upstream remote
46-
if: steps.check.outputs.exists == 'true'
47-
run: |
4840
git remote add upstream https://github.com/MicrosoftDocs/PowerShell-Docs.git
4941
git fetch upstream main
5042
@@ -64,125 +56,109 @@ jobs:
6456
run: |
6557
set -euo pipefail
6658
67-
# ── 1. Reset branch to upstream main (clean slate, avoids rebase conflicts) ──
68-
# Save our modified files first
59+
# Save our modified New-Guid and the upstream original before resetting
6960
cp reference/7.6/Microsoft.PowerShell.Utility/New-Guid.md /tmp/New-Guid-ours.md
70-
git diff upstream/main -- reference/docs-conceptual/whats-new/What-s-New-in-PowerShell-76.md \
71-
> /tmp/whats-new-76.patch || true
61+
git show upstream/main:reference/7.6/Microsoft.PowerShell.Utility/New-Guid.md \
62+
> /tmp/New-Guid-upstream-76.md
7263
64+
# Reset to upstream main (clean slate)
7365
git reset --hard upstream/main
7466
75-
# ── 2. Create 7.7 New-Guid.md from our modified version ──
67+
# ── Create 7.7 New-Guid.md ──
7668
mkdir -p reference/7.7/Microsoft.PowerShell.Utility
7769
78-
# Start with upstream's 7.7 base if it exists, otherwise use our modified file
7970
if git show upstream/main:reference/7.7/Microsoft.PowerShell.Utility/New-Guid.md > /dev/null 2>&1; then
80-
# Upstream has a 7.7 base — use it and apply our changes on top
71+
# Upstream has a 7.7 base — extract it and apply our diff on top
8172
git show upstream/main:reference/7.7/Microsoft.PowerShell.Utility/New-Guid.md \
8273
> reference/7.7/Microsoft.PowerShell.Utility/New-Guid.md
8374
84-
# Generate a patch from upstream 7.6 → our 7.6 and apply to 7.7
85-
git diff upstream/main -- reference/7.6/Microsoft.PowerShell.Utility/New-Guid.md \
86-
< /dev/null > /tmp/new-guid.patch || true
87-
if [ -s /tmp/new-guid.patch ]; then
88-
# Repath the patch from 7.6 to 7.7
89-
sed -i 's|reference/7.6/|reference/7.7/|g' /tmp/new-guid.patch
90-
git apply --3way /tmp/new-guid.patch || {
91-
echo "::warning::Patch apply failed — falling back to direct copy"
75+
# Build a unified diff from upstream-7.6 to our-modified-7.6
76+
diff -u /tmp/New-Guid-upstream-76.md /tmp/New-Guid-ours.md \
77+
> /tmp/our-changes.patch || true
78+
79+
if [ -s /tmp/our-changes.patch ]; then
80+
patch --no-backup-if-mismatch -p0 \
81+
reference/7.7/Microsoft.PowerShell.Utility/New-Guid.md \
82+
/tmp/our-changes.patch || {
83+
echo "::warning::Patch failed — falling back to direct copy"
9284
cp /tmp/New-Guid-ours.md reference/7.7/Microsoft.PowerShell.Utility/New-Guid.md
9385
}
9486
fi
9587
else
96-
# No upstream 7.7 base — copy our modified 7.6 file directly
88+
# No upstream 7.7 base — copy our modified file directly
9789
cp /tmp/New-Guid-ours.md reference/7.7/Microsoft.PowerShell.Utility/New-Guid.md
9890
fi
9991
100-
# Update version URL: 7.6 → 7.7
92+
# Fix version URLs
10193
sed -i 's/view=powershell-7\.6/view=powershell-7.7/g' \
10294
reference/7.7/Microsoft.PowerShell.Utility/New-Guid.md
10395
104-
# ── 3. Handle What's New ──
105-
# The 7.6 file is already at upstream state (we did reset --hard).
106-
# Check if a 7.7 What's New file exists.
96+
# ── Handle What's New for 7.7 ──
10797
WHATS_NEW_77="reference/docs-conceptual/whats-new/What-s-New-in-PowerShell-77.md"
108-
if [ -f "$WHATS_NEW_77" ]; then
109-
if ! grep -q "New-Guid" "$WHATS_NEW_77"; then
110-
python3 <<'PYEOF'
111-
import sys
112-
113-
path = "reference/docs-conceptual/whats-new/What-s-New-in-PowerShell-77.md"
114-
with open(path, 'r') as f:
115-
content = f.read()
116-
117-
breaking_entry = ("- `New-Guid` now generates Version 7 (time-sortable) UUIDs by default instead of Version 4\n"
118-
" (random). The output format is unchanged. Scripts that depend on fully random GUIDs should use\n"
119-
" `[guid]::NewGuid()` directly. ([#27033][27033])")
120-
121-
cmdlet_entry = ("- Change `New-Guid` to generate UUID v7 by default using `Guid.CreateVersion7()`\n"
122-
" ([#27033][27033])")
123-
124-
link_entry = "[27033]: https://github.com/PowerShell/PowerShell/pull/27033"
125-
126-
modified = False
127-
128-
if '## Breaking changes' in content:
129-
content = content.replace('## Breaking changes\n', '## Breaking changes\n\n' + breaking_entry + '\n', 1)
130-
modified = True
131-
132-
if '## Cmdlet improvements' in content:
133-
content = content.replace('## Cmdlet improvements\n', '## Cmdlet improvements\n\n' + cmdlet_entry + '\n', 1)
134-
modified = True
135-
elif '## Updated cmdlets' in content:
136-
content = content.replace('## Updated cmdlets\n', '## Updated cmdlets\n\n' + cmdlet_entry + '\n', 1)
137-
modified = True
138-
139-
if link_entry not in content:
140-
content = content.rstrip() + '\n' + link_entry + '\n'
141-
modified = True
142-
143-
if not modified:
144-
print("::warning::What's New 7.7 exists but has no recognized section headers. Manual edit needed.")
145-
sys.exit(0)
146-
147-
with open(path, 'w') as f:
148-
f.write(content)
149-
print("Updated What's New 7.7 with New-Guid entries.")
150-
PYEOF
151-
fi
152-
else
153-
echo "::notice::What-s-New-in-PowerShell-77.md does not exist yet. Only New-Guid.md will be included."
98+
if [ -f "$WHATS_NEW_77" ] && ! grep -q "New-Guid" "$WHATS_NEW_77"; then
99+
# Write the update script to a temp file to avoid YAML quoting issues
100+
cat > /tmp/update-whats-new.py << 'PYEOF'
101+
import sys
102+
path = sys.argv[1]
103+
with open(path, 'r') as f:
104+
content = f.read()
105+
breaking = (
106+
'- `New-Guid` now generates Version 7 (time-sortable) UUIDs by default instead of Version 4\n'
107+
' (random). The output format is unchanged. Scripts that depend on fully random GUIDs should use\n'
108+
' `[guid]::NewGuid()` directly. ([#27033][27033])'
109+
)
110+
cmdlet = (
111+
'- Change `New-Guid` to generate UUID v7 by default using `Guid.CreateVersion7()`\n'
112+
' ([#27033][27033])'
113+
)
114+
link = '[27033]: https://github.com/PowerShell/PowerShell/pull/27033'
115+
changed = False
116+
for h in ['## Breaking changes', '## Breaking Changes']:
117+
if h in content:
118+
content = content.replace(h + '\n', h + '\n\n' + breaking + '\n', 1)
119+
changed = True
120+
break
121+
for h in ['## Cmdlet improvements', '## Cmdlet Improvements', '## Updated cmdlets', '## Updated Cmdlets']:
122+
if h in content:
123+
content = content.replace(h + '\n', h + '\n\n' + cmdlet + '\n', 1)
124+
changed = True
125+
break
126+
if link not in content:
127+
content = content.rstrip() + '\n' + link + '\n'
128+
changed = True
129+
if changed:
130+
with open(path, 'w') as f:
131+
f.write(content)
132+
print('Updated ' + path)
133+
else:
134+
print('::warning::No recognized section headers in ' + path + '. Manual edit needed.')
135+
PYEOF
136+
python3 /tmp/update-whats-new.py "$WHATS_NEW_77"
137+
elif [ ! -f "$WHATS_NEW_77" ]; then
138+
echo "::notice::What-s-New-in-PowerShell-77.md not found. Only New-Guid.md included."
154139
fi
155140
156-
# ── 4. Stage and commit ──
141+
# ── Stage and commit ──
157142
git add reference/7.7/Microsoft.PowerShell.Utility/New-Guid.md
158-
if [ -f "$WHATS_NEW_77" ]; then
159-
git add "$WHATS_NEW_77"
160-
fi
143+
[ -f "$WHATS_NEW_77" ] && git add "$WHATS_NEW_77" 2>/dev/null || true
161144
162145
git commit -m "Retarget New-Guid UUID v7 docs from 7.6 to 7.7
163146
164-
The 7.7 reference folder is now available upstream.
165-
Moved New-Guid changes to reference/7.7/ and reverted
166-
reference/7.6/ to upstream state."
147+
The 7.7 reference folder is now available upstream.
148+
Moved New-Guid changes to reference/7.7/ and reverted
149+
reference/7.6/ to upstream state."
167150
168151
- name: Push changes
169152
if: steps.check.outputs.exists == 'true' && steps.already.outputs.done != 'true'
170-
run: git push origin doc/new-guid-uuid-v7 --force-with-lease
153+
run: git push origin doc/new-guid-uuid-v7 --force
171154

172-
- name: Comment on PR
173-
if: steps.check.outputs.exists == 'true' && steps.already.outputs.done != 'true'
174-
env:
175-
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
176-
run: |
177-
gh pr comment 12888 --repo MicrosoftDocs/PowerShell-Docs --body \
178-
"The \`reference/7.7/\` folder is now available upstream. I've retargeted this PR — \`New-Guid.md\` changes now live under \`reference/7.7/\` and the 7.6 file has been reverted to upstream state. Ready for review when convenient."
179-
180-
- name: Post summary
155+
- name: Summary
181156
if: steps.check.outputs.exists == 'true' && steps.already.outputs.done != 'true'
182157
run: |
183158
echo "## Retarget complete" >> "$GITHUB_STEP_SUMMARY"
184159
echo "" >> "$GITHUB_STEP_SUMMARY"
185-
echo "- Moved \`New-Guid.md\` from \`reference/7.6/\` → \`reference/7.7/\`" >> "$GITHUB_STEP_SUMMARY"
186-
echo "- Reverted \`reference/7.6/\` to upstream state" >> "$GITHUB_STEP_SUMMARY"
187-
echo "- Updated PR branch \`doc/new-guid-uuid-v7\`" >> "$GITHUB_STEP_SUMMARY"
188-
echo "- Posted comment on PR #12888" >> "$GITHUB_STEP_SUMMARY"
160+
echo "- Moved New-Guid.md from reference/7.6/ to reference/7.7/" >> "$GITHUB_STEP_SUMMARY"
161+
echo "- Reverted reference/7.6/ to upstream state" >> "$GITHUB_STEP_SUMMARY"
162+
echo "- Force-pushed branch doc/new-guid-uuid-v7" >> "$GITHUB_STEP_SUMMARY"
163+
echo "" >> "$GITHUB_STEP_SUMMARY"
164+
echo "Action needed: comment on PR 12888 to mark ready for review." >> "$GITHUB_STEP_SUMMARY"

0 commit comments

Comments
 (0)