From 47a9aa9ec67b688774b4637d9e10bb8b7ddf8fd3 Mon Sep 17 00:00:00 2001 From: Jingwen Owen Ou Date: Mon, 7 Apr 2014 21:44:08 -0700 Subject: [PATCH] Check repository permission for clone URL --- Godeps/Godeps.json | 4 ++-- .../octokit/go-octokit/octokit/repositories.go | 10 +++++++++- .../go-octokit/octokit/repositories_test.go | 6 +++++- commands/clone.go | 10 +++++++--- commands/clone_test.go | 7 ++++--- github/util.go | 15 +++++++++++++++ 6 files changed, 42 insertions(+), 10 deletions(-) diff --git a/Godeps/Godeps.json b/Godeps/Godeps.json index 7965625a..0ff49dc6 100644 --- a/Godeps/Godeps.json +++ b/Godeps/Godeps.json @@ -60,8 +60,8 @@ }, { "ImportPath": "github.com/octokit/go-octokit/octokit", - "Comment": "v0.4.0-71-g4fee5e3", - "Rev": "4fee5e3019ca20d12c93c1b7f18f20c4a6afff24" + "Comment": "v0.4.0-72-ga381371", + "Rev": "a38137165e45c66d9b89e2b2b49ad01da497185f" }, { "ImportPath": "github.com/ogier/pflag", diff --git a/Godeps/_workspace/src/github.com/octokit/go-octokit/octokit/repositories.go b/Godeps/_workspace/src/github.com/octokit/go-octokit/octokit/repositories.go index 27f56cbe..69ae8805 100644 --- a/Godeps/_workspace/src/github.com/octokit/go-octokit/octokit/repositories.go +++ b/Godeps/_workspace/src/github.com/octokit/go-octokit/octokit/repositories.go @@ -1,9 +1,10 @@ package octokit import ( - "github.com/lostisland/go-sawyer/hypermedia" "net/url" "time" + + "github.com/lostisland/go-sawyer/hypermedia" ) var ( @@ -68,6 +69,7 @@ type Repository struct { PushedAt time.Time `json:"pushed_at,omitempty"` CreatedAt time.Time `json:"created_at,omitempty"` UpdatedAt time.Time `json:"updated_at,omitempty"` + Permissions Permissions `json:"permissions,omitempty"` Organization *Organization `json:"organization,omitempty"` Parent *Repository `json:"parent,omitempty"` Source *Repository `json:"source,omitempty"` @@ -75,3 +77,9 @@ type Repository struct { HasWiki bool `json:"has_wiki,omitempty"` HasDownloads bool `json:"has_downloads,omitempty"` } + +type Permissions struct { + Admin bool + Push bool + Pull bool +} diff --git a/Godeps/_workspace/src/github.com/octokit/go-octokit/octokit/repositories_test.go b/Godeps/_workspace/src/github.com/octokit/go-octokit/octokit/repositories_test.go index a2a1a089..14880cd2 100644 --- a/Godeps/_workspace/src/github.com/octokit/go-octokit/octokit/repositories_test.go +++ b/Godeps/_workspace/src/github.com/octokit/go-octokit/octokit/repositories_test.go @@ -3,9 +3,10 @@ package octokit import ( "encoding/json" "fmt" - "github.com/bmizerany/assert" "net/http" "testing" + + "github.com/bmizerany/assert" ) func TestRepositoresService_One(t *testing.T) { @@ -34,6 +35,9 @@ func TestRepositoresService_One(t *testing.T) { assert.Equal(t, "git://github.com/jingweno/octokat.git", repo.GitURL) assert.Equal(t, "git@github.com:jingweno/octokat.git", repo.SSHURL) assert.Equal(t, "master", repo.MasterBranch) + assert.T(t, !repo.Permissions.Admin) + assert.T(t, !repo.Permissions.Push) + assert.T(t, repo.Permissions.Pull) } func TestRepositoresService_All(t *testing.T) { diff --git a/commands/clone.go b/commands/clone.go index 702029a9..0d0bda54 100644 --- a/commands/clone.go +++ b/commands/clone.go @@ -75,10 +75,14 @@ func transformCloneArgs(args *Args) { } project := github.NewProject(owner, name, hostStr) - isSSH = isSSH || + if !isSSH && args.Command != "submodule" && - host != nil && - project.Owner == host.User + !args.Noop && + !github.IsHttpsProtocol() { + client := github.NewClient(project.Host) + repo, err := client.Repository(project) + isSSH = (err == nil) && (repo.Private || repo.Permissions.Push) + } url := project.GitURL(name, owner, isSSH) args.ReplaceParam(i, url) diff --git a/commands/clone_test.go b/commands/clone_test.go index a879cce3..06b92fa3 100644 --- a/commands/clone_test.go +++ b/commands/clone_test.go @@ -1,10 +1,11 @@ package commands import ( - "github.com/bmizerany/assert" - "github.com/github/hub/github" "os" "testing" + + "github.com/bmizerany/assert" + "github.com/github/hub/github" ) func TestTransformCloneArgs(t *testing.T) { @@ -39,7 +40,7 @@ func TestTransformCloneArgs(t *testing.T) { transformCloneArgs(args) assert.Equal(t, 1, args.ParamsSize()) - assert.Equal(t, "git@github.com:jingweno/jekyll_and_hyde.git", args.FirstParam()) + assert.Equal(t, "git://github.com/jingweno/jekyll_and_hyde.git", args.FirstParam()) args = NewArgs([]string{"clone", "-p", "jekyll_and_hyde"}) transformCloneArgs(args) diff --git a/github/util.go b/github/util.go index 486352c2..4ea3db4b 100644 --- a/github/util.go +++ b/github/util.go @@ -4,8 +4,23 @@ package github import ( "code.google.com/p/go.crypto/ssh/terminal" + "github.com/github/hub/git" ) +func IsHttpsProtocol() bool { + httpProcotol, _ := git.Config("hub.protocol") + if httpProcotol == "https" { + return true + } + + httpClone, _ := git.Config("--bool hub.http-clone") + if httpClone == "true" { + return true + } + + return false +} + func isTerminal(fd uintptr) bool { return terminal.IsTerminal(int(fd)) }