Skip to content

Commit 30e798d

Browse files
feat(gh-cli): publish dependency draft releases (#173)
* fix(gh-cli): print pull request URLs Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: bed3e0ff-d6c7-426a-8591-4c4b8e74705c * feat(gh-cli): publish dependency draft releases Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: 14d24b4b-af6d-42d0-8743-bbda5d4767a8 * feat(gh-cli): delete intermediate draft releases Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: 14d24b4b-af6d-42d0-8743-bbda5d4767a8 * fix(gh-cli): allow tagged draft release deletion Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: 14d24b4b-af6d-42d0-8743-bbda5d4767a8 * feat(gh-cli): limit draft release deletion age Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: 14d24b4b-af6d-42d0-8743-bbda5d4767a8 * fix(gh-cli): separate draft cleanup output Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: 14d24b4b-af6d-42d0-8743-bbda5d4767a8 * fix(gh-cli): delete intermediate release tags Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: 14d24b4b-af6d-42d0-8743-bbda5d4767a8 * fix(gh-cli): align release summary counts Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: 14d24b4b-af6d-42d0-8743-bbda5d4767a8 * fix(gh-cli): harden release automation failures Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: 14d24b4b-af6d-42d0-8743-bbda5d4767a8 * fix(gh-cli): order draft release documentation Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: 14d24b4b-af6d-42d0-8743-bbda5d4767a8 --------- Copilot-Session: bed3e0ff-d6c7-426a-8591-4c4b8e74705c Copilot-Session: 14d24b4b-af6d-42d0-8743-bbda5d4767a8
1 parent 5bea905 commit 30e798d

5 files changed

Lines changed: 501 additions & 3 deletions

File tree

‎.gitignore‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ test*.sh
1010
temp
1111
tool
1212
.env
13+
.release-manifests/

‎gh-cli/README.md‎

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,18 @@ test11-team/test11111-team/textxxx-team
500500
501501
```
502502

503+
### delete-draft-releases.sh
504+
505+
Deletes intermediate draft releases and their exact tags using the latest merge manifest. It deletes only a unique draft created after the recorded merge whose target contains that merge commit, then deletes the tag named by that verified draft.
506+
507+
Requires `gh` authentication with Contents write permission and `jq`.
508+
509+
```bash
510+
./delete-draft-releases.sh --max-age-minutes 15 --no-prompt
511+
```
512+
513+
Run this after an intermediate dependency merge has created its draft, then run the merge script for the next dependency. `--max-age-minutes` skips older drafts as an additional safety guard. Do not run it after the final merge.
514+
503515
### delete-packages-in-organization.sh
504516

505517
Deletes all packages in an organization for a given package type.
@@ -1564,7 +1576,7 @@ Creates a (mostly) empty migration for a given organization repository so that i
15641576

15651577
### merge-pull-requests-by-title.sh
15661578

1567-
Finds and merges pull requests matching a title pattern across multiple repositories. Supports batch merging Dependabot PRs, bumping npm patch versions, and enabling auto-merge. Repositories can be specified via a file list or dynamically via `--owner` with optional `--topic` filtering.
1579+
Finds and merges pull requests matching a title pattern across multiple repositories. Supports batch merging Dependabot PRs, bumping npm patch versions, and enabling auto-merge. Successful immediate merges are automatically recorded in the ignored `.release-manifests/latest.json` file for safely publishing generated drafts. Repositories can be specified via a file list or dynamically via `--owner` with optional `--topic` filtering.
15681580

15691581
```bash
15701582
# Merge PRs matching a wildcard title pattern
@@ -1578,6 +1590,7 @@ Finds and merges pull requests matching a title pattern across multiple reposito
15781590

15791591
# Search by owner and topic instead of file list
15801592
./merge-pull-requests-by-title.sh --owner joshjohanning --topic node-action "chore(deps)*" --dry-run
1593+
15811594
```
15821595

15831596
Input file format (`repos.txt`):
@@ -1588,6 +1601,8 @@ https://github.com/joshjohanning/repo2
15881601
https://github.com/joshjohanning/repo3
15891602
```
15901603

1604+
Automatic manifest output requires `jq` and applies only to immediate merges, not dry runs, version bumps, or auto-merge.
1605+
15911606
### merge-pull-requests-from-list.sh
15921607

15931608
Merges a list of pull requests from a file containing PR URLs with customizable commit messages. Useful for batch merging similar PRs across multiple repositories (e.g., Dependabot updates). Supports dry-run mode to preview merges.
@@ -1633,6 +1648,18 @@ The script has three parameters:
16331648
- `target-org` - The target organization name to which teams will be updated OR created
16341649
- `create parent(s) if not exist` - OPTIONAL (default `false`) if set to true, the teams which have parents that do not exist in the target org, they will be created. (also creates parents of parents) otherwise it will print a message parent doesn't exist and it will skipped.
16351650

1651+
### publish-draft-releases.sh
1652+
1653+
Publishes only draft releases associated with PRs in a manifest written by `merge-pull-requests-by-title.sh`. A draft must be newer than its PR merge, its target must contain the merge commit, and it must be the only matching draft for that repository.
1654+
1655+
Requires `gh` authentication with Contents write permission and `jq`.
1656+
1657+
```bash
1658+
./publish-draft-releases.sh
1659+
```
1660+
1661+
The script defaults to `.release-manifests/latest.json`; pass a different manifest path only when publishing from a saved prior run.
1662+
16361663
### remove-branch-protection-status-check-contexts.sh
16371664

16381665
Removes specific branch protection status check(s) from a branch protection rule

‎gh-cli/delete-draft-releases.sh‎

Lines changed: 229 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,229 @@
1+
#!/bin/bash
2+
3+
# Deletes intermediate draft releases created for PRs recorded by merge-pull-requests-by-title.sh
4+
#
5+
# Usage:
6+
# ./delete-draft-releases.sh [manifest_file] [--max-age-minutes <minutes>] [--no-prompt]
7+
#
8+
# Examples:
9+
# ./delete-draft-releases.sh
10+
# ./delete-draft-releases.sh --max-age-minutes 15 --no-prompt
11+
# ./delete-draft-releases.sh .release-manifests/previous.json --max-age-minutes 15
12+
#
13+
# Requirements:
14+
# - gh authenticated with Contents: write permission for each repository
15+
# - jq installed
16+
#
17+
# Safety:
18+
# - Only draft releases created after the recorded PR merge are considered
19+
# - The draft target must contain the recorded merge commit
20+
# - Only the exact tag named by the verified draft is deleted
21+
# - Exactly one draft must match each manifest entry
22+
23+
print_help() {
24+
echo "Delete intermediate draft releases associated with a merge manifest"
25+
echo ""
26+
echo "Usage: $0 [manifest_file] [--max-age-minutes <minutes>] [--no-prompt]"
27+
echo ""
28+
echo "Defaults to .release-manifests/latest.json"
29+
}
30+
31+
no_prompt=false
32+
max_age_minutes=""
33+
manifest_file=".release-manifests/latest.json"
34+
manifest_provided=false
35+
36+
args=("$@")
37+
i=0
38+
while [ $i -lt ${#args[@]} ]; do
39+
arg="${args[$i]}"
40+
case "$arg" in
41+
-h|--help)
42+
print_help
43+
exit 0
44+
;;
45+
--no-prompt)
46+
no_prompt=true
47+
;;
48+
--max-age-minutes)
49+
((i++))
50+
max_age_minutes="${args[$i]}"
51+
if ! [[ "$max_age_minutes" =~ ^[1-9][0-9]*$ ]]; then
52+
echo "Error: --max-age-minutes requires a positive integer"
53+
exit 1
54+
fi
55+
;;
56+
--*)
57+
echo "Error: Unknown flag '$arg'"
58+
exit 1
59+
;;
60+
*)
61+
if [ "$manifest_provided" = true ]; then
62+
echo "Error: Only one manifest file may be provided"
63+
exit 1
64+
fi
65+
manifest_file="$arg"
66+
manifest_provided=true
67+
;;
68+
esac
69+
((i++))
70+
done
71+
72+
if [ ! -f "$manifest_file" ]; then
73+
echo "Error: Manifest file does not exist: $manifest_file"
74+
exit 1
75+
fi
76+
77+
if ! command -v gh > /dev/null 2>&1; then
78+
echo "Error: gh is required but not installed"
79+
exit 1
80+
fi
81+
82+
if ! command -v jq > /dev/null 2>&1; then
83+
echo "Error: jq is required but not installed"
84+
exit 1
85+
fi
86+
87+
if ! jq -e '.schemaVersion == 1 and (.pullRequests | type == "array")' "$manifest_file" > /dev/null 2>&1; then
88+
echo "Error: Invalid or unsupported release manifest: $manifest_file"
89+
exit 1
90+
fi
91+
92+
if [ "$no_prompt" = false ] && { ! [[ -t 1 ]] || ! [[ -r /dev/tty ]]; }; then
93+
echo "Error: No TTY available for interactive prompt - use --no-prompt"
94+
exit 1
95+
fi
96+
97+
deleted_count=0
98+
skipped_count=0
99+
failed_count=0
100+
101+
while IFS=$'\t' read -r repo pr_url merged_at merge_sha; do
102+
if [ "$deleted_count" -gt 0 ] || [ "$skipped_count" -gt 0 ] || [ "$failed_count" -gt 0 ]; then
103+
echo ""
104+
fi
105+
echo "Checking $pr_url"
106+
107+
default_branch=$(gh api "/repos/$repo" --jq '.default_branch' 2>/dev/null)
108+
if [ -z "$default_branch" ]; then
109+
echo " ❌ Could not read repository metadata"
110+
((failed_count++))
111+
continue
112+
fi
113+
114+
matching_drafts=""
115+
release_query_error=$(mktemp)
116+
releases=$(gh api --paginate "/repos/$repo/releases?per_page=100" \
117+
--jq ".[] | select(.draft == true and .created_at >= \"$merged_at\") | [.id, .tag_name, (.target_commitish // \"\"), .created_at, .html_url] | @tsv" 2>"$release_query_error")
118+
release_query_status=$?
119+
if [ $release_query_status -ne 0 ]; then
120+
echo " ❌ Failed to list releases: $(cat "$release_query_error")"
121+
rm -f "$release_query_error"
122+
((failed_count++))
123+
continue
124+
fi
125+
rm -f "$release_query_error"
126+
127+
while IFS=$'\t' read -r release_id tag_name target created_at release_url; do
128+
[ -z "$release_id" ] && continue
129+
target="${target:-$default_branch}"
130+
target_sha=$(gh api "/repos/$repo/commits/$target" --jq '.sha' 2>/dev/null)
131+
if [ -z "$target_sha" ]; then
132+
continue
133+
fi
134+
135+
comparison=$(gh api "/repos/$repo/compare/$merge_sha...$target_sha" --jq '.status' 2>/dev/null)
136+
if [ "$comparison" = "identical" ] || [ "$comparison" = "ahead" ]; then
137+
if [ -n "$matching_drafts" ]; then
138+
matching_drafts+=$'\n'
139+
fi
140+
matching_drafts+="$release_id"$'\t'"$tag_name"$'\t'"$created_at"$'\t'"$release_url"
141+
fi
142+
done <<< "$releases"
143+
144+
match_count=$(printf '%s\n' "$matching_drafts" | awk 'NF { count++ } END { print count+0 }')
145+
if [ "$match_count" -eq 0 ]; then
146+
echo " ⏭️ No matching draft release found"
147+
((skipped_count++))
148+
continue
149+
fi
150+
if [ "$match_count" -gt 1 ]; then
151+
echo " ❌ Found $match_count matching drafts; refusing to guess"
152+
printf '%s\n' "$matching_drafts" | while IFS=$'\t' read -r _ tag _ url; do
153+
echo " $tag - $url"
154+
done
155+
((failed_count++))
156+
continue
157+
fi
158+
159+
IFS=$'\t' read -r release_id tag_name created_at release_url <<< "$matching_drafts"
160+
if [ -n "$max_age_minutes" ]; then
161+
created_epoch=$(date -u -j -f "%Y-%m-%dT%H:%M:%SZ" "$created_at" "+%s" 2>/dev/null)
162+
if [ -z "$created_epoch" ]; then
163+
created_epoch=$(date -u -d "$created_at" "+%s" 2>/dev/null)
164+
fi
165+
if [ -z "$created_epoch" ]; then
166+
echo " ❌ Could not parse draft creation time: $created_at"
167+
((failed_count++))
168+
continue
169+
fi
170+
171+
current_epoch=$(date -u "+%s")
172+
age_seconds=$((current_epoch - created_epoch))
173+
max_age_seconds=$((max_age_minutes * 60))
174+
if [ "$age_seconds" -gt "$max_age_seconds" ]; then
175+
echo " ⏭️ Draft is older than $max_age_minutes minutes; skipping $release_url"
176+
((skipped_count++))
177+
continue
178+
fi
179+
fi
180+
181+
current_release=$(gh api "/repos/$repo/releases/$release_id" --jq '[.draft, .tag_name] | @tsv' 2>/dev/null)
182+
if [ "$current_release" != $'true\t'"$tag_name" ]; then
183+
echo " ❌ Release changed during verification; refusing to delete $release_url"
184+
((failed_count++))
185+
continue
186+
fi
187+
188+
encoded_tag=$(jq -rn --arg value "$tag_name" '$value | @uri')
189+
tag_ref=$(gh api "/repos/$repo/git/ref/tags/$encoded_tag" --jq '.ref' 2>/dev/null)
190+
if [ "$tag_ref" != "refs/tags/$tag_name" ]; then
191+
echo " ❌ Exact tag $tag_name was not found; refusing partial cleanup"
192+
((failed_count++))
193+
continue
194+
fi
195+
196+
echo " 📦 Matching draft: $tag_name ($created_at) - $release_url"
197+
if [ "$no_prompt" = false ]; then
198+
read -r -p " ❓ Delete this intermediate draft release and tag $tag_name? [y/N] " confirm < /dev/tty
199+
if [[ ! "$confirm" =~ ^[Yy]$ ]]; then
200+
echo " ⏭️ Skipped $release_url"
201+
((skipped_count++))
202+
continue
203+
fi
204+
fi
205+
206+
if gh api --method DELETE "/repos/$repo/releases/$release_id" > /dev/null; then
207+
if gh api --method DELETE "/repos/$repo/git/refs/tags/$encoded_tag" > /dev/null; then
208+
echo " ✅ Deleted intermediate draft $release_url and tag $tag_name"
209+
((deleted_count++))
210+
else
211+
echo " ❌ Deleted the draft but failed to delete tag $tag_name"
212+
((failed_count++))
213+
fi
214+
else
215+
echo " ❌ Failed to delete $release_url"
216+
((failed_count++))
217+
fi
218+
done < <(jq -r '.pullRequests[] | [.repository, .pullRequestUrl, .mergedAt, .mergeCommitSha] | @tsv' "$manifest_file")
219+
220+
echo "========================================"
221+
echo "Summary:"
222+
printf " ✅ %-10s %d\n" "Deleted:" "$deleted_count"
223+
printf " ❌ %-10s %d\n" "Failed:" "$failed_count"
224+
printf " ⏭️ %-10s %d\n" "Skipped:" "$skipped_count"
225+
echo "========================================"
226+
227+
if [ "$failed_count" -gt 0 ]; then
228+
exit 1
229+
fi

0 commit comments

Comments
 (0)