// PutRepo saves a repo in the datastore. func (db *Repostore) PutRepo(repo *model.Repo) error { if repo.Created == 0 { repo.Created = time.Now().UTC().Unix() } repo.Updated = time.Now().UTC().Unix() return meddler.Save(db, repoTable, repo) }
// GetRepos fetches all repositories that the specified // user has access to in the remote system. func (r *Bitbucket) GetRepos(user *model.User) ([]*model.Repo, error) { var repos []*model.Repo var client = bitbucket.New( r.Client, r.Secret, user.Access, user.Secret, ) var list, err = client.Repos.List() if err != nil { return nil, err } var remote = r.GetKind() var hostname = r.GetHost() for _, item := range list { // for now we only support git repos if item.Scm != "git" { continue } // 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. var html = fmt.Sprintf("https://bitbucket.org/%s/%s", item.Owner, item.Slug) var clone = fmt.Sprintf("https://bitbucket.org/%s/%s.git", item.Owner, item.Slug) var ssh = fmt.Sprintf("[email protected]:%s/%s.git", item.Owner, item.Slug) var repo = model.Repo{ UserID: user.ID, Remote: remote, Host: hostname, Owner: item.Owner, Name: item.Slug, Private: item.Private, URL: html, CloneURL: clone, GitURL: clone, SSHURL: ssh, Role: &model.Perm{ Admin: true, Write: true, Read: true, }, } if repo.Private { repo.CloneURL = repo.SSHURL } repos = append(repos, &repo) } return repos, err }
// GetRepos fetches all repositories that the specified // user has access to in the remote system. func (r *Gitlab) GetRepos(user *model.User) ([]*model.Repo, error) { var repos []*model.Repo var client = NewClient(r.url, user.Access, r.SkipVerify) var list, err = client.AllProjects() if err != nil { return nil, err } var remote = r.GetKind() var hostname = r.GetHost() for _, item := range list { var repo = model.Repo{ UserID: user.ID, Remote: remote, Host: hostname, Owner: item.Namespace.Path, Name: item.Path, Private: !item.Public, CloneURL: item.HttpRepoUrl, GitURL: item.HttpRepoUrl, SSHURL: item.SshRepoUrl, URL: item.Url, Role: &model.Perm{}, } if repo.Private { repo.CloneURL = repo.SSHURL } // if the user is the owner we can assume full access, // otherwise check for the permission items. if repo.Owner == user.Login { repo.Role = new(model.Perm) repo.Role.Admin = true repo.Role.Write = true repo.Role.Read = true } else { // Fetch current project project, err := client.Project(strconv.Itoa(item.Id)) if err != nil || project.Permissions == nil { continue } repo.Role.Admin = IsAdmin(project) repo.Role.Write = IsWrite(project) repo.Role.Read = IsRead(project) } repos = append(repos, &repo) } return repos, err }
// GetRepos fetches all repositories that the specified // user has access to in the remote system. func (r *Gogs) GetRepos(user *model.User) ([]*model.Repo, error) { var repos []*model.Repo var remote = r.GetKind() var hostname = r.GetHost() var client = gogs.NewClient(r.URL, user.Access) gogsRepos, err := client.ListMyRepos() if err != nil { return nil, err } for _, repo := range gogsRepos { var repoName = strings.Split(repo.FullName, "/") if len(repoName) < 2 { log.Println("invalid repo full_name", repo.FullName) continue } var owner = repoName[0] var name = repoName[1] var repo = model.Repo{ UserID: user.ID, Remote: remote, Host: hostname, Owner: owner, Name: name, Private: repo.Private, CloneURL: repo.CloneUrl, GitURL: repo.CloneUrl, SSHURL: repo.SshUrl, URL: repo.HtmlUrl, Role: &model.Perm{ Admin: repo.Permissions.Admin, Write: repo.Permissions.Push, Read: repo.Permissions.Pull, }, } if repo.Private { repo.CloneURL = repo.SSHURL } repos = append(repos, &repo) } return repos, err }
// GetRepos fetches all repositories that the specified // user has access to in the remote system. func (r *GitHub) GetRepos(user *model.User) ([]*model.Repo, error) { var repos []*model.Repo var client = NewClient(r.API, user.Access, r.SkipVerify) var list, err = GetAllRepos(client) if err != nil { return nil, err } var remote = r.GetKind() var hostname = r.GetHost() for _, item := range list { var repo = model.Repo{ UserID: user.ID, Remote: remote, Host: hostname, Owner: *item.Owner.Login, Name: *item.Name, Private: *item.Private, URL: *item.HTMLURL, CloneURL: *item.GitURL, GitURL: *item.GitURL, SSHURL: *item.SSHURL, Role: &model.Perm{}, } if r.Private || repo.Private { repo.CloneURL = *item.SSHURL repo.Private = true } // if no permissions we should skip the repository // entirely, since this should never happen if item.Permissions == nil { continue } repo.Role.Admin = (*item.Permissions)["admin"] repo.Role.Write = (*item.Permissions)["push"] repo.Role.Read = (*item.Permissions)["pull"] repos = append(repos, &repo) } return repos, err }
func Test_Github(t *testing.T) { // setup a dummy github server var server = testdata.NewServer() defer server.Close() var github = GitHub{ URL: server.URL, API: server.URL, } var user = model.User{ Access: "e3b0c44298fc1c149afbf4c8996fb", } var repo = model.Repo{ Owner: "octocat", Name: "Hello-World", } var hook = model.Hook{ Sha: "6dcb09b5b57875f334f61aebed695e2e4193db5e", } g := goblin.Goblin(t) g.Describe("GitHub Plugin", func() { g.It("Should identify github vs github enterprise", func() { var ghc = &GitHub{URL: "https://github.com"} var ghe = &GitHub{URL: "https://github.drone.io"} g.Assert(ghc.IsEnterprise()).IsFalse() g.Assert(ghe.IsEnterprise()).IsTrue() g.Assert(ghc.GetKind()).Equal(model.RemoteGithub) g.Assert(ghe.GetKind()).Equal(model.RemoteGithubEnterprise) }) g.It("Should parse the hostname", func() { var ghc = &GitHub{URL: "https://github.com"} var ghe = &GitHub{URL: "https://github.drone.io:80"} g.Assert(ghc.GetHost()).Equal("github.com") g.Assert(ghe.GetHost()).Equal("github.drone.io:80") }) g.It("Should get the repo list", func() { var repos, err = github.GetRepos(&user) g.Assert(err == nil).IsTrue() g.Assert(len(repos)).Equal(4) g.Assert(repos[0].Name).Equal("Hello-World") g.Assert(repos[0].Owner).Equal("octocat") g.Assert(repos[0].Host).Equal(github.GetHost()) g.Assert(repos[0].Remote).Equal(github.GetKind()) g.Assert(repos[0].Private).Equal(true) g.Assert(repos[0].CloneURL).Equal("[email protected]:octocat/Hello-World.git") g.Assert(repos[0].SSHURL).Equal("[email protected]:octocat/Hello-World.git") g.Assert(repos[0].GitURL).Equal("git://github.com/octocat/Hello-World.git") g.Assert(repos[0].Role.Admin).Equal(true) g.Assert(repos[0].Role.Read).Equal(true) g.Assert(repos[0].Role.Write).Equal(true) }) g.It("Should get the build script", func() { var script, err = github.GetScript(&user, &repo, &hook) g.Assert(err == nil).IsTrue() g.Assert(string(script)).Equal("image: go") }) g.It("Should activate a public repo", func() { repo.Private = false repo.CloneURL = "git://github.com/octocat/Hello-World.git" repo.SSHURL = "[email protected]:octocat/Hello-World.git" var err = github.Activate(&user, &repo, "http://example.com") g.Assert(err == nil).IsTrue() }) g.It("Should activate a private repo", func() { repo.Name = "Hola-Mundo" repo.Private = true repo.CloneURL = "[email protected]:octocat/Hola-Mundo.git" repo.SSHURL = "[email protected]:octocat/Hola-Mundo.git" var err = github.Activate(&user, &repo, "http://example.com") g.Assert(err == nil).IsTrue() }) g.It("Should parse a commit hook") g.It("Should parse a pull request hook") }) }
// GetRepos fetches all repositories that the specified // user has access to in the remote system. func (r *Stash) GetRepos(user *model.User) ([]*model.Repo, error) { var repos []*model.Repo var client = stash.New( r.URL, r.Secret, user.Access, user.Secret, r.PrivateKey, ) // parse the hostname from the stash url var stashurl, err = url.Parse(r.URL) if err != nil { return nil, err } // parse the hostname from the stash api stashapi, err := url.Parse(r.API) if err != nil { return nil, err } list, err := client.Repos.List() if err != nil { return nil, err } var remote = r.GetKind() var hostname = r.GetHost() for _, item := range list { // for now we only support git repos if item.ScmId != "git" { continue } // these are the urls required to clone the repository var clone = fmt.Sprintf("https://%s%s/%s/%s.git", stashurl.Host, stashurl.Path, item.Project.Key, item.Name) var ssh = fmt.Sprintf("ssh://git@%s/%s/%s.git", stashapi.Host, item.Project.Key, item.Name) var repo = model.Repo{ UserID: user.ID, Remote: remote, Host: hostname, Owner: item.Project.Key, Name: item.Name, Private: !item.Public, CloneURL: clone, GitURL: clone, SSHURL: ssh, Role: &model.Perm{ Admin: true, Write: true, Read: true, }, } if repo.Private { repo.CloneURL = repo.SSHURL } repos = append(repos, &repo) } return repos, err }
func Test_Github(t *testing.T) { // setup a dummy github server var server = testdata.NewServer() defer server.Close() var github = GitHub{ URL: server.URL, API: server.URL, } var user = model.User{ Access: "e3b0c44298fc1c149afbf4c8996fb", } var repo = model.Repo{ Owner: "octocat", Name: "Hello-World", } var hook = model.Hook{ Sha: "6dcb09b5b57875f334f61aebed695e2e4193db5e", } g := goblin.Goblin(t) g.Describe("GitHub Plugin", func() { g.It("Should identify github vs github enterprise", func() { var ghc = &GitHub{URL: "https://github.com"} var ghe = &GitHub{URL: "https://github.drone.io"} g.Assert(ghc.IsEnterprise()).IsFalse() g.Assert(ghe.IsEnterprise()).IsTrue() g.Assert(ghc.GetKind()).Equal(model.RemoteGithub) g.Assert(ghe.GetKind()).Equal(model.RemoteGithubEnterprise) }) g.It("Should parse the hostname", func() { var ghc = &GitHub{URL: "https://github.com"} var ghe = &GitHub{URL: "https://github.drone.io:80"} g.Assert(ghc.GetHost()).Equal("github.com") g.Assert(ghe.GetHost()).Equal("github.drone.io:80") }) g.It("Should get the repo list", func() { var repos, err = github.GetRepos(&user) g.Assert(err == nil).IsTrue() g.Assert(len(repos)).Equal(4) g.Assert(repos[0].Name).Equal("Hello-World") g.Assert(repos[0].Owner).Equal("octocat") g.Assert(repos[0].Host).Equal(github.GetHost()) g.Assert(repos[0].Remote).Equal(github.GetKind()) g.Assert(repos[0].Private).Equal(true) g.Assert(repos[0].CloneURL).Equal("[email protected]:octocat/Hello-World.git") g.Assert(repos[0].SSHURL).Equal("[email protected]:octocat/Hello-World.git") g.Assert(repos[0].GitURL).Equal("git://github.com/octocat/Hello-World.git") g.Assert(repos[0].Role.Admin).Equal(true) g.Assert(repos[0].Role.Read).Equal(true) g.Assert(repos[0].Role.Write).Equal(true) }) g.It("Should get the build script", func() { var script, err = github.GetScript(&user, &repo, &hook) g.Assert(err == nil).IsTrue() g.Assert(string(script)).Equal("image: go") }) g.It("Should activate a public repo", func() { repo.Private = false repo.CloneURL = "git://github.com/octocat/Hello-World.git" repo.SSHURL = "[email protected]:octocat/Hello-World.git" var err = github.Activate(&user, &repo, "http://example.com") g.Assert(err == nil).IsTrue() }) g.It("Should activate a private repo", func() { repo.Name = "Hola-Mundo" repo.Private = true repo.CloneURL = "[email protected]:octocat/Hola-Mundo.git" repo.SSHURL = "[email protected]:octocat/Hola-Mundo.git" var err = github.Activate(&user, &repo, "http://example.com") g.Assert(err == nil).IsTrue() }) g.It("Should parse a commit hook") g.It("Should parse a pull request hook") g.Describe("Authorize", func() { g.AfterEach(func() { github.Orgs = []string{} }) var resp = httptest.NewRecorder() var state = "validstate" var req, _ = http.NewRequest( "GET", fmt.Sprintf("%s/?code=sekret&state=%s", server.URL, state), nil, ) req.AddCookie(&http.Cookie{Name: "github_state", Value: state}) g.It("Should authorize a valid user with no org restrictions", func() { var login, err = github.Authorize(resp, req) g.Assert(err == nil).IsTrue() g.Assert(login == nil).IsFalse() }) g.It("Should authorize a valid user in the correct org", func() { github.Orgs = []string{"octocats-inc"} var login, err = github.Authorize(resp, req) g.Assert(err == nil).IsTrue() g.Assert(login == nil).IsFalse() }) g.It("Should not authorize a valid user in the wrong org", func() { github.Orgs = []string{"acme"} var login, err = github.Authorize(resp, req) g.Assert(err != nil).IsTrue() g.Assert(login == nil).IsTrue() }) }) }) }