gitsources: list only repo with enough permissions

ListUserRepos will return only repos where the user has enough permissions to
create webhooks and deploy keys
This commit is contained in:
Simone Gotti 2019-05-23 16:58:20 +02:00
parent 4d7605a86b
commit 51945513bf
4 changed files with 13 additions and 1 deletions

View File

@ -377,6 +377,10 @@ func (c *Client) ListUserRepos() ([]*gitsource.RepoInfo, error) {
repos := []*gitsource.RepoInfo{} repos := []*gitsource.RepoInfo{}
for _, rr := range remoteRepos { for _, rr := range remoteRepos {
// keep only repos with admin permissions
if !rr.Permissions.Admin {
continue
}
repos = append(repos, fromGiteaRepo(rr)) repos = append(repos, fromGiteaRepo(rr))
} }

View File

@ -371,8 +371,14 @@ func (c *Client) ListUserRepos() ([]*gitsource.RepoInfo, error) {
repos := []*gitsource.RepoInfo{} repos := []*gitsource.RepoInfo{}
for _, rr := range remoteRepos { for _, rr := range remoteRepos {
// keep only repos with admin permissions
if rr.Permissions != nil {
if !(*rr.Permissions)["admin"] {
continue
}
repos = append(repos, fromGithubRepo(rr)) repos = append(repos, fromGithubRepo(rr))
} }
}
return repos, nil return repos, nil
} }

View File

@ -256,6 +256,7 @@ func (c *Client) CreateCommitStatus(repopath, commitSHA string, status gitsource
} }
func (c *Client) ListUserRepos() ([]*gitsource.RepoInfo, error) { func (c *Client) ListUserRepos() ([]*gitsource.RepoInfo, error) {
// get only repos with permission greater or equal to maintainer
opts := &gitlab.ListProjectsOptions{MinAccessLevel: gitlab.AccessLevel(gitlab.MaintainerPermissions)} opts := &gitlab.ListProjectsOptions{MinAccessLevel: gitlab.AccessLevel(gitlab.MaintainerPermissions)}
remoteRepos, _, err := c.client.Projects.ListProjects(opts) remoteRepos, _, err := c.client.Projects.ListProjects(opts)
if err != nil { if err != nil {

View File

@ -43,6 +43,7 @@ type GitSource interface {
CreateRepoWebhook(repopath, url, secret string) error CreateRepoWebhook(repopath, url, secret string) error
ParseWebhook(r *http.Request, secret string) (*types.WebhookData, error) ParseWebhook(r *http.Request, secret string) (*types.WebhookData, error)
CreateCommitStatus(repopath, commitSHA string, status CommitStatus, targetURL, description, context string) error CreateCommitStatus(repopath, commitSHA string, status CommitStatus, targetURL, description, context string) error
// ListUserRepos report repos where the user has the permission to create deploy keys and webhooks
ListUserRepos() ([]*RepoInfo, error) ListUserRepos() ([]*RepoInfo, error)
} }