Script to move issues to new milestone (proof-of-concept)

Hey @bep and all:

I am writing regarding the recent GoHugoIO tweet about how cumbersome it is to move 200+ issues to a new milestone on GitHub — https://twitter.com/GoHugoIO/status/1421181681717944323
(I am Twitter-shy, so I am replying here on Discourse instead.)

I presume that the complaint is due to GitHub web UI’s limitation of allows at most 25 issues to be selected at once, so with 299 issues, the whole select-all-move-milestone operation needs to be tediously repeated 12 times? I feel your pain.

I was a bit surprised by the timing, as our team at work has been thinking about a similar problem, not with so many issues in one single repo, but rather, same milestones (Sprint #) across multiple repos. So, apparently, issues and milestones management can be done with gh the GitHub CLI with relative ease. For example, to create the next milestone:

gh api -X POST repos/{owner}/{repo}/milestones -f title="v0.89"

and to move v0.88 milestone issues to v0.89, here is a proof-of-concept script that worked for me on Linux (not yet tested on macOS):

#!/bin/bash

if [[ -z $1 ]] || [[ -z $2 ]]; then
  echo "Usage: $(basename "$0") OLD_MILESTONE NEW_MILESTONE"
  exit 1
fi

OLD_MILESTONE_TITLE="$1"
NEW_MILESTONE_TITLE="$2"

OLD_MILESTONE_NUMBER=$(gh api -X GET "repos/{owner}/{repo}/milestones" --jq ".[] | select(.title == \"$OLD_MILESTONE_TITLE\").number")
NEW_MILESTONE_NUMBER=$(gh api -X GET "repos/{owner}/{repo}/milestones" --jq ".[] | select(.title == \"$NEW_MILESTONE_TITLE\").number")

# Print open issues in the previous milestone 
gh api -X GET "repos/{owner}/{repo}/issues" \
  -f milestone="$OLD_MILESTONE_NUMBER" \
  --jq '.[] | [.number, .title, .assignee.login]' \
  --paginate

# Move issues to the new milestone
for i in $(gh api -X GET "repos/{owner}/{repo}/issues" -f milestone="$OLD_MILESTONE_NUMBER" -f direction=asc --jq '.[].number' --paginate); do
  echo "Moving Issue $i from \"$OLD_MILESTONE_TITLE\" to \"$NEW_MILESTONE_TITLE\"..."
  gh api -X GET "repos/{owner}/{repo}/issues/$i" > "Issue-${i}-${OLD_MILESTONE_TITLE// /-}.json"
  gh api -X PATCH "repos/{owner}/{repo}/issues/$i" -f milestone="$NEW_MILESTONE_NUMBER" > "Issue-${i}-${NEW_MILESTONE_TITLE// /-}.json"
done

[Edited to add --paginate]

So, hopefully, this script (or future improved version of it) would be of help?
Let me know. Cheers!

P.S. I’ll be slowly working on this script over at Automatically create a new milestone and move issues to it · Issue #126 · OpenDRR/opendrr-platform · GitHub

1 Like