Exemplo n.º 1
0
Arquivo: repo.go Projeto: voxxit/drone
// 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)
}
Exemplo n.º 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
}
Exemplo n.º 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
}
Exemplo n.º 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
}