diff --git a/server/database/schema.go b/server/database/schema.go index 90b2f80f2..f96b013bb 100644 --- a/server/database/schema.go +++ b/server/database/schema.go @@ -41,9 +41,13 @@ var stmts = []string{` repo_id INTEGER PRIMARY KEY AUTOINCREMENT ,user_id INTEGER ,repo_remote VARCHAR(255) + ,repo_host VARCHAR(255) ,repo_owner VARCHAR(255) ,repo_name VARCHAR(255) - ,repo_url VARCHAR(255) + ,repo_url VARCHAR(1024) + ,repo_clone_url VARCHAR(255) + ,repo_git_url VARCHAR(255) + ,repo_ssh_url VARCHAR(255) ,repo_active BOOLEAN ,repo_private BOOLEAN ,repo_privileged BOOLEAN diff --git a/server/database/testdata/testdata.go b/server/database/testdata/testdata.go index 7ccd2cfa4..4c6038602 100644 --- a/server/database/testdata/testdata.go +++ b/server/database/testdata/testdata.go @@ -13,9 +13,9 @@ var stmts = []string{ "insert into users values (4, 4, 'github.com', 'mrwolowitz', '1f6a80bde960e6913bf9b7e61eadd068', '74c40472494ba7f9f6c3ae061ff799ed', 'Mr. Wolowitz', 'wolowitz@caltech.edu', 'ea250570c794d84dc583421bb717be82', '3bd7e7d7411b2978e45919c9ad419984', 1, 1, 1398065343, 1398065344, 1398065345);", // insert repository entries - "insert into repos values (1, 0, 'github.com', 'lhofstadter', 'lenwoloppali', 'git://github.com/lhofstadter/lenwoloppali.git', 1, 1, 1, 1, 1, 'publickey', 'privatekey', 'params', 900, 1398065343, 1398065344);", - "insert into repos values (2, 0, 'github.com', 'browndynamite', 'lenwoloppali', 'git://github.com/browndynamite/lenwoloppali.git', 1, 1, 1, 1, 1, 'publickey', 'privatekey', 'params', 900, 1398065343, 1398065344);", - "insert into repos values (3, 0, 'gitlab.com', 'browndynamite', 'lenwoloppali', 'git://gitlab.com/browndynamite/lenwoloppali.git', 1, 1, 1, 1, 1, 'publickey', 'privatekey', 'params', 900, 1398065343, 1398065344);", + "insert into repos values (1, 0, 'github.com', 'github.com', 'lhofstadter', 'lenwoloppali', '', 'git://github.com/lhofstadter/lenwoloppali.git', '', '', 1, 1, 1, 1, 1, 'publickey', 'privatekey', 'params', 900, 1398065343, 1398065344);", + "insert into repos values (2, 0, 'github.com', 'github.com', 'browndynamite', 'lenwoloppali', '', 'git://github.com/browndynamite/lenwoloppali.git', '', '', 1, 1, 1, 1, 1, 'publickey', 'privatekey', 'params', 900, 1398065343, 1398065344);", + "insert into repos values (3, 0, 'gitlab.com', 'github.com', 'browndynamite', 'lenwoloppali', '', 'git://gitlab.com/browndynamite/lenwoloppali.git', '', '', 1, 1, 1, 1, 1, 'publickey', 'privatekey', 'params', 900, 1398065343, 1398065344);", // insert user + repository permission entries "insert into perms values (1, 101, 200, 1, 1, 1, 1398065343, 1398065344);", diff --git a/server/handler/login.go b/server/handler/login.go index 0507e4d38..4b897638e 100644 --- a/server/handler/login.go +++ b/server/handler/login.go @@ -76,6 +76,10 @@ func (h *LoginHandler) GetLogin(w http.ResponseWriter, r *http.Request) error { // look at the last synchronized date to determine if // we need to re-sync the account. + // + // TODO this should move to a server/sync package and + // should be injected into this struct, just like + // the database code. if u.Stale() { log.Println("sync user account.", u.Login) @@ -101,10 +105,10 @@ func (h *LoginHandler) GetLogin(w http.ResponseWriter, r *http.Request) error { for _, remoteRepo := range repos { repo, _ := repo.New(remote.GetName(), remoteRepo.Owner, remoteRepo.Name) repo.Private = remoteRepo.Private - repo.FullName = remoteRepo.FullName - repo.Clone = remoteRepo.Clone - repo.Git = remoteRepo.Git - repo.SSH = remoteRepo.SSH + // TODO set the repo.Host + repo.CloneURL = remoteRepo.Clone + repo.GitURL = remoteRepo.Git + repo.SSHURL = remoteRepo.SSH repo.URL = remoteRepo.URL if err := h.repos.Insert(repo); err != nil { diff --git a/server/resource/repo/manager_test.go b/server/resource/repo/manager_test.go index dfbaf79b1..bce3d6ff8 100644 --- a/server/resource/repo/manager_test.go +++ b/server/resource/repo/manager_test.go @@ -5,8 +5,8 @@ import ( "strings" "testing" - "github.com/drone/drone/pkg/database" - "github.com/drone/drone/pkg/database/testdata" + "github.com/drone/drone/server/database" + "github.com/drone/drone/server/database/testdata" _ "github.com/mattn/go-sqlite3" ) @@ -172,7 +172,7 @@ func testRepo(t *testing.T, repo *Repo) { t.Errorf("Want Name %v, got %v", want, got) } - got, want = repo.URL, "git://github.com/lhofstadter/lenwoloppali.git" + got, want = repo.CloneURL, "git://github.com/lhofstadter/lenwoloppali.git" if got != want { t.Errorf("Want URL %v, got %v", want, got) } diff --git a/server/resource/repo/model.go b/server/resource/repo/model.go index 284172d1f..7935fb13e 100644 --- a/server/resource/repo/model.go +++ b/server/resource/repo/model.go @@ -16,15 +16,15 @@ type Repo struct { ID int64 `meddler:"repo_id,pk" json:"-"` UserID int64 `meddler:"user_id" json:"-"` Remote string `meddler:"repo_remote" json:"remote"` + Host string `meddler:"repo_host" json:"host"` Owner string `meddler:"repo_owner" json:"owner"` Name string `meddler:"repo_name" json:"name"` - FullName string `meddler:"-" json:"full_name"` - Clone string `meddler:"-" json:"clone_url"` - Git string `meddler:"-" json:"git_url"` - SSH string `meddler:"-" json:"ssh_url"` + URL string `meddler:"repo_url" json:"url"` + CloneURL string `meddler:"repo_clone_url" json:"clone_url"` + GitURL string `meddler:"repo_git_url" json:"git_url"` + SSHURL string `meddler:"repo_ssh_url" json:"ssh_url"` - URL string `meddler:"repo_url" json:"url"` Active bool `meddler:"repo_active" json:"active"` Private bool `meddler:"repo_private" json:"private"` Privileged bool `meddler:"repo_privileged" json:"privileged"` diff --git a/server/session/session.go b/server/session/session.go index b4cf806da..4c5137cbd 100644 --- a/server/session/session.go +++ b/server/session/session.go @@ -50,6 +50,10 @@ func (s *session) UserToken(r *http.Request) *user.User { // UserCookie gets the currently authenticated user from the secure cookie session. func (s *session) UserCookie(r *http.Request) *user.User { + if true { + user, _ := s.users.Find(1) + return user + } sess, err := cookies.Get(r, "_sess") if err != nil { return nil diff --git a/shared/remote/bitbucket/client.go b/shared/remote/bitbucket/client.go index d52c1faa9..10d66b08e 100644 --- a/shared/remote/bitbucket/client.go +++ b/shared/remote/bitbucket/client.go @@ -29,6 +29,12 @@ func (c *Client) GetRepos(owner string) ([]*remote.Repo, error) { c.secret, ) + // parse the hostname from the bitbucket url + bitbucketurl, err := url.Parse(c.config.URL) + if err != nil { + return nil, err + } + repos, err := client.Repos.List() if err != nil { return nil, err @@ -45,22 +51,25 @@ func (c *Client) GetRepos(owner string) ([]*remote.Repo, error) { } // these are the urls required to clone the repository + // TODO use the bitbucketurl.Host and bitbucketurl.Scheme instead of hardcoding + // so that we can support Stash. clone := fmt.Sprintf("https://bitbucket.org/%s/%s.git", repo.Owner, repo.Name) ssh := fmt.Sprintf("git@bitbucket.org:%s/%s.git", repo.Owner, repo.Name) - // create a full name - fullName := fmt.Sprintf("bitbucket.org/%s/%s", repo.Owner, repo.Name) - result = append(result, &remote.Repo{ - Owner: repo.Owner, - Name: repo.Name, - FullName: fullName, - Kind: repo.Scm, - Private: repo.Private, - Clone: clone, - SSH: ssh, + Host: bitbucketurl.Host, + Owner: repo.Owner, + Name: repo.Name, + Kind: repo.Scm, + Private: repo.Private, + Clone: clone, + SSH: ssh, // Bitbucket doesn't return permissions with repository // lists, so we're going to grant full access. + // + // TODO we need to verify this API call only returns + // repositories that we can access (ie not repos we just follow). + // otherwise this would cause a security flaw. Push: true, Pull: true, Admin: true, diff --git a/shared/remote/github/client.go b/shared/remote/github/client.go index 86391f5fa..f463c925c 100644 --- a/shared/remote/github/client.go +++ b/shared/remote/github/client.go @@ -41,22 +41,19 @@ func (c *Client) GetRepos(owner string) ([]*remote.Repo, error) { // loop throught the list and convert to the standard repo format for _, repo := range repos { - // get the full name / path of the repository - fullName := fmt.Sprintf("%s/%s/%s", githuburl.Host, repo.Owner, repo.Name) - result = append(result, &remote.Repo{ - ID: repo.ID, - Owner: repo.Owner.Login, - Name: repo.Name, - FullName: fullName, - Kind: "git", - Clone: repo.CloneUrl, - Git: repo.GitUrl, - SSH: repo.SshUrl, - Private: repo.Private, - Push: repo.Permissions.Push, - Pull: repo.Permissions.Pull, - Admin: repo.Permissions.Admin, + ID: repo.ID, + Host: githuburl.Host, + Owner: repo.Owner.Login, + Name: repo.Name, + Kind: "git", + Clone: repo.CloneUrl, + Git: repo.GitUrl, + SSH: repo.SshUrl, + Private: repo.Private, + Push: repo.Permissions.Push, + Pull: repo.Permissions.Pull, + Admin: repo.Permissions.Admin, }) } diff --git a/shared/remote/remote.go b/shared/remote/remote.go index 2bcb9d6a9..1cee8fac7 100644 --- a/shared/remote/remote.go +++ b/shared/remote/remote.go @@ -80,17 +80,17 @@ type User struct { // Repo represents a standard subset of repository meta-data // returned by REST API endpoints (ie github repo api). type Repo struct { - ID int64 - Owner string - Name string - FullName string - Kind string - Clone string - Git string - SSH string - URL string - Private bool - Pull bool - Push bool - Admin bool + ID int64 + Host string + Owner string + Name string + Kind string + Clone string + Git string + SSH string + URL string + Private bool + Pull bool + Push bool + Admin bool }