Skip to content

Commit

Permalink
use simple path for cloning in adhoc flow (#737)
Browse files Browse the repository at this point in the history
smonero authored Apr 5, 2024

Unverified

This commit is not signed, but one or more authors requires that any commit attributed to them is signed.
1 parent 3a231b9 commit bcf01ec
Showing 2 changed files with 54 additions and 0 deletions.
11 changes: 11 additions & 0 deletions server/vcs/provider/github/repo_fetcher.go
Original file line number Diff line number Diff line change
@@ -37,6 +37,8 @@ type RepoFetcher struct {

type RepoFetcherOptions struct {
CloneDepth int
// Use simple path for adhoc mode, where there is only 1 repo so we can use a simpler path rather than one with UUID and repos prefix
SimplePath bool
}

func (g *RepoFetcher) Fetch(ctx context.Context, repo models.Repo, branch string, sha string, options RepoFetcherOptions) (string, func(ctx context.Context, filePath string), error) {
@@ -71,6 +73,11 @@ func (g *RepoFetcher) Fetch(ctx context.Context, repo models.Repo, branch string

func (g *RepoFetcher) clone(ctx context.Context, repo models.Repo, branch string, sha string, options RepoFetcherOptions) (string, func(ctx context.Context, filePath string), error) {
destinationPath := g.generateDirPath(repo.Name)
// If simple path is enabled, we don't need a prefix and UUID
if options.SimplePath {
destinationPath = g.generateSimpleDirPath(repo.Name)
}

// Create the directory and parents if necessary.
if err := os.MkdirAll(destinationPath, 0700); err != nil {
return "", nil, errors.Wrap(err, "creating new directory")
@@ -112,6 +119,10 @@ func (g *RepoFetcher) generateDirPath(repoName string) string {
return filepath.Join(g.DataDir, workingDirPrefix, repoName, uuid.New().String())
}

func (g *RepoFetcher) generateSimpleDirPath(repoName string) string {
return filepath.Join(g.DataDir, repoName)
}

func (g *RepoFetcher) run(ctx context.Context, args []string, destinationPath string) ([]byte, error) {
cmd := subprocess_exec.Command(g.Logger, args[0], args[1:]...) // nolint: gosec
cmd.Dir = destinationPath
43 changes: 43 additions & 0 deletions server/vcs/provider/github/repo_fetcher_test.go
Original file line number Diff line number Diff line change
@@ -290,6 +290,48 @@ func TestFetch_ErrorGettingGHToken(t *testing.T) {
assert.ErrorContains(t, err, "error")
}

func TestDirPaths(t *testing.T) {
// Initialize the git repo.
repoDir, cleanupRepo := initRepo(t)
defer cleanupRepo()
sha1 := appendCommit(t, repoDir, ".gitkeep", "initial commit")
_ = runCmd(t, repoDir, "git", "rev-parse", "HEAD")
sha2 := appendCommit(t, repoDir, ".gitignore", "second commit")
expCommit2 := runCmd(t, repoDir, "git", "rev-parse", "HEAD")

dataDir, cleanupDataDir := tempDir(t)
defer cleanupDataDir()
defer disableSSLVerification()()
testServer, err := fixtures.GithubAppTestServer(t)
assert.NoError(t, err)
logger := logging.NewNoopCtxLogger(t)
fetcher := &github.RepoFetcher{
DataDir: dataDir,
GithubHostname: testServer,
Logger: logger,
GithubCredentials: &testTokenGetter{},
Scope: tally.NewTestScope("test", map[string]string{}),
}
repo := newBaseRepo(repoDir)
optionsSimplePathTrue := github.RepoFetcherOptions{
SimplePath: true,
}
destinationPath, _, err := fetcher.Fetch(context.Background(), repo, repo.DefaultBranch, sha2, optionsSimplePathTrue)
assert.NoError(t, err)

// Use rev-parse to verify at correct commit.
actCommit := runCmd(t, destinationPath, "git", "rev-parse", "HEAD")
assert.Equal(t, expCommit2, actCommit)

cpCmd := exec.Command("git", "checkout", sha1)
cpCmd.Dir = destinationPath
_, err = cpCmd.CombinedOutput()
assert.NoError(t, err)

// make sure the simple file path is correct
assert.Equal(t, fmt.Sprintf("%s/%s", dataDir, "repo"), destinationPath)
}

func newBaseRepo(repoDir string) models.Repo {
return models.Repo{
VCSHost: models.VCSHost{
@@ -298,6 +340,7 @@ func newBaseRepo(repoDir string) models.Repo {
FullName: "nish/repo",
DefaultBranch: "branch",
CloneURL: fmt.Sprintf("file://%s", repoDir),
Name: "repo",
}
}

0 comments on commit bcf01ec

Please sign in to comment.