From e728b5581291d6d9a62fcd8ab2c3b2e6c89b7138 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Tue, 2 Jul 2019 05:17:16 +0800 Subject: [PATCH] Fix bug conflict between SyncReleasesWithTags and InsertReleases (#7337) * fix bug conflict between SyncReleasesWithTags and InsertReleases * fix tests * fix fmt --- models/release_test.go | 13 +++++++------ models/repo.go | 15 ++++++++------- modules/migrations/base/uploader.go | 2 +- modules/migrations/gitea.go | 22 ++++++++++++++-------- modules/migrations/migrate.go | 2 +- 5 files changed, 31 insertions(+), 23 deletions(-) diff --git a/models/release_test.go b/models/release_test.go index f3f61240eadf..83c3fe2f77c4 100644 --- a/models/release_test.go +++ b/models/release_test.go @@ -102,12 +102,13 @@ func TestRelease_MirrorDelete(t *testing.T) { repo := AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository) repoPath := RepoPath(user.Name, repo.Name) migrationOptions := MigrateRepoOptions{ - Name: "test_mirror", - Description: "Test mirror", - IsPrivate: false, - IsMirror: true, - RemoteAddr: repoPath, - Wiki: true, + Name: "test_mirror", + Description: "Test mirror", + IsPrivate: false, + IsMirror: true, + RemoteAddr: repoPath, + Wiki: true, + SyncReleasesWithTags: true, } mirror, err := MigrateRepository(user, user, migrationOptions) assert.NoError(t, err) diff --git a/models/repo.go b/models/repo.go index 1b4ff1f18678..59ce18fa88e9 100644 --- a/models/repo.go +++ b/models/repo.go @@ -845,12 +845,13 @@ func (repo *Repository) CloneLink() (cl *CloneLink) { // MigrateRepoOptions contains the repository migrate options type MigrateRepoOptions struct { - Name string - Description string - IsPrivate bool - IsMirror bool - RemoteAddr string - Wiki bool // include wiki repository + Name string + Description string + IsPrivate bool + IsMirror bool + RemoteAddr string + Wiki bool // include wiki repository + SyncReleasesWithTags bool // sync releases from tags } /* @@ -942,7 +943,7 @@ func MigrateRepository(doer, u *User, opts MigrateRepoOptions) (*Repository, err return repo, fmt.Errorf("git.IsEmpty: %v", err) } - if !repo.IsEmpty { + if opts.SyncReleasesWithTags && !repo.IsEmpty { // Try to get HEAD branch and set it as default branch. headBranch, err := gitRepo.GetHEADBranch() if err != nil { diff --git a/modules/migrations/base/uploader.go b/modules/migrations/base/uploader.go index 096a8ab8f0aa..9d2fd2af6acb 100644 --- a/modules/migrations/base/uploader.go +++ b/modules/migrations/base/uploader.go @@ -7,7 +7,7 @@ package base // Uploader uploads all the informations of one repository type Uploader interface { - CreateRepo(repo *Repository, includeWiki bool) error + CreateRepo(repo *Repository, opts MigrateOptions) error CreateMilestones(milestones ...*Milestone) error CreateReleases(releases ...*Release) error CreateLabels(labels ...*Label) error diff --git a/modules/migrations/gitea.go b/modules/migrations/gitea.go index 7d5a50d30732..bfc5e49033d7 100644 --- a/modules/migrations/gitea.go +++ b/modules/migrations/gitea.go @@ -54,19 +54,20 @@ func NewGiteaLocalUploader(doer *models.User, repoOwner, repoName string) *Gitea } // CreateRepo creates a repository -func (g *GiteaLocalUploader) CreateRepo(repo *base.Repository, includeWiki bool) error { +func (g *GiteaLocalUploader) CreateRepo(repo *base.Repository, opts base.MigrateOptions) error { owner, err := models.GetUserByName(g.repoOwner) if err != nil { return err } r, err := models.MigrateRepository(g.doer, owner, models.MigrateRepoOptions{ - Name: g.repoName, - Description: repo.Description, - IsMirror: repo.IsMirror, - RemoteAddr: repo.CloneURL, - IsPrivate: repo.IsPrivate, - Wiki: includeWiki, + Name: g.repoName, + Description: repo.Description, + IsMirror: repo.IsMirror, + RemoteAddr: repo.CloneURL, + IsPrivate: repo.IsPrivate, + Wiki: opts.Wiki, + SyncReleasesWithTags: !opts.Releases, // if didn't get releases, then sync them from tags }) g.repo = r if err != nil { @@ -198,7 +199,12 @@ func (g *GiteaLocalUploader) CreateReleases(releases ...*base.Release) error { rels = append(rels, &rel) } - return models.InsertReleases(rels...) + if err := models.InsertReleases(rels...); err != nil { + return err + } + + // sync tags to releases in database + return models.SyncReleasesWithTags(g.repo, g.gitRepo) } // CreateIssues creates issues diff --git a/modules/migrations/migrate.go b/modules/migrations/migrate.go index 3a4e6b2dbefb..ce8f9b8022cd 100644 --- a/modules/migrations/migrate.go +++ b/modules/migrations/migrate.go @@ -80,7 +80,7 @@ func migrateRepository(downloader base.Downloader, uploader base.Uploader, opts repo.Description = opts.Description } log.Trace("migrating git data") - if err := uploader.CreateRepo(repo, opts.Wiki); err != nil { + if err := uploader.CreateRepo(repo, opts); err != nil { return err }