Skip to content

Commit

Permalink
Fix .git remote endpoint url (#72)
Browse files Browse the repository at this point in the history
  • Loading branch information
omerzi committed Mar 8, 2023
1 parent d0c93d3 commit 260c3d1
Show file tree
Hide file tree
Showing 8 changed files with 208 additions and 398 deletions.
39 changes: 23 additions & 16 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,43 +4,50 @@ go 1.19

require (
github.com/gfleury/go-bitbucket-v1 v0.0.0-20220418082332-711d7d5e805f
github.com/go-git/go-git/v5 v5.4.2
github.com/go-git/go-git/v5 v5.5.2
github.com/google/go-github/v45 v45.2.0
github.com/google/uuid v1.3.0
github.com/grokify/mogo v0.40.4
github.com/jfrog/gofrog v1.2.5
github.com/ktrysmt/go-bitbucket v0.9.32
github.com/microsoft/azure-devops-go-api/azuredevops v1.0.0-b5
github.com/mitchellh/mapstructure v1.4.3
github.com/stretchr/testify v1.8.0
github.com/mitchellh/mapstructure v1.5.0
github.com/stretchr/testify v1.8.1
github.com/xanzy/go-gitlab v0.52.2
golang.org/x/oauth2 v0.0.0-20211104180415-d3ed0bb246c8
golang.org/x/oauth2 v0.0.0-20221014153046-6fdb5e3db783
)

require (
github.com/Microsoft/go-winio v0.4.16 // indirect
github.com/ProtonMail/go-crypto v0.0.0-20220819155348-b76dceea2ffa // indirect
github.com/Microsoft/go-winio v0.5.2 // indirect
github.com/ProtonMail/go-crypto v0.0.0-20221026131551-cf6655e29de4 // indirect
github.com/acomagu/bufpipe v1.0.3 // indirect
github.com/cloudflare/circl v1.1.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/emirpasic/gods v1.12.0 // indirect
github.com/emirpasic/gods v1.18.1 // indirect
github.com/fatih/color v1.13.0 // indirect
github.com/go-git/gcfg v1.5.0 // indirect
github.com/go-git/go-billy/v5 v5.3.1 // indirect
github.com/golang/protobuf v1.5.0 // indirect
github.com/go-git/go-billy/v5 v5.4.0 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/go-querystring v1.1.0 // indirect
github.com/hashicorp/go-cleanhttp v0.5.1 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
github.com/hashicorp/go-hclog v1.2.0 // indirect
github.com/hashicorp/go-retryablehttp v0.6.8 // indirect
github.com/imdario/mergo v0.3.12 // indirect
github.com/imdario/mergo v0.3.13 // indirect
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
github.com/kevinburke/ssh_config v0.0.0-20201106050909-4977a11b4351 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/kevinburke/ssh_config v1.2.0 // indirect
github.com/kr/pretty v0.3.0 // indirect
github.com/mattn/go-colorable v0.1.12 // indirect
github.com/mattn/go-isatty v0.0.16 // indirect
github.com/pjbgf/sha1cd v0.2.3 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rogpeppe/go-internal v1.8.1 // indirect
github.com/sergi/go-diff v1.1.0 // indirect
github.com/xanzy/ssh-agent v0.3.0 // indirect
golang.org/x/crypto v0.0.0-20220817201139-bc19a97f63c8 // indirect
github.com/skeema/knownhosts v1.1.0 // indirect
github.com/xanzy/ssh-agent v0.3.3 // indirect
golang.org/x/crypto v0.6.0 // indirect
golang.org/x/net v0.7.0 // indirect
golang.org/x/sys v0.5.0 // indirect
golang.org/x/time v0.0.0-20191024005414-555d28b269f0 // indirect
golang.org/x/time v0.1.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/protobuf v1.28.1 // indirect
gopkg.in/warnings.v0 v0.1.2 // indirect
Expand Down
471 changes: 101 additions & 370 deletions go.sum

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion vcsclient/azurerepos.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,9 @@ func (client *AzureReposClient) DownloadRepository(ctx context.Context, owner, r
}
client.logger.Info("extracted repository successfully")
// Generate .git folder with remote details
return vcsutils.CreateDotGitFolderWithRemote(localPath, "origin",
return vcsutils.CreateDotGitFolderWithRemote(
localPath,
vcsutils.RemoteName,
fmt.Sprintf("https://%s@%s/%s/_git/%s", owner, strings.TrimPrefix(client.connectionDetails.BaseUrl, "https://"), client.vcsInfo.Project, repository))
}

Expand Down
6 changes: 4 additions & 2 deletions vcsclient/bitbucketserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,8 +278,10 @@ func (client *BitbucketServerClient) DownloadRepository(ctx context.Context, own
}
client.logger.Info("extracted repository successfully")
// Generate .git folder with remote details
return vcsutils.CreateDotGitFolderWithRemote(localPath, "origin",
fmt.Sprintf("%s/scm/%s/%s.git", strings.TrimSuffix(client.vcsInfo.APIEndpoint, "/rest"), owner, repository))
return vcsutils.CreateDotGitFolderWithRemote(
localPath,
vcsutils.RemoteName,
vcsutils.GetGenericGitRemoteUrl(fmt.Sprintf("%s/scm", strings.TrimSuffix(client.vcsInfo.APIEndpoint, "/rest")), owner, repository))
}

// CreatePullRequest on Bitbucket server
Expand Down
3 changes: 1 addition & 2 deletions vcsclient/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,8 +223,7 @@ func (client *GitHubClient) DownloadRepository(ctx context.Context, owner, repos
return err
}
client.logger.Info("extracted repository successfully")
return vcsutils.CreateDotGitFolderWithRemote(localPath, "origin",
fmt.Sprintf("https://github.com/%s/%s.git", owner, repository))
return vcsutils.CreateDotGitFolderWithRemote(localPath, vcsutils.RemoteName, vcsutils.GetGenericGitRemoteUrl(client.vcsInfo.APIEndpoint, owner, repository))
}

// CreatePullRequest on GitHub
Expand Down
5 changes: 3 additions & 2 deletions vcsclient/gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
// GitLabClient API version 4
type GitLabClient struct {
glClient *gitlab.Client
vcsInfo VcsInfo
logger Log
}

Expand All @@ -37,6 +38,7 @@ func NewGitLabClient(vcsInfo VcsInfo, logger Log) (*GitLabClient, error) {

return &GitLabClient{
glClient: client,
vcsInfo: vcsInfo,
logger: logger,
}, nil
}
Expand Down Expand Up @@ -193,8 +195,7 @@ func (client *GitLabClient) DownloadRepository(ctx context.Context, owner, repos
return err
}
client.logger.Info("extracted repository successfully")
return vcsutils.CreateDotGitFolderWithRemote(localPath, "origin",
fmt.Sprintf("https://gitlab.com/%s/%s.git", owner, repository))
return vcsutils.CreateDotGitFolderWithRemote(localPath, vcsutils.RemoteName, vcsutils.GetGenericGitRemoteUrl(client.vcsInfo.APIEndpoint, owner, repository))
}

// CreatePullRequest on GitLab
Expand Down
6 changes: 6 additions & 0 deletions vcsutils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import (
"strings"
)

const RemoteName = "origin"

// CreateToken create a random UUID
func CreateToken() string {
return uuid.New().String()
Expand Down Expand Up @@ -289,3 +291,7 @@ func CreateDotGitFolderWithRemote(path, remoteName, remoteUrl string) error {
})
return err
}

func GetGenericGitRemoteUrl(apiEndpoint, owner, repo string) string {
return fmt.Sprintf("%s/%s/%s.git", strings.TrimSuffix(apiEndpoint, "/"), owner, repo)
}
72 changes: 67 additions & 5 deletions vcsutils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ import (

func TestUntar(t *testing.T) {
destDir, tarball := openTarball(t)
defer tarball.Close()
defer func() {
assert.NoError(t, tarball.Close())
}()

err := Untar(destDir, tarball, false)
assert.NoError(t, err)
Expand All @@ -27,7 +29,9 @@ func TestUntar(t *testing.T) {

func TestUntarRemoveBaseDir(t *testing.T) {
destDir, tarball := openTarball(t)
defer tarball.Close()
defer func() {
assert.NoError(t, tarball.Close())
}()

err := Untar(destDir, tarball, true)
assert.NoError(t, err)
Expand Down Expand Up @@ -100,7 +104,9 @@ func TestDiscardResponseBody(t *testing.T) {
func openTarball(t *testing.T) (string, *os.File) {
dir, err := os.MkdirTemp("", "")
assert.NoError(t, err)
defer os.RemoveAll(dir)
defer func() {
assert.NoError(t, os.RemoveAll(dir))
}()

tarball, err := os.Open(filepath.Join("testdata", "a.tar.gz"))
assert.NoError(t, err)
Expand Down Expand Up @@ -173,7 +179,9 @@ func TestCheckResponseStatusWithBody(t *testing.T) {
func TestCreateDotGitFolderWithRemote(t *testing.T) {
dir1, err := os.MkdirTemp("", "tmp")
assert.NoError(t, err)
defer os.RemoveAll(dir1)
defer func() {
assert.NoError(t, os.RemoveAll(dir1))
}()
err = CreateDotGitFolderWithRemote(dir1, "origin", "fakeurl")
assert.NoError(t, err)
repo, err := git.PlainOpen(filepath.Join(dir1, ".git"))
Expand All @@ -190,11 +198,65 @@ func TestCreateDotGitFolderWithoutRemote(t *testing.T) {
// Return error if remote name is empty
dir2, err := os.MkdirTemp("", "tmp")
assert.NoError(t, err)
defer os.RemoveAll(dir2)
defer func() {
assert.NoError(t, os.RemoveAll(dir2))
}()
assert.Error(t, CreateDotGitFolderWithRemote(dir2, "", "fakeurl"))
}

func TestPointerOf(t *testing.T) {
require.Equal(t, 5, *PointerOf(5))
require.Equal(t, "some", *PointerOf("some"))
}

func TestGetGenericGitRemoteUrl(t *testing.T) {
testCases := []struct {
name string
apiEndpoint string
owner string
repo string
expectedResult string
}{
{
name: "Bitbucket Server",
apiEndpoint: "https://bitbucket.example.com/scm",
owner: "my-org",
repo: "my-repo",
expectedResult: "https://bitbucket.example.com/scm/my-org/my-repo.git",
},
{
name: "GitHub Cloud",
apiEndpoint: "https://api.github.com",
owner: "my-org",
repo: "my-repo",
expectedResult: "https://api.github.com/my-org/my-repo.git",
},
{
name: "GitHub On-Premises",
apiEndpoint: "https://github.example.com/api/v3",
owner: "my-org",
repo: "my-repo",
expectedResult: "https://github.example.com/api/v3/my-org/my-repo.git",
},
{
name: "GitLab",
apiEndpoint: "https://gitlab.com/",
owner: "my-org",
repo: "my-repo",
expectedResult: "https://gitlab.com/my-org/my-repo.git",
},
{
name: "GitLab On-Premises",
apiEndpoint: "https://gitlab.example.com/api/v4",
owner: "my-org",
repo: "my-repo",
expectedResult: "https://gitlab.example.com/api/v4/my-org/my-repo.git",
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
assert.Equal(t, tc.expectedResult, GetGenericGitRemoteUrl(tc.apiEndpoint, tc.owner, tc.repo))
})
}
}

0 comments on commit 260c3d1

Please sign in to comment.