From f3706ee3ea61720eccddd7df4f2dabcb67b15b79 Mon Sep 17 00:00:00 2001 From: ChristopherHX Date: Sun, 18 Feb 2024 04:53:22 +0100 Subject: [PATCH] refactor: simpilfy go-git cache (#2208) --- act/runner/action_cache.go | 35 ++-------------- act/runner/action_cache_test.go | 72 +++++++++++++++++++++++++-------- 2 files changed, 59 insertions(+), 48 deletions(-) diff --git a/act/runner/action_cache.go b/act/runner/action_cache.go index da4e651c..1099d78f 100644 --- a/act/runner/action_cache.go +++ b/act/runner/action_cache.go @@ -6,7 +6,6 @@ import ( "crypto/rand" "encoding/hex" "errors" - "fmt" "io" "io/fs" "path" @@ -43,17 +42,7 @@ func (c GoGitActionCache) Fetch(ctx context.Context, cacheDir, url, ref, token s return "", err } branchName := hex.EncodeToString(tmpBranch) - var refSpec config.RefSpec - spec := config.RefSpec(ref + ":" + branchName) - tagOrSha := false - if spec.IsExactSHA1() { - refSpec = spec - } else if strings.HasPrefix(ref, "refs/") { - refSpec = config.RefSpec(ref + ":refs/heads/" + branchName) - } else { - tagOrSha = true - refSpec = config.RefSpec("refs/*/" + ref + ":refs/heads/*/" + branchName) - } + var auth transport.AuthMethod if token != "" { auth = &http.BasicAuth{ @@ -71,35 +60,17 @@ func (c GoGitActionCache) Fetch(ctx context.Context, cacheDir, url, ref, token s return "", err } defer func() { - if refs, err := gogitrepo.References(); err == nil { - _ = refs.ForEach(func(r *plumbing.Reference) error { - if strings.Contains(r.Name().String(), branchName) { - return gogitrepo.DeleteBranch(r.Name().String()) - } - return nil - }) - } + _ = gogitrepo.DeleteBranch(branchName) }() if err := remote.FetchContext(ctx, &git.FetchOptions{ RefSpecs: []config.RefSpec{ - refSpec, + config.RefSpec(ref + ":" + branchName), }, Auth: auth, Force: true, }); err != nil { - if tagOrSha && errors.Is(err, git.NoErrAlreadyUpToDate) { - return "", fmt.Errorf("couldn't find remote ref \"%s\"", ref) - } return "", err } - if tagOrSha { - for _, prefix := range []string{"refs/heads/tags/", "refs/heads/heads/"} { - hash, err := gogitrepo.ResolveRevision(plumbing.Revision(prefix + branchName)) - if err == nil { - return hash.String(), nil - } - } - } hash, err := gogitrepo.ResolveRevision(plumbing.Revision(branchName)) if err != nil { return "", err diff --git a/act/runner/action_cache_test.go b/act/runner/action_cache_test.go index e222cfb9..58fac5bd 100644 --- a/act/runner/action_cache_test.go +++ b/act/runner/action_cache_test.go @@ -18,20 +18,60 @@ func TestActionCache(t *testing.T) { Path: os.TempDir(), } ctx := context.Background() - sha, err := cache.Fetch(ctx, "christopherhx/script", "https://github.com/christopherhx/script", "main", "") - a.NoError(err) - a.NotEmpty(sha) - atar, err := cache.GetTarArchive(ctx, "christopherhx/script", sha, "node_modules") - a.NoError(err) - a.NotEmpty(atar) - mytar := tar.NewReader(atar) - th, err := mytar.Next() - a.NoError(err) - a.NotEqual(0, th.Size) - buf := &bytes.Buffer{} - // G110: Potential DoS vulnerability via decompression bomb (gosec) - _, err = io.Copy(buf, mytar) - a.NoError(err) - str := buf.String() - a.NotEmpty(str) + cacheDir := "nektos/act-test-actions" + repo := "https://github.com/nektos/act-test-actions" + refs := []struct { + Name string + CacheDir string + Repo string + Ref string + }{ + { + Name: "Fetch Branch Name", + CacheDir: cacheDir, + Repo: repo, + Ref: "main", + }, + { + Name: "Fetch Branch Name Absolutely", + CacheDir: cacheDir, + Repo: repo, + Ref: "refs/heads/main", + }, + { + Name: "Fetch HEAD", + CacheDir: cacheDir, + Repo: repo, + Ref: "HEAD", + }, + { + Name: "Fetch Sha", + CacheDir: cacheDir, + Repo: repo, + Ref: "de984ca37e4df4cb9fd9256435a3b82c4a2662b1", + }, + } + for _, c := range refs { + t.Run(c.Name, func(t *testing.T) { + sha, err := cache.Fetch(ctx, c.CacheDir, c.Repo, c.Ref, "") + if !a.NoError(err) || !a.NotEmpty(sha) { + return + } + atar, err := cache.GetTarArchive(ctx, c.CacheDir, sha, "js") + if !a.NoError(err) || !a.NotEmpty(atar) { + return + } + mytar := tar.NewReader(atar) + th, err := mytar.Next() + if !a.NoError(err) || !a.NotEqual(0, th.Size) { + return + } + buf := &bytes.Buffer{} + // G110: Potential DoS vulnerability via decompression bomb (gosec) + _, err = io.Copy(buf, mytar) + a.NoError(err) + str := buf.String() + a.NotEmpty(str) + }) + } }