diff --git a/.gitignore b/.gitignore index 5bcc307..f399d6b 100644 --- a/.gitignore +++ b/.gitignore @@ -59,3 +59,4 @@ go.sum sonar-project.properties unit-tests.xml coverage.out +republisher.sh diff --git a/cmd/main.go b/cmd/main.go index 26da183..284534b 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -8,6 +8,7 @@ import ( "gitlab-sync/internal/mirroring" "gitlab-sync/internal/utils" + "gitlab-sync/pkg/helpers" "github.com/spf13/cobra" "go.uber.org/zap" @@ -58,14 +59,14 @@ func main() { zap.L().Debug("Parsing mirror mapping file") mapping, mappingErrors := utils.OpenMirrorMapping(mirrorMappingPath) if mappingErrors != nil { - zap.L().Fatal("Error opening mirror mapping file", zap.Array("errors", utils.ErrorArray(mappingErrors))) + zap.L().Fatal("Error opening mirror mapping file", zap.Array("errors", helpers.ErrorArray(mappingErrors))) } zap.L().Debug("Mirror mapping file parsed successfully") args.MirrorMapping = mapping mirroringErrors := mirroring.MirrorGitlabs(&args) if mirroringErrors != nil { - zap.L().Error("Error during mirroring process", zap.Array("errors", utils.ErrorArray(mirroringErrors))) + zap.L().Error("Error during mirroring process", zap.Array("errors", helpers.ErrorArray(mirroringErrors))) } zap.L().Info("Mirroring completed") }, diff --git a/internal/mirroring/get.go b/internal/mirroring/get.go index ffcb10b..ab31539 100644 --- a/internal/mirroring/get.go +++ b/internal/mirroring/get.go @@ -3,6 +3,7 @@ package mirroring import ( "fmt" "gitlab-sync/internal/utils" + "gitlab-sync/pkg/helpers" "path/filepath" "strings" "sync" @@ -39,7 +40,7 @@ func (g *GitlabInstance) fetchAll(projectFilters map[string]struct{}, groupFilte wg.Wait() close(errCh) - return utils.MergeErrors(errCh) + return helpers.MergeErrors(errCh) } // getParentNamespaceID retrieves the parent namespace ID for a given project or group path. diff --git a/internal/mirroring/get_group.go b/internal/mirroring/get_group.go index 895615e..3c48ad1 100644 --- a/internal/mirroring/get_group.go +++ b/internal/mirroring/get_group.go @@ -3,6 +3,7 @@ package mirroring import ( "fmt" "gitlab-sync/internal/utils" + "gitlab-sync/pkg/helpers" "path/filepath" "sync" @@ -166,7 +167,7 @@ func (g *GitlabInstance) fetchAndProcessGroupsLargeInstance(groupFilters *map[st // Wait for all goroutines to finish wg.Wait() close(errChan) - return utils.MergeErrors(errs) + return helpers.MergeErrors(errs) } // fetchAndProcessGroupRecursive fetches a group and its projects recursively diff --git a/internal/mirroring/get_project.go b/internal/mirroring/get_project.go index a078d94..58d9dca 100644 --- a/internal/mirroring/get_project.go +++ b/internal/mirroring/get_project.go @@ -6,6 +6,7 @@ import ( "sync" "gitlab-sync/internal/utils" + "gitlab-sync/pkg/helpers" gitlab "gitlab.com/gitlab-org/api/client-go" "go.uber.org/zap" @@ -170,7 +171,7 @@ func (g *GitlabInstance) fetchAndProcessProjectsBigInstance(projectFilters *map[ for project := range projectsChan { g.storeProject(project, project.PathWithNamespace, mirrorMapping) } - return utils.MergeErrors(errCh) + return helpers.MergeErrors(errCh) } // fetchAndProcessGroupProjects retrieves all projects from the group and processes them to store in the instance cache. diff --git a/internal/mirroring/main.go b/internal/mirroring/main.go index a65411b..194c7fd 100644 --- a/internal/mirroring/main.go +++ b/internal/mirroring/main.go @@ -6,6 +6,7 @@ import ( "sync" "gitlab-sync/internal/utils" + "gitlab-sync/pkg/helpers" gitlab "gitlab.com/gitlab-org/api/client-go" "go.uber.org/zap" @@ -80,7 +81,7 @@ func MirrorGitlabs(gitlabMirrorArgs *utils.ParserArgs) []error { errCh <- destinationGitlabInstance.createProjects(sourceGitlabInstance, gitlabMirrorArgs.MirrorMapping) close(errCh) - return utils.MergeErrors(errCh) + return helpers.MergeErrors(errCh) } // processFilters processes the filters for groups and projects. diff --git a/internal/mirroring/post.go b/internal/mirroring/post.go index 1304c1e..5bc05fd 100644 --- a/internal/mirroring/post.go +++ b/internal/mirroring/post.go @@ -6,6 +6,7 @@ import ( "sync" "gitlab-sync/internal/utils" + "gitlab-sync/pkg/helpers" gitlab "gitlab.com/gitlab-org/api/client-go" "go.uber.org/zap" @@ -33,7 +34,7 @@ func (destinationGitlab *GitlabInstance) createGroups(sourceGitlab *GitlabInstan } } close(errorChan) - return utils.MergeErrors(errorChan) + return helpers.MergeErrors(errorChan) } // createGroup creates a GitLab group in the destination GitLab instance based on the source group and mirror mapping. @@ -147,7 +148,7 @@ func (destinationGitlab *GitlabInstance) createProjects(sourceGitlab *GitlabInst wg.Wait() close(errorChan) - return utils.MergeErrors(errorChan) + return helpers.MergeErrors(errorChan) } // createProject creates a GitLab project in the destination GitLab instance based on the source project and mirror mapping. @@ -285,7 +286,7 @@ func (destinationGitlab *GitlabInstance) mirrorReleases(sourceGitlab *GitlabInst close(errorChan) zap.L().Info("Releases mirroring completed", zap.String(ROLE_SOURCE, sourceProject.HTTPURLToRepo), zap.String(ROLE_DESTINATION, destinationProject.HTTPURLToRepo)) - return utils.MergeErrors(errorChan) + return helpers.MergeErrors(errorChan) } // ============================================================ // diff --git a/internal/mirroring/put.go b/internal/mirroring/put.go index 93dfd59..a470af4 100644 --- a/internal/mirroring/put.go +++ b/internal/mirroring/put.go @@ -6,6 +6,7 @@ import ( "time" "gitlab-sync/internal/utils" + "gitlab-sync/pkg/helpers" gitlab "gitlab.com/gitlab-org/api/client-go" "go.uber.org/zap" @@ -112,7 +113,7 @@ func (destinationGitlabInstance *GitlabInstance) syncProjectAttributes(sourcePro } if missmatched { - destinationProject, _, err := destinationGitlabInstance.Gitlab.Projects.EditProject(destinationProject.ID, gitlabEditOptions) + _, _, err := destinationGitlabInstance.Gitlab.Projects.EditProject(destinationProject.ID, gitlabEditOptions) if err != nil { return fmt.Errorf("failed to edit project %s: %s", destinationProject.HTTPURLToRepo, err) } @@ -193,7 +194,7 @@ func (destinationGitlabInstance *GitlabInstance) updateGroupFromSource(sourceGit wg.Wait() close(errorChan) - return utils.MergeErrors(errorChan) + return helpers.MergeErrors(errorChan) } // copyGroupAvatar copies the avatar from the source group to the destination group. diff --git a/internal/utils/types.go b/internal/utils/types.go index 19a193b..4b3d781 100644 --- a/internal/utils/types.go +++ b/internal/utils/types.go @@ -7,6 +7,7 @@ import ( "encoding/json" "errors" "fmt" + "gitlab-sync/pkg/helpers" "os" "path/filepath" "strings" @@ -139,7 +140,7 @@ func (m *MirrorMapping) check() []error { m.checkGroups(errChan) close(errChan) - return MergeErrors(errChan) + return helpers.MergeErrors(errChan) } // checkProjects checks if the projects are valid diff --git a/internal/utils/types_test.go b/internal/utils/types_test.go index 140bedd..75823bf 100644 --- a/internal/utils/types_test.go +++ b/internal/utils/types_test.go @@ -2,6 +2,7 @@ package utils import ( "fmt" + "gitlab-sync/pkg/helpers" "os" "reflect" "testing" @@ -245,7 +246,7 @@ func TestCheck(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { errs := tt.mapping.check() // now returns []error - got := toStrings(errs) + got := helpers.ToStrings(errs) if !reflect.DeepEqual(got, tt.wantMsgs) { t.Errorf("check() = %v, want %v", got, tt.wantMsgs) } diff --git a/internal/utils/concurrency_manager.go b/pkg/helpers/concurrency_manager.go similarity index 98% rename from internal/utils/concurrency_manager.go rename to pkg/helpers/concurrency_manager.go index be0285b..e63f531 100644 --- a/internal/utils/concurrency_manager.go +++ b/pkg/helpers/concurrency_manager.go @@ -1,4 +1,4 @@ -package utils +package helpers import ( "fmt" diff --git a/internal/utils/concurrency_manager_test.go b/pkg/helpers/concurrency_manager_test.go similarity index 91% rename from internal/utils/concurrency_manager_test.go rename to pkg/helpers/concurrency_manager_test.go index 26be68b..03ab6ad 100644 --- a/internal/utils/concurrency_manager_test.go +++ b/pkg/helpers/concurrency_manager_test.go @@ -1,4 +1,4 @@ -package utils +package helpers import ( "errors" @@ -15,18 +15,6 @@ const ( EXPECT_NIL_GOT_MESSAGE = "Expected nil, Got: %q" ) -// toStrings converts a []error into a []string for easy comparison -func toStrings(errs []error) []string { - if errs == nil { - return nil - } - out := make([]string, len(errs)) - for i, e := range errs { - out[i] = e.Error() - } - return out -} - func TestMergeErrors(t *testing.T) { type args struct { // factory builds the argument to pass into MergeErrors @@ -171,7 +159,7 @@ func TestMergeErrors(t *testing.T) { } return } - got := toStrings(gotErrs) + got := ToStrings(gotErrs) if !reflect.DeepEqual(got, tc.want) { t.Errorf("got %v, want %v", got, tc.want) } diff --git a/pkg/helpers/formats.go b/pkg/helpers/formats.go new file mode 100644 index 0000000..63a31be --- /dev/null +++ b/pkg/helpers/formats.go @@ -0,0 +1,13 @@ +package helpers + +// toStrings converts a []error into a []string for easy comparison +func ToStrings(errs []error) []string { + if errs == nil { + return nil + } + out := make([]string, len(errs)) + for i, e := range errs { + out[i] = e.Error() + } + return out +}