Skip to content

Instantly share code, notes, and snippets.

@doi-t
Last active August 7, 2024 07:49
Show Gist options
  • Save doi-t/5735f9f0f7f8b7664aa6739bc810a2cc to your computer and use it in GitHub Desktop.
Save doi-t/5735f9f0f7f8b7664aa6739bc810a2cc to your computer and use it in GitHub Desktop.
Manage milestones with gh

Use gh v0.11.0 or later for the "shell" aliases.

Run the following gh alias set commands for each operation. You can confirm the created aliases with gh alias list.

gh listMilestones

gh alias set listMilestones "api graphql -F owner=':owner' -F name=':repo' -f query='
    query ListMilestones(\$name: String\!, \$owner: String\!) {
        repository(owner: \$owner, name: \$name) {
            milestones(first: 100) {
                nodes {
                    title
                    number
                    description
                    dueOn
                    url
                    state
                    closed
                    closedAt
                    updatedAt
                }
            }
        }
    }
'"

gh viewMilestone 123

gh alias set --shell viewMilestone "gh api graphql -F owner=':owner' -F name=':repo' -F number=\$1 -f query='
    query GetMilestones(\$name: String\!, \$owner: String\!, \$number: Int\!) {
        repository(owner: \$owner, name: \$name) {
            milestone(number: \$number) {
                title
                number
                description
                dueOn
                url
                state
                closed
                closedAt
                updatedAt
                issues(first: 100) {
                    nodes {
                        title
                        number
                        url
                        state
                    }
                }
                pullRequests(first: 100) {
                    nodes {
                        title
                        number
                        url
                        state
                    }
                }
            }
        }
    }
'"

gh createMilestone

gh alias set --shell createMilestone "gh api --method POST repos/:owner/:repo/milestones --input - | jq '{ html_url: .html_url, state: .state, created_at: .created_at }'"

Provide inputs from stdin.

echo '{
  "title": "Create a miletsone via gh",
  "state": "open",
  "due_on": "2020-12-31T23:59:59Z",
  "description": "Description foo bar"
}' | gh createMilestone

(I coundn't figure out a way to use gh alias arguments for title, due_on and description...)

gh closeMilestone 123

gh alias set --shell closeMilestone "echo '{\"state\": \"closed\"}' | gh api --method PATCH repos/:owner/:repo/milestones/\$1 --input - | jq '{ html_url: .html_url, state: .state, closed_at: .closed_at }'"

gh reopenMilestone 123

gh alias set --shell reopenMilestone "echo '{\"state\": \"open\"}' | gh api --method PATCH repos/:owner/:repo/milestones/\$1 --input - | jq '{ html_url: .html_url, state: .state, updated_at: .updated_at }'"

gh deleteMilestone 123

gh alias set --shell deleteMilestone "gh api --method DELETE repos/:owner/:repo/milestones/\$1"
@Luxcium
Copy link

Luxcium commented Aug 30, 2020

I am trying to do pseudo gh milestone add 5 6 7 8 9 do you think it should be possible to do that ??

I have 100 PR (for each day of #100DaysOfCode) and I have fifteen milestone's (one for each week of the project) I want to set each of my 100 PR to the according milestone and don't feel like doing it 100 times on the web site...

Originally posted by @Luxcium in cli/cli#1200 (comment)

@potatoqualitee
Copy link

awesome gist, thank you!

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