Skip to content

Commit

Permalink
improve remoteClients
Browse files Browse the repository at this point in the history
  • Loading branch information
pPrecel committed Mar 21, 2024
1 parent 6880738 commit 93e9d22
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 24 deletions.
10 changes: 4 additions & 6 deletions pkg/generator/authors.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,14 @@ package generator

import (
"fmt"

"github.com/pPrecel/PKUP/pkg/github"
)

func buildUrlAuthors(remoteClients map[string]github.Client, user *User) (map[string][]string, error) {
func buildUrlAuthors(remoteClients *remoteClients, user *User) (map[string][]string, error) {
authorsMap := map[string][]string{}

// get signatures for opensource if not empty
if user.Username != "" {
signatures, err := remoteClients[""].GetUserSignatures(user.Username)
signatures, err := remoteClients.Get("").GetUserSignatures(user.Username)
if err != nil {
return nil, fmt.Errorf("failed to list user signatures for opensource: %s", err.Error())
}
Expand All @@ -21,8 +19,8 @@ func buildUrlAuthors(remoteClients map[string]github.Client, user *User) (map[st

// get signatures for every enterprise
for url, username := range user.EnterpriseUsernames {
c, ok := remoteClients[url]
if !ok {
c := remoteClients.Get(url)
if c == nil {
// enterprise user is specified by not enterpriese org/repo
continue
}
Expand Down
33 changes: 22 additions & 11 deletions pkg/generator/clients.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,34 @@ import (
"github.com/pterm/pterm"
)

func buildClients(ctx context.Context, logger *pterm.Logger, config *Config, buildClient buildClientFunc) (map[string]github.Client, error) {
urlClients := map[string]github.Client{}
type remoteClients map[string]github.Client

var err error
urlClients, err = appendRemoteClients(urlClients, ctx, logger, config.Orgs, buildClient)
func (rc remoteClients) Set(url string, client github.Client) {
rc[url] = client
}

func (rc remoteClients) Get(url string) github.Client {
return rc[url]
}

func buildClients(ctx context.Context, logger *pterm.Logger, config *Config, buildClient buildClientFunc) (*remoteClients, error) {
remoteClients := &remoteClients{}

err := appendRemoteClients(remoteClients, ctx, logger, config.Orgs, buildClient)
if err != nil {
return nil, err
}

urlClients, err = appendRemoteClients(urlClients, ctx, logger, config.Repos, buildClient)
err = appendRemoteClients(remoteClients, ctx, logger, config.Repos, buildClient)

return urlClients, err
return remoteClients, err
}

func appendRemoteClients(dest map[string]github.Client, ctx context.Context, logger *pterm.Logger, remotes []Remote, buildClient buildClientFunc) (map[string]github.Client, error) {
func appendRemoteClients(dest *remoteClients, ctx context.Context, logger *pterm.Logger, remotes []Remote, buildClient buildClientFunc) error {
for i := range remotes {
if _, ok := dest[remotes[i].EnterpriseUrl]; !ok {
if c := dest.Get(remotes[i].EnterpriseUrl); c == nil {
var err error
dest[remotes[i].EnterpriseUrl], err = buildClient(
client, err := buildClient(
ctx,
logger,
github.ClientOpts{
Expand All @@ -35,10 +44,12 @@ func appendRemoteClients(dest map[string]github.Client, ctx context.Context, log
},
)
if err != nil {
return nil, fmt.Errorf("failed to build client for '%s': %s", remotes[i].Name, err.Error())
return fmt.Errorf("failed to build client for '%s': %s", remotes[i].Name, err.Error())
}

dest.Set(remotes[i].EnterpriseUrl, client)
}
}

return dest, nil
return nil
}
14 changes: 7 additions & 7 deletions pkg/generator/compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func (c *generator) ForConfig(config *Config, opts ComposeOpts) error {
return view.Run()
}

func (c *generator) composeForUser(remoteClients map[string]github.Client, repoCommits []*repoCommits, user *User, config *Config, opts *ComposeOpts) (*github.CommitList, error) {
func (c *generator) composeForUser(remoteClients *remoteClients, repoCommits []*repoCommits, user *User, config *Config, opts *ComposeOpts) (*github.CommitList, error) {
outputDir, err := sanitizeOutputDir(user.OutputDir)
if err != nil {
return nil, fmt.Errorf("failed to sanitize path '%s': %s", user.OutputDir, err.Error())
Expand All @@ -95,7 +95,7 @@ func (c *generator) composeForUser(remoteClients map[string]github.Client, repoC
Commits: github.GetUserCommits(repo.commits.Commits, urlAuthors[repo.enterpriseUrl]),
}

saveErr := artifacts.SaveDiffToFiles(remoteClients[repo.enterpriseUrl], &userCommits, artifacts.Options{
saveErr := artifacts.SaveDiffToFiles(remoteClients.Get(repo.enterpriseUrl), &userCommits, artifacts.Options{
Org: repo.org,
Repo: repo.repo,
Authors: urlAuthors[repo.enterpriseUrl],
Expand Down Expand Up @@ -157,7 +157,7 @@ type repoCommits struct {
commits *github.CommitList
}

func (c *generator) listAllCommits(remoteClients map[string]github.Client, config *Config, opts *ComposeOpts) ([]*repoCommits, error) {
func (c *generator) listAllCommits(remoteClients *remoteClients, config *Config, opts *ComposeOpts) ([]*repoCommits, error) {
repos, err := c.listOrgRepos(remoteClients, config)
if err != nil {
return nil, fmt.Errorf("failed to list repositories for orgs: %s", err.Error())
Expand All @@ -174,7 +174,7 @@ func (c *generator) listAllCommits(remoteClients map[string]github.Client, confi
defer wg.Done()

orgName, repoName := splitRemoteName(repo.Name)
client := remoteClients[repo.EnterpriseUrl]
client := remoteClients.Get(repo.EnterpriseUrl)

c.logger.Trace("listing commits for repo", c.logger.Args("org", orgName, "repo", repoName))
commitList, listErr := client.ListRepoCommits(github.ListRepoCommitsOpts{
Expand Down Expand Up @@ -205,12 +205,12 @@ func (c *generator) listAllCommits(remoteClients map[string]github.Client, confi
return allRepoCommits, err
}

func (c *generator) listOrgRepos(remoteClients map[string]github.Client, config *Config) ([]Remote, error) {
func (c *generator) listOrgRepos(remoteClients *remoteClients, config *Config) ([]Remote, error) {
remotes := []Remote{}

// resolve orgs
for _, org := range config.Orgs {
c := remoteClients[org.EnterpriseUrl]
c := remoteClients.Get(org.EnterpriseUrl)

repos, err := c.ListRepos(org.Name)
if err != nil {
Expand Down Expand Up @@ -240,7 +240,7 @@ func (c *generator) listOrgRepos(remoteClients map[string]github.Client, config

// check if remote has AllBranches set
for i, remote := range remotes {
c := remoteClients[remote.EnterpriseUrl]
c := remoteClients.Get(remote.EnterpriseUrl)
repoOrg := strings.Split(remote.Name, "/")

if remote.AllBranches {
Expand Down

0 comments on commit 93e9d22

Please sign in to comment.