summaryrefslogtreecommitdiffstats
path: root/modules
diff options
context:
space:
mode:
authorAravinth Manivannan <realaravinth@batsense.net>2022-03-17 22:38:35 +0530
committerGitHub <noreply@github.com>2022-03-17 18:08:35 +0100
commitfa73cbf5a7e84fab50ad5c8f3095b4dec8b4e879 (patch)
tree69f928ac87307c36577343853e884bb721718add /modules
parenta7de80db0d69e98b3d5cf9ba0c6d7608365289bc (diff)
downloadgitea-fa73cbf5a7e84fab50ad5c8f3095b4dec8b4e879.tar.gz
gitea-fa73cbf5a7e84fab50ad5c8f3095b4dec8b4e879.zip
Store the foreign ID of issues during migration (#18446)
Storing the foreign identifier of an imported issue in the database is a prerequisite to implement idempotent migrations or mirror for issues. It is a baby step towards mirroring that introduces a new table. At the moment when an issue is created by the Gitea uploader, it fails if the issue already exists. The Gitea uploader could be modified so that, instead of failing, it looks up the database to find an existing issue. And if it does it would update the issue instead of creating a new one. However this is not currently possible because an information is missing from the database: the foreign identifier that uniquely represents the issue being migrated is not persisted. With this change, the foreign identifier is stored in the database and the Gitea uploader will then be able to run a query to figure out if a given issue being imported already exists. The implementation of mirroring for issues, pull requests, releases, etc. can be done in three steps: 1. Store an identifier for the element being mirrored (issue, pull request...) in the database (this is the purpose of these changes) 2. Modify the Gitea uploader to be able to update an existing repository with all it contains (issues, pull request...) instead of failing if it exists 3. Optimize the Gitea uploader to speed up the updates, when possible. The second step creates code that does not yet exist to enable idempotent migrations with the Gitea uploader. When a migration is done for the first time, the behavior is not changed. But when a migration is done for a repository that already exists, this new code is used to update it. The third step can use the code created in the second step to optimize and speed up migrations. For instance, when a migration is resumed, an issue that has an update time that is not more recent can be skipped and only newly created issues or updated ones will be updated. Another example of optimization could be that a webhook notifies Gitea when an issue is updated. The code triggered by the webhook would download only this issue and call the code created in the second step to update the issue, as if it was in the process of an idempotent migration. The ForeignReferences table is added to contain local and foreign ID pairs relative to a given repository. It can later be used for pull requests and other artifacts that can be mirrored. Although the foreign id could be added as a single field in issues or pull requests, it would need to be added to all tables that represent something that can be mirrored. Creating a new table makes for a simpler and more generic design. The drawback is that it requires an extra lookup to obtain the information. However, this extra information is only required during migration or mirroring and does not impact the way Gitea currently works. The foreign identifier of an issue or pull request is similar to the identifier of an external user, which is stored in reactions, issues, etc. as OriginalPosterID and so on. The representation of a user is however different and the ability of users to link their account to an external user at a later time is also a logic that is different from what is involved in mirroring or migrations. For these reasons, despite some commonalities, it is unclear at this time how the two tables (foreign reference and external user) could be merged together. The ForeignID field is extracted from the issue migration context so that it can be dumped in files with dump-repo and later restored via restore-repo. The GetAllComments downloader method is introduced to simplify the implementation and not overload the Context for the purpose of pagination. It also clarifies in which context the comments are paginated and in which context they are not. The Context interface is no longer useful for the purpose of retrieving the LocalID and ForeignID since they are now both available from the PullRequest and Issue struct. The Reviewable and Commentable interfaces replace and serve the same purpose. The Context data member of PullRequest and Issue becomes a DownloaderContext to clarify that its purpose is not to support in memory operations while the current downloader is acting but is not otherwise persisted. It is, for instance, used by the GitLab downloader to store the IsMergeRequest boolean and sort out issues. --- [source](https://lab.forgefriends.org/forgefriends/forgefriends/-/merge_requests/36) Signed-off-by: Loïc Dachary <loic@dachary.org> Co-authored-by: Loïc Dachary <loic@dachary.org>
Diffstat (limited to 'modules')
-rw-r--r--modules/migration/comment.go7
-rw-r--r--modules/migration/downloader.go15
-rw-r--r--modules/migration/issue.go58
-rw-r--r--modules/migration/null_downloader.go11
-rw-r--r--modules/migration/pullrequest.go7
-rw-r--r--modules/migration/retry_downloader.go8
-rw-r--r--modules/migration/review.go6
7 files changed, 59 insertions, 53 deletions
diff --git a/modules/migration/comment.go b/modules/migration/comment.go
index f364ffc93a..0447689b74 100644
--- a/modules/migration/comment.go
+++ b/modules/migration/comment.go
@@ -7,6 +7,13 @@ package migration
import "time"
+// Commentable can be commented upon
+type Commentable interface {
+ GetLocalIndex() int64
+ GetForeignIndex() int64
+ GetContext() DownloaderContext
+}
+
// Comment is a standard comment information
type Comment struct {
IssueIndex int64 `yaml:"issue_index"`
diff --git a/modules/migration/downloader.go b/modules/migration/downloader.go
index 90e149fb1a..7759c96056 100644
--- a/modules/migration/downloader.go
+++ b/modules/migration/downloader.go
@@ -11,13 +11,6 @@ import (
"code.gitea.io/gitea/modules/structs"
)
-// GetCommentOptions represents an options for get comment
-type GetCommentOptions struct {
- Context IssueContext
- Page int
- PageSize int
-}
-
// Downloader downloads the site repo information
type Downloader interface {
SetContext(context.Context)
@@ -27,10 +20,11 @@ type Downloader interface {
GetReleases() ([]*Release, error)
GetLabels() ([]*Label, error)
GetIssues(page, perPage int) ([]*Issue, bool, error)
- GetComments(opts GetCommentOptions) ([]*Comment, bool, error)
+ GetComments(commentable Commentable) ([]*Comment, bool, error)
+ GetAllComments(page, perPage int) ([]*Comment, bool, error)
SupportGetRepoComments() bool
GetPullRequests(page, perPage int) ([]*PullRequest, bool, error)
- GetReviews(pullRequestContext IssueContext) ([]*Review, error)
+ GetReviews(reviewable Reviewable) ([]*Review, error)
FormatCloneURL(opts MigrateOptions, remoteAddr string) (string, error)
}
@@ -39,3 +33,6 @@ type DownloaderFactory interface {
New(ctx context.Context, opts MigrateOptions) (Downloader, error)
GitServiceType() structs.GitServiceType
}
+
+// DownloaderContext has opaque information only relevant to a given downloader
+type DownloaderContext interface{}
diff --git a/modules/migration/issue.go b/modules/migration/issue.go
index 984f07d8c9..78f648dd2d 100644
--- a/modules/migration/issue.go
+++ b/modules/migration/issue.go
@@ -7,44 +7,26 @@ package migration
import "time"
-// IssueContext is used to map between local and foreign issue/PR ids.
-type IssueContext interface {
- LocalID() int64
- ForeignID() int64
-}
-
-// BasicIssueContext is a 1:1 mapping between local and foreign ids.
-type BasicIssueContext int64
-
-// LocalID gets the local id.
-func (c BasicIssueContext) LocalID() int64 {
- return int64(c)
-}
-
-// ForeignID gets the foreign id.
-func (c BasicIssueContext) ForeignID() int64 {
- return int64(c)
-}
-
// Issue is a standard issue information
type Issue struct {
- Number int64 `json:"number"`
- PosterID int64 `yaml:"poster_id" json:"poster_id"`
- PosterName string `yaml:"poster_name" json:"poster_name"`
- PosterEmail string `yaml:"poster_email" json:"poster_email"`
- Title string `json:"title"`
- Content string `json:"content"`
- Ref string `json:"ref"`
- Milestone string `json:"milestone"`
- State string `json:"state"` // closed, open
- IsLocked bool `yaml:"is_locked" json:"is_locked"`
- Created time.Time `json:"created"`
- Updated time.Time `json:"updated"`
- Closed *time.Time `json:"closed"`
- Labels []*Label `json:"labels"`
- Reactions []*Reaction `json:"reactions"`
- Assignees []string `json:"assignees"`
- Context IssueContext `yaml:"-"`
+ Number int64 `json:"number"`
+ PosterID int64 `yaml:"poster_id" json:"poster_id"`
+ PosterName string `yaml:"poster_name" json:"poster_name"`
+ PosterEmail string `yaml:"poster_email" json:"poster_email"`
+ Title string `json:"title"`
+ Content string `json:"content"`
+ Ref string `json:"ref"`
+ Milestone string `json:"milestone"`
+ State string `json:"state"` // closed, open
+ IsLocked bool `yaml:"is_locked" json:"is_locked"`
+ Created time.Time `json:"created"`
+ Updated time.Time `json:"updated"`
+ Closed *time.Time `json:"closed"`
+ Labels []*Label `json:"labels"`
+ Reactions []*Reaction `json:"reactions"`
+ Assignees []string `json:"assignees"`
+ ForeignIndex int64 `json:"foreign_id"`
+ Context DownloaderContext `yaml:"-"`
}
// GetExternalName ExternalUserMigrated interface
@@ -52,3 +34,7 @@ func (i *Issue) GetExternalName() string { return i.PosterName }
// GetExternalID ExternalUserMigrated interface
func (i *Issue) GetExternalID() int64 { return i.PosterID }
+
+func (i *Issue) GetLocalIndex() int64 { return i.Number }
+func (i *Issue) GetForeignIndex() int64 { return i.ForeignIndex }
+func (i *Issue) GetContext() DownloaderContext { return i.Context }
diff --git a/modules/migration/null_downloader.go b/modules/migration/null_downloader.go
index 6192870873..32da720f16 100644
--- a/modules/migration/null_downloader.go
+++ b/modules/migration/null_downloader.go
@@ -47,18 +47,23 @@ func (n NullDownloader) GetIssues(page, perPage int) ([]*Issue, bool, error) {
return nil, false, &ErrNotSupported{Entity: "Issues"}
}
-// GetComments returns comments according the options
-func (n NullDownloader) GetComments(GetCommentOptions) ([]*Comment, bool, error) {
+// GetComments returns comments of an issue or PR
+func (n NullDownloader) GetComments(commentable Commentable) ([]*Comment, bool, error) {
return nil, false, &ErrNotSupported{Entity: "Comments"}
}
+// GetAllComments returns paginated comments
+func (n NullDownloader) GetAllComments(page, perPage int) ([]*Comment, bool, error) {
+ return nil, false, &ErrNotSupported{Entity: "AllComments"}
+}
+
// GetPullRequests returns pull requests according page and perPage
func (n NullDownloader) GetPullRequests(page, perPage int) ([]*PullRequest, bool, error) {
return nil, false, &ErrNotSupported{Entity: "PullRequests"}
}
// GetReviews returns pull requests review
-func (n NullDownloader) GetReviews(pullRequestContext IssueContext) ([]*Review, error) {
+func (n NullDownloader) GetReviews(reviewable Reviewable) ([]*Review, error) {
return nil, &ErrNotSupported{Entity: "Reviews"}
}
diff --git a/modules/migration/pullrequest.go b/modules/migration/pullrequest.go
index 7a681940a7..eaa0dd45e2 100644
--- a/modules/migration/pullrequest.go
+++ b/modules/migration/pullrequest.go
@@ -35,9 +35,14 @@ type PullRequest struct {
Assignees []string
IsLocked bool `yaml:"is_locked"`
Reactions []*Reaction
- Context IssueContext `yaml:"-"`
+ ForeignIndex int64
+ Context DownloaderContext `yaml:"-"`
}
+func (p *PullRequest) GetLocalIndex() int64 { return p.Number }
+func (p *PullRequest) GetForeignIndex() int64 { return p.ForeignIndex }
+func (p *PullRequest) GetContext() DownloaderContext { return p.Context }
+
// IsForkPullRequest returns true if the pull request from a forked repository but not the same repository
func (p *PullRequest) IsForkPullRequest() bool {
return p.Head.RepoPath() != p.Base.RepoPath()
diff --git a/modules/migration/retry_downloader.go b/modules/migration/retry_downloader.go
index 1095a26891..2e40c102be 100644
--- a/modules/migration/retry_downloader.go
+++ b/modules/migration/retry_downloader.go
@@ -148,7 +148,7 @@ func (d *RetryDownloader) GetIssues(page, perPage int) ([]*Issue, bool, error) {
}
// GetComments returns a repository's comments with retry
-func (d *RetryDownloader) GetComments(opts GetCommentOptions) ([]*Comment, bool, error) {
+func (d *RetryDownloader) GetComments(commentable Commentable) ([]*Comment, bool, error) {
var (
comments []*Comment
isEnd bool
@@ -156,7 +156,7 @@ func (d *RetryDownloader) GetComments(opts GetCommentOptions) ([]*Comment, bool,
)
err = d.retry(func() error {
- comments, isEnd, err = d.Downloader.GetComments(opts)
+ comments, isEnd, err = d.Downloader.GetComments(commentable)
return err
})
@@ -180,14 +180,14 @@ func (d *RetryDownloader) GetPullRequests(page, perPage int) ([]*PullRequest, bo
}
// GetReviews returns pull requests reviews
-func (d *RetryDownloader) GetReviews(pullRequestContext IssueContext) ([]*Review, error) {
+func (d *RetryDownloader) GetReviews(reviewable Reviewable) ([]*Review, error) {
var (
reviews []*Review
err error
)
err = d.retry(func() error {
- reviews, err = d.Downloader.GetReviews(pullRequestContext)
+ reviews, err = d.Downloader.GetReviews(reviewable)
return err
})
diff --git a/modules/migration/review.go b/modules/migration/review.go
index 85795385e9..e4db33d98f 100644
--- a/modules/migration/review.go
+++ b/modules/migration/review.go
@@ -6,6 +6,12 @@ package migration
import "time"
+// Reviewable can be reviewed
+type Reviewable interface {
+ GetLocalIndex() int64
+ GetForeignIndex() int64
+}
+
// enumerate all review states
const (
ReviewStatePending = "PENDING"