From bc87e20280a3ab9d5e7b6577c528c3e0fe1f67e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Bru=CC=88ckner?= Date: Wed, 1 Nov 2023 21:03:48 -0700 Subject: [PATCH 01/11] Properly migrate automatic merge GitLab comments --- services/migrations/gitea_uploader.go | 2 + services/migrations/gitlab.go | 50 ++++++++++--------- .../repo/issue/view_content/comments.tmpl | 26 +++++++--- 3 files changed, 47 insertions(+), 31 deletions(-) diff --git a/services/migrations/gitea_uploader.go b/services/migrations/gitea_uploader.go index ddc2cbd4ec832..679f837f48b07 100644 --- a/services/migrations/gitea_uploader.go +++ b/services/migrations/gitea_uploader.go @@ -485,6 +485,8 @@ func (g *GiteaLocalUploader) CreateComments(comments ...*base.Comment) error { if comment.Meta["NewTitle"] != nil { cm.NewTitle = fmt.Sprintf("%s", comment.Meta["NewTitle"]) } + case issues_model.CommentTypePRScheduledToAutoMerge, issues_model.CommentTypePRUnScheduledToAutoMerge: + cm.Content = "" default: } diff --git a/services/migrations/gitlab.go b/services/migrations/gitlab.go index 51dde8b67764f..f9da825a115a1 100644 --- a/services/migrations/gitlab.go +++ b/services/migrations/gitlab.go @@ -14,6 +14,7 @@ import ( "strings" "time" + issues_model "code.gitea.io/gitea/models/issues" "code.gitea.io/gitea/modules/container" "code.gitea.io/gitea/modules/log" base "code.gitea.io/gitea/modules/migration" @@ -488,30 +489,8 @@ func (g *GitlabDownloader) GetComments(commentable base.Commentable) ([]*base.Co return nil, false, fmt.Errorf("error while listing comments: %v %w", g.repoID, err) } for _, comment := range comments { - // Flatten comment threads - if !comment.IndividualNote { - for _, note := range comment.Notes { - allComments = append(allComments, &base.Comment{ - IssueIndex: commentable.GetLocalIndex(), - Index: int64(note.ID), - PosterID: int64(note.Author.ID), - PosterName: note.Author.Username, - PosterEmail: note.Author.Email, - Content: note.Body, - Created: *note.CreatedAt, - }) - } - } else { - c := comment.Notes[0] - allComments = append(allComments, &base.Comment{ - IssueIndex: commentable.GetLocalIndex(), - Index: int64(c.ID), - PosterID: int64(c.Author.ID), - PosterName: c.Author.Username, - PosterEmail: c.Author.Email, - Content: c.Body, - Created: *c.CreatedAt, - }) + for _, note := range comment.Notes { + allComments = append(allComments, convertNoteToComment(commentable.GetLocalIndex(), note)) } } if resp.NextPage == 0 { @@ -522,6 +501,29 @@ func (g *GitlabDownloader) GetComments(commentable base.Commentable) ([]*base.Co return allComments, true, nil } +func convertNoteToComment(localIndex int64, note *gitlab.Note) *base.Comment { + comment := &base.Comment{ + IssueIndex: localIndex, + Index: int64(note.ID), + PosterID: int64(note.Author.ID), + PosterName: note.Author.Username, + PosterEmail: note.Author.Email, + Content: note.Body, + Created: *note.CreatedAt, + } + + // Try to find the underlying event of system notes. + if note.System { + if strings.HasPrefix(note.Body, "enabled an automatic merge") { + comment.CommentType = issues_model.CommentTypePRScheduledToAutoMerge.String() + } else if note.Body == "canceled the automatic merge" { + comment.CommentType = issues_model.CommentTypePRUnScheduledToAutoMerge.String() + } + } + + return comment +} + // GetPullRequests returns pull requests according page and perPage func (g *GitlabDownloader) GetPullRequests(page, perPage int) ([]*base.PullRequest, bool, error) { if perPage > g.maxPerPage { diff --git a/templates/repo/issue/view_content/comments.tmpl b/templates/repo/issue/view_content/comments.tmpl index baa3380c65710..dc7e4871fd902 100644 --- a/templates/repo/issue/view_content/comments.tmpl +++ b/templates/repo/issue/view_content/comments.tmpl @@ -378,12 +378,13 @@ {{svg (printf "octicon-%s" .Review.Type.Icon)}} {{if .OriginalAuthor}} - + {{svg (MigrationIcon $.Repository.GetOriginalURLHostname)}} {{.OriginalAuthor}} - {{if $.Repository.OriginalURL}} - ({{ctx.Locale.Tr "repo.migrated_from" ($.Repository.OriginalURL|Escape) ($.Repository.GetOriginalURLHostname|Escape) | Safe}}){{end}} + {{if $.Repository.OriginalURL}} + ({{ctx.Locale.Tr "repo.migrated_from" ($.Repository.OriginalURL|Escape) ($.Repository.GetOriginalURLHostname|Escape) | Safe}}) + {{end}} {{else}} {{template "shared/user/authorlink" .Poster}} {{end}} @@ -526,12 +527,13 @@ {{end}} {{if .OriginalAuthor}} - + {{svg (MigrationIcon $.Repository.GetOriginalURLHostname)}} {{.OriginalAuthor}} - {{if $.Repository.OriginalURL}} - ({{ctx.Locale.Tr "repo.migrated_from" ($.Repository.OriginalURL|Escape) ($.Repository.GetOriginalURLHostname|Escape) | Safe}}){{end}} + {{if $.Repository.OriginalURL}} + ({{ctx.Locale.Tr "repo.migrated_from" ($.Repository.OriginalURL|Escape) ($.Repository.GetOriginalURLHostname|Escape) | Safe}}) + {{end}} {{else}} {{template "shared/user/authorlink" .Poster}} {{end}} @@ -796,7 +798,17 @@
{{svg "octicon-git-merge" 16}} - {{template "shared/user/authorlink" .Poster}} + {{if .OriginalAuthor}} + + {{svg (MigrationIcon $.Repository.GetOriginalURLHostname)}} + {{.OriginalAuthor}} + + {{if $.Repository.OriginalURL}} + ({{ctx.Locale.Tr "repo.migrated_from" ($.Repository.OriginalURL|Escape) ($.Repository.GetOriginalURLHostname|Escape) | Safe}}) + {{end}} + {{else}} + {{template "shared/user/authorlink" .Poster}} + {{end}} {{if eq .Type 34}}{{ctx.Locale.Tr "repo.pulls.auto_merge_newly_scheduled_comment" $createdStr | Safe}} {{else}}{{ctx.Locale.Tr "repo.pulls.auto_merge_canceled_schedule_comment" $createdStr | Safe}}{{end}} From 202a7f59dfc561be96d3c57596bfbae21a9007af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Bru=CC=88ckner?= Date: Thu, 2 Nov 2023 19:14:53 -0700 Subject: [PATCH 02/11] Bring back text class --- templates/repo/issue/view_content/comments.tmpl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/templates/repo/issue/view_content/comments.tmpl b/templates/repo/issue/view_content/comments.tmpl index dc7e4871fd902..c661ce14159ea 100644 --- a/templates/repo/issue/view_content/comments.tmpl +++ b/templates/repo/issue/view_content/comments.tmpl @@ -378,7 +378,7 @@ {{svg (printf "octicon-%s" .Review.Type.Icon)}} {{if .OriginalAuthor}} - + {{svg (MigrationIcon $.Repository.GetOriginalURLHostname)}} {{.OriginalAuthor}} @@ -527,7 +527,7 @@ {{end}} {{if .OriginalAuthor}} - + {{svg (MigrationIcon $.Repository.GetOriginalURLHostname)}} {{.OriginalAuthor}} @@ -799,7 +799,7 @@ {{svg "octicon-git-merge" 16}} {{if .OriginalAuthor}} - + {{svg (MigrationIcon $.Repository.GetOriginalURLHostname)}} {{.OriginalAuthor}} From 53a5385c912902ffedffe12f42c0450db58e35f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Bru=CC=88ckner?= Date: Mon, 6 Nov 2023 20:39:06 -0800 Subject: [PATCH 03/11] Add unit test --- services/migrations/gitlab.go | 4 +- services/migrations/gitlab_test.go | 65 ++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+), 2 deletions(-) diff --git a/services/migrations/gitlab.go b/services/migrations/gitlab.go index f9da825a115a1..cb3c018a6b23b 100644 --- a/services/migrations/gitlab.go +++ b/services/migrations/gitlab.go @@ -490,7 +490,7 @@ func (g *GitlabDownloader) GetComments(commentable base.Commentable) ([]*base.Co } for _, comment := range comments { for _, note := range comment.Notes { - allComments = append(allComments, convertNoteToComment(commentable.GetLocalIndex(), note)) + allComments = append(allComments, g.convertNoteToComment(commentable.GetLocalIndex(), note)) } } if resp.NextPage == 0 { @@ -501,7 +501,7 @@ func (g *GitlabDownloader) GetComments(commentable base.Commentable) ([]*base.Co return allComments, true, nil } -func convertNoteToComment(localIndex int64, note *gitlab.Note) *base.Comment { +func (g *GitlabDownloader) convertNoteToComment(localIndex int64, note *gitlab.Note) *base.Comment { comment := &base.Comment{ IssueIndex: localIndex, Index: int64(note.ID), diff --git a/services/migrations/gitlab_test.go b/services/migrations/gitlab_test.go index 731486eff21e3..b676217795a97 100644 --- a/services/migrations/gitlab_test.go +++ b/services/migrations/gitlab_test.go @@ -516,3 +516,68 @@ func TestAwardsToReactions(t *testing.T) { }, }, reactions) } + +func TestNoteToComment(t *testing.T) { + downloader := &GitlabDownloader{} + + now := time.Now() + makeTestNote := func(id int, body string, system bool) gitlab.Note { + return gitlab.Note{ + ID: id, + Author: struct { + ID int `json:"id"` + Username string `json:"username"` + Email string `json:"email"` + Name string `json:"name"` + State string `json:"state"` + AvatarURL string `json:"avatar_url"` + WebURL string `json:"web_url"` + }{ + ID: 72, + Email: "test@example.com", + Username: "test", + }, + Body: body, + CreatedAt: &now, + System: system, + } + } + notes := []gitlab.Note{ + makeTestNote(1, "This is a regular comment", false), + makeTestNote(2, "enabled an automatic merge for abcd1234", true), + makeTestNote(3, "canceled the automatic merge", true), + } + comments := []base.Comment{{ + IssueIndex: 17, + Index: 1, + PosterID: 72, + PosterName: "test", + PosterEmail: "test@example.com", + CommentType: "", + Content: "This is a regular comment", + Created: now, + }, { + IssueIndex: 17, + Index: 2, + PosterID: 72, + PosterName: "test", + PosterEmail: "test@example.com", + CommentType: "pull_scheduled_merge", + Content: "enabled an automatic merge for abcd1234", + Created: now, + }, { + IssueIndex: 17, + Index: 3, + PosterID: 72, + PosterName: "test", + PosterEmail: "test@example.com", + CommentType: "pull_cancel_scheduled_merge", + Content: "canceled the automatic merge", + Created: now, + }} + + for i, note := range notes { + actualComment := *downloader.convertNoteToComment(17, ¬e) + assert.EqualValues(t, actualComment, comments[i]) + } +} From 5678c1c63791a17e6ded258312b4a7252a16aedd Mon Sep 17 00:00:00 2001 From: silverwind Date: Thu, 22 Feb 2024 00:42:29 +0100 Subject: [PATCH 04/11] Update templates/repo/issue/view_content/comments.tmpl --- templates/repo/issue/view_content/comments.tmpl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/templates/repo/issue/view_content/comments.tmpl b/templates/repo/issue/view_content/comments.tmpl index 6796c944a0f9a..2cb7d38fa453d 100644 --- a/templates/repo/issue/view_content/comments.tmpl +++ b/templates/repo/issue/view_content/comments.tmpl @@ -382,7 +382,7 @@ {{.OriginalAuthor}} {{if $.Repository.OriginalURL}} - ({{ctx.Locale.Tr "repo.migrated_from" ($.Repository.OriginalURL|Escape) ($.Repository.GetOriginalURLHostname|Escape) | Safe}}) + ({{ctx.Locale.Tr "repo.migrated_from" ($.Repository.OriginalURL|Escape) ($.Repository.GetOriginalURLHostname|Escape) | Safe}}) {{end}} {{else}} {{template "shared/user/authorlink" .Poster}} From b5c16b9d5a42a217369cd230e331aa453f808df0 Mon Sep 17 00:00:00 2001 From: silverwind Date: Thu, 22 Feb 2024 00:42:37 +0100 Subject: [PATCH 05/11] Update templates/repo/issue/view_content/comments.tmpl --- templates/repo/issue/view_content/comments.tmpl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/templates/repo/issue/view_content/comments.tmpl b/templates/repo/issue/view_content/comments.tmpl index 2cb7d38fa453d..76f6a9dd2987a 100644 --- a/templates/repo/issue/view_content/comments.tmpl +++ b/templates/repo/issue/view_content/comments.tmpl @@ -670,7 +670,7 @@ {{.OriginalAuthor}} {{if $.Repository.OriginalURL}} - ({{ctx.Locale.Tr "repo.migrated_from" ($.Repository.OriginalURL|Escape) ($.Repository.GetOriginalURLHostname|Escape) | Safe}}) + ({{ctx.Locale.Tr "repo.migrated_from" ($.Repository.OriginalURL|Escape) ($.Repository.GetOriginalURLHostname|Escape) | Safe}}) {{end}} {{else}} {{template "shared/user/authorlink" .Poster}} From 9302751f361ace178e8875639b01a40280598706 Mon Sep 17 00:00:00 2001 From: silverwind Date: Thu, 22 Feb 2024 00:42:43 +0100 Subject: [PATCH 06/11] Update templates/repo/issue/view_content/conversation.tmpl --- templates/repo/issue/view_content/conversation.tmpl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/templates/repo/issue/view_content/conversation.tmpl b/templates/repo/issue/view_content/conversation.tmpl index 57944f7c7e17e..c632d2fe5f924 100644 --- a/templates/repo/issue/view_content/conversation.tmpl +++ b/templates/repo/issue/view_content/conversation.tmpl @@ -68,7 +68,7 @@ {{.OriginalAuthor}} {{if $.Repository.OriginalURL}} - ({{ctx.Locale.Tr "repo.migrated_from" ($.Repository.OriginalURL|Escape) ($.Repository.GetOriginalURLHostname|Escape) | Safe}}) + ({{ctx.Locale.Tr "repo.migrated_from" ($.Repository.OriginalURL|Escape) ($.Repository.GetOriginalURLHostname|Escape) | Safe}}) {{end}} {{else}} {{template "shared/user/authorlink" .Poster}} From e46a86bbec78fec11c63aff9869c6f5673b953cd Mon Sep 17 00:00:00 2001 From: silverwind Date: Thu, 22 Feb 2024 00:44:21 +0100 Subject: [PATCH 07/11] Update templates/repo/issue/view_content/comments.tmpl --- templates/repo/issue/view_content/comments.tmpl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/templates/repo/issue/view_content/comments.tmpl b/templates/repo/issue/view_content/comments.tmpl index 76f6a9dd2987a..4fbb20d4a59ac 100644 --- a/templates/repo/issue/view_content/comments.tmpl +++ b/templates/repo/issue/view_content/comments.tmpl @@ -382,7 +382,7 @@ {{.OriginalAuthor}} {{if $.Repository.OriginalURL}} - ({{ctx.Locale.Tr "repo.migrated_from" ($.Repository.OriginalURL|Escape) ($.Repository.GetOriginalURLHostname|Escape) | Safe}}) + ({{ctx.Locale.Tr "repo.migrated_from" ($.Repository.OriginalURL|Escape) ($.Repository.GetOriginalURLHostname|Escape) | Safe}}) {{end}} {{else}} {{template "shared/user/authorlink" .Poster}} From 10610710173380a7f46358cc4c8d631a26f03463 Mon Sep 17 00:00:00 2001 From: silverwind Date: Thu, 22 Feb 2024 00:44:39 +0100 Subject: [PATCH 08/11] Update templates/repo/issue/view_content/comments.tmpl --- templates/repo/issue/view_content/comments.tmpl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/templates/repo/issue/view_content/comments.tmpl b/templates/repo/issue/view_content/comments.tmpl index 4fbb20d4a59ac..6796c944a0f9a 100644 --- a/templates/repo/issue/view_content/comments.tmpl +++ b/templates/repo/issue/view_content/comments.tmpl @@ -670,7 +670,7 @@ {{.OriginalAuthor}} {{if $.Repository.OriginalURL}} - ({{ctx.Locale.Tr "repo.migrated_from" ($.Repository.OriginalURL|Escape) ($.Repository.GetOriginalURLHostname|Escape) | Safe}}) + ({{ctx.Locale.Tr "repo.migrated_from" ($.Repository.OriginalURL|Escape) ($.Repository.GetOriginalURLHostname|Escape) | Safe}}) {{end}} {{else}} {{template "shared/user/authorlink" .Poster}} From d2f18ddb633ca690dd4de544b24b0eb332dc89a4 Mon Sep 17 00:00:00 2001 From: silverwind Date: Thu, 22 Feb 2024 00:44:52 +0100 Subject: [PATCH 09/11] Update templates/repo/issue/view_content/conversation.tmpl --- templates/repo/issue/view_content/conversation.tmpl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/templates/repo/issue/view_content/conversation.tmpl b/templates/repo/issue/view_content/conversation.tmpl index c632d2fe5f924..57944f7c7e17e 100644 --- a/templates/repo/issue/view_content/conversation.tmpl +++ b/templates/repo/issue/view_content/conversation.tmpl @@ -68,7 +68,7 @@ {{.OriginalAuthor}} {{if $.Repository.OriginalURL}} - ({{ctx.Locale.Tr "repo.migrated_from" ($.Repository.OriginalURL|Escape) ($.Repository.GetOriginalURLHostname|Escape) | Safe}}) + ({{ctx.Locale.Tr "repo.migrated_from" ($.Repository.OriginalURL|Escape) ($.Repository.GetOriginalURLHostname|Escape) | Safe}}) {{end}} {{else}} {{template "shared/user/authorlink" .Poster}} From fb38b8d88968fc210fe2f865852a27fa4c2008d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Br=C3=BCckner?= Date: Thu, 22 Feb 2024 07:39:47 +0100 Subject: [PATCH 10/11] Apply suggestions from code review Co-authored-by: wxiaoguang --- templates/repo/issue/view_content/comments.tmpl | 4 ++-- templates/repo/issue/view_content/conversation.tmpl | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/templates/repo/issue/view_content/comments.tmpl b/templates/repo/issue/view_content/comments.tmpl index 6796c944a0f9a..be43d6766f2bb 100644 --- a/templates/repo/issue/view_content/comments.tmpl +++ b/templates/repo/issue/view_content/comments.tmpl @@ -382,7 +382,7 @@ {{.OriginalAuthor}} {{if $.Repository.OriginalURL}} - ({{ctx.Locale.Tr "repo.migrated_from" ($.Repository.OriginalURL|Escape) ($.Repository.GetOriginalURLHostname|Escape) | Safe}}) + ({{ctx.Locale.Tr "repo.migrated_from" $.Repository.OriginalURL|Escape $.Repository.GetOriginalURLHostname}}) {{end}} {{else}} {{template "shared/user/authorlink" .Poster}} @@ -670,7 +670,7 @@ {{.OriginalAuthor}} {{if $.Repository.OriginalURL}} - ({{ctx.Locale.Tr "repo.migrated_from" ($.Repository.OriginalURL|Escape) ($.Repository.GetOriginalURLHostname|Escape) | Safe}}) + ({{ctx.Locale.Tr "repo.migrated_from" $.Repository.OriginalURL $.Repository.GetOriginalURLHostname}}) {{end}} {{else}} {{template "shared/user/authorlink" .Poster}} diff --git a/templates/repo/issue/view_content/conversation.tmpl b/templates/repo/issue/view_content/conversation.tmpl index 57944f7c7e17e..fc1d9865f5cce 100644 --- a/templates/repo/issue/view_content/conversation.tmpl +++ b/templates/repo/issue/view_content/conversation.tmpl @@ -68,7 +68,7 @@ {{.OriginalAuthor}} {{if $.Repository.OriginalURL}} - ({{ctx.Locale.Tr "repo.migrated_from" ($.Repository.OriginalURL|Escape) ($.Repository.GetOriginalURLHostname|Escape) | Safe}}) + ({{ctx.Locale.Tr "repo.migrated_from" $.Repository.OriginalURL $.Repository.GetOriginalURLHostname}}) {{end}} {{else}} {{template "shared/user/authorlink" .Poster}} From e02245d6273c4cdcd060448e91b607f8d48d5386 Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Thu, 22 Feb 2024 14:51:17 +0800 Subject: [PATCH 11/11] Update templates/repo/issue/view_content/comments.tmpl --- templates/repo/issue/view_content/comments.tmpl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/templates/repo/issue/view_content/comments.tmpl b/templates/repo/issue/view_content/comments.tmpl index be43d6766f2bb..a10909b3fceda 100644 --- a/templates/repo/issue/view_content/comments.tmpl +++ b/templates/repo/issue/view_content/comments.tmpl @@ -382,7 +382,7 @@ {{.OriginalAuthor}} {{if $.Repository.OriginalURL}} - ({{ctx.Locale.Tr "repo.migrated_from" $.Repository.OriginalURL|Escape $.Repository.GetOriginalURLHostname}}) + ({{ctx.Locale.Tr "repo.migrated_from" $.Repository.OriginalURL $.Repository.GetOriginalURLHostname}}) {{end}} {{else}} {{template "shared/user/authorlink" .Poster}}