Skip to content

Commit

Permalink
feat: improve auth support and github org repos listing
Browse files Browse the repository at this point in the history
  • Loading branch information
emmanuelgautier committed Jan 17, 2024
1 parent 515f0d4 commit 18d7a69
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 21 deletions.
15 changes: 5 additions & 10 deletions api/middlewares/hosting_middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,16 @@ func getTokenFromContext(c *gin.Context) (*string, error) {
}

func getHostingFromURLParam(c *gin.Context) (hosting.Hosting, error) {
token, err := getTokenFromContext(c)
if err != nil {
return nil, err
}

switch c.Param("hosting") {
case "github.com":
token, err := getTokenFromContext(c)
if err != nil {
return nil, err
}

return github.New(token)

case "gitlab.com":
token, err := getTokenFromContext(c)
if err != nil {
return nil, err
}

if token == nil || *token == "" {
return nil, errors.New("gitlab require a token")
}
Expand Down
13 changes: 11 additions & 2 deletions internal/hosting/github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,22 @@ import (
)

type HostingGithub struct {
client *github.Client
client *github.Client
hasToken bool
}

var _ hosting.Hosting = (*HostingGithub)(nil)

func New(token *string) (*HostingGithub, error) {
hasToken := false
client := github.NewClient(nil)
if token != nil {
client = client.WithAuthToken(*token)
hasToken = true
}

return &HostingGithub{
client: github.NewClient(nil),
client: client,
hasToken: hasToken,
}, nil
}
50 changes: 42 additions & 8 deletions internal/hosting/github/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,53 @@ func mapRepository(owner string, r *github.Repository) *hosting.Repository {
return &repo
}

func (h *HostingGithub) GetRepositories(ctx context.Context, owner string) ([]hosting.Repository, error) {
githubRepos, _, err := h.client.Repositories.ListByUser(ctx, owner, &github.RepositoryListByUserOptions{})
if err != nil {
return nil, err
}

func mapRepositories(owner string, rs []*github.Repository) []hosting.Repository {
var repos []hosting.Repository
for _, r := range githubRepos {
for _, r := range rs {
hostingRepo := mapRepository(owner, r)
repos = append(repos, *hostingRepo)
}

return repos, nil
return repos
}

func (h *HostingGithub) GetRepositories(ctx context.Context, owner string) ([]hosting.Repository, error) {
org, _, err := h.client.Organizations.Get(ctx, owner)
if err == nil && org.GetLogin() == owner {
githubRepos, _, err := h.client.Repositories.ListByOrg(ctx, owner, &github.RepositoryListByOrgOptions{
Sort: "updated",
})
if err != nil {
return nil, err
}

return mapRepositories(owner, githubRepos), nil
}

var user *github.User
if h.hasToken {
user, _, _ = h.client.Users.Get(ctx, "")
}

if user != nil && user.GetLogin() == owner {
githubRepos, _, err := h.client.Repositories.ListByAuthenticatedUser(ctx, &github.RepositoryListByAuthenticatedUserOptions{
Sort: "updated",
})
if err != nil {
return nil, err
}

return mapRepositories(owner, githubRepos), nil
}

githubRepos, _, err := h.client.Repositories.ListByUser(ctx, owner, &github.RepositoryListByUserOptions{
Sort: "updated",
})
if err != nil {
return nil, err
}

return mapRepositories(owner, githubRepos), nil
}

func (h *HostingGithub) GetRepository(ctx context.Context, owner string, repo string) (*hosting.Repository, error) {
Expand Down
2 changes: 1 addition & 1 deletion internal/hosting/gitlab/gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type HostingGitlab struct {
var _ hosting.Hosting = (*HostingGitlab)(nil)

func New(token string) (*HostingGitlab, error) {
client, err := gitlab.NewClient(token)
client, err := gitlab.NewOAuthClient(token)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 18d7a69

Please sign in to comment.