diff --git a/internal/gitsources/agolagit/agolagit.go b/internal/gitsources/agolagit/agolagit.go index e0f35a9..3d48591 100644 --- a/internal/gitsources/agolagit/agolagit.go +++ b/internal/gitsources/agolagit/agolagit.go @@ -169,3 +169,7 @@ func (c *Client) CreateCommitStatus(repopath, commitSHA string, status gitsource func (c *Client) ParseWebhook(r *http.Request) (*types.WebhookData, error) { return parseWebhook(r) } + +func (c *Client) ListUserRepos() ([]*gitsource.RepoInfo, error) { + return nil, nil +} diff --git a/internal/gitsources/gitea/gitea.go b/internal/gitsources/gitea/gitea.go index 7de2f9e..26895cd 100644 --- a/internal/gitsources/gitea/gitea.go +++ b/internal/gitsources/gitea/gitea.go @@ -19,6 +19,7 @@ import ( "fmt" "net" "net/http" + "path" "strconv" "strings" "time" @@ -139,15 +140,11 @@ func (c *Client) GetRepoInfo(repopath string) (*gitsource.RepoInfo, error) { if err != nil { return nil, err } - repo, err := c.client.GetRepo(owner, reponame) + rr, err := c.client.GetRepo(owner, reponame) if err != nil { return nil, err } - return &gitsource.RepoInfo{ - ID: strconv.FormatInt(repo.ID, 10), - SSHCloneURL: repo.SSHURL, - HTTPCloneURL: repo.CloneURL, - }, nil + return fromGiteaRepo(rr), nil } 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 } + +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, + } +} diff --git a/internal/gitsources/gitlab/gitlab.go b/internal/gitsources/gitlab/gitlab.go index c651896..5cfb702 100644 --- a/internal/gitsources/gitlab/gitlab.go +++ b/internal/gitsources/gitlab/gitlab.go @@ -132,15 +132,11 @@ func (c *Client) RefreshOauth2Token(refreshToken string) (*oauth2.Token, 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 { return nil, err } - return &gitsource.RepoInfo{ - ID: strconv.Itoa(repo.ID), - SSHCloneURL: repo.SSHURLToRepo, - HTTPCloneURL: repo.HTTPURLToRepo, - }, nil + return fromGitlabRepo(rr), nil } func (c *Client) GetUserInfo() (*gitsource.UserInfo, error) { @@ -257,3 +253,28 @@ func (c *Client) CreateCommitStatus(repopath, commitSHA string, status gitsource }) 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, + } +} diff --git a/internal/gitsources/gitsource.go b/internal/gitsources/gitsource.go index 2fa91d7..ba8330b 100644 --- a/internal/gitsources/gitsource.go +++ b/internal/gitsources/gitsource.go @@ -39,6 +39,7 @@ type GitSource interface { CreateRepoWebhook(repopath, url, secret string) error ParseWebhook(r *http.Request) (*types.WebhookData, error) CreateCommitStatus(repopath, commitSHA string, status CommitStatus, targetURL, description, context string) error + ListUserRepos() ([]*RepoInfo, error) } type UserSource interface { @@ -63,6 +64,7 @@ type Oauth2Source interface { type RepoInfo struct { ID string + Path string SSHCloneURL string HTTPCloneURL string }