-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
feat: Add new Action Cache #1913
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
ac7392b
feat: Add new Action Cache
ChristopherHX b8f01b9
fix some linter errors / warnings
ChristopherHX 64acef3
fix lint
ChristopherHX 1570bea
fix empty fpath parameter returns empty archive
ChristopherHX c4461bb
Merge branch 'master' into action_cache
ChristopherHX 3b5a781
rename fpath to includePrefix
ChristopherHX 8df06d5
Merge branch 'master' into action_cache
ChristopherHX File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,177 @@ | ||
| package runner | ||
|
|
||
| import ( | ||
| "archive/tar" | ||
| "context" | ||
| "crypto/rand" | ||
| "encoding/hex" | ||
| "errors" | ||
| "io" | ||
| "io/fs" | ||
| "path" | ||
| "strings" | ||
|
|
||
| git "github.com/go-git/go-git/v5" | ||
| config "github.com/go-git/go-git/v5/config" | ||
| "github.com/go-git/go-git/v5/plumbing" | ||
| "github.com/go-git/go-git/v5/plumbing/object" | ||
| "github.com/go-git/go-git/v5/plumbing/transport" | ||
| "github.com/go-git/go-git/v5/plumbing/transport/http" | ||
| ) | ||
|
|
||
| type ActionCache interface { | ||
| Fetch(ctx context.Context, cacheDir, url, ref, token string) (string, error) | ||
| GetTarArchive(ctx context.Context, cacheDir, sha, includePrefix string) (io.ReadCloser, error) | ||
| } | ||
|
|
||
| type GoGitActionCache struct { | ||
| Path string | ||
| } | ||
|
|
||
| func (c GoGitActionCache) Fetch(ctx context.Context, cacheDir, url, ref, token string) (string, error) { | ||
| gitPath := path.Join(c.Path, safeFilename(cacheDir)+".git") | ||
| gogitrepo, err := git.PlainInit(gitPath, true) | ||
| if errors.Is(err, git.ErrRepositoryAlreadyExists) { | ||
| gogitrepo, err = git.PlainOpen(gitPath) | ||
| } | ||
| if err != nil { | ||
| return "", err | ||
| } | ||
| tmpBranch := make([]byte, 12) | ||
| if _, err := rand.Read(tmpBranch); err != nil { | ||
| 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{ | ||
| Username: "token", | ||
| Password: token, | ||
| } | ||
| } | ||
| remote, err := gogitrepo.CreateRemoteAnonymous(&config.RemoteConfig{ | ||
| Name: "anonymous", | ||
| URLs: []string{ | ||
| url, | ||
| }, | ||
| }) | ||
| if err != nil { | ||
| 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 | ||
| }) | ||
| } | ||
| }() | ||
| if err := remote.FetchContext(ctx, &git.FetchOptions{ | ||
| RefSpecs: []config.RefSpec{ | ||
| refSpec, | ||
| }, | ||
| Auth: auth, | ||
| Force: true, | ||
| }); err != nil { | ||
| 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 | ||
| } | ||
| return hash.String(), nil | ||
| } | ||
|
|
||
| func (c GoGitActionCache) GetTarArchive(ctx context.Context, cacheDir, sha, includePrefix string) (io.ReadCloser, error) { | ||
| gitPath := path.Join(c.Path, safeFilename(cacheDir)+".git") | ||
| gogitrepo, err := git.PlainOpen(gitPath) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| commit, err := gogitrepo.CommitObject(plumbing.NewHash(sha)) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| files, err := commit.Files() | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| rpipe, wpipe := io.Pipe() | ||
cplee marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // Interrupt io.Copy using ctx | ||
| ch := make(chan int, 1) | ||
| go func() { | ||
| select { | ||
| case <-ctx.Done(): | ||
| wpipe.CloseWithError(ctx.Err()) | ||
| case <-ch: | ||
| } | ||
| }() | ||
| go func() { | ||
| defer wpipe.Close() | ||
| defer close(ch) | ||
| tw := tar.NewWriter(wpipe) | ||
| cleanIncludePrefix := path.Clean(includePrefix) | ||
| wpipe.CloseWithError(files.ForEach(func(f *object.File) error { | ||
| if err := ctx.Err(); err != nil { | ||
| return err | ||
| } | ||
| name := f.Name | ||
| if strings.HasPrefix(name, cleanIncludePrefix+"/") { | ||
| name = name[len(cleanIncludePrefix)+1:] | ||
| } else if cleanIncludePrefix != "." && name != cleanIncludePrefix { | ||
| return nil | ||
| } | ||
| fmode, err := f.Mode.ToOSFileMode() | ||
| if err != nil { | ||
| return err | ||
| } | ||
| if fmode&fs.ModeSymlink == fs.ModeSymlink { | ||
| content, err := f.Contents() | ||
| if err != nil { | ||
| return err | ||
| } | ||
| return tw.WriteHeader(&tar.Header{ | ||
| Name: name, | ||
| Mode: int64(fmode), | ||
| Linkname: content, | ||
| }) | ||
| } | ||
| err = tw.WriteHeader(&tar.Header{ | ||
| Name: name, | ||
| Mode: int64(fmode), | ||
| Size: f.Size, | ||
| }) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| reader, err := f.Reader() | ||
| if err != nil { | ||
| return err | ||
| } | ||
| _, err = io.Copy(tw, reader) | ||
| return err | ||
| })) | ||
| }() | ||
| return rpipe, err | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| package runner | ||
|
|
||
| import ( | ||
| "archive/tar" | ||
| "bytes" | ||
| "context" | ||
| "io" | ||
| "os" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| ) | ||
|
|
||
| //nolint:gosec | ||
| func TestActionCache(t *testing.T) { | ||
| a := assert.New(t) | ||
| cache := &GoGitActionCache{ | ||
| 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) | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wow tests need to be secured against DoS, what on earth |
||
| a.NoError(err) | ||
| str := buf.String() | ||
| a.NotEmpty(str) | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.