未验证 提交 1e5bb959 编写于 作者: M Mislav Marohnić 提交者: GitHub

Merge pull request #1677 from venyii/milestone-name

Add milestone to PR by name instead of number
......@@ -63,8 +63,9 @@ pull-request -i <ISSUE>
-a, --assign <USERS>
A comma-separated list of GitHub handles to assign to this pull request.
-M, --milestone <ID>
Add this pull request to a GitHub milestone with id <ID>.
-M, --milestone <NAME>
The milestone name to add to this pull request. Passing the milestone number
is deprecated.
-l, --labels <LABELS>
Add a comma-separated list of labels to this pull request.
......@@ -85,6 +86,7 @@ var (
flagPullRequestHead,
flagPullRequestIssue,
flagPullRequestMessage,
flagPullRequestMilestone,
flagPullRequestFile string
flagPullRequestBrowse,
......@@ -93,8 +95,6 @@ var (
flagPullRequestPush,
flagPullRequestForce bool
flagPullRequestMilestone uint64
flagPullRequestAssignees,
flagPullRequestReviewers,
flagPullRequestLabels listFlag
......@@ -113,7 +113,7 @@ func init() {
cmdPullRequest.Flag.StringVarP(&flagPullRequestFile, "file", "F", "", "FILE")
cmdPullRequest.Flag.VarP(&flagPullRequestAssignees, "assign", "a", "USERS")
cmdPullRequest.Flag.VarP(&flagPullRequestReviewers, "reviewer", "r", "USERS")
cmdPullRequest.Flag.Uint64VarP(&flagPullRequestMilestone, "milestone", "M", 0, "MILESTONE")
cmdPullRequest.Flag.StringVarP(&flagPullRequestMilestone, "milestone", "M", "", "MILESTONE")
cmdPullRequest.Flag.VarP(&flagPullRequestLabels, "labels", "l", "LABELS")
CmdRunner.Use(cmdPullRequest)
......@@ -288,6 +288,18 @@ of text is the title and the rest is the description.`, fullBase, fullHead))
}
}
milestoneNumber := 0
if flagPullRequestMilestone != "" {
// BC: Don't try to resolve milestone name if it's an integer
milestoneNumber, err = strconv.Atoi(flagPullRequestMilestone)
if err != nil {
milestones, err := client.FetchMilestones(baseProject)
utils.Check(err)
milestoneNumber, err = findMilestoneNumber(milestones, flagPullRequestMilestone)
utils.Check(err)
}
}
var pullRequestURL string
if args.Noop {
args.Before(fmt.Sprintf("Would request a pull request to %s from %s", fullBase, fullHead), "")
......@@ -357,8 +369,8 @@ of text is the title and the rest is the description.`, fullBase, fullHead))
if len(flagPullRequestAssignees) > 0 {
params["assignees"] = flagPullRequestAssignees
}
if flagPullRequestMilestone > 0 {
params["milestone"] = flagPullRequestMilestone
if milestoneNumber > 0 {
params["milestone"] = milestoneNumber
}
if len(params) > 0 {
......@@ -423,3 +435,13 @@ func parsePullRequestIssueNumber(url string) string {
return ""
}
func findMilestoneNumber(milestones []github.Milestone, name string) (int, error) {
for _, milestone := range milestones {
if strings.EqualFold(milestone.Title, name) {
return milestone.Number, nil
}
}
return 0, fmt.Errorf("error: no milestone found with name '%s'", name)
}
......@@ -38,7 +38,7 @@ complete -f -c hub -n ' __fish_hub_using_command pull-request' -s p -d "Push the
complete -f -c hub -n ' __fish_hub_using_command pull-request' -s b -d 'The base branch in "[OWNER:]BRANCH" format'
complete -f -c hub -n ' __fish_hub_using_command pull-request' -s h -d 'The head branch in "[OWNER:]BRANCH" format'
complete -f -c hub -n ' __fish_hub_using_command pull-request' -s a -d 'A comma-separated list of GitHub handles to assign to this pull request'
complete -f -c hub -n ' __fish_hub_using_command pull-request' -s M -d "Add this pull request to a GitHub milestone with id <ID>"
complete -f -c hub -n ' __fish_hub_using_command pull-request' -s M -d "The milestone name to add to this pull request. Passing the milestone number is deprecated."
complete -f -c hub -n ' __fish_hub_using_command pull-request' -s l -d "Add a comma-separated list of labels to this pull request"
# fork
complete -f -c hub -n ' __fish_hub_using_command fork' -l no-remote -d "Skip adding a git remote for the fork"
......
......@@ -757,19 +757,86 @@ BODY
Given I am on the "feature" branch with upstream "origin/feature"
Given the GitHub API server:
"""
get('/repos/mislav/coral/milestones') {
status 200
json [
{ :number => 237, :title => "prerelease" },
{ :number => 1337, :title => "v1" },
{ :number => 41319, :title => "Hello World!" }
]
}
post('/repos/mislav/coral/pulls') {
assert :head => "mislav:feature"
status 201
json :html_url => "the://url", :number => 1234
}
patch('/repos/mislav/coral/issues/1234') {
assert :milestone => 41319
json :html_url => "the://url"
}
"""
When I successfully run `hub pull-request -m hereyougo -M "Hello World!"`
Then the output should contain exactly "the://url\n"
Scenario: Pull request with case-insensitive milestone
Given I am on the "feature" branch with upstream "origin/feature"
Given the GitHub API server:
"""
get('/repos/mislav/coral/milestones') {
status 200
json [
{ :number => 237, :title => "prerelease" },
{ :number => 1337, :title => "v1" },
{ :number => 41319, :title => "Hello World!" }
]
}
post('/repos/mislav/coral/pulls') {
assert :head => "mislav:feature"
status 201
json :html_url => "the://url", :number => 1234
}
patch('/repos/mislav/coral/issues/1234') {
assert :milestone => 41319
json :html_url => "the://url"
}
"""
When I successfully run `hub pull-request -m hereyougo -M "hello world!"`
Then the output should contain exactly "the://url\n"
Scenario: Pull request uses integer milestone number for BC
Given I am on the "feature" branch with upstream "origin/feature"
Given the GitHub API server:
"""
get('/repos/mislav/coral/milestones') {
status 200
json [{ :number => 237, :title => "prerelease" }]
}
post('/repos/mislav/coral/pulls') {
assert :head => "mislav:feature"
status 201
json :html_url => "the://url", :number => 1234
}
patch('/repos/mislav/coral/issues/1234') {
assert :milestone => 1234
assert :milestone => 55
json :html_url => "the://url"
}
"""
When I successfully run `hub pull-request -m hereyougo -M 1234`
When I successfully run `hub pull-request -m hereyougo -M 55`
Then the output should contain exactly "the://url\n"
Scenario: Pull request fails with unknown milestone before it's created
Given I am on the "feature" branch with upstream "origin/feature"
Given the GitHub API server:
"""
get('/repos/mislav/coral/milestones') {
status 200
json []
}
"""
When I run `hub pull-request -m hereyougo -M "unknown"`
Then the exit status should be 1
And the stderr should contain exactly "error: no milestone found with name 'unknown'\n"
Scenario: Pull request with labels
Given I am on the "feature" branch with upstream "origin/feature"
Given the GitHub API server:
......
......@@ -635,6 +635,34 @@ func (client *Client) FetchLabels(project *Project) (labels []IssueLabel, err er
return
}
func (client *Client) FetchMilestones(project *Project) (milestones []Milestone, err error) {
api, err := client.simpleApi()
if err != nil {
return
}
path := fmt.Sprintf("repos/%s/%s/milestones?per_page=100", project.Owner, project.Name)
milestones = []Milestone{}
var res *simpleResponse
for path != "" {
res, err = api.Get(path)
if err = checkStatus(200, "fetching milestones", res, err); err != nil {
return
}
path = res.Link("next")
milestonesPage := []Milestone{}
if err = res.Unmarshal(&milestonesPage); err != nil {
return
}
milestones = append(milestones, milestonesPage...)
}
return
}
func (client *Client) CurrentUser() (user *User, err error) {
api, err := client.simpleApi()
if err != nil {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册