Skip to content

Commit 18febac

Browse files
mrsdizziejeffliu27
authored andcommitted
Display original author and URL information when showing migrated issues/comments (go-gitea#7352)
* Store original author info for migrated issues and comments Keep original author name for displaying in Gitea interface and also store original author user ID for potential future use in linking accounts from old location. * Add original_url for repo Store the original URL for a migrated repo Clean up migrations/tests * fix migration * fix golangci-lint * make 'make revive' happy also * Modify templates to use OriginalAuthor if set Use the original author name in templates if it is set rather than the user who migrated/currently owns the issues * formatting fixes * make generate-swagger * Use default avatar for imported comments * Remove no longer used IgnoreIssueAuthor option * Add OriginalAuthorID to swagger also
1 parent 8254541 commit 18febac

28 files changed

+263
-118
lines changed

models/issue.go

+23-21
Original file line numberDiff line numberDiff line change
@@ -25,27 +25,29 @@ import (
2525

2626
// Issue represents an issue or pull request of repository.
2727
type Issue struct {
28-
ID int64 `xorm:"pk autoincr"`
29-
RepoID int64 `xorm:"INDEX UNIQUE(repo_index)"`
30-
Repo *Repository `xorm:"-"`
31-
Index int64 `xorm:"UNIQUE(repo_index)"` // Index in one repository.
32-
PosterID int64 `xorm:"INDEX"`
33-
Poster *User `xorm:"-"`
34-
Title string `xorm:"name"`
35-
Content string `xorm:"TEXT"`
36-
RenderedContent string `xorm:"-"`
37-
Labels []*Label `xorm:"-"`
38-
MilestoneID int64 `xorm:"INDEX"`
39-
Milestone *Milestone `xorm:"-"`
40-
Priority int
41-
AssigneeID int64 `xorm:"-"`
42-
Assignee *User `xorm:"-"`
43-
IsClosed bool `xorm:"INDEX"`
44-
IsRead bool `xorm:"-"`
45-
IsPull bool `xorm:"INDEX"` // Indicates whether is a pull request or not.
46-
PullRequest *PullRequest `xorm:"-"`
47-
NumComments int
48-
Ref string
28+
ID int64 `xorm:"pk autoincr"`
29+
RepoID int64 `xorm:"INDEX UNIQUE(repo_index)"`
30+
Repo *Repository `xorm:"-"`
31+
Index int64 `xorm:"UNIQUE(repo_index)"` // Index in one repository.
32+
PosterID int64 `xorm:"INDEX"`
33+
Poster *User `xorm:"-"`
34+
OriginalAuthor string
35+
OriginalAuthorID int64
36+
Title string `xorm:"name"`
37+
Content string `xorm:"TEXT"`
38+
RenderedContent string `xorm:"-"`
39+
Labels []*Label `xorm:"-"`
40+
MilestoneID int64 `xorm:"INDEX"`
41+
Milestone *Milestone `xorm:"-"`
42+
Priority int
43+
AssigneeID int64 `xorm:"-"`
44+
Assignee *User `xorm:"-"`
45+
IsClosed bool `xorm:"INDEX"`
46+
IsRead bool `xorm:"-"`
47+
IsPull bool `xorm:"INDEX"` // Indicates whether is a pull request or not.
48+
PullRequest *PullRequest `xorm:"-"`
49+
NumComments int
50+
Ref string
4951

5052
DeadlineUnix util.TimeStamp `xorm:"INDEX"`
5153

models/issue_comment.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,10 @@ const (
101101
type Comment struct {
102102
ID int64 `xorm:"pk autoincr"`
103103
Type CommentType
104-
PosterID int64 `xorm:"INDEX"`
105-
Poster *User `xorm:"-"`
104+
PosterID int64 `xorm:"INDEX"`
105+
Poster *User `xorm:"-"`
106+
OriginalAuthor string
107+
OriginalAuthorID int64
106108
IssueID int64 `xorm:"INDEX"`
107109
Issue *Issue `xorm:"-"`
108110
LabelID int64

models/migrations/migrations.go

+2
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,8 @@ var migrations = []Migration{
232232
NewMigration("add avatar field to repository", addAvatarFieldToRepository),
233233
// v88 -> v89
234234
NewMigration("add commit status context field to commit_status", addCommitStatusContext),
235+
// v89 -> v90
236+
NewMigration("add original author/url migration info to issues, comments, and repo ", addOriginalMigrationInfo),
235237
}
236238

237239
// Migrate database to current version

models/migrations/v89.go

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright 2019 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package migrations
6+
7+
import "github.com/go-xorm/xorm"
8+
9+
func addOriginalMigrationInfo(x *xorm.Engine) error {
10+
// Issue see models/issue.go
11+
type Issue struct {
12+
OriginalAuthor string
13+
OriginalAuthorID int64
14+
}
15+
16+
if err := x.Sync2(new(Issue)); err != nil {
17+
return err
18+
}
19+
20+
// Issue see models/issue_comment.go
21+
type Comment struct {
22+
OriginalAuthor string
23+
OriginalAuthorID int64
24+
}
25+
26+
if err := x.Sync2(new(Comment)); err != nil {
27+
return err
28+
}
29+
30+
// Issue see models/repo.go
31+
type Repository struct {
32+
OriginalURL string
33+
}
34+
35+
return x.Sync2(new(Repository))
36+
}

models/repo.go

+15
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ type Repository struct {
136136
Name string `xorm:"INDEX NOT NULL"`
137137
Description string
138138
Website string
139+
OriginalURL string
139140
DefaultBranch string
140141

141142
NumWatches int
@@ -847,6 +848,7 @@ func (repo *Repository) CloneLink() (cl *CloneLink) {
847848
type MigrateRepoOptions struct {
848849
Name string
849850
Description string
851+
OriginalURL string
850852
IsPrivate bool
851853
IsMirror bool
852854
RemoteAddr string
@@ -878,6 +880,7 @@ func MigrateRepository(doer, u *User, opts MigrateRepoOptions) (*Repository, err
878880
repo, err := CreateRepository(doer, u, CreateRepoOptions{
879881
Name: opts.Name,
880882
Description: opts.Description,
883+
OriginalURL: opts.OriginalURL,
881884
IsPrivate: opts.IsPrivate,
882885
IsMirror: opts.IsMirror,
883886
})
@@ -1088,6 +1091,7 @@ func initRepoCommit(tmpPath string, sig *git.Signature) (err error) {
10881091
type CreateRepoOptions struct {
10891092
Name string
10901093
Description string
1094+
OriginalURL string
10911095
Gitignores string
10921096
License string
10931097
Readme string
@@ -1354,6 +1358,7 @@ func CreateRepository(doer, u *User, opts CreateRepoOptions) (_ *Repository, err
13541358
Name: opts.Name,
13551359
LowerName: strings.ToLower(opts.Name),
13561360
Description: opts.Description,
1361+
OriginalURL: opts.OriginalURL,
13571362
IsPrivate: opts.IsPrivate,
13581363
IsFsckEnabled: !opts.IsMirror,
13591364
CloseIssuesViaCommitInAnyBranch: setting.Repository.DefaultCloseIssuesViaCommitsInAnyBranch,
@@ -2674,3 +2679,13 @@ func (repo *Repository) DeleteAvatar() error {
26742679
}
26752680
return sess.Commit()
26762681
}
2682+
2683+
// GetOriginalURLHostname returns the hostname of a URL or the URL
2684+
func (repo *Repository) GetOriginalURLHostname() string {
2685+
u, err := url.Parse(repo.OriginalURL)
2686+
if err != nil {
2687+
return repo.OriginalURL
2688+
}
2689+
2690+
return u.Host
2691+
}

modules/migrations/base/comment.go

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import "time"
1010
// Comment is a standard comment information
1111
type Comment struct {
1212
IssueIndex int64
13+
PosterID int64
1314
PosterName string
1415
PosterEmail string
1516
Created time.Time

modules/migrations/base/issue.go

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import "time"
1010
// Issue is a standard issue information
1111
type Issue struct {
1212
Number int64
13+
PosterID int64
1314
PosterName string
1415
PosterEmail string
1516
Title string

modules/migrations/base/options.go

+10-10
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@ type MigrateOptions struct {
1212
AuthPassword string
1313
Name string
1414
Description string
15+
OriginalURL string
1516

16-
Wiki bool
17-
Issues bool
18-
Milestones bool
19-
Labels bool
20-
Releases bool
21-
Comments bool
22-
PullRequests bool
23-
Private bool
24-
Mirror bool
25-
IgnoreIssueAuthor bool // if true will not add original author information before issues or comments content.
17+
Wiki bool
18+
Issues bool
19+
Milestones bool
20+
Labels bool
21+
Releases bool
22+
Comments bool
23+
PullRequests bool
24+
Private bool
25+
Mirror bool
2626
}

modules/migrations/base/pullrequest.go

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ type PullRequest struct {
1515
Number int64
1616
Title string
1717
PosterName string
18+
PosterID int64
1819
PosterEmail string
1920
Content string
2021
Milestone string

modules/migrations/base/repo.go

+1
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,5 @@ type Repository struct {
1515
AuthUsername string
1616
AuthPassword string
1717
CloneURL string
18+
OriginalURL string
1819
}

modules/migrations/gitea.go

+35-28
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ func (g *GiteaLocalUploader) CreateRepo(repo *base.Repository, opts base.Migrate
8282
r, err := models.MigrateRepository(g.doer, owner, models.MigrateRepoOptions{
8383
Name: g.repoName,
8484
Description: repo.Description,
85+
OriginalURL: repo.OriginalURL,
8586
IsMirror: repo.IsMirror,
8687
RemoteAddr: repo.CloneURL,
8788
IsPrivate: repo.IsPrivate,
@@ -247,17 +248,19 @@ func (g *GiteaLocalUploader) CreateIssues(issues ...*base.Issue) error {
247248
}
248249

249250
var is = models.Issue{
250-
RepoID: g.repo.ID,
251-
Repo: g.repo,
252-
Index: issue.Number,
253-
PosterID: g.doer.ID,
254-
Title: issue.Title,
255-
Content: issue.Content,
256-
IsClosed: issue.State == "closed",
257-
IsLocked: issue.IsLocked,
258-
MilestoneID: milestoneID,
259-
Labels: labels,
260-
CreatedUnix: util.TimeStamp(issue.Created.Unix()),
251+
RepoID: g.repo.ID,
252+
Repo: g.repo,
253+
Index: issue.Number,
254+
PosterID: g.doer.ID,
255+
OriginalAuthor: issue.PosterName,
256+
OriginalAuthorID: issue.PosterID,
257+
Title: issue.Title,
258+
Content: issue.Content,
259+
IsClosed: issue.State == "closed",
260+
IsLocked: issue.IsLocked,
261+
MilestoneID: milestoneID,
262+
Labels: labels,
263+
CreatedUnix: util.TimeStamp(issue.Created.Unix()),
261264
}
262265
if issue.Closed != nil {
263266
is.ClosedUnix = util.TimeStamp(issue.Closed.Unix())
@@ -293,11 +296,13 @@ func (g *GiteaLocalUploader) CreateComments(comments ...*base.Comment) error {
293296
}
294297

295298
cms = append(cms, &models.Comment{
296-
IssueID: issueID,
297-
Type: models.CommentTypeComment,
298-
PosterID: g.doer.ID,
299-
Content: comment.Content,
300-
CreatedUnix: util.TimeStamp(comment.Created.Unix()),
299+
IssueID: issueID,
300+
Type: models.CommentTypeComment,
301+
PosterID: g.doer.ID,
302+
OriginalAuthor: comment.PosterName,
303+
OriginalAuthorID: comment.PosterID,
304+
Content: comment.Content,
305+
CreatedUnix: util.TimeStamp(comment.Created.Unix()),
301306
})
302307

303308
// TODO: Reactions
@@ -430,18 +435,20 @@ func (g *GiteaLocalUploader) newPullRequest(pr *base.PullRequest) (*models.PullR
430435
HasMerged: pr.Merged,
431436

432437
Issue: &models.Issue{
433-
RepoID: g.repo.ID,
434-
Repo: g.repo,
435-
Title: pr.Title,
436-
Index: pr.Number,
437-
PosterID: g.doer.ID,
438-
Content: pr.Content,
439-
MilestoneID: milestoneID,
440-
IsPull: true,
441-
IsClosed: pr.State == "closed",
442-
IsLocked: pr.IsLocked,
443-
Labels: labels,
444-
CreatedUnix: util.TimeStamp(pr.Created.Unix()),
438+
RepoID: g.repo.ID,
439+
Repo: g.repo,
440+
Title: pr.Title,
441+
Index: pr.Number,
442+
PosterID: g.doer.ID,
443+
OriginalAuthor: pr.PosterName,
444+
OriginalAuthorID: pr.PosterID,
445+
Content: pr.Content,
446+
MilestoneID: milestoneID,
447+
IsPull: true,
448+
IsClosed: pr.State == "closed",
449+
IsLocked: pr.IsLocked,
450+
Labels: labels,
451+
CreatedUnix: util.TimeStamp(pr.Created.Unix()),
445452
},
446453
}
447454

modules/migrations/gitea_test.go

+9-10
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,15 @@ func TestGiteaUploadRepo(t *testing.T) {
3434
Name: repoName,
3535
AuthUsername: "",
3636

37-
Wiki: true,
38-
Issues: true,
39-
Milestones: true,
40-
Labels: true,
41-
Releases: true,
42-
Comments: true,
43-
PullRequests: true,
44-
Private: true,
45-
Mirror: false,
46-
IgnoreIssueAuthor: false,
37+
Wiki: true,
38+
Issues: true,
39+
Milestones: true,
40+
Labels: true,
41+
Releases: true,
42+
Comments: true,
43+
PullRequests: true,
44+
Private: true,
45+
Mirror: false,
4746
})
4847
assert.NoError(t, err)
4948

modules/migrations/github.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -107,13 +107,13 @@ func (g *GithubDownloaderV3) GetRepoInfo() (*base.Repository, error) {
107107
if err != nil {
108108
return nil, err
109109
}
110-
111110
// convert github repo to stand Repo
112111
return &base.Repository{
113112
Owner: g.repoOwner,
114113
Name: gr.GetName(),
115114
IsPrivate: *gr.Private,
116115
Description: gr.GetDescription(),
116+
OriginalURL: gr.GetHTMLURL(),
117117
CloneURL: gr.GetCloneURL(),
118118
}, nil
119119
}
@@ -317,6 +317,7 @@ func (g *GithubDownloaderV3) GetIssues(page, perPage int) ([]*base.Issue, bool,
317317
allIssues = append(allIssues, &base.Issue{
318318
Title: *issue.Title,
319319
Number: int64(*issue.Number),
320+
PosterID: *issue.User.ID,
320321
PosterName: *issue.User.Login,
321322
PosterEmail: email,
322323
Content: body,
@@ -359,6 +360,7 @@ func (g *GithubDownloaderV3) GetComments(issueNumber int64) ([]*base.Comment, er
359360
}
360361
allComments = append(allComments, &base.Comment{
361362
IssueIndex: issueNumber,
363+
PosterID: *comment.User.ID,
362364
PosterName: *comment.User.Login,
363365
PosterEmail: email,
364366
Content: *comment.Body,
@@ -451,6 +453,7 @@ func (g *GithubDownloaderV3) GetPullRequests(page, perPage int) ([]*base.PullReq
451453
Title: *pr.Title,
452454
Number: int64(*pr.Number),
453455
PosterName: *pr.User.Login,
456+
PosterID: *pr.User.ID,
454457
PosterEmail: email,
455458
Content: body,
456459
Milestone: milestone,

0 commit comments

Comments
 (0)