aboutsummaryrefslogtreecommitdiffstats
path: root/bridge
diff options
context:
space:
mode:
authorMichael Muré <batolettre@gmail.com>2019-12-10 21:13:54 +0100
committerGitHub <noreply@github.com>2019-12-10 21:13:54 +0100
commite96d8e6771086e20639a16abf6af30f2faa006a0 (patch)
tree606c0d2897c06e4ca226d377917437c62fdd1560 /bridge
parentef6801a37f75fbc5f65b0a5db194b7f88b439e7b (diff)
parentf6b4830c0b68f3b5c616236bc9d51943765c8b4a (diff)
downloadgit-bug-e96d8e6771086e20639a16abf6af30f2faa006a0.tar.gz
Merge pull request #274 from MichaelMure/gitlab-bridge
bridge/gitlab: support self-hosted GitLab instance
Diffstat (limited to 'bridge')
-rw-r--r--bridge/core/bridge.go1
-rw-r--r--bridge/github/config.go4
-rw-r--r--bridge/gitlab/config.go18
-rw-r--r--bridge/gitlab/export.go14
-rw-r--r--bridge/gitlab/export_test.go20
-rw-r--r--bridge/gitlab/gitlab.go17
-rw-r--r--bridge/gitlab/import.go6
-rw-r--r--bridge/gitlab/import_test.go3
-rw-r--r--bridge/launchpad/config.go3
9 files changed, 71 insertions, 15 deletions
diff --git a/bridge/core/bridge.go b/bridge/core/bridge.go
index 9a46e7b1..c6731ff9 100644
--- a/bridge/core/bridge.go
+++ b/bridge/core/bridge.go
@@ -35,6 +35,7 @@ type BridgeParams struct {
Owner string
Project string
URL string
+ BaseURL string
CredPrefix string
TokenRaw string
}
diff --git a/bridge/github/config.go b/bridge/github/config.go
index a74e7f6b..bc26a2fc 100644
--- a/bridge/github/config.go
+++ b/bridge/github/config.go
@@ -44,6 +44,10 @@ var (
)
func (g *Github) Configure(repo *cache.RepoCache, params core.BridgeParams) (core.Configuration, error) {
+ if params.BaseURL != "" {
+ fmt.Println("warning: --base-url is ineffective for a Github bridge")
+ }
+
conf := make(core.Configuration)
var err error
diff --git a/bridge/gitlab/config.go b/bridge/gitlab/config.go
index 7bc2e577..99c27836 100644
--- a/bridge/gitlab/config.go
+++ b/bridge/gitlab/config.go
@@ -42,6 +42,10 @@ func (g *Gitlab) Configure(repo *cache.RepoCache, params core.BridgeParams) (cor
return nil, fmt.Errorf("you must provide a project URL to configure this bridge with a token")
}
+ if params.URL == "" {
+ params.URL = defaultBaseURL
+ }
+
var url string
// get project url
@@ -56,6 +60,10 @@ func (g *Gitlab) Configure(repo *cache.RepoCache, params core.BridgeParams) (cor
}
}
+ if !strings.HasPrefix(url, params.BaseURL) {
+ return nil, fmt.Errorf("base URL (%s) doesn't match the project URL (%s)", params.BaseURL, url)
+ }
+
user, err := repo.GetUserIdentity()
if err != nil {
return nil, err
@@ -87,13 +95,14 @@ func (g *Gitlab) Configure(repo *cache.RepoCache, params core.BridgeParams) (cor
}
// validate project url and get its ID
- id, err := validateProjectURL(url, token)
+ id, err := validateProjectURL(params.BaseURL, url, token)
if err != nil {
return nil, errors.Wrap(err, "project validation")
}
conf[core.ConfigKeyTarget] = target
conf[keyProjectID] = strconv.Itoa(id)
+ conf[keyGitlabBaseUrl] = params.BaseURL
err = g.ValidateConfig(conf)
if err != nil {
@@ -302,13 +311,16 @@ func getValidGitlabRemoteURLs(remotes map[string]string) []string {
return urls
}
-func validateProjectURL(url string, token *auth.Token) (int, error) {
+func validateProjectURL(baseURL, url string, token *auth.Token) (int, error) {
projectPath, err := getProjectPath(url)
if err != nil {
return 0, err
}
- client := buildClient(token)
+ client, err := buildClient(baseURL, token)
+ if err != nil {
+ return 0, err
+ }
project, _, err := client.Projects.GetProject(projectPath, &gitlab.GetProjectOptions{})
if err != nil {
diff --git a/bridge/gitlab/export.go b/bridge/gitlab/export.go
index 373cf637..d42ef1cd 100644
--- a/bridge/gitlab/export.go
+++ b/bridge/gitlab/export.go
@@ -62,7 +62,11 @@ func (ge *gitlabExporter) cacheAllClient(repo repository.RepoConfig) error {
for _, cred := range creds {
if _, ok := ge.identityClient[cred.UserId()]; !ok {
- client := buildClient(creds[0].(*auth.Token))
+ client, err := buildClient(ge.conf[keyGitlabBaseUrl], creds[0].(*auth.Token))
+ if err != nil {
+ return err
+ }
+
ge.identityClient[cred.UserId()] = client
}
}
@@ -133,6 +137,7 @@ func (ge *gitlabExporter) exportBug(ctx context.Context, b *cache.BugCache, sinc
var err error
var bugGitlabID int
var bugGitlabIDString string
+ var GitlabBaseUrl string
var bugCreationId string
// Special case:
@@ -153,6 +158,12 @@ func (ge *gitlabExporter) exportBug(ctx context.Context, b *cache.BugCache, sinc
// get gitlab bug ID
gitlabID, ok := snapshot.GetCreateMetadata(metaKeyGitlabId)
if ok {
+ gitlabBaseUrl, ok := snapshot.GetCreateMetadata(metaKeyGitlabBaseUrl)
+ if ok && gitlabBaseUrl != ge.conf[gitlabBaseUrl] {
+ out <- core.NewExportNothing(b.Id(), "skipping issue imported from another Gitlab instance")
+ return
+ }
+
projectID, ok := snapshot.GetCreateMetadata(metaKeyGitlabProject)
if !ok {
err := fmt.Errorf("expected to find gitlab project id")
@@ -199,6 +210,7 @@ func (ge *gitlabExporter) exportBug(ctx context.Context, b *cache.BugCache, sinc
metaKeyGitlabId: idString,
metaKeyGitlabUrl: url,
metaKeyGitlabProject: ge.repositoryID,
+ metaKeyGitlabBaseUrl: GitlabBaseUrl,
},
)
if err != nil {
diff --git a/bridge/gitlab/export_test.go b/bridge/gitlab/export_test.go
index 645e2d76..d16defd0 100644
--- a/bridge/gitlab/export_test.go
+++ b/bridge/gitlab/export_test.go
@@ -188,7 +188,8 @@ func TestPushPull(t *testing.T) {
// initialize exporter
exporter := &gitlabExporter{}
err = exporter.Init(backend, core.Configuration{
- keyProjectID: strconv.Itoa(projectID),
+ keyProjectID: strconv.Itoa(projectID),
+ keyGitlabBaseUrl: "https://gitlab.com/",
})
require.NoError(t, err)
@@ -215,7 +216,8 @@ func TestPushPull(t *testing.T) {
importer := &gitlabImporter{}
err = importer.Init(backend, core.Configuration{
- keyProjectID: strconv.Itoa(projectID),
+ keyProjectID: strconv.Itoa(projectID),
+ keyGitlabBaseUrl: "https://gitlab.com/",
})
require.NoError(t, err)
@@ -280,7 +282,11 @@ func generateRepoName() string {
// create repository need a token with scope 'repo'
func createRepository(ctx context.Context, name string, token *auth.Token) (int, error) {
- client := buildClient(token)
+ client, err := buildClient("https://gitlab.com/", token)
+ if err != nil {
+ return 0, err
+ }
+
project, _, err := client.Projects.CreateProject(
&gitlab.CreateProjectOptions{
Name: gitlab.String(name),
@@ -296,7 +302,11 @@ func createRepository(ctx context.Context, name string, token *auth.Token) (int,
// delete repository need a token with scope 'delete_repo'
func deleteRepository(ctx context.Context, project int, token *auth.Token) error {
- client := buildClient(token)
- _, err := client.Projects.DeleteProject(project, gitlab.WithContext(ctx))
+ client, err := buildClient("https://gitlab.com/", token)
+ if err != nil {
+ return err
+ }
+
+ _, err = client.Projects.DeleteProject(project, gitlab.WithContext(ctx))
return err
}
diff --git a/bridge/gitlab/gitlab.go b/bridge/gitlab/gitlab.go
index bcc50e4c..9298dc8e 100644
--- a/bridge/gitlab/gitlab.go
+++ b/bridge/gitlab/gitlab.go
@@ -17,9 +17,12 @@ const (
metaKeyGitlabUrl = "gitlab-url"
metaKeyGitlabLogin = "gitlab-login"
metaKeyGitlabProject = "gitlab-project-id"
+ metaKeyGitlabBaseUrl = "gitlab-base-url"
- keyProjectID = "project-id"
+ keyProjectID = "project-id"
+ keyGitlabBaseUrl = "base-url"
+ defaultBaseURL = "https://gitlab.com/"
defaultTimeout = 60 * time.Second
)
@@ -37,10 +40,16 @@ func (*Gitlab) NewExporter() core.Exporter {
return &gitlabExporter{}
}
-func buildClient(token *auth.Token) *gitlab.Client {
- client := &http.Client{
+func buildClient(baseURL string, token *auth.Token) (*gitlab.Client, error) {
+ httpClient := &http.Client{
Timeout: defaultTimeout,
}
- return gitlab.NewClient(client, token.Value)
+ gitlabClient := gitlab.NewClient(httpClient, token.Value)
+ err := gitlabClient.SetBaseURL(baseURL)
+ if err != nil {
+ return nil, err
+ }
+
+ return gitlabClient, nil
}
diff --git a/bridge/gitlab/import.go b/bridge/gitlab/import.go
index 00dee252..4fa37505 100644
--- a/bridge/gitlab/import.go
+++ b/bridge/gitlab/import.go
@@ -52,7 +52,10 @@ func (gi *gitlabImporter) Init(repo *cache.RepoCache, conf core.Configuration) e
return ErrMissingIdentityToken
}
- gi.client = buildClient(creds[0].(*auth.Token))
+ gi.client, err = buildClient(conf[keyGitlabBaseUrl], creds[0].(*auth.Token))
+ if err != nil {
+ return err
+ }
return nil
}
@@ -151,6 +154,7 @@ func (gi *gitlabImporter) ensureIssue(repo *cache.RepoCache, issue *gitlab.Issue
metaKeyGitlabId: parseID(issue.IID),
metaKeyGitlabUrl: issue.WebURL,
metaKeyGitlabProject: gi.conf[keyProjectID],
+ metaKeyGitlabBaseUrl: gi.conf[keyGitlabBaseUrl],
},
)
diff --git a/bridge/gitlab/import_test.go b/bridge/gitlab/import_test.go
index 1676bdf3..6e378b07 100644
--- a/bridge/gitlab/import_test.go
+++ b/bridge/gitlab/import_test.go
@@ -103,7 +103,8 @@ func TestImport(t *testing.T) {
importer := &gitlabImporter{}
err = importer.Init(backend, core.Configuration{
- keyProjectID: projectID,
+ keyProjectID: projectID,
+ keyGitlabBaseUrl: "https://gitlab.com",
})
require.NoError(t, err)
diff --git a/bridge/launchpad/config.go b/bridge/launchpad/config.go
index 8db39100..edbd941d 100644
--- a/bridge/launchpad/config.go
+++ b/bridge/launchpad/config.go
@@ -29,6 +29,9 @@ func (l *Launchpad) Configure(repo *cache.RepoCache, params core.BridgeParams) (
if params.Owner != "" {
fmt.Println("warning: --owner is ineffective for a Launchpad bridge")
}
+ if params.BaseURL != "" {
+ fmt.Println("warning: --base-url is ineffective for a Launchpad bridge")
+ }
conf := make(core.Configuration)
var err error