Skip to content

Commit 7ae160a

Browse files
authored
GitHub enterprise giops (#853)
* enterprise initial support * added logger * git host corrected * logged host * github testcase * gitops enterprise logic corrected * tls config correct * github host * gitops provider dependency fix * giops config path correction * info logs * host url parsing * removed unused logs * extracted out common method for github org url
1 parent 3558c31 commit 7ae160a

File tree

4 files changed

+101
-13
lines changed

4 files changed

+101
-13
lines changed

internal/util/GitService.go

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ import (
2121
"context"
2222
"fmt"
2323
"io/ioutil"
24+
http2 "net/http"
2425
"net/url"
26+
"path"
2527
"path/filepath"
2628
"strconv"
2729
"time"
@@ -50,6 +52,8 @@ const (
5052
GITHUB_PROVIDER = "GITHUB"
5153
AZURE_DEVOPS_PROVIDER = "AZURE_DEVOPS"
5254
BITBUCKET_PROVIDER = "BITBUCKET_CLOUD"
55+
GITHUB_API_V3 = "api/v3"
56+
GITHUB_HOST = "github.com"
5357
)
5458

5559
type GitClient interface {
@@ -72,7 +76,7 @@ type DetailedErrorGitOpsConfigActions struct {
7276
SuccessfulStages []string `json:"successfulStages"`
7377
StageErrorMap map[string]error `json:"stageErrorMap"`
7478
ValidatedOn time.Time `json:"validatedOn"`
75-
DeleteRepoFailed bool `json:"deleteRepoFailed"`
79+
DeleteRepoFailed bool `json:"deleteRepoFailed"`
7680
}
7781

7882
func (factory *GitFactory) Reload() error {
@@ -218,8 +222,8 @@ func NewGitOpsClient(config *GitConfig, logger *zap.SugaredLogger, gitService Gi
218222
gitLabClient, err := NewGitLabClient(config, logger, gitService)
219223
return gitLabClient, err
220224
} else if config.GitProvider == GITHUB_PROVIDER {
221-
gitHubClient := NewGithubClient(config.GitToken, config.GithubOrganization, logger, gitService)
222-
return gitHubClient, nil
225+
gitHubClient, err := NewGithubClient(config.GitHost, config.GitToken, config.GithubOrganization, logger, gitService)
226+
return gitHubClient, err
223227
} else if config.GitProvider == AZURE_DEVOPS_PROVIDER {
224228
gitAzureClient, err := NewGitAzureClient(config.AzureToken, config.GitHost, config.AzureProject, logger, gitService)
225229
return gitAzureClient, err
@@ -618,14 +622,32 @@ type GitHubClient struct {
618622
gitService GitService
619623
}
620624

621-
func NewGithubClient(token string, org string, logger *zap.SugaredLogger, gitService GitService) GitHubClient {
625+
func NewGithubClient(host string, token string, org string, logger *zap.SugaredLogger, gitService GitService) (GitHubClient, error) {
622626
ctx := context.Background()
627+
httpTransport := &http2.Transport{}
628+
httpClient := &http2.Client{Transport: httpTransport}
623629
ts := oauth2.StaticTokenSource(
624630
&oauth2.Token{AccessToken: token},
625631
)
632+
ctx = context.WithValue(ctx, oauth2.HTTPClient, httpClient)
633+
626634
tc := oauth2.NewClient(ctx, ts)
627-
client := github.NewClient(tc)
628-
return GitHubClient{client: client, org: org, logger: logger, gitService: gitService}
635+
var client *github.Client
636+
var err error
637+
hostUrl, err := url.Parse(host)
638+
if err != nil {
639+
logger.Errorw("error in creating git client ", "host", hostUrl, "err", err)
640+
return GitHubClient{}, err
641+
}
642+
if hostUrl.Host == GITHUB_HOST {
643+
client = github.NewClient(tc)
644+
} else {
645+
logger.Infow("creating github EnterpriseClient with org", "host", host, "org", org)
646+
hostUrl.Path = path.Join(hostUrl.Path, GITHUB_API_V3)
647+
client, err = github.NewEnterpriseClient(hostUrl.String(), hostUrl.String(), tc)
648+
}
649+
650+
return GitHubClient{client: client, org: org, logger: logger, gitService: gitService}, err
629651
}
630652
func (impl GitHubClient) DeleteRepository(name, userName, gitHubOrgName, azureProjectName string, repoOptions *bitbucket.RepositoryOptions) error {
631653
_, err := impl.client.Repositories.Delete(context.Background(), gitHubOrgName, name)

internal/util/GitService_test.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package util
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func getTestGithubClient() GitHubClient {
8+
logger:=NewSugardLogger()
9+
gitCliUtl:=NewGitCliUtil(logger)
10+
gitService:=NewGitServiceImpl(&GitConfig{GitToken: "", GitUserName: "nishant"},logger,gitCliUtl)
11+
12+
githubClient, err := NewGithubClient("", "", "test-org",logger , gitService)
13+
if err != nil {
14+
panic(err)
15+
}
16+
return githubClient
17+
}
18+
19+
func TestGitHubClient_CreateRepository(t *testing.T) {
20+
21+
type args struct {
22+
name string
23+
description string
24+
bitbucketWorkspaceId string
25+
bitbucketProjectKey string
26+
}
27+
tests := []struct {
28+
name string
29+
args args
30+
wantIsNew bool
31+
}{{"test_create", args{
32+
name: "testn3",
33+
description: "desc2",
34+
bitbucketWorkspaceId: "",
35+
bitbucketProjectKey: "",
36+
}, true}} // TODO: Add test cases.
37+
38+
for _, tt := range tests {
39+
t.Run(tt.name, func(t *testing.T) {
40+
impl := getTestGithubClient()
41+
_, gotIsNew, _ := impl.CreateRepository(tt.args.name, tt.args.description, tt.args.bitbucketWorkspaceId, tt.args.bitbucketProjectKey)
42+
43+
if gotIsNew != tt.wantIsNew {
44+
t.Errorf("CreateRepository() gotIsNew = %v, want %v", gotIsNew, tt.wantIsNew)
45+
}
46+
47+
})
48+
}
49+
}

pkg/gitops/GitOpsConfigService.go

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ import (
2222
"fmt"
2323
"math/rand"
2424
"net/http"
25+
"net/url"
2526
"os"
27+
"path"
2628
"strconv"
2729
"strings"
2830
"time"
@@ -69,7 +71,6 @@ const (
6971
CloneHttp = "Clone Http"
7072
CreateReadmeStage = "Create Readme"
7173
GITHUB_PROVIDER = "GITHUB"
72-
GITHUB_HOST = "https://github.com/"
7374
GITLAB_PROVIDER = "GITLAB"
7475
BITBUCKET_PROVIDER = "BITBUCKET_CLOUD"
7576
AZURE_DEVOPS_PROVIDER = "AZURE_DEVOPS"
@@ -80,7 +81,7 @@ type DetailedErrorGitOpsConfigResponse struct {
8081
SuccessfulStages []string `json:"successfulStages"`
8182
StageErrorMap map[string]string `json:"stageErrorMap"`
8283
ValidatedOn time.Time `json:"validatedOn"`
83-
DeleteRepoFailed bool `json:"deleteRepoFailed"`
84+
DeleteRepoFailed bool `json:"deleteRepoFailed"`
8485
}
8586

8687
type GitOpsConfigServiceImpl struct {
@@ -135,6 +136,14 @@ func (impl *GitOpsConfigServiceImpl) ValidateAndUpdateGitOpsConfig(config *bean2
135136
return detailedErrorGitOpsConfigResponse, nil
136137
}
137138

139+
func (impl *GitOpsConfigServiceImpl) buildGithubOrgUrl(host, orgId string) (orgUrl string, err error) {
140+
hostUrl, err := url.Parse(host)
141+
if err != nil {
142+
return "", err
143+
}
144+
hostUrl.Path = path.Join(hostUrl.Path, orgId)
145+
return hostUrl.String(), nil
146+
}
138147
func (impl *GitOpsConfigServiceImpl) CreateGitOpsConfig(request *bean2.GitOpsConfigDto) (*bean2.GitOpsConfigDto, error) {
139148
impl.logger.Debugw("gitops create request", "req", request)
140149
dbConnection := impl.gitOpsRepository.GetConnection()
@@ -238,7 +247,11 @@ func (impl *GitOpsConfigServiceImpl) CreateGitOpsConfig(request *bean2.GitOpsCon
238247
}
239248
}
240249
if strings.ToUpper(request.Provider) == GITHUB_PROVIDER {
241-
request.Host = GITHUB_HOST + request.GitHubOrgId
250+
orgUrl, err := impl.buildGithubOrgUrl(request.Host, request.GitHubOrgId)
251+
if err != nil {
252+
return nil, err
253+
}
254+
request.Host = orgUrl
242255
}
243256
if strings.ToUpper(request.Provider) == GITLAB_PROVIDER {
244257
groupName, err := impl.gitFactory.GetGitLabGroupPath(request)
@@ -408,7 +421,11 @@ func (impl *GitOpsConfigServiceImpl) UpdateGitOpsConfig(request *bean2.GitOpsCon
408421
}
409422
}
410423
if strings.ToUpper(request.Provider) == GITHUB_PROVIDER {
411-
request.Host = GITHUB_HOST + request.GitHubOrgId
424+
orgUrl, err := impl.buildGithubOrgUrl(request.Host, request.GitHubOrgId)
425+
if err != nil {
426+
return err
427+
}
428+
request.Host = orgUrl
412429
}
413430
if strings.ToUpper(request.Provider) == GITLAB_PROVIDER {
414431
groupName, err := impl.gitFactory.GetGitLabGroupPath(request)
@@ -618,9 +635,9 @@ func (impl *GitOpsConfigServiceImpl) GetGitOpsConfigActive() (*bean2.GitOpsConfi
618635
func (impl *GitOpsConfigServiceImpl) GitOpsValidateDryRun(config *bean2.GitOpsConfigDto) DetailedErrorGitOpsConfigResponse {
619636
detailedErrorGitOpsConfigActions := util.DetailedErrorGitOpsConfigActions{}
620637
detailedErrorGitOpsConfigActions.StageErrorMap = make(map[string]error)
621-
if strings.ToUpper(config.Provider) == GITHUB_PROVIDER {
638+
/*if strings.ToUpper(config.Provider) == GITHUB_PROVIDER {
622639
config.Host = GITHUB_HOST
623-
}
640+
}*/
624641
if strings.ToUpper(config.Provider) == BITBUCKET_PROVIDER {
625642
config.Host = util.BITBUCKET_CLONE_BASE_URL
626643
config.BitBucketProjectKey = strings.ToUpper(config.BitBucketProjectKey)

wire_gen.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)