Add a `--org` flag to `hub fork`

This commit is contained in:
Juan Ignacio Donoso 2017-02-15 00:07:53 -05:00
Родитель 3d3facba2c
Коммит cdd7a65e05
3 изменённых файлов: 39 добавлений и 6 удалений

Просмотреть файл

@ -10,28 +10,40 @@ import (
var cmdFork = &Command{
Run: fork,
Usage: "fork [--no-remote]",
Usage: "fork [--no-remote] [--org=<ORGANIZATION>]",
Long: `Fork the current project on GitHub and add a git remote for it.
## Options:
--no-remote
Skip adding a git remote for the fork.
--org=<ORGANIZATION>
Fork the repository within this organization.
## Examples:
$ hub fork
[ repo forked on GitHub ]
> git remote add -f USER git@github.com:USER/REPO.git
$ hub fork --org=ORGANIZATION
[ repo forked on Github into the ORGANIZATION organization]
> git remote add -f ORGANIZATION git@github.com:ORGANIZATION/REPO.git
## See also:
hub-clone(1), hub(1)
`,
}
var flagForkNoRemote bool
var (
flagForkNoRemote bool
flagForkOrganization string
)
func init() {
cmdFork.Flag.BoolVar(&flagForkNoRemote, "no-remote", false, "")
cmdFork.Flag.StringVarP(&flagForkOrganization, "org", "", "", "ORGANIZATION")
CmdRunner.Use(cmdFork)
}
@ -56,7 +68,14 @@ func fork(cmd *Command, args *Args) {
utils.Check(fmt.Errorf("Error creating fork: %s", err))
}
forkProject := github.NewProject(host.User, project.Name, project.Host)
params := map[string]interface{}{}
forkOwner := host.User
if flagForkOrganization != "" {
forkOwner = flagForkOrganization
params["organization"] = forkOwner
}
forkProject := github.NewProject(forkOwner, project.Name, project.Host)
newRemoteName := forkProject.Owner
client := github.NewClient(project.Host)
@ -73,7 +92,7 @@ func fork(cmd *Command, args *Args) {
}
} else {
if !args.Noop {
newRepo, err := client.ForkRepository(project)
newRepo, err := client.ForkRepository(project, params)
utils.Check(err)
forkProject.Owner = newRepo.Owner.Login
forkProject.Name = newRepo.Name

Просмотреть файл

@ -13,6 +13,7 @@ Feature: hub fork
}
get('/repos/mislav/dotfiles', :host_name => 'api.github.com') { 404 }
post('/repos/evilchelu/dotfiles/forks', :host_name => 'api.github.com') {
assert :organization => nil
status 202
json :name => 'dotfiles', :owner => { :login => 'mislav' }
}
@ -202,3 +203,17 @@ Scenario: Related fork already exists
And "git.my.org" is a whitelisted Enterprise host
When I successfully run `hub fork`
Then the url for "mislav" should be "git@git.my.org:mislav/dotfiles.git"
Scenario: Fork a repo to a specific organization
Given the GitHub API server:
"""
get('/repos/acme/dotfiles') { 404 }
post('/repos/evilchelu/dotfiles/forks') {
assert :organization => "acme"
status 202
json :name => 'dotfiles', :owner => { :login => 'acme' }
}
"""
When I successfully run `hub fork --org=acme`
Then the output should contain exactly "new remote: acme\n"
Then the url for "acme" should be "git@github.com:acme/dotfiles.git"

Просмотреть файл

@ -419,13 +419,12 @@ type RepositoryPermissions struct {
Pull bool `json:"pull"`
}
func (client *Client) ForkRepository(project *Project) (repo *Repository, err error) {
func (client *Client) ForkRepository(project *Project, params map[string]interface{}) (repo *Repository, err error) {
api, err := client.simpleApi()
if err != nil {
return
}
params := map[string]interface{}{}
res, err := api.PostJSON(fmt.Sprintf("repos/%s/%s/forks", project.Owner, project.Name), params)
if err = checkStatus(202, "creating fork", res, err); err != nil {
return