Skip to content

Commit

Permalink
chore: Remove dependency on multierror (runatlantis#5275)
Browse files Browse the repository at this point in the history
Signed-off-by: Luke Massa <[email protected]>
Signed-off-by: Petr Bubenik <[email protected]>
  • Loading branch information
lukemassa authored and Petr Bubenik committed Feb 3, 2025
1 parent c485ce7 commit 68ea8c8
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 29 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ require (
github.com/gorilla/mux v1.8.1
github.com/gorilla/websocket v1.5.3
github.com/hashicorp/go-getter/v2 v2.2.3
github.com/hashicorp/go-multierror v1.1.1
github.com/hashicorp/go-version v1.7.0
github.com/hashicorp/golang-lru/v2 v2.0.7
github.com/hashicorp/hc-install v0.9.0
Expand Down Expand Up @@ -96,6 +95,7 @@ require (
github.com/gorilla/css v1.0.1 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/hashicorp/go-retryablehttp v0.7.7 // indirect
github.com/hashicorp/go-safetemp v1.0.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@ Ran Approve Policies for 1 projects:
### 1. dir: `.` workspace: `default`
**Approve Policies Error**
```
1 error occurred:
* policy set: test_policy user runatlantis is not a policy owner - please contact policy owners to approve failing policies


policy set: test_policy user runatlantis is not a policy owner - please contact policy owners to approve failing policies
```
#### Policy Approval Status:
```
Expand Down
21 changes: 8 additions & 13 deletions server/core/runtime/policy/conftest_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package policy

import (
"context"
"errors"
"fmt"
"os"
"path/filepath"
Expand All @@ -12,9 +13,9 @@ import (
"regexp"

"github.com/hashicorp/go-getter/v2"
"github.com/hashicorp/go-multierror"

version "github.com/hashicorp/go-version"
"github.com/pkg/errors"

"github.com/runatlantis/atlantis/server/core/config/valid"
"github.com/runatlantis/atlantis/server/core/runtime/cache"
runtime_models "github.com/runatlantis/atlantis/server/core/runtime/models"
Expand Down Expand Up @@ -139,7 +140,7 @@ func (c ConfTestVersionDownloader) downloadConfTestVersion(v *version.Version, d
fullSrcURL := fmt.Sprintf("%s?checksum=file:%s", binURL, checksumURL)

if err := c.downloader.GetAny(destPath, fullSrcURL); err != nil {
return runtime_models.LocalFilePath(""), errors.Wrapf(err, "downloading conftest version %s at %q", v.String(), fullSrcURL)
return runtime_models.LocalFilePath(""), fmt.Errorf("downloading conftest version %s at %q: %w", v.String(), fullSrcURL, err)
}

binPath := filepath.Join(destPath, "conftest")
Expand Down Expand Up @@ -212,9 +213,9 @@ func (c *ConfTestExecutorWorkflow) Run(ctx command.ProjectContext, executablePat
if cmdErr != nil {
// Since we're running conftest for each policyset, individual command errors should be concatenated.
if isValidConftestOutput(cmdOutput) {
combinedErr = multierror.Append(combinedErr, fmt.Errorf("policy_set: %s: conftest: some policies failed", policySet.Name))
combinedErr = errors.Join(combinedErr, fmt.Errorf("policy_set: %s: conftest: some policies failed", policySet.Name))
} else {
combinedErr = multierror.Append(combinedErr, fmt.Errorf("policy_set: %s: conftest: %s", policySet.Name, cmdOutput))
combinedErr = errors.Join(combinedErr, fmt.Errorf("policy_set: %s: conftest: %s", policySet.Name, cmdOutput))
}
}

Expand Down Expand Up @@ -247,13 +248,7 @@ func (c *ConfTestExecutorWorkflow) Run(ctx command.ProjectContext, executablePat
policyCheckResultFile := filepath.Join(workdir, ctx.GetPolicyCheckResultFileName())
err = os.WriteFile(policyCheckResultFile, marshaledStatus, 0600)

combinedErr = multierror.Append(combinedErr, err)

// Multierror will wrap combined errors in a way that the upstream functions won't be able to read it as nil.
// Let's pass nil back if there are no wrapped errors.
if errors.Unwrap(combinedErr) == nil {
combinedErr = nil
}
combinedErr = errors.Join(combinedErr, err)

output := string(marshaledStatus)

Expand Down Expand Up @@ -306,7 +301,7 @@ func getDefaultVersion() (*version.Version, error) {
wrappedVersion, err := version.NewVersion(defaultVersion)

if err != nil {
return nil, errors.Wrapf(err, "wrapping version %s", defaultVersion)
return nil, fmt.Errorf("wrapping version %s: %w", defaultVersion, err)
}
return wrappedVersion, nil
}
Expand Down
21 changes: 10 additions & 11 deletions server/events/project_command_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,12 @@ package events

import (
"encoding/json"
"errors"
"fmt"
"os"
"path/filepath"
"strings"

"github.com/hashicorp/go-multierror"
"github.com/pkg/errors"
"github.com/runatlantis/atlantis/server/core/config/valid"
"github.com/runatlantis/atlantis/server/core/runtime"
"github.com/runatlantis/atlantis/server/events/command"
Expand Down Expand Up @@ -347,7 +346,7 @@ func (p *DefaultProjectCommandRunner) doApprovePolicies(ctx command.ProjectConte
// Acquire Atlantis lock for this repo/dir/workspace.
lockAttempt, err := p.Locker.TryLock(ctx.Log, ctx.Pull, ctx.User, ctx.Workspace, models.NewProject(ctx.Pull.BaseRepo.FullName, ctx.RepoRelDir, ctx.ProjectName), ctx.RepoLocksMode == valid.RepoLocksOnPlanMode)
if err != nil {
return nil, "", errors.Wrap(err, "acquiring lock")
return nil, "", fmt.Errorf("acquiring lock: %w", err)
}
if !lockAttempt.LockAcquired {
return nil, lockAttempt.LockFailureReason, nil
Expand Down Expand Up @@ -408,10 +407,10 @@ func (p *DefaultProjectCommandRunner) doApprovePolicies(ctx command.ProjectConte
}
// User matches the author and prevent self approve is set to true
} else if isOwner && !ignorePolicy && ctx.User.Username == ctx.Pull.Author && policySet.PreventSelfApprove {
prjErr = multierror.Append(prjErr, fmt.Errorf("policy set: %s the author of pr %s matches the command commenter user %s - please contact another policy owners to approve failing policies", policySet.Name, ctx.Pull.Author, ctx.User.Username))
prjErr = errors.Join(prjErr, fmt.Errorf("policy set: %s the author of pr %s matches the command commenter user %s - please contact another policy owners to approve failing policies", policySet.Name, ctx.Pull.Author, ctx.User.Username))
// User is not authorized to approve policy set.
} else if !ignorePolicy {
prjErr = multierror.Append(prjErr, fmt.Errorf("policy set: %s user %s is not a policy owner - please contact policy owners to approve failing policies", policySet.Name, ctx.User.Username))
prjErr = errors.Join(prjErr, fmt.Errorf("policy set: %s user %s is not a policy owner - please contact policy owners to approve failing policies", policySet.Name, ctx.User.Username))
}
// Still bubble up this failure, even if policy set is not targeted.
if !policyStatus.Passed && (prjPolicyStatus[i].Approvals != policySet.ApproveCount) {
Expand Down Expand Up @@ -449,7 +448,7 @@ func (p *DefaultProjectCommandRunner) doPolicyCheck(ctx command.ProjectContext)
lockAttempt, err := p.Locker.TryLock(ctx.Log, ctx.Pull, ctx.User, ctx.Workspace, models.NewProject(ctx.Pull.BaseRepo.FullName, ctx.RepoRelDir, ctx.ProjectName), ctx.RepoLocksMode == valid.RepoLocksOnPlanMode)

if err != nil {
return nil, "", errors.Wrap(err, "acquiring lock")
return nil, "", fmt.Errorf("acquiring lock: %w", err)
}
if !lockAttempt.LockAcquired {
return nil, lockAttempt.LockFailureReason, nil
Expand Down Expand Up @@ -502,7 +501,7 @@ func (p *DefaultProjectCommandRunner) doPolicyCheck(ctx command.ProjectContext)
}
// Exclude errors for failed policies
if !strings.Contains(err.Error(), "some policies failed") {
errs = multierror.Append(errs, err)
errs = errors.Join(errs, err)
}
}

Expand Down Expand Up @@ -567,7 +566,7 @@ func (p *DefaultProjectCommandRunner) doPlan(ctx command.ProjectContext) (*model
// Acquire Atlantis lock for this repo/dir/workspace.
lockAttempt, err := p.Locker.TryLock(ctx.Log, ctx.Pull, ctx.User, ctx.Workspace, models.NewProject(ctx.Pull.BaseRepo.FullName, ctx.RepoRelDir, ctx.ProjectName), ctx.RepoLocksMode == valid.RepoLocksOnPlanMode)
if err != nil {
return nil, "", errors.Wrap(err, "acquiring lock")
return nil, "", fmt.Errorf("acquiring lock: %w", err)
}
if !lockAttempt.LockAcquired {
return nil, lockAttempt.LockFailureReason, nil
Expand Down Expand Up @@ -644,7 +643,7 @@ func (p *DefaultProjectCommandRunner) doApply(ctx command.ProjectContext) (apply
// Acquire Atlantis lock for this repo/dir/workspace.
lockAttempt, err := p.Locker.TryLock(ctx.Log, ctx.Pull, ctx.User, ctx.Workspace, models.NewProject(ctx.Pull.BaseRepo.FullName, ctx.RepoRelDir, ctx.ProjectName), ctx.RepoLocksMode == valid.RepoLocksOnApplyMode)
if err != nil {
return "", "", errors.Wrap(err, "acquiring lock")
return "", "", fmt.Errorf("acquiring lock: %w", err)
}
if !lockAttempt.LockAcquired {
return "", lockAttempt.LockFailureReason, nil
Expand Down Expand Up @@ -724,7 +723,7 @@ func (p *DefaultProjectCommandRunner) doImport(ctx command.ProjectContext) (out
// Acquire Atlantis lock for this repo/dir/workspace.
lockAttempt, err := p.Locker.TryLock(ctx.Log, ctx.Pull, ctx.User, ctx.Workspace, models.NewProject(ctx.Pull.BaseRepo.FullName, ctx.RepoRelDir, ctx.ProjectName), ctx.RepoLocksMode != valid.RepoLocksDisabledMode)
if err != nil {
return nil, "", errors.Wrap(err, "acquiring lock")
return nil, "", fmt.Errorf("acquiring lock: %w", err)
}
if !lockAttempt.LockAcquired {
return nil, lockAttempt.LockFailureReason, nil
Expand Down Expand Up @@ -765,7 +764,7 @@ func (p *DefaultProjectCommandRunner) doStateRm(ctx command.ProjectContext) (out
// Acquire Atlantis lock for this repo/dir/workspace.
lockAttempt, err := p.Locker.TryLock(ctx.Log, ctx.Pull, ctx.User, ctx.Workspace, models.NewProject(ctx.Pull.BaseRepo.FullName, ctx.RepoRelDir, ctx.ProjectName), ctx.RepoLocksMode != valid.RepoLocksDisabledMode)
if err != nil {
return nil, "", errors.Wrap(err, "acquiring lock")
return nil, "", fmt.Errorf("acquiring lock: %w", err)
}
if !lockAttempt.LockAcquired {
return nil, lockAttempt.LockFailureReason, nil
Expand Down

0 comments on commit 68ea8c8

Please sign in to comment.