Skip to content

Instantly share code, notes, and snippets.

@NewAlexandria
Last active December 15, 2024 23:49
Show Gist options
  • Save NewAlexandria/f9d53a9858279e1d855d80d5f7dc9027 to your computer and use it in GitHub Desktop.
Save NewAlexandria/f9d53a9858279e1d855d80d5f7dc9027 to your computer and use it in GitHub Desktop.
always_preserve_branches = [
'main',
'staging',
'development',
'ci/data-mover',
'ci/data-mover-test',
]
a,b,c = Open3.capture3("git branch -r | grep -v '\\->' | cut -d'/' -f2-")
branches = a.split("\n"); branches.count
lim = branches.size # x=10000
branches_states = branches[0..lim].map do |branch|
a,b,c = Open3.capture3("gh pr list --head \"#{branch}\" --state all ") ;
a
end
#branches_states.size
# preserve PRs that are Open, Draft, and keep branch name with them.
open_and_draft = branches_states.map
.with_index do |l,idx|
l.split("\n").select do |l|
l.match(/OPEN/) || l.match(/DRAFT/)
end << branches[idx]
end
.select{|ll| ll.size > 1 }
pr_and_branch = open_and_draft.map do |ll|
[
ll[0].split("\t")[0], # get PR number
ll[1] # repserved branch name
]
end
apbx = (
[nil] * always_preserve_branches.size
).zip(always_preserve_branches)
open_branches_sorted = (
pr_and_branch + apbx
).sort { |a,b| b[0].to_i <=> a[0].to_i }
# code to iteratively remote-delete all branches that are not in the list of open_branches_sorted
puts open_branches_sorted
@NewAlexandria
Copy link
Author

with the remote-delete operation, this would delete all remote branches that were not open as PRs, or in the safe list.

In running this, the operator needs to be aware of CI and other integrated services, which may create-and-remove branches programmatically in the repo, as these operations could then fail.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment