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 authored 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.