Пример #1
0
// 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)
}
Пример #2
0
// 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
}
Пример #3
0
// 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
}
Пример #4
0
// 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
}
Пример #5
0
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()
			})
		})
	})
}