2021-12-02 17:24:02 -05:00
|
|
|
// Copyright 2021 The Gitea Authors. All rights reserved.
|
2022-11-27 13:20:29 -05:00
|
|
|
// SPDX-License-Identifier: MIT
|
2021-12-02 17:24:02 -05:00
|
|
|
|
|
|
|
package migrations
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/xml"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"code.gitea.io/gitea/modules/log"
|
|
|
|
base "code.gitea.io/gitea/modules/migration"
|
|
|
|
"code.gitea.io/gitea/modules/proxy"
|
|
|
|
"code.gitea.io/gitea/modules/structs"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
_ base.Downloader = &CodebaseDownloader{}
|
|
|
|
_ base.DownloaderFactory = &CodebaseDownloaderFactory{}
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
RegisterDownloaderFactory(&CodebaseDownloaderFactory{})
|
|
|
|
}
|
|
|
|
|
|
|
|
// CodebaseDownloaderFactory defines a downloader factory
|
2022-01-20 12:46:10 -05:00
|
|
|
type CodebaseDownloaderFactory struct{}
|
2021-12-02 17:24:02 -05:00
|
|
|
|
|
|
|
// New returns a downloader related to this factory according MigrateOptions
|
|
|
|
func (f *CodebaseDownloaderFactory) New(ctx context.Context, opts base.MigrateOptions) (base.Downloader, error) {
|
|
|
|
u, err := url.Parse(opts.CloneAddr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
u.User = nil
|
|
|
|
|
|
|
|
fields := strings.Split(strings.Trim(u.Path, "/"), "/")
|
|
|
|
if len(fields) != 2 {
|
|
|
|
return nil, fmt.Errorf("invalid path: %s", u.Path)
|
|
|
|
}
|
|
|
|
project := fields[0]
|
|
|
|
repoName := strings.TrimSuffix(fields[1], ".git")
|
|
|
|
|
|
|
|
log.Trace("Create Codebase downloader. BaseURL: %v RepoName: %s", u, repoName)
|
|
|
|
|
|
|
|
return NewCodebaseDownloader(ctx, u, project, repoName, opts.AuthUsername, opts.AuthPassword), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GitServiceType returns the type of git service
|
|
|
|
func (f *CodebaseDownloaderFactory) GitServiceType() structs.GitServiceType {
|
|
|
|
return structs.CodebaseService
|
|
|
|
}
|
|
|
|
|
|
|
|
type codebaseUser struct {
|
|
|
|
ID int64 `json:"id"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
Email string `json:"email"`
|
|
|
|
}
|
|
|
|
|
2022-01-10 04:32:37 -05:00
|
|
|
// CodebaseDownloader implements a Downloader interface to get repository information
|
2021-12-02 17:24:02 -05:00
|
|
|
// from Codebase
|
|
|
|
type CodebaseDownloader struct {
|
|
|
|
base.NullDownloader
|
|
|
|
ctx context.Context
|
|
|
|
client *http.Client
|
|
|
|
baseURL *url.URL
|
|
|
|
projectURL *url.URL
|
|
|
|
project string
|
|
|
|
repoName string
|
|
|
|
maxIssueIndex int64
|
|
|
|
userMap map[int64]*codebaseUser
|
|
|
|
commitMap map[string]string
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetContext set context
|
|
|
|
func (d *CodebaseDownloader) SetContext(ctx context.Context) {
|
|
|
|
d.ctx = ctx
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewCodebaseDownloader creates a new downloader
|
|
|
|
func NewCodebaseDownloader(ctx context.Context, projectURL *url.URL, project, repoName, username, password string) *CodebaseDownloader {
|
|
|
|
baseURL, _ := url.Parse("https://api3.codebasehq.com")
|
|
|
|
|
2022-01-20 12:46:10 -05:00
|
|
|
downloader := &CodebaseDownloader{
|
2021-12-02 17:24:02 -05:00
|
|
|
ctx: ctx,
|
|
|
|
baseURL: baseURL,
|
|
|
|
projectURL: projectURL,
|
|
|
|
project: project,
|
|
|
|
repoName: repoName,
|
|
|
|
client: &http.Client{
|
|
|
|
Transport: &http.Transport{
|
|
|
|
Proxy: func(req *http.Request) (*url.URL, error) {
|
|
|
|
if len(username) > 0 && len(password) > 0 {
|
|
|
|
req.SetBasicAuth(username, password)
|
|
|
|
}
|
|
|
|
return proxy.Proxy()(req)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
userMap: make(map[int64]*codebaseUser),
|
|
|
|
commitMap: make(map[string]string),
|
|
|
|
}
|
|
|
|
|
2022-09-04 05:47:56 -05:00
|
|
|
log.Trace("Create Codebase downloader. BaseURL: %s Project: %s RepoName: %s", baseURL, project, repoName)
|
2021-12-02 17:24:02 -05:00
|
|
|
return downloader
|
|
|
|
}
|
|
|
|
|
2022-09-04 05:47:56 -05:00
|
|
|
// String implements Stringer
|
|
|
|
func (d *CodebaseDownloader) String() string {
|
|
|
|
return fmt.Sprintf("migration from codebase server %s %s/%s", d.baseURL, d.project, d.repoName)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ColorFormat provides a basic color format for a GogsDownloader
|
|
|
|
func (d *CodebaseDownloader) ColorFormat(s fmt.State) {
|
|
|
|
if d == nil {
|
|
|
|
log.ColorFprintf(s, "<nil: CodebaseDownloader>")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
log.ColorFprintf(s, "migration from codebase server %s %s/%s", d.baseURL, d.project, d.repoName)
|
|
|
|
}
|
|
|
|
|
2022-01-10 04:32:37 -05:00
|
|
|
// FormatCloneURL add authentication into remote URLs
|
2021-12-02 17:24:02 -05:00
|
|
|
func (d *CodebaseDownloader) FormatCloneURL(opts base.MigrateOptions, remoteAddr string) (string, error) {
|
|
|
|
return opts.CloneAddr, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *CodebaseDownloader) callAPI(endpoint string, parameter map[string]string, result interface{}) error {
|
|
|
|
u, err := d.baseURL.Parse(endpoint)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if parameter != nil {
|
|
|
|
query := u.Query()
|
|
|
|
for k, v := range parameter {
|
|
|
|
query.Set(k, v)
|
|
|
|
}
|
|
|
|
u.RawQuery = query.Encode()
|
|
|
|
}
|
|
|
|
|
|
|
|
req, err := http.NewRequestWithContext(d.ctx, "GET", u.String(), nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
req.Header.Add("Accept", "application/xml")
|
|
|
|
|
|
|
|
resp, err := d.client.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
return xml.NewDecoder(resp.Body).Decode(&result)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetRepoInfo returns repository information
|
|
|
|
// https://support.codebasehq.com/kb/projects
|
|
|
|
func (d *CodebaseDownloader) GetRepoInfo() (*base.Repository, error) {
|
|
|
|
var rawRepository struct {
|
|
|
|
XMLName xml.Name `xml:"repository"`
|
|
|
|
Name string `xml:"name"`
|
|
|
|
Description string `xml:"description"`
|
|
|
|
Permalink string `xml:"permalink"`
|
|
|
|
CloneURL string `xml:"clone-url"`
|
|
|
|
Source string `xml:"source"`
|
|
|
|
}
|
|
|
|
|
|
|
|
err := d.callAPI(
|
|
|
|
fmt.Sprintf("/%s/%s", d.project, d.repoName),
|
|
|
|
nil,
|
|
|
|
&rawRepository,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &base.Repository{
|
|
|
|
Name: rawRepository.Name,
|
|
|
|
Description: rawRepository.Description,
|
|
|
|
CloneURL: rawRepository.CloneURL,
|
|
|
|
OriginalURL: d.projectURL.String(),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetMilestones returns milestones
|
|
|
|
// https://support.codebasehq.com/kb/tickets-and-milestones/milestones
|
|
|
|
func (d *CodebaseDownloader) GetMilestones() ([]*base.Milestone, error) {
|
|
|
|
var rawMilestones struct {
|
|
|
|
XMLName xml.Name `xml:"ticketing-milestone"`
|
|
|
|
Type string `xml:"type,attr"`
|
|
|
|
TicketingMilestone []struct {
|
|
|
|
Text string `xml:",chardata"`
|
|
|
|
ID struct {
|
|
|
|
Value int64 `xml:",chardata"`
|
|
|
|
Type string `xml:"type,attr"`
|
|
|
|
} `xml:"id"`
|
|
|
|
Identifier string `xml:"identifier"`
|
|
|
|
Name string `xml:"name"`
|
|
|
|
Deadline struct {
|
|
|
|
Value string `xml:",chardata"`
|
|
|
|
Type string `xml:"type,attr"`
|
|
|
|
} `xml:"deadline"`
|
|
|
|
Description string `xml:"description"`
|
|
|
|
Status string `xml:"status"`
|
|
|
|
} `xml:"ticketing-milestone"`
|
|
|
|
}
|
|
|
|
|
|
|
|
err := d.callAPI(
|
|
|
|
fmt.Sprintf("/%s/milestones", d.project),
|
|
|
|
nil,
|
|
|
|
&rawMilestones,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-01-20 12:46:10 -05:00
|
|
|
milestones := make([]*base.Milestone, 0, len(rawMilestones.TicketingMilestone))
|
2021-12-02 17:24:02 -05:00
|
|
|
for _, milestone := range rawMilestones.TicketingMilestone {
|
|
|
|
var deadline *time.Time
|
|
|
|
if len(milestone.Deadline.Value) > 0 {
|
|
|
|
if val, err := time.Parse("2006-01-02", milestone.Deadline.Value); err == nil {
|
|
|
|
deadline = &val
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
closed := deadline
|
|
|
|
state := "closed"
|
|
|
|
if milestone.Status == "active" {
|
|
|
|
closed = nil
|
|
|
|
state = ""
|
|
|
|
}
|
|
|
|
|
|
|
|
milestones = append(milestones, &base.Milestone{
|
|
|
|
Title: milestone.Name,
|
|
|
|
Deadline: deadline,
|
|
|
|
Closed: closed,
|
|
|
|
State: state,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return milestones, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetLabels returns labels
|
|
|
|
// https://support.codebasehq.com/kb/tickets-and-milestones/statuses-priorities-and-categories
|
|
|
|
func (d *CodebaseDownloader) GetLabels() ([]*base.Label, error) {
|
|
|
|
var rawTypes struct {
|
|
|
|
XMLName xml.Name `xml:"ticketing-types"`
|
|
|
|
Type string `xml:"type,attr"`
|
|
|
|
TicketingType []struct {
|
|
|
|
ID struct {
|
|
|
|
Value int64 `xml:",chardata"`
|
|
|
|
Type string `xml:"type,attr"`
|
|
|
|
} `xml:"id"`
|
|
|
|
Name string `xml:"name"`
|
|
|
|
} `xml:"ticketing-type"`
|
|
|
|
}
|
|
|
|
|
|
|
|
err := d.callAPI(
|
|
|
|
fmt.Sprintf("/%s/tickets/types", d.project),
|
|
|
|
nil,
|
|
|
|
&rawTypes,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-01-20 12:46:10 -05:00
|
|
|
labels := make([]*base.Label, 0, len(rawTypes.TicketingType))
|
2021-12-02 17:24:02 -05:00
|
|
|
for _, label := range rawTypes.TicketingType {
|
|
|
|
labels = append(labels, &base.Label{
|
|
|
|
Name: label.Name,
|
|
|
|
Color: "ffffff",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return labels, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type codebaseIssueContext struct {
|
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>
2022-03-17 12:08:35 -05:00
|
|
|
Comments []*base.Comment
|
2021-12-02 17:24:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetIssues returns issues, limits are not supported
|
|
|
|
// https://support.codebasehq.com/kb/tickets-and-milestones
|
|
|
|
// https://support.codebasehq.com/kb/tickets-and-milestones/updating-tickets
|
|
|
|
func (d *CodebaseDownloader) GetIssues(page, perPage int) ([]*base.Issue, bool, error) {
|
|
|
|
var rawIssues struct {
|
|
|
|
XMLName xml.Name `xml:"tickets"`
|
|
|
|
Type string `xml:"type,attr"`
|
|
|
|
Ticket []struct {
|
|
|
|
TicketID struct {
|
|
|
|
Value int64 `xml:",chardata"`
|
|
|
|
Type string `xml:"type,attr"`
|
|
|
|
} `xml:"ticket-id"`
|
|
|
|
Summary string `xml:"summary"`
|
|
|
|
TicketType string `xml:"ticket-type"`
|
|
|
|
ReporterID struct {
|
|
|
|
Value int64 `xml:",chardata"`
|
|
|
|
Type string `xml:"type,attr"`
|
|
|
|
} `xml:"reporter-id"`
|
|
|
|
Reporter string `xml:"reporter"`
|
|
|
|
Type struct {
|
|
|
|
Name string `xml:"name"`
|
|
|
|
} `xml:"type"`
|
|
|
|
Status struct {
|
|
|
|
TreatAsClosed struct {
|
|
|
|
Value bool `xml:",chardata"`
|
|
|
|
Type string `xml:"type,attr"`
|
|
|
|
} `xml:"treat-as-closed"`
|
|
|
|
} `xml:"status"`
|
|
|
|
Milestone struct {
|
|
|
|
Name string `xml:"name"`
|
|
|
|
} `xml:"milestone"`
|
|
|
|
UpdatedAt struct {
|
|
|
|
Value time.Time `xml:",chardata"`
|
|
|
|
Type string `xml:"type,attr"`
|
|
|
|
} `xml:"updated-at"`
|
|
|
|
CreatedAt struct {
|
|
|
|
Value time.Time `xml:",chardata"`
|
|
|
|
Type string `xml:"type,attr"`
|
|
|
|
} `xml:"created-at"`
|
|
|
|
} `xml:"ticket"`
|
|
|
|
}
|
|
|
|
|
|
|
|
err := d.callAPI(
|
|
|
|
fmt.Sprintf("/%s/tickets", d.project),
|
|
|
|
nil,
|
|
|
|
&rawIssues,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
issues := make([]*base.Issue, 0, len(rawIssues.Ticket))
|
|
|
|
for _, issue := range rawIssues.Ticket {
|
|
|
|
var notes struct {
|
|
|
|
XMLName xml.Name `xml:"ticket-notes"`
|
|
|
|
Type string `xml:"type,attr"`
|
|
|
|
TicketNote []struct {
|
|
|
|
Content string `xml:"content"`
|
|
|
|
CreatedAt struct {
|
|
|
|
Value time.Time `xml:",chardata"`
|
|
|
|
Type string `xml:"type,attr"`
|
|
|
|
} `xml:"created-at"`
|
|
|
|
UpdatedAt struct {
|
|
|
|
Value time.Time `xml:",chardata"`
|
|
|
|
Type string `xml:"type,attr"`
|
|
|
|
} `xml:"updated-at"`
|
|
|
|
ID struct {
|
|
|
|
Value int64 `xml:",chardata"`
|
|
|
|
Type string `xml:"type,attr"`
|
|
|
|
} `xml:"id"`
|
|
|
|
UserID struct {
|
|
|
|
Value int64 `xml:",chardata"`
|
|
|
|
Type string `xml:"type,attr"`
|
|
|
|
} `xml:"user-id"`
|
|
|
|
} `xml:"ticket-note"`
|
|
|
|
}
|
|
|
|
err := d.callAPI(
|
|
|
|
fmt.Sprintf("/%s/tickets/%d/notes", d.project, issue.TicketID.Value),
|
|
|
|
nil,
|
|
|
|
¬es,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, false, err
|
|
|
|
}
|
|
|
|
comments := make([]*base.Comment, 0, len(notes.TicketNote))
|
|
|
|
for _, note := range notes.TicketNote {
|
|
|
|
if len(note.Content) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
poster := d.tryGetUser(note.UserID.Value)
|
|
|
|
comments = append(comments, &base.Comment{
|
|
|
|
IssueIndex: issue.TicketID.Value,
|
2022-03-06 14:00:41 -05:00
|
|
|
Index: note.ID.Value,
|
2021-12-02 17:24:02 -05:00
|
|
|
PosterID: poster.ID,
|
|
|
|
PosterName: poster.Name,
|
|
|
|
PosterEmail: poster.Email,
|
|
|
|
Content: note.Content,
|
|
|
|
Created: note.CreatedAt.Value,
|
|
|
|
Updated: note.UpdatedAt.Value,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
if len(comments) == 0 {
|
|
|
|
comments = append(comments, &base.Comment{})
|
|
|
|
}
|
|
|
|
|
|
|
|
state := "open"
|
|
|
|
if issue.Status.TreatAsClosed.Value {
|
|
|
|
state = "closed"
|
|
|
|
}
|
|
|
|
poster := d.tryGetUser(issue.ReporterID.Value)
|
|
|
|
issues = append(issues, &base.Issue{
|
|
|
|
Title: issue.Summary,
|
|
|
|
Number: issue.TicketID.Value,
|
|
|
|
PosterName: poster.Name,
|
|
|
|
PosterEmail: poster.Email,
|
|
|
|
Content: comments[0].Content,
|
|
|
|
Milestone: issue.Milestone.Name,
|
|
|
|
State: state,
|
|
|
|
Created: issue.CreatedAt.Value,
|
|
|
|
Updated: issue.UpdatedAt.Value,
|
|
|
|
Labels: []*base.Label{
|
2022-01-20 12:46:10 -05:00
|
|
|
{Name: issue.Type.Name},
|
|
|
|
},
|
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>
2022-03-17 12:08:35 -05:00
|
|
|
ForeignIndex: issue.TicketID.Value,
|
2021-12-02 17:24:02 -05:00
|
|
|
Context: codebaseIssueContext{
|
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>
2022-03-17 12:08:35 -05:00
|
|
|
Comments: comments[1:],
|
2021-12-02 17:24:02 -05:00
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
if d.maxIssueIndex < issue.TicketID.Value {
|
|
|
|
d.maxIssueIndex = issue.TicketID.Value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return issues, true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetComments returns comments
|
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>
2022-03-17 12:08:35 -05:00
|
|
|
func (d *CodebaseDownloader) GetComments(commentable base.Commentable) ([]*base.Comment, bool, error) {
|
|
|
|
context, ok := commentable.GetContext().(codebaseIssueContext)
|
2021-12-02 17:24:02 -05:00
|
|
|
if !ok {
|
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>
2022-03-17 12:08:35 -05:00
|
|
|
return nil, false, fmt.Errorf("unexpected context: %+v", commentable.GetContext())
|
2021-12-02 17:24:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return context.Comments, true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetPullRequests returns pull requests
|
|
|
|
// https://support.codebasehq.com/kb/repositories/merge-requests
|
|
|
|
func (d *CodebaseDownloader) GetPullRequests(page, perPage int) ([]*base.PullRequest, bool, error) {
|
|
|
|
var rawMergeRequests struct {
|
|
|
|
XMLName xml.Name `xml:"merge-requests"`
|
|
|
|
Type string `xml:"type,attr"`
|
|
|
|
MergeRequest []struct {
|
|
|
|
ID struct {
|
|
|
|
Value int64 `xml:",chardata"`
|
|
|
|
Type string `xml:"type,attr"`
|
|
|
|
} `xml:"id"`
|
|
|
|
} `xml:"merge-request"`
|
|
|
|
}
|
|
|
|
|
|
|
|
err := d.callAPI(
|
|
|
|
fmt.Sprintf("/%s/%s/merge_requests", d.project, d.repoName),
|
|
|
|
map[string]string{
|
|
|
|
"query": `"Target Project" is "` + d.repoName + `"`,
|
|
|
|
"offset": strconv.Itoa((page - 1) * perPage),
|
|
|
|
"count": strconv.Itoa(perPage),
|
|
|
|
},
|
|
|
|
&rawMergeRequests,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
pullRequests := make([]*base.PullRequest, 0, len(rawMergeRequests.MergeRequest))
|
|
|
|
for i, mr := range rawMergeRequests.MergeRequest {
|
|
|
|
var rawMergeRequest struct {
|
|
|
|
XMLName xml.Name `xml:"merge-request"`
|
|
|
|
ID struct {
|
|
|
|
Value int64 `xml:",chardata"`
|
|
|
|
Type string `xml:"type,attr"`
|
|
|
|
} `xml:"id"`
|
2022-09-04 05:47:56 -05:00
|
|
|
SourceRef string `xml:"source-ref"` // NOTE: from the documentation these are actually just branches NOT full refs
|
|
|
|
TargetRef string `xml:"target-ref"` // NOTE: from the documentation these are actually just branches NOT full refs
|
2021-12-02 17:24:02 -05:00
|
|
|
Subject string `xml:"subject"`
|
|
|
|
Status string `xml:"status"`
|
|
|
|
UserID struct {
|
|
|
|
Value int64 `xml:",chardata"`
|
|
|
|
Type string `xml:"type,attr"`
|
|
|
|
} `xml:"user-id"`
|
|
|
|
CreatedAt struct {
|
|
|
|
Value time.Time `xml:",chardata"`
|
|
|
|
Type string `xml:"type,attr"`
|
|
|
|
} `xml:"created-at"`
|
|
|
|
UpdatedAt struct {
|
|
|
|
Value time.Time `xml:",chardata"`
|
|
|
|
Type string `xml:"type,attr"`
|
|
|
|
} `xml:"updated-at"`
|
|
|
|
Comments struct {
|
|
|
|
Type string `xml:"type,attr"`
|
|
|
|
Comment []struct {
|
|
|
|
Content string `xml:"content"`
|
2022-03-06 14:00:41 -05:00
|
|
|
ID struct {
|
|
|
|
Value int64 `xml:",chardata"`
|
|
|
|
Type string `xml:"type,attr"`
|
|
|
|
} `xml:"id"`
|
|
|
|
UserID struct {
|
2021-12-02 17:24:02 -05:00
|
|
|
Value int64 `xml:",chardata"`
|
|
|
|
Type string `xml:"type,attr"`
|
|
|
|
} `xml:"user-id"`
|
|
|
|
Action struct {
|
|
|
|
Value string `xml:",chardata"`
|
|
|
|
Nil string `xml:"nil,attr"`
|
|
|
|
} `xml:"action"`
|
|
|
|
CreatedAt struct {
|
|
|
|
Value time.Time `xml:",chardata"`
|
|
|
|
Type string `xml:"type,attr"`
|
|
|
|
} `xml:"created-at"`
|
|
|
|
} `xml:"comment"`
|
|
|
|
} `xml:"comments"`
|
|
|
|
}
|
|
|
|
err := d.callAPI(
|
|
|
|
fmt.Sprintf("/%s/%s/merge_requests/%d", d.project, d.repoName, mr.ID.Value),
|
|
|
|
nil,
|
|
|
|
&rawMergeRequest,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
number := d.maxIssueIndex + int64(i) + 1
|
|
|
|
|
|
|
|
state := "open"
|
|
|
|
merged := false
|
|
|
|
var closeTime *time.Time
|
|
|
|
var mergedTime *time.Time
|
|
|
|
if rawMergeRequest.Status != "new" {
|
|
|
|
state = "closed"
|
|
|
|
closeTime = &rawMergeRequest.UpdatedAt.Value
|
|
|
|
}
|
|
|
|
|
|
|
|
comments := make([]*base.Comment, 0, len(rawMergeRequest.Comments.Comment))
|
|
|
|
for _, comment := range rawMergeRequest.Comments.Comment {
|
|
|
|
if len(comment.Content) == 0 {
|
|
|
|
if comment.Action.Value == "merging" {
|
|
|
|
merged = true
|
|
|
|
mergedTime = &comment.CreatedAt.Value
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
poster := d.tryGetUser(comment.UserID.Value)
|
|
|
|
comments = append(comments, &base.Comment{
|
|
|
|
IssueIndex: number,
|
2022-03-06 14:00:41 -05:00
|
|
|
Index: comment.ID.Value,
|
2021-12-02 17:24:02 -05:00
|
|
|
PosterID: poster.ID,
|
|
|
|
PosterName: poster.Name,
|
|
|
|
PosterEmail: poster.Email,
|
|
|
|
Content: comment.Content,
|
|
|
|
Created: comment.CreatedAt.Value,
|
|
|
|
Updated: comment.CreatedAt.Value,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
if len(comments) == 0 {
|
|
|
|
comments = append(comments, &base.Comment{})
|
|
|
|
}
|
|
|
|
|
|
|
|
poster := d.tryGetUser(rawMergeRequest.UserID.Value)
|
|
|
|
|
|
|
|
pullRequests = append(pullRequests, &base.PullRequest{
|
|
|
|
Title: rawMergeRequest.Subject,
|
|
|
|
Number: number,
|
|
|
|
PosterName: poster.Name,
|
|
|
|
PosterEmail: poster.Email,
|
|
|
|
Content: comments[0].Content,
|
|
|
|
State: state,
|
|
|
|
Created: rawMergeRequest.CreatedAt.Value,
|
|
|
|
Updated: rawMergeRequest.UpdatedAt.Value,
|
|
|
|
Closed: closeTime,
|
|
|
|
Merged: merged,
|
|
|
|
MergedTime: mergedTime,
|
|
|
|
Head: base.PullRequestBranch{
|
|
|
|
Ref: rawMergeRequest.SourceRef,
|
|
|
|
SHA: d.getHeadCommit(rawMergeRequest.SourceRef),
|
|
|
|
RepoName: d.repoName,
|
|
|
|
},
|
|
|
|
Base: base.PullRequestBranch{
|
|
|
|
Ref: rawMergeRequest.TargetRef,
|
|
|
|
SHA: d.getHeadCommit(rawMergeRequest.TargetRef),
|
|
|
|
RepoName: d.repoName,
|
|
|
|
},
|
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>
2022-03-17 12:08:35 -05:00
|
|
|
ForeignIndex: rawMergeRequest.ID.Value,
|
2021-12-02 17:24:02 -05:00
|
|
|
Context: codebaseIssueContext{
|
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>
2022-03-17 12:08:35 -05:00
|
|
|
Comments: comments[1:],
|
2021-12-02 17:24:02 -05:00
|
|
|
},
|
|
|
|
})
|
2022-09-04 05:47:56 -05:00
|
|
|
|
|
|
|
// SECURITY: Ensure that the PR is safe
|
|
|
|
_ = CheckAndEnsureSafePR(pullRequests[len(pullRequests)-1], d.baseURL.String(), d)
|
2021-12-02 17:24:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return pullRequests, true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *CodebaseDownloader) tryGetUser(userID int64) *codebaseUser {
|
|
|
|
if len(d.userMap) == 0 {
|
|
|
|
var rawUsers struct {
|
|
|
|
XMLName xml.Name `xml:"users"`
|
|
|
|
Type string `xml:"type,attr"`
|
|
|
|
User []struct {
|
|
|
|
EmailAddress string `xml:"email-address"`
|
|
|
|
ID struct {
|
|
|
|
Value int64 `xml:",chardata"`
|
|
|
|
Type string `xml:"type,attr"`
|
|
|
|
} `xml:"id"`
|
|
|
|
LastName string `xml:"last-name"`
|
|
|
|
FirstName string `xml:"first-name"`
|
|
|
|
Username string `xml:"username"`
|
|
|
|
} `xml:"user"`
|
|
|
|
}
|
|
|
|
|
|
|
|
err := d.callAPI(
|
|
|
|
"/users",
|
|
|
|
nil,
|
|
|
|
&rawUsers,
|
|
|
|
)
|
|
|
|
if err == nil {
|
|
|
|
for _, user := range rawUsers.User {
|
|
|
|
d.userMap[user.ID.Value] = &codebaseUser{
|
|
|
|
Name: user.Username,
|
|
|
|
Email: user.EmailAddress,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
user, ok := d.userMap[userID]
|
|
|
|
if !ok {
|
|
|
|
user = &codebaseUser{
|
|
|
|
Name: fmt.Sprintf("User %d", userID),
|
|
|
|
}
|
|
|
|
d.userMap[userID] = user
|
|
|
|
}
|
|
|
|
|
|
|
|
return user
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *CodebaseDownloader) getHeadCommit(ref string) string {
|
|
|
|
commitRef, ok := d.commitMap[ref]
|
|
|
|
if !ok {
|
|
|
|
var rawCommits struct {
|
|
|
|
XMLName xml.Name `xml:"commits"`
|
|
|
|
Type string `xml:"type,attr"`
|
|
|
|
Commit []struct {
|
|
|
|
Ref string `xml:"ref"`
|
|
|
|
} `xml:"commit"`
|
|
|
|
}
|
|
|
|
err := d.callAPI(
|
|
|
|
fmt.Sprintf("/%s/%s/commits/%s", d.project, d.repoName, ref),
|
|
|
|
nil,
|
|
|
|
&rawCommits,
|
|
|
|
)
|
|
|
|
if err == nil && len(rawCommits.Commit) > 0 {
|
|
|
|
commitRef = rawCommits.Commit[0].Ref
|
|
|
|
d.commitMap[ref] = commitRef
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return commitRef
|
|
|
|
}
|