gitsources: add ListUserRepos method

This commit is contained in:
Simone Gotti 2019-04-29 17:36:29 +02:00
parent 507a9edb87
commit 559a389b59
4 changed files with 60 additions and 12 deletions

View File

@ -169,3 +169,7 @@ func (c *Client) CreateCommitStatus(repopath, commitSHA string, status gitsource
func (c *Client) ParseWebhook(r *http.Request) (*types.WebhookData, error) { func (c *Client) ParseWebhook(r *http.Request) (*types.WebhookData, error) {
return parseWebhook(r) return parseWebhook(r)
} }
func (c *Client) ListUserRepos() ([]*gitsource.RepoInfo, error) {
return nil, nil
}

View File

@ -19,6 +19,7 @@ import (
"fmt" "fmt"
"net" "net"
"net/http" "net/http"
"path"
"strconv" "strconv"
"strings" "strings"
"time" "time"
@ -139,15 +140,11 @@ func (c *Client) GetRepoInfo(repopath string) (*gitsource.RepoInfo, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
repo, err := c.client.GetRepo(owner, reponame) rr, err := c.client.GetRepo(owner, reponame)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return &gitsource.RepoInfo{ return fromGiteaRepo(rr), nil
ID: strconv.FormatInt(repo.ID, 10),
SSHCloneURL: repo.SSHURL,
HTTPCloneURL: repo.CloneURL,
}, nil
} }
func (c *Client) GetFile(repopath, commit, file string) ([]byte, error) { func (c *Client) GetFile(repopath, commit, file string) ([]byte, error) {
@ -288,3 +285,27 @@ func (c *Client) CreateCommitStatus(repopath, commitSHA string, status gitsource
}) })
return err return err
} }
func (c *Client) ListUserRepos() ([]*gitsource.RepoInfo, error) {
remoteRepos, err := c.client.ListMyRepos()
if err != nil {
return nil, err
}
repos := []*gitsource.RepoInfo{}
for _, rr := range remoteRepos {
repos = append(repos, fromGiteaRepo(rr))
}
return repos, nil
}
func fromGiteaRepo(rr *gitea.Repository) *gitsource.RepoInfo {
return &gitsource.RepoInfo{
ID: strconv.FormatInt(rr.ID, 10),
Path: path.Join(rr.Owner.UserName, rr.Name),
SSHCloneURL: rr.SSHURL,
HTTPCloneURL: rr.CloneURL,
}
}

View File

@ -132,15 +132,11 @@ func (c *Client) RefreshOauth2Token(refreshToken string) (*oauth2.Token, error)
} }
func (c *Client) GetRepoInfo(repopath string) (*gitsource.RepoInfo, error) { func (c *Client) GetRepoInfo(repopath string) (*gitsource.RepoInfo, error) {
repo, _, err := c.client.Projects.GetProject(repopath) rr, _, err := c.client.Projects.GetProject(repopath)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return &gitsource.RepoInfo{ return fromGitlabRepo(rr), nil
ID: strconv.Itoa(repo.ID),
SSHCloneURL: repo.SSHURLToRepo,
HTTPCloneURL: repo.HTTPURLToRepo,
}, nil
} }
func (c *Client) GetUserInfo() (*gitsource.UserInfo, error) { func (c *Client) GetUserInfo() (*gitsource.UserInfo, error) {
@ -257,3 +253,28 @@ func (c *Client) CreateCommitStatus(repopath, commitSHA string, status gitsource
}) })
return err return err
} }
func (c *Client) ListUserRepos() ([]*gitsource.RepoInfo, error) {
opts := &gitlab.ListProjectsOptions{MinAccessLevel: gitlab.AccessLevel(gitlab.MaintainerPermissions)}
remoteRepos, _, err := c.client.Projects.ListProjects(opts)
if err != nil {
return nil, err
}
repos := []*gitsource.RepoInfo{}
for _, rr := range remoteRepos {
repos = append(repos, fromGitlabRepo(rr))
}
return repos, nil
}
func fromGitlabRepo(rr *gitlab.Project) *gitsource.RepoInfo {
return &gitsource.RepoInfo{
ID: strconv.Itoa(rr.ID),
Path: rr.PathWithNamespace,
SSHCloneURL: rr.SSHURLToRepo,
HTTPCloneURL: rr.HTTPURLToRepo,
}
}

View File

@ -39,6 +39,7 @@ type GitSource interface {
CreateRepoWebhook(repopath, url, secret string) error CreateRepoWebhook(repopath, url, secret string) error
ParseWebhook(r *http.Request) (*types.WebhookData, error) ParseWebhook(r *http.Request) (*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() ([]*RepoInfo, error)
} }
type UserSource interface { type UserSource interface {
@ -63,6 +64,7 @@ type Oauth2Source interface {
type RepoInfo struct { type RepoInfo struct {
ID string ID string
Path string
SSHCloneURL string SSHCloneURL string
HTTPCloneURL string HTTPCloneURL string
} }