Skip to content

Commit

Permalink
chore: upgrade to go 1.22
Browse files Browse the repository at this point in the history
also upgrade dependencies, fixing lint issues and removing use of deprecated pkg/errors

related to jenkins-x/jx#8670

Signed-off-by: Mårten Svantesson <[email protected]>
  • Loading branch information
msvticket committed Jun 24, 2024
1 parent 0d6e3e7 commit c5f7b2b
Show file tree
Hide file tree
Showing 17 changed files with 1,313 additions and 1,595 deletions.
57 changes: 18 additions & 39 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
linters-settings:
depguard:
list-type: blacklist
packages:
- github.com/jenkins-x/jx/v2/pkg/log/
- github.com/satori/go.uuid
- github.com/pborman/uuid
packages-with-error-message:
- github.com/jenkins-x/jx/v2/pkg/log/: "use jenkins-x/jx-logging instead"
- github.com/satori/go.uuid: "use github.com/google/uuid instead"
- github.com/pborman/uuid: "use github.com/google/uuid instead"
rules:
# Name of a rule.
Main:
list-mode: lax
deny:
- pkg: github.com/jenkins-x/jx/v2/pkg/log/
desc: "use jenkins-x/jx-logging instead"
- pkg: github.com/satori/go.uuid
desc: "use github.com/google/uuid instead"
- pkg: github.com/pborman/uuid
desc: "use github.com/google/uuid instead"
dupl:
threshold: 100
exhaustive:
Expand Down Expand Up @@ -37,17 +39,14 @@ linters-settings:
gocyclo:
min-complexity: 15
goimports: {}
golint:
min-confidence: 0
revive:
confidence: 0
gofmt:
simplify: true
gomnd:
settings:
mnd:
# don't include the "operation" and "assign"
checks: [argument, case, condition, return]
mnd:
# don't include the "operation" and "assign"
checks: [argument, case, condition, return]
govet:
check-shadowing: true
settings:
printf:
funcs:
Expand All @@ -58,11 +57,8 @@ linters-settings:
- (github.com/jenkins-x/jx-logging/v3/pkg/log/Logger()).Fatalf
lll:
line-length: 140
maligned:
suggest-new: true
misspell: {}
nolintlint:
allow-leading-space: true # don't require machine-readable nolint directives (i.e. with no leading space)
allow-unused: false # report any unused nolint directives
require-explanation: false # don't require an explanation for nolint directives
require-specific: false # don't require nolint directives to be specific about which linter is being skipped
Expand Down Expand Up @@ -93,26 +89,9 @@ linters:
- gocritic
- govet
issues:
# Excluding configuration per-path, per-linter, per-text and per-source
exclude-rules:
# - path: _test\.go
# linters:
# - gomnd
# https://github.com/go-critic/go-critic/issues/926
- linters:
- gocritic
text: "unnecessaryDefer:"
exclude:
- 'shadow: declaration of "err" shadows declaration at'
max-same-issues: 0

exclude-dirs:
- cmd/docs
run:
timeout: 30m
skip-dirs:
- cmd/docs
# golangci.com configuration
# https://github.com/golangci/golangci/wiki/Configuration
service:
golangci-lint-version: 1.42.x # use the fixed version to not introduce new linters unexpectedly
prepare:
- echo "here I can run custom commands, but no preparation needed for this repo"
395 changes: 259 additions & 136 deletions go.mod

Large diffs are not rendered by default.

2,152 changes: 884 additions & 1,268 deletions go.sum

Large diffs are not rendered by default.

28 changes: 14 additions & 14 deletions pkg/envctx/load.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package envctx

import (
"fmt"
"os"
"path/filepath"

Expand All @@ -17,7 +18,6 @@ import (
"github.com/jenkins-x/jx-logging/v3/pkg/log"

"github.com/jenkins-x/jx-api/v4/pkg/client/clientset/versioned"
"github.com/pkg/errors"
)

// LazyLoad lazy loads any missing values
Expand All @@ -26,21 +26,21 @@ func (e *EnvironmentContext) LazyLoad(gclient gitclient.Interface, jxClient vers
if e.DevEnv == nil {
e.DevEnv, err = jxenv.GetDevEnvironment(jxClient, ns)
if err != nil {
return errors.Wrapf(err, "failed to find dev environment in namespace %s", ns)
return fmt.Errorf("failed to find dev environment in namespace %s: %w", ns, err)
}
}
if e.DevEnv == nil {
return errors.Errorf("no dev environment in namespace %s", ns)
return fmt.Errorf("no dev environment in namespace %s", ns)
}
if e.Requirements == nil {
e.Requirements, err = variablefinders.FindRequirements(gclient, jxClient, ns, dir, e.GitOwner, e.GitRepository)
if err != nil {
return errors.Wrapf(err, "failed to load requirements from dev environment")
return fmt.Errorf("failed to load requirements from dev environment: %w", err)
}

}
if e.Requirements == nil {
return errors.Errorf("no Requirements in TeamSettings of dev environment in namespace %s", ns)
return fmt.Errorf("no Requirements in TeamSettings of dev environment in namespace %s", ns)
}

// lets override the dev git URL if its changed in the requirements via the .jx/settings.yaml file
Expand All @@ -53,17 +53,17 @@ func (e *EnvironmentContext) LazyLoad(gclient gitclient.Interface, jxClient vers
// lets use the dev environment git repository
url := e.DevEnv.Spec.Source.URL
if url == "" {
return errors.Errorf("environment %s does not have a source URL", e.DevEnv.Name)
return fmt.Errorf("environment %s does not have a source URL", e.DevEnv.Name)
}
if e.GitUsername == "" || e.GitToken == "" {
creds, err := loadcreds.LoadGitCredential()
if err != nil {
return errors.Wrapf(err, "failed to load git credentials")
return fmt.Errorf("failed to load git credentials: %w", err)
}

gitInfo, err := giturl.ParseGitURL(url)
if err != nil {
return errors.Wrapf(err, "failed to parse git URL %s", url)
return fmt.Errorf("failed to parse git URL %s: %w", url, err)
}
gitServerURL := gitInfo.HostURL()
serverCreds := loadcreds.GetServerCredentials(creds, gitServerURL)
Expand All @@ -79,33 +79,33 @@ func (e *EnvironmentContext) LazyLoad(gclient gitclient.Interface, jxClient vers
}

if e.GitUsername == "" {
return errors.Errorf("could not find git user for git server %s", gitServerURL)
return fmt.Errorf("could not find git user for git server %s", gitServerURL)
}
if e.GitToken == "" {
return errors.Errorf("could not find git token for git server %s", gitServerURL)
return fmt.Errorf("could not find git token for git server %s", gitServerURL)
}
}

gitCloneURL, err := stringhelpers.URLSetUserPassword(url, e.GitUsername, e.GitToken)
if err != nil {
return errors.Wrapf(err, "failed to add user and token to git url %s", url)
return fmt.Errorf("failed to add user and token to git url %s: %w", url, err)
}

cloneDir, err := gitclient.CloneToDir(gitter, gitCloneURL, "")
if err != nil {
return errors.Wrapf(err, "failed to clone URL %s", gitCloneURL)
return fmt.Errorf("failed to clone URL %s: %w", gitCloneURL, err)
}

versionsDir := filepath.Join(cloneDir, "versionStream")
exists, err := files.DirExists(versionsDir)
if err != nil {
return errors.Wrapf(err, "failed to check if version stream exists %s", versionsDir)
return fmt.Errorf("failed to check if version stream exists %s: %w", versionsDir, err)
}
if !exists {
log.Logger().Warnf("dev environment git repository %s does not have a versionStream dir", url)
err = os.MkdirAll(versionsDir, files.DefaultDirWritePermissions)
if err != nil {
return errors.Wrapf(err, "failed to create version stream dir %s", versionsDir)
return fmt.Errorf("failed to create version stream dir %s: %w", versionsDir, err)
}
}

Expand Down
14 changes: 7 additions & 7 deletions pkg/environments/fork.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@ package environments

import (
"context"
"fmt"

"github.com/jenkins-x/go-scm/scm"
"github.com/jenkins-x/jx-helpers/v3/pkg/gitclient"
"github.com/jenkins-x/jx-helpers/v3/pkg/scmhelpers"
"github.com/pkg/errors"
)

// EnsureForked ensures that the git repository is forked
func (o *EnvironmentPullRequestOptions) EnsureForked(client *scm.Client, repoName string) (string, error) {
ctx := context.TODO()
_, localName := scm.Split(repoName)
if localName == "" {
return "", errors.Errorf("no local name for repository %s", repoName)
return "", fmt.Errorf("no local name for repository %s", repoName)
}
createFork := false

Expand All @@ -25,7 +25,7 @@ func (o *EnvironmentPullRequestOptions) EnsureForked(client *scm.Client, repoNam
createFork = true
}
if err != nil {
return "", errors.Wrapf(err, "failed to find repository %s", forkFullName)
return "", fmt.Errorf("failed to find repository %s: %w", forkFullName, err)
}
if !createFork && repo != nil {
return repo.Clone, nil
Expand All @@ -36,7 +36,7 @@ func (o *EnvironmentPullRequestOptions) EnsureForked(client *scm.Client, repoNam
}
repo, _, err = client.Repositories.Fork(ctx, input, repoName)
if err != nil {
return "", errors.Wrapf(err, "failed to fork repository %s", repoName)
return "", fmt.Errorf("failed to fork repository %s: %w", repoName, err)
}
return repo.Clone, nil
}
Expand All @@ -45,17 +45,17 @@ func (o *EnvironmentPullRequestOptions) rebaseForkFromUpstream(dir, gitURL strin
g := o.Git()
branch, err := gitclient.Branch(g, dir)
if err != nil {
return errors.Wrapf(err, "failed to find current branch")
return fmt.Errorf("failed to find current branch: %w", err)
}
remoteName := "upstream"
err = gitclient.AddRemote(g, dir, remoteName, gitURL)
if err != nil {
return errors.Wrapf(err, "failed to add remote %s to %s", remoteName, gitURL)
return fmt.Errorf("failed to add remote %s to %s: %w", remoteName, gitURL, err)
}

_, err = g.Command(dir, "pull", "-r", remoteName, branch)
if err != nil {
return errors.Wrapf(err, "failed to rebase from %s", gitURL)
return fmt.Errorf("failed to rebase from %s: %w", gitURL, err)
}
return nil
}
31 changes: 16 additions & 15 deletions pkg/environments/gitops.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package environments

import (
"context"
"fmt"
"os"
"sort"

Expand All @@ -12,7 +13,7 @@ import (
"github.com/jenkins-x/jx-helpers/v3/pkg/scmhelpers"
"github.com/jenkins-x/jx-helpers/v3/pkg/termcolor"
"github.com/jenkins-x/jx-logging/v3/pkg/log"
"github.com/pkg/errors"

"github.com/sirupsen/logrus"
)

Expand All @@ -32,15 +33,15 @@ const (
func (o *EnvironmentPullRequestOptions) Create(gitURL, prDir string, labels []string, autoMerge bool) (*scm.PullRequest, error) {
scmClient, repoFullName, err := o.GetScmClient(gitURL, o.GitKind)
if err != nil {
return nil, errors.Wrapf(err, "failed to create ScmClient")
return nil, fmt.Errorf("failed to create ScmClient: %w", err)
}
if scmClient == nil {
return nil, nil
}

existingPr, err := o.FindExistingPullRequest(scmClient, repoFullName)
if err != nil {
return nil, errors.Wrapf(err, "failed to find existing PullRequest")
return nil, fmt.Errorf("failed to find existing PullRequest: %w", err)
}

if prDir == "" {
Expand All @@ -55,14 +56,14 @@ func (o *EnvironmentPullRequestOptions) Create(gitURL, prDir string, labels []st
if o.Fork {
cloneGitURL, err = o.EnsureForked(scmClient, repoFullName)
if err != nil {
return nil, errors.Wrapf(err, "failed to ensure repository is forked %s", gitURL)
return nil, fmt.Errorf("failed to ensure repository is forked %s: %w", gitURL, err)
}
}
cloneGitURLSafe := cloneGitURL
if o.ScmClientFactory.GitToken != "" && o.ScmClientFactory.GitUsername != "" {
cloneGitURL, err = o.ScmClientFactory.CreateAuthenticatedURL(cloneGitURL)
if err != nil {
return nil, errors.Wrapf(err, "failed to create authenticated git URL to clone with for private repositories")
return nil, fmt.Errorf("failed to create authenticated git URL to clone with for private repositories: %w", err)
}
}

Expand All @@ -75,18 +76,18 @@ func (o *EnvironmentPullRequestOptions) Create(gitURL, prDir string, labels []st
log.Logger().Infof("checking out remote base branch %s from %s", o.BaseBranchName, gitURL)
err = gitclient.CheckoutRemoteBranch(o.Gitter, dir, o.BaseBranchName)
if err != nil {
return nil, errors.Wrapf(err, "failed to checkout remote branch %s from %s", o.BaseBranchName, gitURL)
return nil, fmt.Errorf("failed to checkout remote branch %s from %s: %w", o.BaseBranchName, gitURL, err)
}
}
}
if err != nil {
return nil, errors.Wrapf(err, "failed to clone git URL %s", cloneGitURLSafe)
return nil, fmt.Errorf("failed to clone git URL %s: %w", cloneGitURLSafe, err)
}

if o.Fork {
err = o.rebaseForkFromUpstream(dir, gitURL)
if err != nil {
return nil, errors.Wrapf(err, "failed to rebase forked repository")
return nil, fmt.Errorf("failed to rebase forked repository: %w", err)
}
}

Expand All @@ -95,15 +96,15 @@ func (o *EnvironmentPullRequestOptions) Create(gitURL, prDir string, labels []st

currentSha, err := gitclient.GetLatestCommitSha(o.Gitter, dir)
if err != nil {
return nil, errors.Wrap(err, "could not get current commit sha")
return nil, fmt.Errorf("could not get current commit sha: %w", err)
}

if o.Function == nil {
return nil, errors.Errorf("no change function configured")
return nil, fmt.Errorf("no change function configured")
}
err = o.Function()
if err != nil {
return nil, errors.Wrapf(err, "failed to invoke change function in dir %s", dir)
return nil, fmt.Errorf("failed to invoke change function in dir %s: %w", dir, err)
}

o.Labels = nil
Expand All @@ -122,14 +123,14 @@ func (o *EnvironmentPullRequestOptions) Create(gitURL, prDir string, labels []st

latestSha, err := gitclient.GetLatestCommitSha(o.Gitter, dir)
if err != nil {
return nil, errors.Wrap(err, "could not get current latest commit sha")
return nil, fmt.Errorf("could not get current latest commit sha: %w", err)
}

doneCommit := true
if latestSha == currentSha {
changed, err := gitclient.HasChanges(o.Gitter, dir)
if err != nil {
return nil, errors.Wrapf(err, "failed to detect changes in dir %s", dir)
return nil, fmt.Errorf("failed to detect changes in dir %s: %w", dir, err)
}
if !changed {
// lets avoid failing to create the PR as we really have made changes
Expand All @@ -139,7 +140,7 @@ func (o *EnvironmentPullRequestOptions) Create(gitURL, prDir string, labels []st

prInfo, err := o.CreatePullRequest(scmClient, gitURL, repoFullName, dir, doneCommit, existingPr)
if err != nil {
return prInfo, errors.Wrapf(err, "failed to create pull request in dir %s", dir)
return prInfo, fmt.Errorf("failed to create pull request in dir %s: %w", dir, err)
}
return prInfo, nil
}
Expand All @@ -160,7 +161,7 @@ func (o *EnvironmentPullRequestOptions) FindExistingPullRequest(scmClient *scm.C
return nil, nil
}
if err != nil {
return nil, errors.Wrapf(err, "Error listing PRs")
return nil, fmt.Errorf("Error listing PRs: %w", err)
}
if log.Logger().Logger.IsLevelEnabled(logrus.TraceLevel) {
log.Logger().Tracef("Found PRs: %s", spew.Sdump(prs))
Expand Down
4 changes: 2 additions & 2 deletions pkg/environments/helpers.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package environments

import "github.com/pkg/errors"
import "fmt"

func (o *EnvironmentPullRequestOptions) ResolveChartRepositoryURL() (string, error) {
return "", errors.Errorf("TODO")
return "", fmt.Errorf("TODO")
}
Loading

0 comments on commit c5f7b2b

Please sign in to comment.