diff --git a/.github/workflows/reviewdog.yaml b/.github/workflows/reviewdog.yaml new file mode 100644 index 0000000..79e7d3b --- /dev/null +++ b/.github/workflows/reviewdog.yaml @@ -0,0 +1,25 @@ +name: reviewdog +on: [pull_request] + +jobs: + reviewdog: + name: linters + runs-on: ubuntu-latest + steps: + # UPDATE_HERE + # https://github.com/actions/checkout/releases + - name: check out code into the go module directory + uses: actions/checkout@v4 + + + # UPDATE_HERE + # https://github.com/reviewdog/action-golangci-lint/releases + - name: golangci-lint + uses: reviewdog/action-golangci-lint@v2 + # with: + # golangci_lint_flags: "--timeout=15m" + + # UPDATE_HERE + # https://github.com/reviewdog/action-actionlint/releases + - name: action-lint + uses: reviewdog/action-actionlint@v1 diff --git a/.golangci.yml b/.golangci.yml new file mode 100644 index 0000000..971ea1a --- /dev/null +++ b/.golangci.yml @@ -0,0 +1,98 @@ +--- +run: + concurrency: 6 + deadline: 5m +issues: + exclude-rules: + # counterfeiter fakes are usually named 'fake_.go' + - path: fake_.*\.go + linters: + - gocritic + - golint + - dupl +linters: + # please, do not use `enable-all`: it's deprecated and will be removed soon. + # inverted configuration with `enable-all` and `disable` is not scalable during updates of golangci-lint + disable-all: true + + enable: + - asciicheck + - bodyclose + - errcheck + - goconst + - gocritic + - gofmt + - gofumpt + - goimports + - gosec + - gosimple + - govet + - ineffassign + - misspell + - nakedret + - nolintlint + - prealloc + - unconvert + - unconvert + - whitespace + - unused + - ineffassign + # - unparam + # - revive + # - stylecheck + # - lll + # - gomnd + # - goprintffuncname + # - interfacer + # - staticcheck + # - structcheck + # - typecheck + # - goerr113 + # - noctx + +linters-settings: + gosec: + excludes: + - G401 + - G505 + gocritic: + enabled-checks: + # Diagnostic + - commentedOutCode + - nilValReturn + - sloppyReassign + - weakCond + - octalLiteral + + # Performance + - appendCombine + - equalFold + - hugeParam + - indexAlloc + - rangeExprCopy + # - rangeValCopy + + # Style + - boolExprSimplify + - commentedOutImport + - docStub + - emptyFallthrough + - emptyStringTest + - hexLiteral + - methodExprCall + - stringXbytes + - typeAssertChain + - unlabelStmt + - yodaStyleExpr + # - ifElseChain + + # Opinionated + - builtinShadow + - importShadow + - initClause + - nestingReduce + - paramTypeCombine + - ptrToRefParam + - typeUnparen + - unnamedResult + - unnecessaryBlock diff --git a/bitbucket/bitbucket.go b/bitbucket/bitbucket.go index c764c2c..bab4d19 100644 --- a/bitbucket/bitbucket.go +++ b/bitbucket/bitbucket.go @@ -58,7 +58,7 @@ func GenerateProjectKey(projectName string) string { } // RepositoryExists - checks if bitbucket repository exists -func RepositoryExists(repoSha string, owner string, repository string) bool { +func RepositoryExists(repoSha, owner, repository string) bool { git := bitbucketAuth(repoSha) repoOptions := &bitbucket.RepositoryOptions{ @@ -66,7 +66,6 @@ func RepositoryExists(repoSha string, owner string, repository string) bool { RepoSlug: repository, } repo, err := git.Repositories.Repository.Get(repoOptions) - if err != nil { log.Debugf("%s: Error fetching repository '%s/%s': %+v", repoSha, owner, repository, err) return false @@ -77,7 +76,7 @@ func RepositoryExists(repoSha string, owner string, repository string) bool { } // ProjectExists - checks if bitbucket project exists -func ProjectExists(git *bitbucket.Client, repoSha string, workspace string, project string) bool { +func ProjectExists(git *bitbucket.Client, repoSha, workspace, project string) bool { opt := &bitbucket.ProjectOptions{ Owner: workspace, Name: project, @@ -85,7 +84,6 @@ func ProjectExists(git *bitbucket.Client, repoSha string, workspace string, proj log.Debugf("%s: Parameter ProjectOptions '%+v'", repoSha, opt) prj, err := git.Workspaces.GetProject(opt) - if err != nil { log.Debugf("%s: Error fetching project '%s' in workspace '%s': %+v\n", repoSha, project, workspace, err) return false @@ -125,7 +123,6 @@ func CreateRepository(repoSha, repository, mirrorVisibilityMode, sourceURL, proj log.Debugf("%s: Creating repository with parameters: '%+v'", repoSha, repoOptions) resultingRepository, err := git.Repositories.Repository.Create(repoOptions) - if err != nil { log.Fatalf("%s: Error - while trying to create github repository '%s': '%s'", repoSha, repository, err) os.Exit(1) @@ -156,7 +153,6 @@ func FetchOwnerRepos(repoSha, owner, bitbucketRole string) []bitbucket.Repositor reposToReutrn = repos.Items } else { log.Errorf("%s: Can't fetch repository list for '%s' '%+v'", repoSha, owner, err) - } for i := 0; repos != nil && i < len(repos.Items) && err == nil; i++ { diff --git a/cmd/config_gen.go b/cmd/config_gen.go index 3d2464e..ad47a24 100644 --- a/cmd/config_gen.go +++ b/cmd/config_gen.go @@ -62,7 +62,7 @@ git-get config-gen -f Gitfile -p "github" -u "git@github.com:AcmeOrg" -t AcmeOrg gitCloudProviderRootURL, gitCloudProvider, targetClonePath, - configGenParams, + &configGenParams, ) }, } diff --git a/cmd/root.go b/cmd/root.go index 396a81e..a15acb0 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -36,24 +36,28 @@ import ( var configGenParams gitget.ConfigGenParamsStruct -var cfgFile string -var cfgFiles []string -var ignoreFiles []string -var logLevel string -var stayOnRef bool -var shallow bool -var concurrencyLevel int -var pushMirror bool -var dryRun bool -var gitCloudProviderRootURL string -var targetClonePath string -var defaultMainBranch string -var status bool -var gitCloudProvider string +var ( + cfgFile string + cfgFiles []string + ignoreFiles []string + logLevel string + stayOnRef bool + shallow bool + concurrencyLevel int + pushMirror bool + dryRun bool + gitCloudProviderRootURL string + targetClonePath string + defaultMainBranch string + status bool + gitCloudProvider string +) // Mirroring specific vars -var mirrorVisibilityMode string -var mirrorBitbucketProjectName string +var ( + mirrorVisibilityMode string + mirrorBitbucketProjectName string +) var levels = map[string]log.Level{ "panic": log.PanicLevel, diff --git a/cmd/version.go b/cmd/version.go index 3129424..7f50835 100644 --- a/cmd/version.go +++ b/cmd/version.go @@ -38,10 +38,9 @@ var versionCmd = &cobra.Command{ Use: "version", Short: "Prints version information", Run: func(cmd *cobra.Command, args []string) { + printLongVersion, _ := cmd.Flags().GetBool("long") - long, _ := cmd.Flags().GetBool("long") - - if long == true { + if printLongVersion { fmt.Printf( "%s %s %s %s %s/%s\n", filepath.Base(os.Args[0]), diff --git a/gitget/get.go b/gitget/get.go index 6b92235..2a202c6 100644 --- a/gitget/get.go +++ b/gitget/get.go @@ -49,14 +49,21 @@ import ( "github.com/isindir/git-get/gitlab" ) -var stayOnRef bool -var defaultMainBranch = "master" -var gitProvider string -var mirrorVisibilityMode = "private" -var bitbucketMirrorProject = "" -var colorHighlight *color.Color -var colorRef *color.Color -var shellRunner = new(exec.ShellRunner) +const ( + HTTPS = "https" + SSH = "ssh" +) + +var ( + stayOnRef bool + defaultMainBranch = "master" + gitProvider string + mirrorVisibilityMode = "private" + bitbucketMirrorProject = "" + colorHighlight *color.Color + colorRef *color.Color + shellRunner = new(exec.ShellRunner) +) // ConfigGenParamsStruct - data structure to store parameters passed via cli flags type ConfigGenParamsStruct struct { @@ -230,12 +237,12 @@ func (repo *Repo) PrepareForGet() { repo.SetRepoFullPath() repo.SetSha() - log.Infof("%s: url: %s (%s) -> %s", repo.sha, repo.URL, colorRef.Sprintf(repo.Ref), repo.fullPath) + log.Infof("%s: url: %s (%s) -> %s", repo.sha, repo.URL, colorRef.Sprintf("%s", repo.Ref), repo.fullPath) log.Debugf("%s: Repository structure: '%+v'", repo.sha, repo) } // PrepareForMirror - set repository structure fields for mirror operation -func (repo *Repo) PrepareForMirror(pathPrefix string, mirrorRootURL string) { +func (repo *Repo) PrepareForMirror(pathPrefix, mirrorRootURL string) { repo.SetShellRunner(shellRunner) repo.SetTempRepoPathForMirror(pathPrefix) repo.SetDefaultRef() @@ -244,7 +251,7 @@ func (repo *Repo) PrepareForMirror(pathPrefix string, mirrorRootURL string) { repo.SetRepoFullPath() repo.SetSha() - log.Infof("%s: url: %s (%s) -> %s", repo.sha, repo.URL, colorRef.Sprintf(repo.Ref), repo.fullPath) + log.Infof("%s: url: %s (%s) -> %s", repo.sha, repo.URL, colorRef.Sprintf("%s", repo.Ref), repo.fullPath) log.Debugf("%s: Repository structure: '%+v'", repo.sha, repo) } @@ -269,8 +276,8 @@ func (repo *Repo) SetRepoFullPath() { } // PathExists returns `true` if given `path` passed as sting exists, otherwise returns false. -func PathExists(path string) (bool, os.FileInfo) { - finfo, err := os.Stat(path) +func PathExists(pathToCheck string) (bool, os.FileInfo) { + finfo, err := os.Stat(pathToCheck) if os.IsNotExist(err) { return false, finfo @@ -304,7 +311,12 @@ func (repo *Repo) CloneMirror() bool { func (repo *Repo) PushMirror() bool { log.Infof("%s: Push repository '%s' as a mirror '%s'", repo.sha, repo.URL, repo.mirrorURL) var serr bytes.Buffer - _, err := (*repo.executor).ExecGitCommand([]string{"push", "--mirror", repo.mirrorURL}, nil, &serr, repo.fullPath) + _, err := (*repo.executor).ExecGitCommand( + []string{"push", "--mirror", repo.mirrorURL}, + nil, + &serr, + repo.fullPath, + ) if err != nil { log.Errorf("%s: %v %v", repo.sha, err, serr.String()) return false @@ -368,15 +380,17 @@ func (repo *Repo) IsClean() bool { return false } _, err = (*repo.executor).ExecGitCommand([]string{"diff", "--staged", "--quiet"}, nil, nil, repo.fullPath) - if err != nil { - return false - } - return true + return err == nil } func (repo *Repo) IsCurrentBranchRef() bool { var outb, errb bytes.Buffer - _, err := (*repo.executor).ExecGitCommand([]string{"rev-parse", "--abbrev-ref", "HEAD"}, &outb, &errb, repo.fullPath) + _, err := (*repo.executor).ExecGitCommand( + []string{"rev-parse", "--abbrev-ref", "HEAD"}, + &outb, + &errb, + repo.fullPath, + ) if err != nil { log.Errorf("%s: Error when checking branch %v", repo.sha, err) } @@ -385,7 +399,12 @@ func (repo *Repo) IsCurrentBranchRef() bool { func (repo *Repo) GetCurrentBranch() string { var outb, errb bytes.Buffer - _, err := (*repo.executor).ExecGitCommand([]string{"rev-parse", "--abbrev-ref", "HEAD"}, &outb, &errb, repo.fullPath) + _, err := (*repo.executor).ExecGitCommand( + []string{"rev-parse", "--abbrev-ref", "HEAD"}, + &outb, + &errb, + repo.fullPath, + ) if err != nil { log.Errorf("%s: Error when getting branch %v", repo.sha, err) return "" @@ -426,10 +445,7 @@ func (repo *Repo) IsRefBranch() bool { nil, repo.fullPath, ) - if err != nil { - return false - } - return true + return err == nil } func (repo *Repo) IsRefTag() bool { @@ -440,10 +456,7 @@ func (repo *Repo) IsRefTag() bool { nil, repo.fullPath, ) - if err != nil { - return false - } - return true + return err == nil } func (repo *Repo) GitPull() { @@ -458,7 +471,7 @@ func (repo *Repo) GitPull() { } else { log.Debugf( "%s: Skip pulling upstream changes for '%s' which is not a branch", - repo.sha, colorRef.Sprintf(repo.Ref)) + repo.sha, colorRef.Sprintf("%s", repo.Ref)) } } @@ -479,7 +492,7 @@ func (repo *Repo) ProcessRepoBasedOnCleaness() { } func (repo *Repo) GitCheckout(branch string) bool { - log.Infof("%s: Checkout to '%s' branch in '%s'", repo.sha, colorHighlight.Sprintf(branch), repo.fullPath) + log.Infof("%s: Checkout to '%s' branch in '%s'", repo.sha, colorHighlight.Sprintf("%s", branch), repo.fullPath) var serr bytes.Buffer res := true @@ -511,7 +524,7 @@ func (repo *Repo) ProcessRepoBasedOnCurrentBranch() { if !stayOnRef { repo.GitCheckout(currentBranch) } else { - log.Debugf("%s: Stay on ref branch '%s'", repo.sha, colorRef.Sprintf(repo.Ref)) + log.Debugf("%s: Stay on ref branch '%s'", repo.sha, colorRef.Sprintf("%s", repo.Ref)) } } } @@ -526,27 +539,38 @@ func (repo *Repo) CreateSymlink(symlink string) { } // check if directory of symlink exists - symnlinkDir := filepath.Dir(symlink) - exists, finfo := PathExists(symnlinkDir) + symlinkDir := filepath.Dir(symlink) + exists, fileInfo := PathExists(symlinkDir) if exists { // create symlink in directory if it does exist - if finfo.IsDir() { - os.Symlink(repo.fullPath, symlink) + if fileInfo.IsDir() { + err := os.Symlink(repo.fullPath, symlink) + if err != nil { + log.Fatalln(err) + os.Exit(1) + } } else { errorMessage := fmt.Sprintf( "%s: path for symlink '%s' directory '%s' exists, but is not directory - check configuration", - repo.sha, symlink, symnlinkDir) + repo.sha, + symlink, + symlinkDir, + ) repo.status.Error = true log.Error(errorMessage) } } else { // Otherwise ensure directory and create symlink - err := os.MkdirAll(symnlinkDir, os.ModePerm) + err := os.MkdirAll(symlinkDir, os.ModePerm) + if err != nil { + log.Fatalln(err) + os.Exit(1) + } + err = os.Symlink(repo.fullPath, symlink) if err != nil { log.Fatalln(err) os.Exit(1) } - os.Symlink(repo.fullPath, symlink) } } @@ -624,7 +648,7 @@ func (repo *Repo) EnsureGitlabMirrorExists() { } } -func DecomposeGitURL(gitURL string) (string, string, string) { +func DecomposeGitURL(gitURL string) (baseURL, fullName, shortName string) { // input: git@abc.com:b/c/d.git or https://abc.com/b/c/d.git -> abc.com/b/c/d // remove unwanted parts of the git repo url re := regexp.MustCompile(`.git$|^https://|^git@`) @@ -634,10 +658,10 @@ func DecomposeGitURL(gitURL string) (string, string, string) { // baseURL and longPath for checking repo existence ( abc.com/b/c/d -> abc.com , b/c/d ) urlParts := strings.SplitN(url, "/", 2) - baseURL, fullName := urlParts[0], urlParts[1] + baseURL, fullName = urlParts[0], urlParts[1] // baseURL and project Name for creating missing repository ( abc.com/b/c/d -> abc.com/b/c , d) - _, shortName := filepath.Split(url) + _, shortName = filepath.Split(url) // ( abc.com/b/c/d -> abc.com, b/c/d, d ) return baseURL, fullName, shortName @@ -671,7 +695,7 @@ func (repo *Repo) EnsureBitbucketMirrorExists() { } func getShallowReposFromConfigInParallel(repoList *RepoList, ignoreRepoList []Repo, concurrencyLevel int) { - var throttle = make(chan int, concurrencyLevel) + throttle := make(chan int, concurrencyLevel) var wait sync.WaitGroup @@ -705,7 +729,7 @@ func getShallowReposFromConfigInParallel(repoList *RepoList, ignoreRepoList []Re } func getReposFromConfigInParallel(repoList *RepoList, ignoreRepoList []Repo, concurrencyLevel int) { - var throttle = make(chan int, concurrencyLevel) + throttle := make(chan int, concurrencyLevel) var wait sync.WaitGroup @@ -746,7 +770,7 @@ func mirrorReposFromConfigInParallel( pushMirror bool, mirrorRootURL string, ) { - var throttle = make(chan int, concurrencyLevel) + throttle := make(chan int, concurrencyLevel) var wait sync.WaitGroup @@ -855,7 +879,7 @@ func fetchGithubRepos( ignoreRepoList []Repo, gitCloudProviderRootURL string, targetClonePath string, - configGenParams ConfigGenParamsStruct, + configGenParams *ConfigGenParamsStruct, ) []Repo { var repoList []Repo ctx := context.Background() @@ -874,12 +898,12 @@ func fetchGithubRepos( for repo := 0; repo < len(ghRepoList); repo++ { var gitGetRepoDefinition Repo switch configGenParams.GitSchema { - case "ssh": + case SSH: gitGetRepoDefinition = Repo{ URL: *ghRepoList[repo].SSHURL, Ref: *ghRepoList[repo].DefaultBranch, } - case "https": + case HTTPS: gitGetRepoDefinition = Repo{ URL: *ghRepoList[repo].HTMLURL, Ref: *ghRepoList[repo].DefaultBranch, @@ -907,7 +931,7 @@ func getBitbucketRepositoryGitURL( v map[string]interface{}, gitCloudProviderRootURL string, fullName string, - GitSchema string, + gitSchema string, ) string { var bbLinks []bitbucketLinks cloneLinks := v["clone"] @@ -930,10 +954,10 @@ func getBitbucketRepositoryGitURL( for j := 0; j < len(bbLinks); j++ { log.Debugf("%+v", bbLinks[j]) - if (bbLinks[j].Name == "ssh") && (GitSchema == "ssh") { + if (bbLinks[j].Name == SSH) && (gitSchema == SSH) { return bbLinks[j].HREF } - if (bbLinks[j].Name == "https") && (GitSchema == "https") { + if (bbLinks[j].Name == HTTPS) && (gitSchema == HTTPS) { return bbLinks[j].HREF } } @@ -947,7 +971,7 @@ func fetchBitbucketRepos( ignoreRepoList []Repo, gitCloudProviderRootURL string, targetClonePath string, - configGenParams ConfigGenParamsStruct, + configGenParams *ConfigGenParamsStruct, ) []Repo { var repoList []Repo @@ -984,7 +1008,7 @@ func fetchGitlabRepos( ignoreRepoList []Repo, gitCloudProviderRootURL string, targetClonePath string, - configGenParams ConfigGenParamsStruct, + configGenParams *ConfigGenParamsStruct, ) []Repo { var repoList []Repo @@ -1009,12 +1033,12 @@ func fetchGitlabRepos( var gitGetRepoDefinition Repo switch configGenParams.GitSchema { - case "ssh": + case SSH: gitGetRepoDefinition = Repo{ URL: glRepoList[repo].SSHURLToRepo, Ref: glRepoList[repo].DefaultBranch, } - case "https": + case HTTPS: gitGetRepoDefinition = Repo{ URL: glRepoList[repo].HTTPURLToRepo, Ref: glRepoList[repo].DefaultBranch, @@ -1039,7 +1063,7 @@ func fetchGitlabRepos( return repoList } -func writeReposToFile(repoSha string, cfgFile string, repoList []Repo) { +func writeReposToFile(repoSha, cfgFile string, repoList []Repo) { if len(repoList) > 0 { log.Infof( "%s: Final number of repositories to be written to '%s': '%d'", @@ -1050,7 +1074,7 @@ func writeReposToFile(repoSha string, cfgFile string, repoList []Repo) { } log.Infof("%s: Writing file '%s'", repoSha, cfgFile) - err = ioutil.WriteFile(cfgFile, repoData, 0644) + err = os.WriteFile(cfgFile, repoData, 0o600) if err != nil { log.Fatalf("%s: %s", cfgFile, err) } @@ -1115,7 +1139,7 @@ func GenerateGitfileConfig( gitCloudProviderRootURL string, gitCloudProvider string, targetClonePath string, - configGenParams ConfigGenParamsStruct, + configGenParams *ConfigGenParamsStruct, ) { initColors() repoSha := generateSha(gitCloudProviderRootURL) @@ -1128,11 +1152,29 @@ func GenerateGitfileConfig( switch gitCloudProvider { case "github": - repoList = fetchGithubRepos(repoSha, ignoreRepoList, gitCloudProviderRootURL, targetClonePath, configGenParams) + repoList = fetchGithubRepos( + repoSha, + ignoreRepoList, + gitCloudProviderRootURL, + targetClonePath, + configGenParams, + ) case "gitlab": - repoList = fetchGitlabRepos(repoSha, ignoreRepoList, gitCloudProviderRootURL, targetClonePath, configGenParams) + repoList = fetchGitlabRepos( + repoSha, + ignoreRepoList, + gitCloudProviderRootURL, + targetClonePath, + configGenParams, + ) case "bitbucket": - repoList = fetchBitbucketRepos(repoSha, ignoreRepoList, gitCloudProviderRootURL, targetClonePath, configGenParams) + repoList = fetchBitbucketRepos( + repoSha, + ignoreRepoList, + gitCloudProviderRootURL, + targetClonePath, + configGenParams, + ) default: log.Fatalf("%s: Error: unknown '%s' git mirror provider", repoSha, gitCloudProvider) os.Exit(1) diff --git a/gitget/get_test.go b/gitget/get_test.go index a15880a..2eab975 100644 --- a/gitget/get_test.go +++ b/gitget/get_test.go @@ -35,7 +35,6 @@ func Test_SetDefaultRef(t *testing.T) { } func Test_GetRepoLocalName(t *testing.T) { - // Test extracting altname from git repo name, if altname is not specified var altname string for repoURL, expectedAltName := range repoUrls { @@ -52,7 +51,6 @@ func Test_GetRepoLocalName(t *testing.T) { } func Test_SetRepoLocalName(t *testing.T) { - for repoURL, expectedAltName := range repoUrls { repo := Repo{} repo.URL = repoURL @@ -368,14 +366,22 @@ func Test_Repo_IsClean(t *testing.T) { } testCases := []testCase{ - {name: "test 1: no error", expectedResult: true, - repo: Repo{fullPath: "cde_p"}, err1: nil, err2: nil}, - {name: "test 2: err1 raised", expectedResult: false, - repo: Repo{fullPath: "cde_a"}, err1: fmt.Errorf("test error"), err2: nil}, - {name: "test 3: err2 raised", expectedResult: false, - repo: Repo{fullPath: "cde_a"}, err1: nil, err2: fmt.Errorf("test error")}, - {name: "test 3: err1 and err2 can be raised", expectedResult: false, - repo: Repo{fullPath: "cde_a"}, err1: fmt.Errorf("test error"), err2: fmt.Errorf("test error")}, + { + name: "test 1: no error", expectedResult: true, + repo: Repo{fullPath: "cde_p"}, err1: nil, err2: nil, + }, + { + name: "test 2: err1 raised", expectedResult: false, + repo: Repo{fullPath: "cde_a"}, err1: fmt.Errorf("test error"), err2: nil, + }, + { + name: "test 3: err2 raised", expectedResult: false, + repo: Repo{fullPath: "cde_a"}, err1: nil, err2: fmt.Errorf("test error"), + }, + { + name: "test 3: err1 and err2 can be raised", expectedResult: false, + repo: Repo{fullPath: "cde_a"}, err1: fmt.Errorf("test error"), err2: fmt.Errorf("test error"), + }, } for _, tc := range testCases { diff --git a/github/github.go b/github/github.go index 9f7324a..44696a8 100644 --- a/github/github.go +++ b/github/github.go @@ -50,7 +50,7 @@ func githubAuth(ctx context.Context, repositorySha string) *github.Client { } // RepositoryExists - check if remote github repository exists -func RepositoryExists(ctx context.Context, repositorySha string, owner string, repository string) bool { +func RepositoryExists(ctx context.Context, repositorySha, owner, repository string) bool { git := githubAuth(ctx, repositorySha) repo, _, err := git.Repositories.Get(ctx, owner, repository) diff --git a/gitlab/gitlab.go b/gitlab/gitlab.go index 8a18c1d..50c0679 100644 --- a/gitlab/gitlab.go +++ b/gitlab/gitlab.go @@ -41,10 +41,25 @@ type GitGetGitlab struct { type GitGetGitlabI interface { Init() bool - gitlabAuth(repositorySha string, baseUrl string) bool - ProjectExists(repositorySha string, baseUrl string, projectName string) bool - GetProjectNamespace(repositorySha string, baseUrl string, projectNameFullPath string) (*gitlab.Namespace, string) - getGroupID(repoSha string, git *gitlab.Client, groupName string) (int, string, error) + gitlabAuth( + repositorySha string, + baseUrl string, + ) bool + ProjectExists( + repositorySha string, + baseUrl string, + projectName string, + ) bool + GetProjectNamespace( + repositorySha string, + baseUrl string, + projectNameFullPath string, + ) (*gitlab.Namespace, string) + getGroupID( + repoSha string, + git *gitlab.Client, + groupName string, + ) (int, string, error) CreateProject( repositorySha string, @@ -93,7 +108,7 @@ func (gitProvider *GitGetGitlab) Init() bool { return tokenFound } -func (gitProvider *GitGetGitlab) auth(repositorySha string, baseUrl string) bool { +func (gitProvider *GitGetGitlab) auth(repositorySha, baseUrl string) bool { clientOptions := gitlab.WithBaseURL("https://" + baseUrl) var err error gitProvider.client, err = gitlab.NewClient(gitProvider.token, clientOptions) @@ -106,7 +121,7 @@ func (gitProvider *GitGetGitlab) auth(repositorySha string, baseUrl string) bool } // ProjectExists checks if project exists and returns boolean if API call is successful -func (gitProvider *GitGetGitlab) ProjectExists(repositorySha string, baseUrl string, projectName string) bool { +func (gitProvider *GitGetGitlab) ProjectExists(repositorySha, baseUrl, projectName string) bool { log.Debugf("%s: Checking repository '%s' '%s' existence", repositorySha, baseUrl, projectName) gitProvider.auth(repositorySha, baseUrl) @@ -114,15 +129,15 @@ func (gitProvider *GitGetGitlab) ProjectExists(repositorySha string, baseUrl str log.Debugf("%s: project: '%+v'", repositorySha, prj) - if err != nil { - return false - } - - return true + return err == nil } // GetProjectNamespace - return Project Namespace and namespace full path -func (gitProvider *GitGetGitlab) GetProjectNamespace(repositorySha string, baseUrl string, projectNameFullPath string) (*gitlab.Namespace, string) { +func (gitProvider *GitGetGitlab) GetProjectNamespace( + repositorySha string, + baseUrl string, + projectNameFullPath string, +) (namespaceObject *gitlab.Namespace, namespaceFullPath string) { log.Debugf("%s: Getting Project FullPath Namespace '%s'", repositorySha, projectNameFullPath) gitProvider.auth(repositorySha, baseUrl) @@ -131,7 +146,7 @@ func (gitProvider *GitGetGitlab) GetProjectNamespace(repositorySha string, baseU if len(pathElements) > 0 { pathElements = pathElements[:len(pathElements)-1] } - namespaceFullPath := strings.Join(pathElements, "/") + namespaceFullPath = strings.Join(pathElements, "/") namespaceObject, _, err := gitProvider.client.Namespaces.GetNamespace(namespaceFullPath, nil, nil) @@ -146,6 +161,22 @@ func (gitProvider *GitGetGitlab) GetProjectNamespace(repositorySha string, baseU return namespaceObject, namespaceFullPath } +func boolPtr(value bool) *bool { + return &value +} + +func stringPtr(value string) *string { + return &value +} + +func intPtr(value int) *int { + return &value +} + +func gitlabVisibilityValuePtr(value gitlab.VisibilityValue) *gitlab.VisibilityValue { + return &value +} + // CreateProject - Create new code repository func (gitProvider *GitGetGitlab) CreateProject( repositorySha string, @@ -158,30 +189,40 @@ func (gitProvider *GitGetGitlab) CreateProject( gitProvider.auth(repositorySha, baseUrl) p := &gitlab.CreateProjectOptions{ - Name: gitlab.String(projectName), - Description: gitlab.String(fmt.Sprintf("Mirror of the '%s'", sourceURL)), - MergeRequestsEnabled: gitlab.Bool(true), - Visibility: gitlab.Visibility(gitlab.VisibilityValue(mirrorVisibilityMode)), + Name: stringPtr(projectName), + Description: stringPtr( + fmt.Sprintf("Mirror of the '%s'", sourceURL), + ), + MergeRequestsEnabled: boolPtr(true), + Visibility: gitlabVisibilityValuePtr( + gitlab.VisibilityValue(mirrorVisibilityMode), + ), } if namespaceID != 0 { - p.NamespaceID = gitlab.Int(namespaceID) + p.NamespaceID = intPtr(namespaceID) } project, _, err := gitProvider.client.Projects.CreateProject(p) if err != nil { log.Fatalf( "%s: Error - while trying to create gitlab project '%s': '%s'", - repositorySha, projectName, err) + repositorySha, + projectName, + err, + ) os.Exit(1) } return project } -func (gitProvider *GitGetGitlab) getGroupID(repoSha string, git *gitlab.Client, groupName string) (int, string, error) { +func (gitProvider *GitGetGitlab) getGroupID( + repoSha string, + git *gitlab.Client, + groupName string, +) (foundGroupId int, foundGroupFullName string, err error) { // Fetch group ID needed for other operations _, shortName := filepath.Split(groupName) - //escapedGroupName := url.QueryEscape(groupName) escapedGroupName := url.QueryEscape(shortName) foundGroups, _, err := git.Groups.SearchGroup(escapedGroupName) @@ -192,8 +233,11 @@ func (gitProvider *GitGetGitlab) getGroupID(repoSha string, git *gitlab.Client, for group := 0; group < len(foundGroups); group++ { log.Debugf( "%s: Checking group '%d:%s' to be as specified", - repoSha, foundGroups[group].ID, foundGroups[group].FullName) - if groupName == strings.Replace(foundGroups[group].FullName, " ", "", -1) { + repoSha, + foundGroups[group].ID, + foundGroups[group].FullName, + ) + if groupName == strings.ReplaceAll(foundGroups[group].FullName, " ", "") { return foundGroups[group].ID, foundGroups[group].FullName, nil } } @@ -220,9 +264,9 @@ func (gitProvider *GitGetGitlab) processSubgroups( subGrpOpt := &gitlab.ListSubGroupsOptions{ ListOptions: lstOpts, - AllAvailable: gitlab.Bool(true), - TopLevelOnly: gitlab.Bool(false), - Owned: gitlab.Bool(gitlabOwned), + AllAvailable: boolPtr(true), + TopLevelOnly: boolPtr(false), + Owned: boolPtr(gitlabOwned), } switch gitlabMinAccessLevel { case "min": @@ -319,8 +363,8 @@ func (gitProvider *GitGetGitlab) appendGroupsProjects( // https://docs.gitlab.com/ee/api/groups.html#list-a-groups-projects prjOpt := &gitlab.ListGroupProjectsOptions{ ListOptions: lstOpts, - Owned: gitlab.Bool(gitlabOwned), - Simple: gitlab.Bool(true), + Owned: boolPtr(gitlabOwned), + Simple: boolPtr(true), } switch gitlabVisibility { case "private":