From 4858259b46fd0e65a29a06ec4b210eaec64829ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Bru=CC=88ckner?= Date: Thu, 2 Nov 2023 20:50:00 -0700 Subject: [PATCH 1/3] Properly migrate target branch change GitLab comment --- services/migrations/gitea_uploader.go | 6 ++++++ services/migrations/gitlab.go | 10 +++++++++- services/migrations/gitlab_test.go | 19 ++++++++++++++++++- .../repo/issue/view_content/comments.tmpl | 17 +++++++++++++++-- 4 files changed, 48 insertions(+), 4 deletions(-) diff --git a/services/migrations/gitea_uploader.go b/services/migrations/gitea_uploader.go index 2891977c7c2ec..c333ee5266540 100644 --- a/services/migrations/gitea_uploader.go +++ b/services/migrations/gitea_uploader.go @@ -487,6 +487,12 @@ func (g *GiteaLocalUploader) CreateComments(comments ...*base.Comment) error { if comment.Meta["NewTitle"] != nil { cm.NewTitle = fmt.Sprintf("%s", comment.Meta["NewTitle"]) } + case issues_model.CommentTypeChangeTargetBranch: + if comment.Meta["OldRef"] != nil && comment.Meta["NewRef"] != nil { + cm.OldRef = fmt.Sprintf("%s", comment.Meta["OldRef"]) + cm.NewRef = fmt.Sprintf("%s", comment.Meta["NewRef"]) + cm.Content = "" + } case issues_model.CommentTypePRScheduledToAutoMerge, issues_model.CommentTypePRUnScheduledToAutoMerge: cm.Content = "" default: diff --git a/services/migrations/gitlab.go b/services/migrations/gitlab.go index d08eaf0f84fcd..5e49ae6d57e6b 100644 --- a/services/migrations/gitlab.go +++ b/services/migrations/gitlab.go @@ -11,6 +11,7 @@ import ( "net/http" "net/url" "path" + "regexp" "strings" "time" @@ -519,6 +520,8 @@ func (g *GitlabDownloader) GetComments(commentable base.Commentable) ([]*base.Co return allComments, true, nil } +var targetBranchChangeRegexp = regexp.MustCompile("^changed target branch from `(.*?)` to `(.*?)`$") + func (g *GitlabDownloader) convertNoteToComment(localIndex int64, note *gitlab.Note) *base.Comment { comment := &base.Comment{ IssueIndex: localIndex, @@ -528,11 +531,16 @@ func (g *GitlabDownloader) convertNoteToComment(localIndex int64, note *gitlab.N PosterEmail: note.Author.Email, Content: note.Body, Created: *note.CreatedAt, + Meta: map[string]any{}, } // Try to find the underlying event of system notes. if note.System { - if strings.HasPrefix(note.Body, "enabled an automatic merge") { + if match := targetBranchChangeRegexp.FindStringSubmatch(note.Body); match != nil { + comment.CommentType = issues_model.CommentTypeChangeTargetBranch.String() + comment.Meta["OldRef"] = match[1] + comment.Meta["NewRef"] = match[2] + } else 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() diff --git a/services/migrations/gitlab_test.go b/services/migrations/gitlab_test.go index 2b87a1dfe63d7..0b9eeaed54085 100644 --- a/services/migrations/gitlab_test.go +++ b/services/migrations/gitlab_test.go @@ -545,7 +545,8 @@ func TestNoteToComment(t *testing.T) { 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), + makeTestNote(3, "changed target branch from `master` to `main`", true), + makeTestNote(4, "canceled the automatic merge", true), } comments := []base.Comment{{ IssueIndex: 17, @@ -556,6 +557,7 @@ func TestNoteToComment(t *testing.T) { CommentType: "", Content: "This is a regular comment", Created: now, + Meta: map[string]any{}, }, { IssueIndex: 17, Index: 2, @@ -565,15 +567,30 @@ func TestNoteToComment(t *testing.T) { CommentType: "pull_scheduled_merge", Content: "enabled an automatic merge for abcd1234", Created: now, + Meta: map[string]any{}, }, { IssueIndex: 17, Index: 3, PosterID: 72, PosterName: "test", PosterEmail: "test@example.com", + CommentType: "change_target_branch", + Content: "changed target branch from `master` to `main`", + Created: now, + Meta: map[string]any{ + "OldRef": "master", + "NewRef": "main", + }, + }, { + IssueIndex: 17, + Index: 4, + PosterID: 72, + PosterName: "test", + PosterEmail: "test@example.com", CommentType: "pull_cancel_scheduled_merge", Content: "canceled the automatic merge", Created: now, + Meta: map[string]any{}, }} for i, note := range notes { diff --git a/templates/repo/issue/view_content/comments.tmpl b/templates/repo/issue/view_content/comments.tmpl index 597f025470e64..8119f980b5f55 100644 --- a/templates/repo/issue/view_content/comments.tmpl +++ b/templates/repo/issue/view_content/comments.tmpl @@ -495,9 +495,22 @@ {{else if eq .Type 25}}
{{svg "octicon-git-branch"}} - {{template "shared/user/avatarlink" dict "user" .Poster}} + {{if .OriginalAuthor}} + {{else}} + {{template "shared/user/avatarlink" dict "user" .Poster}} + {{end}} - {{.Poster.Name}} + {{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}} {{ctx.Locale.Tr "repo.pulls.change_target_branch_at" (.OldRef|Escape) (.NewRef|Escape) $createdStr}}
From 3a54c6f2252c368a9469c4564b1963d330fe6669 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Bru=CC=88ckner?= Date: Thu, 22 Feb 2024 23:27:36 -0800 Subject: [PATCH 2/3] Remove unnecessary escape and safe --- 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 8119f980b5f55..faecfbd1458de 100644 --- a/templates/repo/issue/view_content/comments.tmpl +++ b/templates/repo/issue/view_content/comments.tmpl @@ -506,7 +506,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 9fa95dd6b603c31eea72d868f058c63303ad5288 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Bru=CC=88ckner?= Date: Fri, 23 Feb 2024 21:29:56 -0800 Subject: [PATCH 3/3] Address comments --- services/migrations/gitea_uploader.go | 8 ++++---- templates/repo/issue/view_content/comments.tmpl | 6 ++---- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/services/migrations/gitea_uploader.go b/services/migrations/gitea_uploader.go index c333ee5266540..40f446898bb27 100644 --- a/services/migrations/gitea_uploader.go +++ b/services/migrations/gitea_uploader.go @@ -482,15 +482,15 @@ func (g *GiteaLocalUploader) CreateComments(comments ...*base.Comment) error { } case issues_model.CommentTypeChangeTitle: if comment.Meta["OldTitle"] != nil { - cm.OldTitle = fmt.Sprintf("%s", comment.Meta["OldTitle"]) + cm.OldTitle = fmt.Sprint(comment.Meta["OldTitle"]) } if comment.Meta["NewTitle"] != nil { - cm.NewTitle = fmt.Sprintf("%s", comment.Meta["NewTitle"]) + cm.NewTitle = fmt.Sprint(comment.Meta["NewTitle"]) } case issues_model.CommentTypeChangeTargetBranch: if comment.Meta["OldRef"] != nil && comment.Meta["NewRef"] != nil { - cm.OldRef = fmt.Sprintf("%s", comment.Meta["OldRef"]) - cm.NewRef = fmt.Sprintf("%s", comment.Meta["NewRef"]) + cm.OldRef = fmt.Sprint(comment.Meta["OldRef"]) + cm.NewRef = fmt.Sprint(comment.Meta["NewRef"]) cm.Content = "" } case issues_model.CommentTypePRScheduledToAutoMerge, issues_model.CommentTypePRUnScheduledToAutoMerge: diff --git a/templates/repo/issue/view_content/comments.tmpl b/templates/repo/issue/view_content/comments.tmpl index faecfbd1458de..7bd7e8c35d6d0 100644 --- a/templates/repo/issue/view_content/comments.tmpl +++ b/templates/repo/issue/view_content/comments.tmpl @@ -365,8 +365,7 @@ {{else if eq .Type 22}}
- {{if .OriginalAuthor}} - {{else}} + {{if not .OriginalAuthor}} {{/* Some timeline avatars need a offset to correctly align with their speech bubble. The condition depends on review type and for positive reviews whether there is a comment element or not */}} @@ -495,8 +494,7 @@ {{else if eq .Type 25}}
{{svg "octicon-git-branch"}} - {{if .OriginalAuthor}} - {{else}} + {{if not .OriginalAuthor}} {{template "shared/user/avatarlink" dict "user" .Poster}} {{end}}