summaryrefslogtreecommitdiffstats
path: root/pkg/runner/action_cache.go
blob: da4e651ce624ca5647129878e2d23efb707f48d6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package runner

import (
	"archive/tar"
	"context"
	"crypto/rand"
	"encoding/hex"
	"errors"
	"fmt"
	"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 {
		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
	}
	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()
	// 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
}