Skip to content

Commit 4858259

Browse files
committed
Properly migrate target branch change GitLab comment
1 parent 7fbdb60 commit 4858259

File tree

4 files changed

+48
-4
lines changed

4 files changed

+48
-4
lines changed

services/migrations/gitea_uploader.go

+6
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,12 @@ func (g *GiteaLocalUploader) CreateComments(comments ...*base.Comment) error {
487487
if comment.Meta["NewTitle"] != nil {
488488
cm.NewTitle = fmt.Sprintf("%s", comment.Meta["NewTitle"])
489489
}
490+
case issues_model.CommentTypeChangeTargetBranch:
491+
if comment.Meta["OldRef"] != nil && comment.Meta["NewRef"] != nil {
492+
cm.OldRef = fmt.Sprintf("%s", comment.Meta["OldRef"])
493+
cm.NewRef = fmt.Sprintf("%s", comment.Meta["NewRef"])
494+
cm.Content = ""
495+
}
490496
case issues_model.CommentTypePRScheduledToAutoMerge, issues_model.CommentTypePRUnScheduledToAutoMerge:
491497
cm.Content = ""
492498
default:

services/migrations/gitlab.go

+9-1
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

+18-1
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: "test@example.com",
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: "test@example.com",
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

+15-2
Original file line numberDiff line numberDiff line change
@@ -495,9 +495,22 @@
495495
{{else if eq .Type 25}}
496496
<div class="timeline-item event">
497497
<span class="badge">{{svg "octicon-git-branch"}}</span>
498-
{{template "shared/user/avatarlink" dict "user" .Poster}}
498+
{{if .OriginalAuthor}}
499+
{{else}}
500+
{{template "shared/user/avatarlink" dict "user" .Poster}}
501+
{{end}}
499502
<span class="text grey muted-links">
500-
<a{{if gt .Poster.ID 0}} href="{{.Poster.HomeLink}}"{{end}}>{{.Poster.Name}}</a>
503+
{{if .OriginalAuthor}}
504+
<span class="text black">
505+
{{svg (MigrationIcon $.Repository.GetOriginalURLHostname)}}
506+
{{.OriginalAuthor}}
507+
</span>
508+
{{if $.Repository.OriginalURL}}
509+
<span class="migrate">({{ctx.Locale.Tr "repo.migrated_from" ($.Repository.OriginalURL|Escape) ($.Repository.GetOriginalURLHostname|Escape) | Safe}})</span>
510+
{{end}}
511+
{{else}}
512+
{{template "shared/user/authorlink" .Poster}}
513+
{{end}}
501514
{{ctx.Locale.Tr "repo.pulls.change_target_branch_at" (.OldRef|Escape) (.NewRef|Escape) $createdStr}}
502515
</span>
503516
</div>

0 commit comments

Comments
 (0)