Skip to content

Commit 2aa0699

Browse files
Rmergify[bot]
andauthored
refactor: remove github.com/pkg/errors dependency (nektos#1077)
* refactor: split out common/git * refactor: move git options to separate func * refactor: remove github.com/pkg/errors dependency * fix(golangci-lint): forbid github.com/pkg/errors * style: fix typo * style: fix typo Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
1 parent 8a47394 commit 2aa0699

File tree

13 files changed

+133
-97
lines changed

13 files changed

+133
-97
lines changed

.golangci.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Minimum golangci-lint version required: v1.42.0
1+
# Minimum golangci-lint version required: v1.46.0
22
run:
33
timeout: 3m
44

@@ -22,9 +22,11 @@ linters-settings:
2222
list-type: blacklist
2323
include-go-root: true
2424
packages:
25+
- github.com/pkg/errors
2526
- gotest.tools/v3/assert
2627
- log
2728
packages-with-error-message:
29+
- github.com/pkg/errors: 'Please use "errors" package from standard library'
2830
- gotest.tools/v3: 'Please keep tests unified using only github.com/stretchr/testify'
2931
- log: 'Please keep logging unified using only github.com/sirupsen/logrus'
3032

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ require (
2020
github.com/moby/buildkit v0.10.3
2121
github.com/opencontainers/image-spec v1.0.3-0.20211202183452-c5a74bcca799
2222
github.com/opencontainers/selinux v1.10.1
23-
github.com/pkg/errors v0.9.1
2423
github.com/rhysd/actionlint v1.6.13
2524
github.com/sabhiram/go-gitignore v0.0.0-20210923224102-525f6e181f06
2625
github.com/sirupsen/logrus v1.8.1
@@ -58,6 +57,7 @@ require (
5857
github.com/moby/sys/mountinfo v0.6.0 // indirect
5958
github.com/opencontainers/go-digest v1.0.0 // indirect
6059
github.com/opencontainers/runc v1.1.1 // indirect
60+
github.com/pkg/errors v0.9.1 // indirect
6161
github.com/pmezard/go-difflib v1.0.0 // indirect
6262
github.com/rivo/uniseg v0.2.0 // indirect
6363
github.com/robfig/cron v1.2.0 // indirect
Lines changed: 50 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
package common
1+
package git
22

33
import (
44
"context"
5+
"errors"
56
"fmt"
67
"io"
78
"io/ioutil"
@@ -12,13 +13,14 @@ import (
1213
"strings"
1314
"sync"
1415

15-
git "github.com/go-git/go-git/v5"
16+
"github.com/nektos/act/pkg/common"
17+
18+
"github.com/go-git/go-git/v5"
1619
"github.com/go-git/go-git/v5/config"
1720
"github.com/go-git/go-git/v5/plumbing"
1821
"github.com/go-git/go-git/v5/plumbing/transport/http"
1922
"github.com/go-ini/ini"
2023
"github.com/mattn/go-isatty"
21-
"github.com/pkg/errors"
2224
log "github.com/sirupsen/logrus"
2325
)
2426

@@ -29,8 +31,28 @@ var (
2931
githubSSHRegex = regexp.MustCompile(`github.com[:/](.+)/(.+?)(?:.git)?$`)
3032

3133
cloneLock sync.Mutex
34+
35+
ErrShortRef = errors.New("short SHA references are not supported")
36+
ErrNoRepo = errors.New("unable to find git repo")
3237
)
3338

39+
type Error struct {
40+
err error
41+
commit string
42+
}
43+
44+
func (e *Error) Error() string {
45+
return e.err.Error()
46+
}
47+
48+
func (e *Error) Unwrap() error {
49+
return e.err
50+
}
51+
52+
func (e *Error) Commit() string {
53+
return e.commit
54+
}
55+
3456
// FindGitRevision get the current git revision
3557
func FindGitRevision(file string) (shortSha string, sha string, err error) {
3658
gitDir, err := findGitDirectory(file)
@@ -222,7 +244,7 @@ func findGitDirectory(fromFile string) (string, error) {
222244
if err == nil && fi.Mode().IsDir() {
223245
return gitPath, nil
224246
} else if dir == "/" || dir == "C:\\" || dir == "c:\\" {
225-
return "", errors.New("unable to find git repo")
247+
return "", &Error{err: ErrNoRepo}
226248
}
227249

228250
return findGitDirectory(filepath.Dir(dir))
@@ -277,11 +299,27 @@ func CloneIfRequired(ctx context.Context, refName plumbing.ReferenceName, input
277299
return r, nil
278300
}
279301

302+
func gitOptions(token string) (fetchOptions git.FetchOptions, pullOptions git.PullOptions) {
303+
fetchOptions.RefSpecs = []config.RefSpec{"refs/*:refs/*", "HEAD:refs/heads/HEAD"}
304+
pullOptions.Force = true
305+
306+
if token != "" {
307+
auth := &http.BasicAuth{
308+
Username: "token",
309+
Password: token,
310+
}
311+
fetchOptions.Auth = auth
312+
pullOptions.Auth = auth
313+
}
314+
315+
return fetchOptions, pullOptions
316+
}
317+
280318
// NewGitCloneExecutor creates an executor to clone git repos
281319
// nolint:gocyclo
282-
func NewGitCloneExecutor(input NewGitCloneExecutorInput) Executor {
320+
func NewGitCloneExecutor(input NewGitCloneExecutorInput) common.Executor {
283321
return func(ctx context.Context) error {
284-
logger := Logger(ctx)
322+
logger := common.Logger(ctx)
285323
logger.Infof(" \u2601 git clone '%s' # ref=%s", input.URL, input.Ref)
286324
logger.Debugf(" cloning %s to %s", input.URL, input.Dir)
287325

@@ -295,15 +333,7 @@ func NewGitCloneExecutor(input NewGitCloneExecutorInput) Executor {
295333
}
296334

297335
// fetch latest changes
298-
fetchOptions := git.FetchOptions{
299-
RefSpecs: []config.RefSpec{"refs/*:refs/*", "HEAD:refs/heads/HEAD"},
300-
}
301-
if input.Token != "" {
302-
fetchOptions.Auth = &http.BasicAuth{
303-
Username: "token",
304-
Password: input.Token,
305-
}
306-
}
336+
fetchOptions, pullOptions := gitOptions(input.Token)
307337

308338
err = r.Fetch(&fetchOptions)
309339
if err != nil && !errors.Is(err, git.NoErrAlreadyUpToDate) {
@@ -317,7 +347,10 @@ func NewGitCloneExecutor(input NewGitCloneExecutorInput) Executor {
317347
}
318348

319349
if hash.String() != input.Ref && strings.HasPrefix(hash.String(), input.Ref) {
320-
return errors.Wrap(errors.New(hash.String()), "short SHA references are not supported")
350+
return &Error{
351+
err: ErrShortRef,
352+
commit: hash.String(),
353+
}
321354
}
322355

323356
// At this point we need to know if it's a tag or a branch
@@ -365,17 +398,7 @@ func NewGitCloneExecutor(input NewGitCloneExecutorInput) Executor {
365398
}
366399
}
367400

368-
pullOptions := git.PullOptions{
369-
Force: true,
370-
}
371-
if input.Token != "" {
372-
pullOptions.Auth = &http.BasicAuth{
373-
Username: "token",
374-
Password: input.Token,
375-
}
376-
}
377-
378-
if err = w.Pull(&pullOptions); err != nil && err.Error() != "already up-to-date" {
401+
if err = w.Pull(&pullOptions); err != nil && err != git.NoErrAlreadyUpToDate {
379402
logger.Debugf("Unable to pull %s: %v", refName, err)
380403
}
381404
logger.Debugf("Cloned %s to %s", input.URL, input.Dir)
Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package common
1+
package git
22

33
import (
44
"context"
@@ -173,25 +173,26 @@ func TestGitFindRef(t *testing.T) {
173173

174174
func TestGitCloneExecutor(t *testing.T) {
175175
for name, tt := range map[string]struct {
176-
Err, URL, Ref string
176+
Err error
177+
URL, Ref string
177178
}{
178179
"tag": {
179-
Err: "",
180+
Err: nil,
180181
URL: "https://github.com/actions/checkout",
181182
Ref: "v2",
182183
},
183184
"branch": {
184-
Err: "",
185+
Err: nil,
185186
URL: "https://github.com/anchore/scan-action",
186187
Ref: "act-fails",
187188
},
188189
"sha": {
189-
Err: "",
190+
Err: nil,
190191
URL: "https://github.com/actions/checkout",
191192
Ref: "5a4ac9002d0be2fb38bd78e4b4dbde5606d7042f", // v2
192193
},
193194
"short-sha": {
194-
Err: "short SHA references are not supported: 5a4ac9002d0be2fb38bd78e4b4dbde5606d7042f",
195+
Err: &Error{ErrShortRef, "5a4ac9002d0be2fb38bd78e4b4dbde5606d7042f"},
195196
URL: "https://github.com/actions/checkout",
196197
Ref: "5a4ac90", // v2
197198
},
@@ -204,10 +205,11 @@ func TestGitCloneExecutor(t *testing.T) {
204205
})
205206

206207
err := clone(context.Background())
207-
if tt.Err == "" {
208-
assert.Empty(t, err)
208+
if tt.Err != nil {
209+
assert.Error(t, err)
210+
assert.Equal(t, tt.Err, err)
209211
} else {
210-
assert.EqualError(t, err, tt.Err)
212+
assert.Empty(t, err)
211213
}
212214
})
213215
}

pkg/container/docker_pull.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ import (
44
"context"
55
"encoding/base64"
66
"encoding/json"
7+
"fmt"
78

89
"github.com/docker/distribution/reference"
910
"github.com/docker/docker/api/types"
10-
"github.com/pkg/errors"
1111
log "github.com/sirupsen/logrus"
1212

1313
"github.com/nektos/act/pkg/common"
@@ -37,7 +37,7 @@ func NewDockerPullExecutor(input NewDockerPullExecutorInput) common.Executor {
3737
imageExists, err := ImageExistsLocally(ctx, input.Image, input.Platform)
3838
log.Debugf("Image exists? %v", imageExists)
3939
if err != nil {
40-
return errors.WithMessagef(err, "unable to determine if image already exists for image %q (%s)", input.Image, input.Platform)
40+
return fmt.Errorf("unable to determine if image already exists for image '%s' (%s): %w", input.Image, input.Platform, err)
4141
}
4242

4343
if !imageExists {

0 commit comments

Comments
 (0)