Skip to content

Commit

Permalink
Fix GitHub cloud remote URLs (#83)
Browse files Browse the repository at this point in the history
  • Loading branch information
EyalDelarea committed May 11, 2023
1 parent cd07b84 commit 03d12f4
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 15 deletions.
14 changes: 13 additions & 1 deletion vcsclient/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ import (
"golang.org/x/oauth2"
)

const (
GitHubCloudApiEndpoint = "https://api.github.com"
GitHubCloneUrl = "https://github.com"
)

// GitHubClient API version 3
type GitHubClient struct {
vcsInfo VcsInfo
Expand Down Expand Up @@ -246,7 +251,7 @@ func (client *GitHubClient) DownloadRepository(ctx context.Context, owner, repos
}

client.logger.Info(successfulRepoExtraction)
return vcsutils.CreateDotGitFolderWithRemote(localPath, vcsutils.RemoteName, vcsutils.GetGenericGitRemoteUrl(client.vcsInfo.APIEndpoint, owner, repository))
return vcsutils.CreateDotGitFolderWithRemote(localPath, vcsutils.RemoteName, getGitHubGitRemoteUrl(client, owner, repository))
}

// CreatePullRequest on GitHub
Expand Down Expand Up @@ -751,6 +756,13 @@ func packScanningResult(data string) (string, error) {
return compressedScan, err
}

func getGitHubGitRemoteUrl(client *GitHubClient, owner, repo string) string {
if client.vcsInfo.APIEndpoint == GitHubCloudApiEndpoint {
return fmt.Sprintf("%s/%s/%s.git", GitHubCloneUrl, owner, repo)
}
return fmt.Sprintf("%s/%s/%s.git", client.vcsInfo.APIEndpoint, owner, repo)
}

type repositoryEnvironmentReviewer struct {
Login string `mapstructure:"login"`
}
33 changes: 33 additions & 0 deletions vcsclient/github_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -732,6 +732,39 @@ func TestGitHubClient_TestGetCommitStatus(t *testing.T) {
})
}

func TestGitHubClient_getGitHubGitRemoteUrl(t *testing.T) {
testCases := []struct {
name string
apiEndpoint string
owner string
repo string
expectedResult string
}{
{
name: "GitHub Cloud",
apiEndpoint: "https://api.github.com",
owner: "my-org",
repo: "my-repo",
expectedResult: "https://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",
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
info := VcsInfo{APIEndpoint: tc.apiEndpoint}
client, err := NewGitHubClient(info, nil)
assert.NoError(t, err)
assert.Equal(t, tc.expectedResult, getGitHubGitRemoteUrl(client, tc.owner, tc.repo))
})
}
}

func createBadGitHubClient(t *testing.T) VcsClient {
client, err := NewClientBuilder(vcsutils.GitHub).ApiEndpoint("https://bad^endpoint").Build()
require.NoError(t, err)
Expand Down
14 changes: 0 additions & 14 deletions vcsutils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,20 +225,6 @@ func TestGetGenericGitRemoteUrl(t *testing.T) {
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/",
Expand Down

0 comments on commit 03d12f4

Please sign in to comment.