Skip to content

Commit 151b1a9

Browse files
authored
Support importing comment types (#22510)
This commit adds support for specifying comment types when importing with `gitea restore-repo`. It makes it possible to import issue changes, such as "title changed" or "assigned user changed". An earlier version of this pull request was made by Matti Ranta, in https://future.projects.blender.org/blender-migration/gitea-bf/pulls/3 There are two changes with regard to Matti's original code: 1. The comment type was an `int64` in Matti's code, and is now using a string. This makes it possible to use `comment_type: title`, which is more reliable and future-proof than an index into an internal list in the Gitea Go code. 2. Matti's code also had support for including labels, but in a way that would require knowing the database ID of the labels before the import even starts, which is impossible. This can be solved by using label names instead of IDs; for simplicity I I left that out of this PR.
1 parent cdf53fa commit 151b1a9

File tree

4 files changed

+39
-2
lines changed

4 files changed

+39
-2
lines changed

models/issues/comment.go

+9
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,15 @@ func (t CommentType) String() string {
175175
return commentStrings[t]
176176
}
177177

178+
func AsCommentType(typeName string) CommentType {
179+
for index, name := range commentStrings {
180+
if typeName == name {
181+
return CommentType(index)
182+
}
183+
}
184+
return CommentTypeUnknown
185+
}
186+
178187
// RoleDescriptor defines comment tag type
179188
type RoleDescriptor int
180189

models/issues/comment_test.go

+7
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,10 @@ func TestFetchCodeComments(t *testing.T) {
6262
assert.NoError(t, err)
6363
assert.Len(t, res, 1)
6464
}
65+
66+
func TestAsCommentType(t *testing.T) {
67+
assert.Equal(t, issues_model.CommentTypeUnknown, issues_model.AsCommentType(""))
68+
assert.Equal(t, issues_model.CommentTypeUnknown, issues_model.AsCommentType("nonsense"))
69+
assert.Equal(t, issues_model.CommentTypeComment, issues_model.AsCommentType("comment"))
70+
assert.Equal(t, issues_model.CommentTypePRUnScheduledToAutoMerge, issues_model.AsCommentType("pull_cancel_scheduled_merge"))
71+
}

modules/migration/comment.go

+2
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,15 @@ type Commentable interface {
1717
type Comment struct {
1818
IssueIndex int64 `yaml:"issue_index"`
1919
Index int64
20+
CommentType string `yaml:"comment_type"` // see `commentStrings` in models/issues/comment.go
2021
PosterID int64 `yaml:"poster_id"`
2122
PosterName string `yaml:"poster_name"`
2223
PosterEmail string `yaml:"poster_email"`
2324
Created time.Time
2425
Updated time.Time
2526
Content string
2627
Reactions []*Reaction
28+
Meta map[string]interface{} `yaml:"meta,omitempty"` // see models/issues/comment.go for fields in Comment struct
2729
}
2830

2931
// GetExternalName ExternalUserMigrated interface

services/migrations/gitea_uploader.go

+21-2
Original file line numberDiff line numberDiff line change
@@ -454,15 +454,34 @@ func (g *GiteaLocalUploader) CreateComments(comments ...*base.Comment) error {
454454
if comment.Updated.IsZero() {
455455
comment.Updated = comment.Created
456456
}
457-
457+
if comment.CommentType == "" {
458+
// if type field is missing, then assume a normal comment
459+
comment.CommentType = issues_model.CommentTypeComment.String()
460+
}
458461
cm := issues_model.Comment{
459462
IssueID: issue.ID,
460-
Type: issues_model.CommentTypeComment,
463+
Type: issues_model.AsCommentType(comment.CommentType),
461464
Content: comment.Content,
462465
CreatedUnix: timeutil.TimeStamp(comment.Created.Unix()),
463466
UpdatedUnix: timeutil.TimeStamp(comment.Updated.Unix()),
464467
}
465468

469+
switch cm.Type {
470+
case issues_model.CommentTypeAssignees:
471+
cm.AssigneeID = comment.Meta["AssigneeID"].(int64)
472+
if comment.Meta["RemovedAssigneeID"] != nil {
473+
cm.RemovedAssignee = true
474+
}
475+
case issues_model.CommentTypeChangeTitle:
476+
if comment.Meta["OldTitle"] != nil {
477+
cm.OldTitle = fmt.Sprintf("%s", comment.Meta["OldTitle"])
478+
}
479+
if comment.Meta["NewTitle"] != nil {
480+
cm.NewTitle = fmt.Sprintf("%s", comment.Meta["NewTitle"])
481+
}
482+
default:
483+
}
484+
466485
if err := g.remapUser(comment, &cm); err != nil {
467486
return err
468487
}

0 commit comments

Comments
 (0)