altered repo to track repo hostname (ie github.com) and multiple clone urls

This commit is contained in:
Brad 2014-06-05 15:04:59 -07:00
parent 603883a6cf
commit d84b1ac3e4
9 changed files with 72 additions and 54 deletions

View file

@ -41,9 +41,13 @@ var stmts = []string{`
repo_id INTEGER PRIMARY KEY AUTOINCREMENT repo_id INTEGER PRIMARY KEY AUTOINCREMENT
,user_id INTEGER ,user_id INTEGER
,repo_remote VARCHAR(255) ,repo_remote VARCHAR(255)
,repo_host VARCHAR(255)
,repo_owner VARCHAR(255) ,repo_owner VARCHAR(255)
,repo_name 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_active BOOLEAN
,repo_private BOOLEAN ,repo_private BOOLEAN
,repo_privileged BOOLEAN ,repo_privileged BOOLEAN

View file

@ -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 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 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 (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', 'browndynamite', 'lenwoloppali', 'git://github.com/browndynamite/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', 'browndynamite', 'lenwoloppali', 'git://gitlab.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 user + repository permission entries
"insert into perms values (1, 101, 200, 1, 1, 1, 1398065343, 1398065344);", "insert into perms values (1, 101, 200, 1, 1, 1, 1398065343, 1398065344);",

View file

@ -76,6 +76,10 @@ func (h *LoginHandler) GetLogin(w http.ResponseWriter, r *http.Request) error {
// look at the last synchronized date to determine if // look at the last synchronized date to determine if
// we need to re-sync the account. // 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() { if u.Stale() {
log.Println("sync user account.", u.Login) 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 { for _, remoteRepo := range repos {
repo, _ := repo.New(remote.GetName(), remoteRepo.Owner, remoteRepo.Name) repo, _ := repo.New(remote.GetName(), remoteRepo.Owner, remoteRepo.Name)
repo.Private = remoteRepo.Private repo.Private = remoteRepo.Private
repo.FullName = remoteRepo.FullName // TODO set the repo.Host
repo.Clone = remoteRepo.Clone repo.CloneURL = remoteRepo.Clone
repo.Git = remoteRepo.Git repo.GitURL = remoteRepo.Git
repo.SSH = remoteRepo.SSH repo.SSHURL = remoteRepo.SSH
repo.URL = remoteRepo.URL repo.URL = remoteRepo.URL
if err := h.repos.Insert(repo); err != nil { if err := h.repos.Insert(repo); err != nil {

View file

@ -5,8 +5,8 @@ import (
"strings" "strings"
"testing" "testing"
"github.com/drone/drone/pkg/database" "github.com/drone/drone/server/database"
"github.com/drone/drone/pkg/database/testdata" "github.com/drone/drone/server/database/testdata"
_ "github.com/mattn/go-sqlite3" _ "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) 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 { if got != want {
t.Errorf("Want URL %v, got %v", want, got) t.Errorf("Want URL %v, got %v", want, got)
} }

View file

@ -16,15 +16,15 @@ type Repo struct {
ID int64 `meddler:"repo_id,pk" json:"-"` ID int64 `meddler:"repo_id,pk" json:"-"`
UserID int64 `meddler:"user_id" json:"-"` UserID int64 `meddler:"user_id" json:"-"`
Remote string `meddler:"repo_remote" json:"remote"` Remote string `meddler:"repo_remote" json:"remote"`
Host string `meddler:"repo_host" json:"host"`
Owner string `meddler:"repo_owner" json:"owner"` Owner string `meddler:"repo_owner" json:"owner"`
Name string `meddler:"repo_name" json:"name"` Name string `meddler:"repo_name" json:"name"`
FullName string `meddler:"-" json:"full_name"` URL string `meddler:"repo_url" json:"url"`
Clone string `meddler:"-" json:"clone_url"` CloneURL string `meddler:"repo_clone_url" json:"clone_url"`
Git string `meddler:"-" json:"git_url"` GitURL string `meddler:"repo_git_url" json:"git_url"`
SSH string `meddler:"-" json:"ssh_url"` SSHURL string `meddler:"repo_ssh_url" json:"ssh_url"`
URL string `meddler:"repo_url" json:"url"`
Active bool `meddler:"repo_active" json:"active"` Active bool `meddler:"repo_active" json:"active"`
Private bool `meddler:"repo_private" json:"private"` Private bool `meddler:"repo_private" json:"private"`
Privileged bool `meddler:"repo_privileged" json:"privileged"` Privileged bool `meddler:"repo_privileged" json:"privileged"`

View file

@ -50,6 +50,10 @@ func (s *session) UserToken(r *http.Request) *user.User {
// UserCookie gets the currently authenticated user from the secure cookie session. // UserCookie gets the currently authenticated user from the secure cookie session.
func (s *session) UserCookie(r *http.Request) *user.User { func (s *session) UserCookie(r *http.Request) *user.User {
if true {
user, _ := s.users.Find(1)
return user
}
sess, err := cookies.Get(r, "_sess") sess, err := cookies.Get(r, "_sess")
if err != nil { if err != nil {
return nil return nil

View file

@ -29,6 +29,12 @@ func (c *Client) GetRepos(owner string) ([]*remote.Repo, error) {
c.secret, 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() repos, err := client.Repos.List()
if err != nil { if err != nil {
return nil, err 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 // 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) 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) 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{ result = append(result, &remote.Repo{
Owner: repo.Owner, Host: bitbucketurl.Host,
Name: repo.Name, Owner: repo.Owner,
FullName: fullName, Name: repo.Name,
Kind: repo.Scm, Kind: repo.Scm,
Private: repo.Private, Private: repo.Private,
Clone: clone, Clone: clone,
SSH: ssh, SSH: ssh,
// Bitbucket doesn't return permissions with repository // Bitbucket doesn't return permissions with repository
// lists, so we're going to grant full access. // 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, Push: true,
Pull: true, Pull: true,
Admin: true, Admin: true,

View file

@ -41,22 +41,19 @@ func (c *Client) GetRepos(owner string) ([]*remote.Repo, error) {
// loop throught the list and convert to the standard repo format // loop throught the list and convert to the standard repo format
for _, repo := range repos { 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{ result = append(result, &remote.Repo{
ID: repo.ID, ID: repo.ID,
Owner: repo.Owner.Login, Host: githuburl.Host,
Name: repo.Name, Owner: repo.Owner.Login,
FullName: fullName, Name: repo.Name,
Kind: "git", Kind: "git",
Clone: repo.CloneUrl, Clone: repo.CloneUrl,
Git: repo.GitUrl, Git: repo.GitUrl,
SSH: repo.SshUrl, SSH: repo.SshUrl,
Private: repo.Private, Private: repo.Private,
Push: repo.Permissions.Push, Push: repo.Permissions.Push,
Pull: repo.Permissions.Pull, Pull: repo.Permissions.Pull,
Admin: repo.Permissions.Admin, Admin: repo.Permissions.Admin,
}) })
} }

View file

@ -80,17 +80,17 @@ type User struct {
// Repo represents a standard subset of repository meta-data // Repo represents a standard subset of repository meta-data
// returned by REST API endpoints (ie github repo api). // returned by REST API endpoints (ie github repo api).
type Repo struct { type Repo struct {
ID int64 ID int64
Owner string Host string
Name string Owner string
FullName string Name string
Kind string Kind string
Clone string Clone string
Git string Git string
SSH string SSH string
URL string URL string
Private bool Private bool
Pull bool Pull bool
Push bool Push bool
Admin bool Admin bool
} }