Remove local action cache if remote has changed (#2284)
* fix: remove local cache if remote is changed * test: TestCloneIfRequired
This commit is contained in:
@@ -230,6 +230,15 @@ type NewGitCloneExecutorInput struct {
|
|||||||
|
|
||||||
// CloneIfRequired ...
|
// CloneIfRequired ...
|
||||||
func CloneIfRequired(ctx context.Context, refName plumbing.ReferenceName, input NewGitCloneExecutorInput, logger log.FieldLogger) (*git.Repository, error) {
|
func CloneIfRequired(ctx context.Context, refName plumbing.ReferenceName, input NewGitCloneExecutorInput, logger log.FieldLogger) (*git.Repository, error) {
|
||||||
|
// If the remote URL has changed, remove the directory and clone again.
|
||||||
|
if r, err := git.PlainOpen(input.Dir); err == nil {
|
||||||
|
if remote, err := r.Remote("origin"); err == nil {
|
||||||
|
if len(remote.Config().URLs) > 0 && remote.Config().URLs[0] != input.URL {
|
||||||
|
_ = os.RemoveAll(input.Dir)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
r, err := git.PlainOpen(input.Dir)
|
r, err := git.PlainOpen(input.Dir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
var progressWriter io.Writer
|
var progressWriter io.Writer
|
||||||
|
|||||||
@@ -12,6 +12,8 @@ import (
|
|||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
|
|
||||||
|
"github.com/nektos/act/pkg/common"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestFindGitSlug(t *testing.T) {
|
func TestFindGitSlug(t *testing.T) {
|
||||||
@@ -247,3 +249,31 @@ func gitCmd(args ...string) error {
|
|||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestCloneIfRequired(t *testing.T) {
|
||||||
|
tempDir := t.TempDir()
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
|
t.Run("clone", func(t *testing.T) {
|
||||||
|
repo, err := CloneIfRequired(ctx, "refs/heads/main", NewGitCloneExecutorInput{
|
||||||
|
URL: "https://github.com/actions/checkout",
|
||||||
|
Dir: tempDir,
|
||||||
|
}, common.Logger(ctx))
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.NotNil(t, repo)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("clone different remote", func(t *testing.T) {
|
||||||
|
repo, err := CloneIfRequired(ctx, "refs/heads/main", NewGitCloneExecutorInput{
|
||||||
|
URL: "https://github.com/actions/setup-go",
|
||||||
|
Dir: tempDir,
|
||||||
|
}, common.Logger(ctx))
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.NotNil(t, repo)
|
||||||
|
|
||||||
|
remote, err := repo.Remote("origin")
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.Len(t, remote.Config().URLs, 1)
|
||||||
|
assert.Equal(t, "https://github.com/actions/setup-go", remote.Config().URLs[0])
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user