From ec6a35aeb1d006c4eb65d8ae149e64dc5ea1c947 Mon Sep 17 00:00:00 2001
From: John Olheiser <john.olheiser@gmail.com>
Date: Mon, 21 Sep 2020 09:36:51 -0500
Subject: [PATCH] Hopefully support GH enterprise (#12863)

Signed-off-by: jolheiser <john.olheiser@gmail.com>

Co-authored-by: zeripath <art27@cantab.net>
Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
---
 modules/migrations/gitea_test.go  | 2 +-
 modules/migrations/github.go      | 8 ++++++--
 modules/migrations/github_test.go | 2 +-
 3 files changed, 8 insertions(+), 4 deletions(-)

diff --git a/modules/migrations/gitea_test.go b/modules/migrations/gitea_test.go
index 2dbd8ffd44..8432a1eecd 100644
--- a/modules/migrations/gitea_test.go
+++ b/modules/migrations/gitea_test.go
@@ -28,7 +28,7 @@ func TestGiteaUploadRepo(t *testing.T) {
 	user := models.AssertExistsAndLoadBean(t, &models.User{ID: 1}).(*models.User)
 
 	var (
-		downloader = NewGithubDownloaderV3(context.Background(), "", "", "", "go-xorm", "builder")
+		downloader = NewGithubDownloaderV3(context.Background(), "https://github.com", "", "", "", "go-xorm", "builder")
 		repoName   = "builder-" + time.Now().Format("2006-01-02-15-04-05")
 		uploader   = NewGiteaLocalUploader(graceful.GetManager().HammerContext(), user, user.Name, repoName)
 	)
diff --git a/modules/migrations/github.go b/modules/migrations/github.go
index d31db136e6..c053596c17 100644
--- a/modules/migrations/github.go
+++ b/modules/migrations/github.go
@@ -47,13 +47,14 @@ func (f *GithubDownloaderV3Factory) New(ctx context.Context, opts base.MigrateOp
 		return nil, err
 	}
 
+	baseURL := u.Scheme + "://" + u.Host
 	fields := strings.Split(u.Path, "/")
 	oldOwner := fields[1]
 	oldName := strings.TrimSuffix(fields[2], ".git")
 
 	log.Trace("Create github downloader: %s/%s", oldOwner, oldName)
 
-	return NewGithubDownloaderV3(ctx, opts.AuthUsername, opts.AuthPassword, opts.AuthToken, oldOwner, oldName), nil
+	return NewGithubDownloaderV3(ctx, baseURL, opts.AuthUsername, opts.AuthPassword, opts.AuthToken, oldOwner, oldName), nil
 }
 
 // GitServiceType returns the type of git service
@@ -74,7 +75,7 @@ type GithubDownloaderV3 struct {
 }
 
 // NewGithubDownloaderV3 creates a github Downloader via github v3 API
-func NewGithubDownloaderV3(ctx context.Context, userName, password, token, repoOwner, repoName string) *GithubDownloaderV3 {
+func NewGithubDownloaderV3(ctx context.Context, baseURL, userName, password, token, repoOwner, repoName string) *GithubDownloaderV3 {
 	var downloader = GithubDownloaderV3{
 		userName:  userName,
 		password:  password,
@@ -98,6 +99,9 @@ func NewGithubDownloaderV3(ctx context.Context, userName, password, token, repoO
 		client = oauth2.NewClient(downloader.ctx, ts)
 	}
 	downloader.client = github.NewClient(client)
+	if baseURL != "https://github.com" {
+		downloader.client, _ = github.NewEnterpriseClient(baseURL, baseURL, client)
+	}
 	return &downloader
 }
 
diff --git a/modules/migrations/github_test.go b/modules/migrations/github_test.go
index 955050107c..3a1affbc2f 100644
--- a/modules/migrations/github_test.go
+++ b/modules/migrations/github_test.go
@@ -65,7 +65,7 @@ func assertLabelEqual(t *testing.T, name, color, description string, label *base
 
 func TestGitHubDownloadRepo(t *testing.T) {
 	GithubLimitRateRemaining = 3 //Wait at 3 remaining since we could have 3 CI in //
-	downloader := NewGithubDownloaderV3(context.Background(), "", "", os.Getenv("GITHUB_READ_TOKEN"), "go-gitea", "test_repo")
+	downloader := NewGithubDownloaderV3(context.Background(), "https://github.com", "", "", os.Getenv("GITHUB_READ_TOKEN"), "go-gitea", "test_repo")
 	err := downloader.RefreshRate()
 	assert.NoError(t, err)