Skip to content

Commit 0f283d7

Browse files
committed
refactor(GitlabInstance): update class attributes
use builtin retryablehttp client instead of homemade one refactor the GitlabInstance init method to use a custom dict as singular arg instead of too many separate args add a "Role" field to the GitlabInstance
1 parent 04a2989 commit 0f283d7

File tree

4 files changed

+86
-11
lines changed

4 files changed

+86
-11
lines changed

internal/mirroring/const.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package mirroring
2+
3+
const (
4+
// GitLab Instance role keyword
5+
ROLE = "role"
6+
// Source GitLab Instance role
7+
ROLE_SOURCE = "source"
8+
// Destination GitLab Instance role
9+
ROLE_DESTINATION = "destination"
10+
)

internal/mirroring/instance.go

Lines changed: 54 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,68 @@ import (
66

77
"gitlab-sync/internal/utils"
88

9+
"github.com/hashicorp/go-retryablehttp"
910
gitlab "gitlab.com/gitlab-org/api/client-go"
11+
"go.uber.org/zap"
1012
)
1113

1214
type GitlabInstance struct {
13-
Gitlab *gitlab.Client
14-
Projects map[string]*gitlab.Project
15-
muProjects sync.RWMutex
16-
Groups map[string]*gitlab.Group
17-
muGroups sync.RWMutex
15+
// Gitlab is the GitLab client used to interact with the GitLab API
16+
Gitlab *gitlab.Client
17+
// Projects is a map of project paths to GitLab project objects, it is used to
18+
// cache projects and avoid unnecessary API calls
19+
Projects map[string]*gitlab.Project
20+
// muProjects is a mutex used to synchronize access to the Projects map
21+
// It ensures that only one goroutine can read or write to the Projects map at a time
22+
muProjects sync.RWMutex
23+
// Groups is a map of group paths to GitLab group objects, it is used to
24+
// cache groups and avoid unnecessary API calls
25+
Groups map[string]*gitlab.Group
26+
// muGroups is a mutex used to synchronize access to the Groups map
27+
// It ensures that only one goroutine can read or write to the Groups map at a time
28+
muGroups sync.RWMutex
29+
// GraphQLClient is the GraphQL client used to interact with the GitLab GraphQL API
30+
// It is used to perform GraphQL queries and mutations
31+
// It is initialized with the GitLab token and URL
1832
GraphQLClient *utils.GraphQLClient
33+
// Role is the role of the GitLab instance, it can be either "source" or "destination"
34+
// It is used to determine the behavior of the mirroring process
35+
Role string
36+
// BigInstance indicates whether the GitLab instance is a large instance
37+
// It is used to determine the behavior of the fetching process
38+
BigInstance bool
39+
}
40+
41+
type GitlabInstanceOpts struct {
42+
// GitlabURL is the URL of the GitLab instance
43+
GitlabURL string
44+
// GitlabToken is the token used to authenticate with the GitLab API
45+
GitlabToken string
46+
// Role is the role of the GitLab instance, it can be either "source" or "destination"
47+
Role string
48+
// Timeout is the timeout for GitLab API requests
49+
Timeout time.Duration
50+
// MaxRetries is the maximum number of retries for GitLab API requests
51+
MaxRetries int
52+
// BigInstance indicates whether the GitLab instance is a large instance
53+
BigInstance bool
1954
}
2055

2156
// newGitlabInstance creates a new GitlabInstance with the provided parameters
2257
// and initializes the GitLab client with a custom HTTP client.
23-
func newGitlabInstance(gitlabURL string, gitlabToken string, timeout time.Duration, maxRetries int) (*GitlabInstance, error) {
58+
func newGitlabInstance(initArgs *GitlabInstanceOpts) (*GitlabInstance, error) {
59+
// Create a custom HTTP client with the specified timeout and max retries
60+
retryClient := retryablehttp.NewClient()
61+
retryClient.HTTPClient.Timeout = initArgs.Timeout
62+
retryClient.RetryMax = initArgs.MaxRetries
63+
retryClient.CheckRetry = retryablehttp.DefaultRetryPolicy
64+
retryClient.RetryWaitMin = 1 * time.Second
65+
retryClient.RetryWaitMax = 5 * time.Second
66+
retryClient.Backoff = retryablehttp.DefaultBackoff
67+
retryClient.Logger = zap.L()
68+
2469
// Initialize the GitLab client with the custom HTTP client
25-
gitlabClient, err := gitlab.NewClient(gitlabToken, gitlab.WithBaseURL(gitlabURL))
70+
gitlabClient, err := gitlab.NewClient(initArgs.GitlabToken, gitlab.WithBaseURL(initArgs.GitlabURL), gitlab.WithHTTPClient(retryClient.StandardClient()))
2671
if err != nil {
2772
return nil, err
2873
}
@@ -31,7 +76,8 @@ func newGitlabInstance(gitlabURL string, gitlabToken string, timeout time.Durati
3176
Gitlab: gitlabClient,
3277
Projects: make(map[string]*gitlab.Project),
3378
Groups: make(map[string]*gitlab.Group),
34-
GraphQLClient: utils.NewGitlabGraphQLClient(gitlabToken, gitlabURL),
79+
GraphQLClient: utils.NewGitlabGraphQLClient(initArgs.GitlabToken, initArgs.GitlabURL),
80+
Role: initArgs.Role,
3581
}
3682

3783
return gitlabInstance, nil

internal/mirroring/instance_test.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,14 @@ func TestNewGitlabInstance(t *testing.T) {
1010
gitlabURL := "https://gitlab.example.com"
1111
gitlabToken := "test-token"
1212

13-
instance, err := newGitlabInstance(gitlabURL, gitlabToken, 10, 3)
13+
instance, err := newGitlabInstance(&GitlabInstanceOpts{
14+
GitlabURL: gitlabURL,
15+
GitlabToken: gitlabToken,
16+
Role: ROLE_SOURCE,
17+
Timeout: 10,
18+
MaxRetries: 3,
19+
BigInstance: false,
20+
})
1421
if err != nil {
1522
t.Fatalf("expected no error, got %v", err)
1623
}

internal/mirroring/main.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,24 @@ import (
1616
// It then processes the filters for groups and projects, and finally creates the groups and projects in the destination GitLab instance.
1717
// If the dry run flag is set, it will only print the groups and projects that would be created or updated.
1818
func MirrorGitlabs(gitlabMirrorArgs *utils.ParserArgs) error {
19-
sourceGitlabInstance, err := newGitlabInstance(gitlabMirrorArgs.SourceGitlabURL, gitlabMirrorArgs.SourceGitlabToken, gitlabMirrorArgs.Timeout, gitlabMirrorArgs.Retry)
19+
sourceGitlabInstance, err := newGitlabInstance(&GitlabInstanceOpts{
20+
GitlabURL: gitlabMirrorArgs.SourceGitlabURL,
21+
GitlabToken: gitlabMirrorArgs.SourceGitlabToken,
22+
Role: ROLE_SOURCE,
23+
Timeout: gitlabMirrorArgs.Timeout,
24+
MaxRetries: gitlabMirrorArgs.Retry,
25+
})
2026
if err != nil {
2127
return err
2228
}
2329

24-
destinationGitlabInstance, err := newGitlabInstance(gitlabMirrorArgs.DestinationGitlabURL, gitlabMirrorArgs.DestinationGitlabToken, gitlabMirrorArgs.Timeout, gitlabMirrorArgs.Retry)
30+
destinationGitlabInstance, err := newGitlabInstance(&GitlabInstanceOpts{
31+
GitlabURL: gitlabMirrorArgs.DestinationGitlabURL,
32+
GitlabToken: gitlabMirrorArgs.DestinationGitlabToken,
33+
Role: ROLE_DESTINATION,
34+
Timeout: gitlabMirrorArgs.Timeout,
35+
MaxRetries: gitlabMirrorArgs.Retry,
36+
})
2537
if err != nil {
2638
return err
2739
}

0 commit comments

Comments
 (0)