From bd41a04a5613a2e017b458351bea0f02b767b893 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Sat, 5 Oct 2019 19:09:27 +0800 Subject: [PATCH] Add extra user information when migrating release (#8331) * add extra user information when migrating release * add migrations * fix tests --- models/migrations/migrations.go | 2 ++ models/migrations/v98.go | 17 +++++++++++++++++ models/release.go | 2 ++ modules/migrations/base/release.go | 3 +++ modules/migrations/gitea.go | 26 ++++++++++++++------------ modules/migrations/github.go | 8 ++++++++ modules/migrations/github_test.go | 2 ++ templates/repo/release/list.tmpl | 7 +++++++ 8 files changed, 55 insertions(+), 12 deletions(-) create mode 100644 models/migrations/v98.go diff --git a/models/migrations/migrations.go b/models/migrations/migrations.go index 7680e7747c..1f5b918de8 100644 --- a/models/migrations/migrations.go +++ b/models/migrations/migrations.go @@ -250,6 +250,8 @@ var migrations = []Migration{ NewMigration("delete orphaned attachments", deleteOrphanedAttachments), // v97 -> v98 NewMigration("add repo_admin_change_team_access to user", addRepoAdminChangeTeamAccessColumnForUser), + // v98 -> v99 + NewMigration("add original author name and id on migrated release", addOriginalAuthorOnMigratedReleases), } // Migrate database to current version diff --git a/models/migrations/v98.go b/models/migrations/v98.go new file mode 100644 index 0000000000..3b9fdbb1c5 --- /dev/null +++ b/models/migrations/v98.go @@ -0,0 +1,17 @@ +// Copyright 2019 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package migrations + +import "github.com/go-xorm/xorm" + +func addOriginalAuthorOnMigratedReleases(x *xorm.Engine) error { + type Release struct { + ID int64 + OriginalAuthor string + OriginalAuthorID int64 `xorm:"index"` + } + + return x.Sync2(new(Release)) +} diff --git a/models/release.go b/models/release.go index 3729a6f690..243cc2fa3c 100644 --- a/models/release.go +++ b/models/release.go @@ -26,6 +26,8 @@ type Release struct { PublisherID int64 `xorm:"INDEX"` Publisher *User `xorm:"-"` TagName string `xorm:"INDEX UNIQUE(n)"` + OriginalAuthor string + OriginalAuthorID int64 `xorm:"index"` LowerTagName string Target string Title string diff --git a/modules/migrations/base/release.go b/modules/migrations/base/release.go index 4ebc37315d..b2541f1bf5 100644 --- a/modules/migrations/base/release.go +++ b/modules/migrations/base/release.go @@ -25,6 +25,9 @@ type Release struct { Body string Draft bool Prerelease bool + PublisherID int64 + PublisherName string + PublisherEmail string Assets []ReleaseAsset Created time.Time Published time.Time diff --git a/modules/migrations/gitea.go b/modules/migrations/gitea.go index 271c5616a7..1edac47a6e 100644 --- a/modules/migrations/gitea.go +++ b/modules/migrations/gitea.go @@ -175,18 +175,20 @@ func (g *GiteaLocalUploader) CreateReleases(releases ...*base.Release) error { var rels = make([]*models.Release, 0, len(releases)) for _, release := range releases { var rel = models.Release{ - RepoID: g.repo.ID, - PublisherID: g.doer.ID, - TagName: release.TagName, - LowerTagName: strings.ToLower(release.TagName), - Target: release.TargetCommitish, - Title: release.Name, - Sha1: release.TargetCommitish, - Note: release.Body, - IsDraft: release.Draft, - IsPrerelease: release.Prerelease, - IsTag: false, - CreatedUnix: timeutil.TimeStamp(release.Created.Unix()), + RepoID: g.repo.ID, + PublisherID: g.doer.ID, + TagName: release.TagName, + LowerTagName: strings.ToLower(release.TagName), + Target: release.TargetCommitish, + Title: release.Name, + Sha1: release.TargetCommitish, + Note: release.Body, + IsDraft: release.Draft, + IsPrerelease: release.Prerelease, + IsTag: false, + CreatedUnix: timeutil.TimeStamp(release.Created.Unix()), + OriginalAuthor: release.PublisherName, + OriginalAuthorID: release.PublisherID, } // calc NumCommits diff --git a/modules/migrations/github.go b/modules/migrations/github.go index c5f29cabb7..754f98941c 100644 --- a/modules/migrations/github.go +++ b/modules/migrations/github.go @@ -214,6 +214,11 @@ func (g *GithubDownloaderV3) convertGithubRelease(rel *github.RepositoryRelease) name = *rel.Name } + var email string + if rel.Author.Email != nil { + email = *rel.Author.Email + } + r := &base.Release{ TagName: *rel.TagName, TargetCommitish: *rel.TargetCommitish, @@ -222,6 +227,9 @@ func (g *GithubDownloaderV3) convertGithubRelease(rel *github.RepositoryRelease) Draft: *rel.Draft, Prerelease: *rel.Prerelease, Created: rel.CreatedAt.Time, + PublisherID: *rel.Author.ID, + PublisherName: *rel.Author.Login, + PublisherEmail: email, Published: rel.PublishedAt.Time, } diff --git a/modules/migrations/github_test.go b/modules/migrations/github_test.go index 62fce6bbcc..2a0a4edf32 100644 --- a/modules/migrations/github_test.go +++ b/modules/migrations/github_test.go @@ -167,6 +167,8 @@ func TestGitHubDownloadRepo(t *testing.T) { Body: "Forked source from Gogs into Gitea\n", Created: time.Date(2016, 10, 17, 02, 17, 59, 0, time.UTC), Published: time.Date(2016, 11, 17, 15, 37, 0, 0, time.UTC), + PublisherID: 4726179, + PublisherName: "bkcsoft", }, }, releases[len(releases)-1:]) diff --git a/templates/repo/release/list.tmpl b/templates/repo/release/list.tmpl index c57a180873..777d3a2b05 100644 --- a/templates/repo/release/list.tmpl +++ b/templates/repo/release/list.tmpl @@ -54,8 +54,15 @@

+ {{if .OriginalAuthor}} + + {{.OriginalAuthor}} + {{else if .Publisher}} {{.Publisher.Name}} + {{else}} + Ghost + {{end}} {{if .CreatedUnix}}{{TimeSinceUnix .CreatedUnix $.Lang}}{{end}} {{$.i18n.Tr "repo.release.ahead" .NumCommitsBehind .Target | Str2html}}