Skip to content

Commit

Permalink
Merge pull request #56 from davidkl97/fix/remote-repo-tagging
Browse files Browse the repository at this point in the history
fix(remote-module): clone remote module by referenced tag
  • Loading branch information
itamar-marom authored Jan 18, 2024
2 parents e91c4d1 + 2d02a8a commit 6a40ee6
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 13 deletions.
40 changes: 32 additions & 8 deletions internal/services/drivers/version_control/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package version_control

import (
"fmt"
"github.com/go-git/go-git/v5/plumbing"
"os"
"os/exec"
"strings"
Expand All @@ -16,7 +17,10 @@ import (
const (
FolderPathFormat = "%s/%s"
TempFolderPath = "%s/temp_clone_path/%s"
remoteName = "origin"
GitlabTokenENV = "GITLAB_TOKEN"
GitRefTag = "refs/tags/%s"
GitRefBranch = "refs/remotes/%s/%s"
GitlabUserENV = "GITLAB_USER"
GithubTokenENV = "GITHUB_TOKEN"
GithubUserENV = "GITHUB_USER"
Expand Down Expand Up @@ -74,24 +78,44 @@ func (g *Git) CloneModules(modules map[string]*RemoteModule, modulesSource strin

func (g *Git) clone(moduleData *RemoteModule, directoryPath string, externalGit bool) error {

remoteName := "origin"
if moduleData.Version != "" {
remoteName = moduleData.Version
}

if externalGit {
err := exec.Command("git", "clone", moduleData.Url, directoryPath, "--depth", "1", "-o", remoteName).Run()
args := []string{"clone", moduleData.Url, directoryPath, "--no-tags", "--single-branch", "--depth", "1", "-o", remoteName}
if moduleData.Version != "" {
args = append(args, "--branch", moduleData.Version)
}
err := exec.Command("git", args...).Run()
return err
}
userName, token := g.getGitUserNameAndToken(moduleData.Url)

_, err := git.PlainClone(directoryPath, false, &git.CloneOptions{
cloneOpts := git.CloneOptions{
URL: moduleData.Url,
Auth: &http.BasicAuth{Password: token, Username: userName},
RemoteName: remoteName,
Depth: 1,
})
}

repo, err := git.PlainClone(directoryPath, false, &cloneOpts)

if moduleData.Version != "" {
workTree, err := repo.Worktree()
if err != nil {
return err
}
tagRef := fmt.Sprintf(GitRefTag, moduleData.Version)
g.log.Debugf("searching %s in %s", moduleData.Version, tagRef)
err = workTree.Checkout(&git.CheckoutOptions{
Branch: plumbing.ReferenceName(tagRef),
})
if err != nil {
branchRef := fmt.Sprintf(GitRefBranch, remoteName, moduleData.Version)
g.log.Debugf("version not found in tags ref, searching %s in %s", moduleData.Version, branchRef)
bErr := workTree.Checkout(&git.CheckoutOptions{
Branch: plumbing.ReferenceName(branchRef),
})
return bErr
}
}
return err
}

Expand Down
48 changes: 43 additions & 5 deletions internal/services/drivers/version_control/git_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package version_control_test

import (
"fmt"
"os"
"testing"

Expand All @@ -20,12 +21,19 @@ var ModulesTestPath = map[bool]string{
false: "./int-temp-git-test",
}

var mockBadModules = map[string]*version_control.RemoteModule{
var mockBadUrl = map[string]*version_control.RemoteModule{
TerraCrustModuleName: {
Url: "https://github.com/appsflyer/terra-crust/test/bad",
},
}

var mockBadVersion = map[string]*version_control.RemoteModule{
NamingModuleName: {
Url: "https://github.com/fajrinazis/terraform-aws-resource-naming.git",
Version: "bad-tag",
},
}

func TestCloneAndCleanupInternalGit(t *testing.T) {
CloneAndCleanup(t, false)
}
Expand All @@ -34,14 +42,29 @@ func TestCloneAndCleanupExternalGit(t *testing.T) {
CloneAndCleanup(t, true)
}

func CloneAndCleanupModules(modules map[string]*version_control.RemoteModule, externalGit bool) error {
log := logger.NewSimple()

gitDriver := version_control.InitGitProvider(log)

err := gitDriver.CloneModules(modules, ModulesTestPath[externalGit], externalGit)
cErr := gitDriver.CleanModulesFolders(modules, ModulesTestPath[externalGit])
if err != nil {
if cErr != nil {
return fmt.Errorf("failed to clone and cleanup module %v %v", err, cErr)
}
return err
}
return nil
}
func CloneAndCleanup(t *testing.T, externalGit bool) {
var mockModules = map[string]*version_control.RemoteModule{
TerraCrustModuleName: {
Url: TerraCrustURL,
},
NamingModuleName: {
Url: "https://github.com/fajrinazis/terraform-aws-resource-naming.git",
Version: "v0.23.1",
Version: "v0.3.0",
},
ZonesModuleName: {
Url: "https://github.com/terraform-aws-modules/terraform-aws-route53.git",
Expand Down Expand Up @@ -112,12 +135,27 @@ func TestFailBadUrlExternalGit(t *testing.T) {
FailBadUrl(t, true)
}

func TestFailBadVersionInternalGit(t *testing.T) {
FailBadVersion(t, false)
}

func TestFailBadVersionExternalGit(t *testing.T) {
FailBadVersion(t, true)
}

func FailBadUrl(t *testing.T, externalGit bool) {
t.Parallel()
log := logger.NewSimple()
gitDriver := version_control.InitGitProvider(log)

err := gitDriver.CloneModules(mockBadModules, ModulesTestPath[externalGit], externalGit)
err := CloneAndCleanupModules(mockBadUrl, externalGit)
if err == nil {
t.Errorf("expected error received error nil")
}
}

func FailBadVersion(t *testing.T, externalGit bool) {
t.Parallel()

err := CloneAndCleanupModules(mockBadVersion, externalGit)
if err == nil {
t.Errorf("expected error received error nil")
}
Expand Down

0 comments on commit 6a40ee6

Please sign in to comment.