Skip to content

Commit 6e59665

Browse files
authored
Properly migrate target branch change GitLab comment (#29340)
GitLab generates "system notes" whenever an event happens within the platform. Unlike Gitea, those events are stored and retrieved as text comments with no semantic details. The only way to tell whether a comment was generated in this manner is the `system` flag on the note type. This PR adds detection for a new specific kind of event: Changing the target branch of a PR. When detected, it is downloaded using Gitea's type for this event, and eventually uploaded into Gitea in the expected format, i.e. with no text content in the comment. This PR also updates the template used to render comments to add support for migrated comments of this type. ref: https://gitlab.com/gitlab-org/gitlab/-/blob/11bd6dc826e0bea2832324a1d7356949a9398884/app/services/system_notes/merge_requests_service.rb#L102
1 parent b79c304 commit 6e59665

File tree

4 files changed

+50
-8
lines changed

4 files changed

+50
-8
lines changed

services/migrations/gitea_uploader.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -492,10 +492,16 @@ func (g *GiteaLocalUploader) CreateComments(comments ...*base.Comment) error {
492492
}
493493
case issues_model.CommentTypeChangeTitle:
494494
if comment.Meta["OldTitle"] != nil {
495-
cm.OldTitle = fmt.Sprintf("%s", comment.Meta["OldTitle"])
495+
cm.OldTitle = fmt.Sprint(comment.Meta["OldTitle"])
496496
}
497497
if comment.Meta["NewTitle"] != nil {
498-
cm.NewTitle = fmt.Sprintf("%s", comment.Meta["NewTitle"])
498+
cm.NewTitle = fmt.Sprint(comment.Meta["NewTitle"])
499+
}
500+
case issues_model.CommentTypeChangeTargetBranch:
501+
if comment.Meta["OldRef"] != nil && comment.Meta["NewRef"] != nil {
502+
cm.OldRef = fmt.Sprint(comment.Meta["OldRef"])
503+
cm.NewRef = fmt.Sprint(comment.Meta["NewRef"])
504+
cm.Content = ""
499505
}
500506
case issues_model.CommentTypePRScheduledToAutoMerge, issues_model.CommentTypePRUnScheduledToAutoMerge:
501507
cm.Content = ""

services/migrations/gitlab.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"net/http"
1212
"net/url"
1313
"path"
14+
"regexp"
1415
"strings"
1516
"time"
1617

@@ -519,6 +520,8 @@ func (g *GitlabDownloader) GetComments(commentable base.Commentable) ([]*base.Co
519520
return allComments, true, nil
520521
}
521522

523+
var targetBranchChangeRegexp = regexp.MustCompile("^changed target branch from `(.*?)` to `(.*?)`$")
524+
522525
func (g *GitlabDownloader) convertNoteToComment(localIndex int64, note *gitlab.Note) *base.Comment {
523526
comment := &base.Comment{
524527
IssueIndex: localIndex,
@@ -528,11 +531,16 @@ func (g *GitlabDownloader) convertNoteToComment(localIndex int64, note *gitlab.N
528531
PosterEmail: note.Author.Email,
529532
Content: note.Body,
530533
Created: *note.CreatedAt,
534+
Meta: map[string]any{},
531535
}
532536

533537
// Try to find the underlying event of system notes.
534538
if note.System {
535-
if strings.HasPrefix(note.Body, "enabled an automatic merge") {
539+
if match := targetBranchChangeRegexp.FindStringSubmatch(note.Body); match != nil {
540+
comment.CommentType = issues_model.CommentTypeChangeTargetBranch.String()
541+
comment.Meta["OldRef"] = match[1]
542+
comment.Meta["NewRef"] = match[2]
543+
} else if strings.HasPrefix(note.Body, "enabled an automatic merge") {
536544
comment.CommentType = issues_model.CommentTypePRScheduledToAutoMerge.String()
537545
} else if note.Body == "canceled the automatic merge" {
538546
comment.CommentType = issues_model.CommentTypePRUnScheduledToAutoMerge.String()

services/migrations/gitlab_test.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -545,7 +545,8 @@ func TestNoteToComment(t *testing.T) {
545545
notes := []gitlab.Note{
546546
makeTestNote(1, "This is a regular comment", false),
547547
makeTestNote(2, "enabled an automatic merge for abcd1234", true),
548-
makeTestNote(3, "canceled the automatic merge", true),
548+
makeTestNote(3, "changed target branch from `master` to `main`", true),
549+
makeTestNote(4, "canceled the automatic merge", true),
549550
}
550551
comments := []base.Comment{{
551552
IssueIndex: 17,
@@ -556,6 +557,7 @@ func TestNoteToComment(t *testing.T) {
556557
CommentType: "",
557558
Content: "This is a regular comment",
558559
Created: now,
560+
Meta: map[string]any{},
559561
}, {
560562
IssueIndex: 17,
561563
Index: 2,
@@ -565,15 +567,30 @@ func TestNoteToComment(t *testing.T) {
565567
CommentType: "pull_scheduled_merge",
566568
Content: "enabled an automatic merge for abcd1234",
567569
Created: now,
570+
Meta: map[string]any{},
568571
}, {
569572
IssueIndex: 17,
570573
Index: 3,
571574
PosterID: 72,
572575
PosterName: "test",
573576
PosterEmail: "[email protected]",
577+
CommentType: "change_target_branch",
578+
Content: "changed target branch from `master` to `main`",
579+
Created: now,
580+
Meta: map[string]any{
581+
"OldRef": "master",
582+
"NewRef": "main",
583+
},
584+
}, {
585+
IssueIndex: 17,
586+
Index: 4,
587+
PosterID: 72,
588+
PosterName: "test",
589+
PosterEmail: "[email protected]",
574590
CommentType: "pull_cancel_scheduled_merge",
575591
Content: "canceled the automatic merge",
576592
Created: now,
593+
Meta: map[string]any{},
577594
}}
578595

579596
for i, note := range notes {

templates/repo/issue/view_content/comments.tmpl

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -365,8 +365,7 @@
365365
{{else if eq .Type 22}}
366366
<div class="timeline-item-group" id="{{.HashTag}}">
367367
<div class="timeline-item event">
368-
{{if .OriginalAuthor}}
369-
{{else}}
368+
{{if not .OriginalAuthor}}
370369
{{/* Some timeline avatars need a offset to correctly align with their speech
371370
bubble. The condition depends on review type and for positive reviews whether
372371
there is a comment element or not */}}
@@ -495,9 +494,21 @@
495494
{{else if eq .Type 25}}
496495
<div class="timeline-item event">
497496
<span class="badge">{{svg "octicon-git-branch"}}</span>
498-
{{template "shared/user/avatarlink" dict "user" .Poster}}
497+
{{if not .OriginalAuthor}}
498+
{{template "shared/user/avatarlink" dict "user" .Poster}}
499+
{{end}}
499500
<span class="text grey muted-links">
500-
<a{{if gt .Poster.ID 0}} href="{{.Poster.HomeLink}}"{{end}}>{{.Poster.Name}}</a>
501+
{{if .OriginalAuthor}}
502+
<span class="text black">
503+
{{svg (MigrationIcon $.Repository.GetOriginalURLHostname)}}
504+
{{.OriginalAuthor}}
505+
</span>
506+
{{if $.Repository.OriginalURL}}
507+
<span class="migrate">({{ctx.Locale.Tr "repo.migrated_from" $.Repository.OriginalURL $.Repository.GetOriginalURLHostname}})</span>
508+
{{end}}
509+
{{else}}
510+
{{template "shared/user/authorlink" .Poster}}
511+
{{end}}
501512
{{ctx.Locale.Tr "repo.pulls.change_target_branch_at" (.OldRef|Escape) (.NewRef|Escape) $createdStr}}
502513
</span>
503514
</div>

0 commit comments

Comments
 (0)