Repository: lindell/multi-gitter
Pull request #333 (2023-03-24 12:24:23)

refactor(github): replaced parameters with one config parameter


Patch:
diff --git a/cmd/platform.go b/cmd/platform.go
index 7e408f1..4c56cb2 100644
--- a/cmd/platform.go
+++ b/cmd/platform.go
@@ -147,12 +147,22 @@ func createGithubClient(flag *flag.FlagSet, verifyFlags bool, readOnly bool) (mu
 		return nil, err
 	}
 
-	vc, err := github.New(token, gitBaseURL, http.NewLoggingRoundTripper, github.RepositoryListing{
-		Organizations: orgs,
-		Users:         users,
-		Repositories:  repoRefs,
-		Topics:        topics,
-	}, mergeTypes, forkMode, forkOwner, sshAuth, readOnly)
+	vc, err := github.New(github.Config{
+		Token:               token,
+		BaseURL:             gitBaseURL,
+		TransportMiddleware: http.NewLoggingRoundTripper,
+		RepoListing: github.RepositoryListing{
+			Organizations: orgs,
+			Users:         users,
+			Repositories:  repoRefs,
+			Topics:        topics,
+		},
+		MergeTypes: mergeTypes,
+		ForkMode:   forkMode,
+		ForkOwner:  forkOwner,
+		SSHAuth:    sshAuth,
+		ReadOnly:   readOnly,
+	})
 	if err != nil {
 		return nil, err
 	}
diff --git a/internal/scm/github/github.go b/internal/scm/github/github.go
index e890472..2e153ab 100755
--- a/internal/scm/github/github.go
+++ b/internal/scm/github/github.go
@@ -16,29 +16,33 @@ import (
 	"golang.org/x/oauth2"
 )
 
+type Config struct {
+	Token               string
+	BaseURL             string
+	TransportMiddleware func(http.RoundTripper) http.RoundTripper
+	RepoListing         RepositoryListing
+	MergeTypes          []scm.MergeType
+	ForkMode            bool
+	ForkOwner           string
+	SSHAuth             bool
+	ReadOnly            bool
+}
+
 // New create a new Github client
 func New(
-	token string,
-	baseURL string,
-	transportMiddleware func(http.RoundTripper) http.RoundTripper,
-	repoListing RepositoryListing,
-	mergeTypes []scm.MergeType,
-	forkMode bool,
-	forkOwner string,
-	sshAuth bool,
-	readOnly bool,
+	config Config,
 ) (*Github, error) {
 	ctx := context.Background()
 	ts := oauth2.StaticTokenSource(
-		&oauth2.Token{AccessToken: token},
+		&oauth2.Token{AccessToken: config.Token},
 	)
 	tc := oauth2.NewClient(ctx, ts)
-	tc.Transport = transportMiddleware(tc.Transport)
+	tc.Transport = config.TransportMiddleware(tc.Transport)
 
 	var client *github.Client
-	if baseURL != "" {
+	if config.BaseURL != "" {
 		var err error
-		client, err = github.NewEnterpriseClient(baseURL, "", tc)
+		client, err = github.NewEnterpriseClient(config.BaseURL, "", tc)
 		if err != nil {
 			return nil, err
 		}
@@ -47,17 +51,17 @@ func New(
 	}
 
 	return &Github{
-		RepositoryListing: repoListing,
-		MergeTypes:        mergeTypes,
-		token:             token,
-		baseURL:           baseURL,
-		Fork:              forkMode,
-		ForkOwner:         forkOwner,
-		SSHAuth:           sshAuth,
+		RepositoryListing: config.RepoListing,
+		MergeTypes:        config.MergeTypes,
+		token:             config.Token,
+		baseURL:           config.BaseURL,
+		Fork:              config.ForkMode,
+		ForkOwner:         config.ForkOwner,
+		SSHAuth:           config.SSHAuth,
 		ghClient:          client,
-		ReadOnly:          readOnly,
+		ReadOnly:          config.ReadOnly,
 		httpClient: &http.Client{
-			Transport: transportMiddleware(http.DefaultTransport),
+			Transport: config.TransportMiddleware(http.DefaultTransport),
 		},
 	}, nil
 }


Test patch:
diff --git a/internal/scm/github/github_test.go b/internal/scm/github/github_test.go
index e16bcfe..8976547 100644
--- a/internal/scm/github/github_test.go
+++ b/internal/scm/github/github_test.go
@@ -122,9 +122,13 @@ func Test_GetRepositories(t *testing.T) {
 
 	// Organization
 	{
-		gh, err := github.New("", "", transport.Wrapper, github.RepositoryListing{
-			Organizations: []string{"test-org"},
-		}, []scm.MergeType{scm.MergeTypeMerge}, false, "", false, false)
+		gh, err := github.New(github.Config{
+			TransportMiddleware: transport.Wrapper,
+			RepoListing: github.RepositoryListing{
+				Organizations: []string{"test-org"},
+			},
+			MergeTypes: []scm.MergeType{scm.MergeTypeMerge},
+		})
 		require.NoError(t, err)
 
 		repos, err := gh.GetRepositories(context.Background())
@@ -137,14 +141,18 @@ func Test_GetRepositories(t *testing.T) {
 
 	// repository
 	{
-		gh, err := github.New("", "", transport.Wrapper, github.RepositoryListing{
-			Repositories: []github.RepositoryReference{
-				{
-					OwnerName: "test-org",
-					Name:      "test1",
+		gh, err := github.New(github.Config{
+			TransportMiddleware: transport.Wrapper,
+			RepoListing: github.RepositoryListing{
+				Repositories: []github.RepositoryReference{
+					{
+						OwnerName: "test-org",
+						Name:      "test1",
+					},
 				},
 			},
-		}, []scm.MergeType{scm.MergeTypeMerge}, false, "", false, false)
+			MergeTypes: []scm.MergeType{scm.MergeTypeMerge},
+		})
 		require.NoError(t, err)
 
 		repos, err := gh.GetRepositories(context.Background())
@@ -157,9 +165,13 @@ func Test_GetRepositories(t *testing.T) {
 
 	// User
 	{
-		gh, err := github.New("", "", transport.Wrapper, github.RepositoryListing{
-			Users: []string{"test-user"},
-		}, []scm.MergeType{scm.MergeTypeMerge}, false, "", false, false)
+		gh, err := github.New(github.Config{
+			TransportMiddleware: transport.Wrapper,
+			RepoListing: github.RepositoryListing{
+				Users: []string{"test-user"},
+			},
+			MergeTypes: []scm.MergeType{scm.MergeTypeMerge},
+		})
 		require.NoError(t, err)
 
 		repos, err := gh.GetRepositories(context.Background())
@@ -172,10 +184,14 @@ func Test_GetRepositories(t *testing.T) {
 
 	// Topics
 	{
-		gh, err := github.New("", "", transport.Wrapper, github.RepositoryListing{
-			Organizations: []string{"test-org"},
-			Topics:        []string{"frontend", "backend"},
-		}, []scm.MergeType{scm.MergeTypeMerge}, false, "", false, false)
+		gh, err := github.New(github.Config{
+			TransportMiddleware: transport.Wrapper,
+			RepoListing: github.RepositoryListing{
+				Organizations: []string{"test-org"},
+				Topics:        []string{"frontend", "backend"},
+			},
+			MergeTypes: []scm.MergeType{scm.MergeTypeMerge},
+		})
 		require.NoError(t, err)
 
 		repos, err := gh.GetRepositories(context.Background())
@@ -188,16 +204,20 @@ func Test_GetRepositories(t *testing.T) {
 
 	// Multiple
 	{
-		gh, err := github.New("", "", transport.Wrapper, github.RepositoryListing{
-			Organizations: []string{"test-org"},
-			Users:         []string{"test-user"},
-			Repositories: []github.RepositoryReference{
-				{
-					OwnerName: "test-org",
-					Name:      "test1",
+		gh, err := github.New(github.Config{
+			TransportMiddleware: transport.Wrapper,
+			RepoListing: github.RepositoryListing{
+				Organizations: []string{"test-org"},
+				Users:         []string{"test-user"},
+				Repositories: []github.RepositoryReference{
+					{
+						OwnerName: "test-org",
+						Name:      "test1",
+					},
 				},
 			},
-		}, []scm.MergeType{scm.MergeTypeMerge}, false, "", false, false)
+			MergeTypes: []scm.MergeType{scm.MergeTypeMerge},
+		})
 		require.NoError(t, err)
 
 		repos, err := gh.GetRepositories(context.Background())