From 6305f07fdc1cc4f1dc4f04108ca9ad3e1833b3f2 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Tue, 15 Sep 2020 18:12:07 +0200 Subject: [PATCH] On Migration respect old DefaultBranch (#12843) (#12858) * On Migration respect old DefaultBranch * add DefaultBranch int test set Co-authored-by: zeripath Co-authored-by: zeripath --- modules/migrations/base/repo.go | 19 ++++++++++--------- modules/migrations/gitea.go | 1 + modules/migrations/github.go | 18 ++++++++++++------ modules/migrations/github_test.go | 11 ++++++----- modules/migrations/gitlab.go | 13 +++++++------ modules/migrations/gitlab_test.go | 11 ++++++----- modules/repository/repo.go | 24 ++++++++++++++---------- 7 files changed, 56 insertions(+), 41 deletions(-) diff --git a/modules/migrations/base/repo.go b/modules/migrations/base/repo.go index 5cfb0de92..d2052da90 100644 --- a/modules/migrations/base/repo.go +++ b/modules/migrations/base/repo.go @@ -7,13 +7,14 @@ package base // Repository defines a standard repository information type Repository struct { - Name string - Owner string - IsPrivate bool - IsMirror bool - Description string - AuthUsername string - AuthPassword string - CloneURL string - OriginalURL string + Name string + Owner string + IsPrivate bool + IsMirror bool + Description string + AuthUsername string + AuthPassword string + CloneURL string + OriginalURL string + DefaultBranch string } diff --git a/modules/migrations/gitea.go b/modules/migrations/gitea.go index 7d446fb40..b51dc9903 100644 --- a/modules/migrations/gitea.go +++ b/modules/migrations/gitea.go @@ -119,6 +119,7 @@ func (g *GiteaLocalUploader) CreateRepo(repo *base.Repository, opts base.Migrate if err != nil { return err } + r.DefaultBranch = repo.DefaultBranch r, err = repository.MigrateRepositoryGitData(g.doer, owner, r, structs.MigrateRepoOption{ RepoName: g.repoName, diff --git a/modules/migrations/github.go b/modules/migrations/github.go index 97d62b994..a6b8fab59 100644 --- a/modules/migrations/github.go +++ b/modules/migrations/github.go @@ -154,14 +154,20 @@ func (g *GithubDownloaderV3) GetRepoInfo() (*base.Repository, error) { } g.rate = &resp.Rate + defaultBranch := "" + if gr.DefaultBranch != nil { + defaultBranch = *gr.DefaultBranch + } + // convert github repo to stand Repo return &base.Repository{ - Owner: g.repoOwner, - Name: gr.GetName(), - IsPrivate: *gr.Private, - Description: gr.GetDescription(), - OriginalURL: gr.GetHTMLURL(), - CloneURL: gr.GetCloneURL(), + Owner: g.repoOwner, + Name: gr.GetName(), + IsPrivate: *gr.Private, + Description: gr.GetDescription(), + OriginalURL: gr.GetHTMLURL(), + CloneURL: gr.GetCloneURL(), + DefaultBranch: defaultBranch, }, nil } diff --git a/modules/migrations/github_test.go b/modules/migrations/github_test.go index 814c771e8..708ea6dc9 100644 --- a/modules/migrations/github_test.go +++ b/modules/migrations/github_test.go @@ -71,11 +71,12 @@ func TestGitHubDownloadRepo(t *testing.T) { repo, err := downloader.GetRepoInfo() assert.NoError(t, err) assert.EqualValues(t, &base.Repository{ - Name: "test_repo", - Owner: "go-gitea", - Description: "Test repository for testing migration from github to gitea", - CloneURL: "https://github.com/go-gitea/test_repo.git", - OriginalURL: "https://github.com/go-gitea/test_repo", + Name: "test_repo", + Owner: "go-gitea", + Description: "Test repository for testing migration from github to gitea", + CloneURL: "https://github.com/go-gitea/test_repo.git", + OriginalURL: "https://github.com/go-gitea/test_repo", + DefaultBranch: "master", }, repo) topics, err := downloader.GetTopics() diff --git a/modules/migrations/gitlab.go b/modules/migrations/gitlab.go index 4f218c95f..cca036aa2 100644 --- a/modules/migrations/gitlab.go +++ b/modules/migrations/gitlab.go @@ -157,12 +157,13 @@ func (g *GitlabDownloader) GetRepoInfo() (*base.Repository, error) { // convert gitlab repo to stand Repo return &base.Repository{ - Owner: owner, - Name: gr.Name, - IsPrivate: private, - Description: gr.Description, - OriginalURL: gr.WebURL, - CloneURL: gr.HTTPURLToRepo, + Owner: owner, + Name: gr.Name, + IsPrivate: private, + Description: gr.Description, + OriginalURL: gr.WebURL, + CloneURL: gr.HTTPURLToRepo, + DefaultBranch: gr.DefaultBranch, }, nil } diff --git a/modules/migrations/gitlab_test.go b/modules/migrations/gitlab_test.go index 003da5bbd..a06646e2c 100644 --- a/modules/migrations/gitlab_test.go +++ b/modules/migrations/gitlab_test.go @@ -35,11 +35,12 @@ func TestGitlabDownloadRepo(t *testing.T) { assert.NoError(t, err) // Repo Owner is blank in Gitlab Group repos assert.EqualValues(t, &base.Repository{ - Name: "test_repo", - Owner: "", - Description: "Test repository for testing migration from gitlab to gitea", - CloneURL: "https://gitlab.com/gitea/test_repo.git", - OriginalURL: "https://gitlab.com/gitea/test_repo", + Name: "test_repo", + Owner: "", + Description: "Test repository for testing migration from gitlab to gitea", + CloneURL: "https://gitlab.com/gitea/test_repo.git", + OriginalURL: "https://gitlab.com/gitea/test_repo", + DefaultBranch: "master", }, repo) topics, err := downloader.GetTopics() diff --git a/modules/repository/repo.go b/modules/repository/repo.go index d57b16c91..2651dc048 100644 --- a/modules/repository/repo.go +++ b/modules/repository/repo.go @@ -102,18 +102,22 @@ func MigrateRepositoryGitData(doer, u *models.User, repo *models.Repository, opt return repo, fmt.Errorf("git.IsEmpty: %v", err) } - if !opts.Releases && !repo.IsEmpty { - // Try to get HEAD branch and set it as default branch. - headBranch, err := gitRepo.GetHEADBranch() - if err != nil { - return repo, fmt.Errorf("GetHEADBranch: %v", err) - } - if headBranch != nil { - repo.DefaultBranch = headBranch.Name + if !repo.IsEmpty { + if len(repo.DefaultBranch) == 0 { + // Try to get HEAD branch and set it as default branch. + headBranch, err := gitRepo.GetHEADBranch() + if err != nil { + return repo, fmt.Errorf("GetHEADBranch: %v", err) + } + if headBranch != nil { + repo.DefaultBranch = headBranch.Name + } } - if err = SyncReleasesWithTags(repo, gitRepo); err != nil { - log.Error("Failed to synchronize tags to releases for repository: %v", err) + if !opts.Releases { + if err = SyncReleasesWithTags(repo, gitRepo); err != nil { + log.Error("Failed to synchronize tags to releases for repository: %v", err) + } } }