Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions charts/mccp/templates/clusters-service/configmap.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ data:
INJECT_PRUNE_ANNOTATION: {{ .Values.config.capi.templates.injectPruneAnnotation }}
CAPI_TEMPLATES_REPOSITORY_URL: {{ .Values.config.capi.repositoryURL | quote }}
CAPI_REPOSITORY_PATH: {{ .Values.config.capi.repositoryPath | quote }}
CAPI_TEMPLATES_REPOSITORY_API_URL: {{ .Values.config.capi.repositoryApiURL | quote }}
CAPI_TEMPLATES_REPOSITORY_BASE_BRANCH: {{ .Values.config.capi.baseBranch | quote }}
{{ if not .Values.config.checkpoint.enabled }}
CHECKPOINT_DISABLE: 1
Expand Down
1 change: 1 addition & 0 deletions charts/mccp/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ config:
clusters:
namespace: default
repositoryURL: ''
repositoryApiURL: ''
repositoryPath: ''
baseBranch: main
checkpoint:
Expand Down
4 changes: 4 additions & 0 deletions cmd/clusters-service/api/capi_server.proto
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ message CreatePullRequestRequest {
Credential credentials = 9;
// The values for each profile that will be installed.
repeated ProfileValues values = 10;
// The repo api url.
string repository_api_url = 11;
}
message CreatePullRequestResponse {
// The url of the new pull request.
Expand All @@ -159,6 +161,8 @@ message DeleteClustersPullRequestRequest {
string commit_message = 7;
// Credentials
Credential credentials = 8;
// The repo api url.
string repository_api_url = 9;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just curious, why do we accept it in the request and also send it back in the response?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My mistake, I think we should receive it in request only and if supplied we should ignore creating scm client. Thank you very much for noticing 🙂

}
message DeleteClustersPullRequestResponse {
// The url of the new pull request.
Expand Down
8 changes: 8 additions & 0 deletions cmd/clusters-service/api/capi_server.swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,10 @@
"$ref": "#/definitions/v1ProfileValues"
},
"description": "The values for each profile that will be installed."
},
"repositoryApiUrl": {
"type": "string",
"description": "The repo api url."
}
}
},
Expand Down Expand Up @@ -518,6 +522,10 @@
"credentials": {
"$ref": "#/definitions/v1Credential",
"title": "Credentials"
},
"repositoryApiUrl": {
"type": "string",
"description": "The repo api url."
}
}
},
Expand Down
45 changes: 36 additions & 9 deletions cmd/clusters-service/pkg/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"io/ioutil"
"os"
"time"

"github.com/fluxcd/go-git-providers/github"
Expand All @@ -13,6 +14,7 @@ import (
"github.com/go-logr/logr"
go_git "gopkg.in/src-d/go-git.v4"
"gopkg.in/src-d/go-git.v4/plumbing"
"gopkg.in/src-d/go-git.v4/plumbing/transport"
"gopkg.in/src-d/go-git.v4/plumbing/transport/http"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/util/retry"
Expand Down Expand Up @@ -49,14 +51,15 @@ type GitProvider struct {
}

type WriteFilesToBranchAndCreatePullRequestRequest struct {
GitProvider GitProvider
RepositoryURL string
HeadBranch string
BaseBranch string
Title string
Description string
CommitMessage string
Files []gitprovider.CommitFile
GitProvider GitProvider
RepositoryURL string
ReposistoryAPIURL string
HeadBranch string
BaseBranch string
Title string
Description string
CommitMessage string
Files []gitprovider.CommitFile
}

type WriteFilesToBranchAndCreatePullRequestResponse struct {
Expand All @@ -79,7 +82,12 @@ type CloneRepoToTempDirResponse struct {
// It returns the URL of the pull request.
func (s *GitProviderService) WriteFilesToBranchAndCreatePullRequest(ctx context.Context,
req WriteFilesToBranchAndCreatePullRequestRequest) (*WriteFilesToBranchAndCreatePullRequestResponse, error) {
repo, err := s.GetRepository(ctx, req.GitProvider, req.RepositoryURL)
repoURL, err := GetGitProviderUrl(req.RepositoryURL)
if err != nil {
return nil, fmt.Errorf("unable to get git porivder url: %w", err)
}

repo, err := s.GetRepository(ctx, req.GitProvider, repoURL)
if err != nil {
return nil, fmt.Errorf("unable to get repo: %w", err)
}
Expand Down Expand Up @@ -293,3 +301,22 @@ func getGitProviderClient(gpi GitProvider) (gitprovider.Client, error) {
}
return client, err
}

func GetGitProviderUrl(giturl string) (string, error) {
repositoryAPIURL := os.Getenv("CAPI_TEMPLATES_REPOSITORY_API_URL")
if repositoryAPIURL != "" {
return repositoryAPIURL, nil
}

ep, err := transport.NewEndpoint(giturl)
if err != nil {
return "", err
}
if ep.Protocol == "http" || ep.Protocol == "https" {
return giturl, nil
}

httpsEp := transport.Endpoint{Protocol: "https", Host: ep.Host, Path: ep.Path}

return httpsEp.String(), nil
}
23 changes: 23 additions & 0 deletions cmd/clusters-service/pkg/git/git_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build integration
// +build integration

package git_test
Expand Down Expand Up @@ -231,6 +232,28 @@ func TestCreatePullRequestInGitLab(t *testing.T) {
assert.Equal(t, pr.Description, "Creates a cluster through a CAPI template")
}

func TestGetGitProviderUrl(t *testing.T) {
expected := "https://github.com/user/repo.git"

os.Setenv("CAPI_TEMPLATES_REPOSITORY_API_URL", "https://github.com/user/repo.git")
dummyUrl := "example.com"
repoURL, err := git.GetGitProviderUrl(dummyUrl)
require.NoError(t, err)
assert.Equal(t, expected, repoURL)

os.Unsetenv("CAPI_TEMPLATES_REPOSITORY_API_URL")

gitUrl := "[email protected]:user/repo.git"
repoURL, err = git.GetGitProviderUrl(gitUrl)
require.NoError(t, err)
assert.Equal(t, expected, repoURL)

httpsUrl := "https://github.com/user/repo.git"
repoURL, err = git.GetGitProviderUrl(httpsUrl)
require.NoError(t, err)
assert.Equal(t, expected, repoURL)
}

func findGitHubRepo(repos []*github.Repository, name string) *github.Repository {
if name == "" {
return nil
Expand Down
Loading