Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save eriktorsner/376e1630693546e51959c9b27fd7b53b to your computer and use it in GitHub Desktop.
Save eriktorsner/376e1630693546e51959c9b27fd7b53b 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"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment