go-gitea / gitea

Git with a cup of tea! Painless self-hosted all-in-one software development service, including Git hosting, code review, team collaboration, package registry and CI/CD
https://gitea.com
MIT License
44.15k stars 5.41k forks source link

`restore-repo` does not correctly load "issue foreign ID"s #22414

Open sybrenstuvel opened 1 year ago

sybrenstuvel commented 1 year ago

Description

In short: the struct field migration.Issue.ForeignIndex is missing a yaml:"foreign_id" annotation.

When importing a dump from YAML files via gitea restore-repo, the "foreign ID" of an issue is used to load that issue's comments (see func (r *RepositoryRestorer) GetComments(...)). On first glance this ID should be stored in issue.yml in the foreign_id field, but because there is only a JSON tag and not a YAML one, effectively Gitea is reading it from foreignindex.

This issue was introduced in fa73cbf5a7e84fab50ad5c8f3095b4dec8b4e879. Where all the fields that should get an underscore in the name are annotated for both YAML and JSON, the ForeignIndex is not:

type Issue struct {
    PosterID     int64             `yaml:"poster_id" json:"poster_id"`
    PosterName   string            `yaml:"poster_name" json:"poster_name"`
    PosterEmail  string            `yaml:"poster_email" json:"poster_email"`
    // ...
    IsLocked     bool              `yaml:"is_locked" json:"is_locked"`
    // ...
    ForeignIndex int64             `json:"foreign_id"`
}

Gitea Version

1.19.0+dev-289-gb36854df3

Can you reproduce the bug on the Gitea demo site?

No

Log Gist

No response

Screenshots

No response

Git Version

2.34.1

Operating System

No response

How are you running Gitea?

Built Gitea myself from its main branch.

Database

PostgreSQL

techknowlogick commented 1 year ago

The fix is:

From 128e4f237cf952f9219f32bdfe86ffd40cec0f4c Mon Sep 17 00:00:00 2001
From: Matti-Ranta <matti-ranta@noreply.localhost>
Date: Wed, 21 Sep 2022 16:32:21 +0200
Subject: [PATCH] accept yaml for foreign_id

---
 modules/migration/issue.go | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/modules/migration/issue.go b/modules/migration/issue.go
index 78f648dd2..a23812eaf 100644
--- a/modules/migration/issue.go
+++ b/modules/migration/issue.go
@@ -25,7 +25,7 @@ type Issue struct {
    Labels       []*Label          `json:"labels"`
    Reactions    []*Reaction       `json:"reactions"`
    Assignees    []string          `json:"assignees"`
-   ForeignIndex int64             `json:"foreign_id"`
+   ForeignIndex int64             `yaml:"foreign_id" json:"foreign_id"`
    Context      DownloaderContext `yaml:"-"`
 }

-- 
2.30.2

and

From 7620c236a86d9cd9771e805ca4ee18e56cb48ede Mon Sep 17 00:00:00 2001
From: Matti-Ranta <matti-ranta@noreply.localhost>
Date: Wed, 21 Sep 2022 16:24:01 +0200
Subject: [PATCH] if id is 0, then use local

---
 services/migrations/gitea_uploader.go | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/services/migrations/gitea_uploader.go b/services/migrations/gitea_uploader.go
index 21f587604..795ff4974 100644
--- a/services/migrations/gitea_uploader.go
+++ b/services/migrations/gitea_uploader.go
@@ -411,6 +411,10 @@ func (g *GiteaLocalUploader) CreateIssues(issues ...*base.Issue) error {
                Type:         foreignreference.TypeIssue,
            },
        }
+       
+       if is.ForeignReference.ForeignIndex == "0" {
+           is.ForeignReference.ForeignIndex = strconv.FormatInt(is.Index, 10)
+       }

        if err := g.remapUser(issue, &is); err != nil {
            return err
-- 
2.30.2
techknowlogick commented 1 year ago

The above will resolve the issue in 1.18, although foreign reference is removed in 1.19 (https://github.com/go-gitea/gitea/pull/21721)