2020-04-20 00:44:11 +09:00
|
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
2022-11-28 03:20:29 +09:00
|
|
|
// SPDX-License-Identifier: MIT
|
2020-04-20 00:44:11 +09:00
|
|
|
|
|
|
|
package migrations
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2020-08-28 10:36:37 +09:00
|
|
|
"io"
|
|
|
|
"net/http"
|
2020-04-20 00:44:11 +09:00
|
|
|
"net/url"
|
2020-11-17 04:22:49 +09:00
|
|
|
"path"
|
2024-02-24 16:49:16 +09:00
|
|
|
"regexp"
|
2020-04-20 00:44:11 +09:00
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2024-02-22 16:29:03 +09:00
|
|
|
issues_model "code.gitea.io/gitea/models/issues"
|
2023-08-30 15:55:25 +09:00
|
|
|
"code.gitea.io/gitea/modules/container"
|
2020-04-20 00:44:11 +09:00
|
|
|
"code.gitea.io/gitea/modules/log"
|
2021-11-17 00:25:33 +09:00
|
|
|
base "code.gitea.io/gitea/modules/migration"
|
2020-04-20 00:44:11 +09:00
|
|
|
"code.gitea.io/gitea/modules/structs"
|
|
|
|
|
|
|
|
"github.com/xanzy/go-gitlab"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
_ base.Downloader = &GitlabDownloader{}
|
|
|
|
_ base.DownloaderFactory = &GitlabDownloaderFactory{}
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
RegisterDownloaderFactory(&GitlabDownloaderFactory{})
|
|
|
|
}
|
|
|
|
|
|
|
|
// GitlabDownloaderFactory defines a gitlab downloader factory
|
2022-01-21 02:46:10 +09:00
|
|
|
type GitlabDownloaderFactory struct{}
|
2020-04-20 00:44:11 +09:00
|
|
|
|
|
|
|
// New returns a Downloader related to this factory according MigrateOptions
|
2020-09-03 02:49:25 +09:00
|
|
|
func (f *GitlabDownloaderFactory) New(ctx context.Context, opts base.MigrateOptions) (base.Downloader, error) {
|
2020-04-20 00:44:11 +09:00
|
|
|
u, err := url.Parse(opts.CloneAddr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
baseURL := u.Scheme + "://" + u.Host
|
|
|
|
repoNameSpace := strings.TrimPrefix(u.Path, "/")
|
2020-08-28 10:36:37 +09:00
|
|
|
repoNameSpace = strings.TrimSuffix(repoNameSpace, ".git")
|
2020-04-20 00:44:11 +09:00
|
|
|
|
|
|
|
log.Trace("Create gitlab downloader. BaseURL: %s RepoName: %s", baseURL, repoNameSpace)
|
|
|
|
|
2020-09-10 17:27:49 +09:00
|
|
|
return NewGitlabDownloader(ctx, baseURL, repoNameSpace, opts.AuthUsername, opts.AuthPassword, opts.AuthToken)
|
2020-04-20 00:44:11 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
// GitServiceType returns the type of git service
|
|
|
|
func (f *GitlabDownloaderFactory) GitServiceType() structs.GitServiceType {
|
|
|
|
return structs.GitlabService
|
|
|
|
}
|
|
|
|
|
2023-12-27 02:57:25 +09:00
|
|
|
type gitlabIIDResolver struct {
|
|
|
|
maxIssueIID int64
|
|
|
|
frozen bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *gitlabIIDResolver) recordIssueIID(issueIID int) {
|
|
|
|
if r.frozen {
|
|
|
|
panic("cannot record issue IID after pull request IID generation has started")
|
|
|
|
}
|
|
|
|
r.maxIssueIID = max(r.maxIssueIID, int64(issueIID))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *gitlabIIDResolver) generatePullRequestNumber(mrIID int) int64 {
|
|
|
|
r.frozen = true
|
|
|
|
return r.maxIssueIID + int64(mrIID)
|
|
|
|
}
|
|
|
|
|
2021-07-08 20:38:13 +09:00
|
|
|
// GitlabDownloader implements a Downloader interface to get repository information
|
2020-04-20 00:44:11 +09:00
|
|
|
// from gitlab via go-gitlab
|
|
|
|
// - issueCount is incremented in GetIssues() to ensure PR and Issue numbers do not overlap,
|
|
|
|
// because Gitlab has individual Issue and Pull Request numbers.
|
|
|
|
type GitlabDownloader struct {
|
2021-01-22 04:33:58 +09:00
|
|
|
base.NullDownloader
|
2023-12-27 02:57:25 +09:00
|
|
|
ctx context.Context
|
|
|
|
client *gitlab.Client
|
|
|
|
baseURL string
|
|
|
|
repoID int
|
|
|
|
repoName string
|
|
|
|
iidResolver gitlabIIDResolver
|
|
|
|
maxPerPage int
|
2020-04-20 00:44:11 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewGitlabDownloader creates a gitlab Downloader via gitlab API
|
2022-08-31 11:15:45 +09:00
|
|
|
//
|
|
|
|
// Use either a username/password, personal token entered into the username field, or anonymous/public access
|
|
|
|
// Note: Public access only allows very basic access
|
2020-09-10 17:27:49 +09:00
|
|
|
func NewGitlabDownloader(ctx context.Context, baseURL, repoPath, username, password, token string) (*GitlabDownloader, error) {
|
2021-11-20 18:34:05 +09:00
|
|
|
gitlabClient, err := gitlab.NewClient(token, gitlab.WithBaseURL(baseURL), gitlab.WithHTTPClient(NewMigrationHTTPClient()))
|
2020-09-16 04:32:14 +09:00
|
|
|
// Only use basic auth if token is blank and password is NOT
|
|
|
|
// Basic auth will fail with empty strings, but empty token will allow anonymous public API usage
|
|
|
|
if token == "" && password != "" {
|
2021-11-20 18:34:05 +09:00
|
|
|
gitlabClient, err = gitlab.NewBasicAuthClient(username, password, gitlab.WithBaseURL(baseURL), gitlab.WithHTTPClient(NewMigrationHTTPClient()))
|
2020-04-20 00:44:11 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Trace("Error logging into gitlab: %v", err)
|
2020-09-10 17:27:49 +09:00
|
|
|
return nil, err
|
2020-04-20 00:44:11 +09:00
|
|
|
}
|
|
|
|
|
2020-11-17 04:22:49 +09:00
|
|
|
// split namespace and subdirectory
|
|
|
|
pathParts := strings.Split(strings.Trim(repoPath, "/"), "/")
|
2020-11-19 14:17:56 +09:00
|
|
|
var resp *gitlab.Response
|
|
|
|
u, _ := url.Parse(baseURL)
|
2020-11-20 08:18:34 +09:00
|
|
|
for len(pathParts) >= 2 {
|
2020-11-19 14:17:56 +09:00
|
|
|
_, resp, err = gitlabClient.Version.GetVersion()
|
2022-03-23 13:54:07 +09:00
|
|
|
if err == nil || resp != nil && resp.StatusCode == http.StatusUnauthorized {
|
2020-11-19 14:17:56 +09:00
|
|
|
err = nil // if no authentication given, this still should work
|
2020-11-17 04:22:49 +09:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2020-11-19 14:17:56 +09:00
|
|
|
u.Path = path.Join(u.Path, pathParts[0])
|
|
|
|
baseURL = u.String()
|
2020-11-17 04:22:49 +09:00
|
|
|
pathParts = pathParts[1:]
|
|
|
|
_ = gitlab.WithBaseURL(baseURL)(gitlabClient)
|
|
|
|
repoPath = strings.Join(pathParts, "/")
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
log.Trace("Error could not get gitlab version: %v", err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-11-19 14:17:56 +09:00
|
|
|
log.Trace("gitlab downloader: use BaseURL: '%s' and RepoPath: '%s'", baseURL, repoPath)
|
|
|
|
|
2020-04-20 00:44:11 +09:00
|
|
|
// Grab and store project/repo ID here, due to issues using the URL escaped path
|
2020-09-03 02:49:25 +09:00
|
|
|
gr, _, err := gitlabClient.Projects.GetProject(repoPath, nil, nil, gitlab.WithContext(ctx))
|
2020-04-20 00:44:11 +09:00
|
|
|
if err != nil {
|
|
|
|
log.Trace("Error retrieving project: %v", err)
|
2020-09-10 17:27:49 +09:00
|
|
|
return nil, err
|
2020-04-20 00:44:11 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
if gr == nil {
|
|
|
|
log.Trace("Error getting project, project is nil")
|
2020-09-10 17:27:49 +09:00
|
|
|
return nil, errors.New("Error getting project, project is nil")
|
2020-04-20 00:44:11 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
return &GitlabDownloader{
|
2020-10-25 14:11:03 +09:00
|
|
|
ctx: ctx,
|
|
|
|
client: gitlabClient,
|
2022-09-04 19:47:56 +09:00
|
|
|
baseURL: baseURL,
|
2020-10-25 14:11:03 +09:00
|
|
|
repoID: gr.ID,
|
|
|
|
repoName: gr.Name,
|
|
|
|
maxPerPage: 100,
|
2020-09-10 17:27:49 +09:00
|
|
|
}, nil
|
2020-04-20 00:44:11 +09:00
|
|
|
}
|
|
|
|
|
2022-09-04 19:47:56 +09:00
|
|
|
// String implements Stringer
|
|
|
|
func (g *GitlabDownloader) String() string {
|
|
|
|
return fmt.Sprintf("migration from gitlab server %s [%d]/%s", g.baseURL, g.repoID, g.repoName)
|
|
|
|
}
|
|
|
|
|
Rewrite logger system (#24726)
## ⚠️ Breaking
The `log.<mode>.<logger>` style config has been dropped. If you used it,
please check the new config manual & app.example.ini to make your
instance output logs as expected.
Although many legacy options still work, it's encouraged to upgrade to
the new options.
The SMTP logger is deleted because SMTP is not suitable to collect logs.
If you have manually configured Gitea log options, please confirm the
logger system works as expected after upgrading.
## Description
Close #12082 and maybe more log-related issues, resolve some related
FIXMEs in old code (which seems unfixable before)
Just like rewriting queue #24505 : make code maintainable, clear legacy
bugs, and add the ability to support more writers (eg: JSON, structured
log)
There is a new document (with examples): `logging-config.en-us.md`
This PR is safer than the queue rewriting, because it's just for
logging, it won't break other logic.
## The old problems
The logging system is quite old and difficult to maintain:
* Unclear concepts: Logger, NamedLogger, MultiChannelledLogger,
SubLogger, EventLogger, WriterLogger etc
* Some code is diffuclt to konw whether it is right:
`log.DelNamedLogger("console")` vs `log.DelNamedLogger(log.DEFAULT)` vs
`log.DelLogger("console")`
* The old system heavily depends on ini config system, it's difficult to
create new logger for different purpose, and it's very fragile.
* The "color" trick is difficult to use and read, many colors are
unnecessary, and in the future structured log could help
* It's difficult to add other log formats, eg: JSON format
* The log outputer doesn't have full control of its goroutine, it's
difficult to make outputer have advanced behaviors
* The logs could be lost in some cases: eg: no Fatal error when using
CLI.
* Config options are passed by JSON, which is quite fragile.
* INI package makes the KEY in `[log]` section visible in `[log.sub1]`
and `[log.sub1.subA]`, this behavior is quite fragile and would cause
more unclear problems, and there is no strong requirement to support
`log.<mode>.<logger>` syntax.
## The new design
See `logger.go` for documents.
## Screenshot
<details>
![image](https://github.com/go-gitea/gitea/assets/2114189/4462d713-ba39-41f5-bb08-de912e67e1ff)
![image](https://github.com/go-gitea/gitea/assets/2114189/b188035e-f691-428b-8b2d-ff7b2199b2f9)
![image](https://github.com/go-gitea/gitea/assets/2114189/132e9745-1c3b-4e00-9e0d-15eaea495dee)
</details>
## TODO
* [x] add some new tests
* [x] fix some tests
* [x] test some sub-commands (manually ....)
---------
Co-authored-by: Jason Song <i@wolfogre.com>
Co-authored-by: delvh <dev.lh@web.de>
Co-authored-by: Giteabot <teabot@gitea.io>
2023-05-22 07:35:11 +09:00
|
|
|
func (g *GitlabDownloader) LogString() string {
|
2022-09-04 19:47:56 +09:00
|
|
|
if g == nil {
|
Rewrite logger system (#24726)
## ⚠️ Breaking
The `log.<mode>.<logger>` style config has been dropped. If you used it,
please check the new config manual & app.example.ini to make your
instance output logs as expected.
Although many legacy options still work, it's encouraged to upgrade to
the new options.
The SMTP logger is deleted because SMTP is not suitable to collect logs.
If you have manually configured Gitea log options, please confirm the
logger system works as expected after upgrading.
## Description
Close #12082 and maybe more log-related issues, resolve some related
FIXMEs in old code (which seems unfixable before)
Just like rewriting queue #24505 : make code maintainable, clear legacy
bugs, and add the ability to support more writers (eg: JSON, structured
log)
There is a new document (with examples): `logging-config.en-us.md`
This PR is safer than the queue rewriting, because it's just for
logging, it won't break other logic.
## The old problems
The logging system is quite old and difficult to maintain:
* Unclear concepts: Logger, NamedLogger, MultiChannelledLogger,
SubLogger, EventLogger, WriterLogger etc
* Some code is diffuclt to konw whether it is right:
`log.DelNamedLogger("console")` vs `log.DelNamedLogger(log.DEFAULT)` vs
`log.DelLogger("console")`
* The old system heavily depends on ini config system, it's difficult to
create new logger for different purpose, and it's very fragile.
* The "color" trick is difficult to use and read, many colors are
unnecessary, and in the future structured log could help
* It's difficult to add other log formats, eg: JSON format
* The log outputer doesn't have full control of its goroutine, it's
difficult to make outputer have advanced behaviors
* The logs could be lost in some cases: eg: no Fatal error when using
CLI.
* Config options are passed by JSON, which is quite fragile.
* INI package makes the KEY in `[log]` section visible in `[log.sub1]`
and `[log.sub1.subA]`, this behavior is quite fragile and would cause
more unclear problems, and there is no strong requirement to support
`log.<mode>.<logger>` syntax.
## The new design
See `logger.go` for documents.
## Screenshot
<details>
![image](https://github.com/go-gitea/gitea/assets/2114189/4462d713-ba39-41f5-bb08-de912e67e1ff)
![image](https://github.com/go-gitea/gitea/assets/2114189/b188035e-f691-428b-8b2d-ff7b2199b2f9)
![image](https://github.com/go-gitea/gitea/assets/2114189/132e9745-1c3b-4e00-9e0d-15eaea495dee)
</details>
## TODO
* [x] add some new tests
* [x] fix some tests
* [x] test some sub-commands (manually ....)
---------
Co-authored-by: Jason Song <i@wolfogre.com>
Co-authored-by: delvh <dev.lh@web.de>
Co-authored-by: Giteabot <teabot@gitea.io>
2023-05-22 07:35:11 +09:00
|
|
|
return "<GitlabDownloader nil>"
|
2022-09-04 19:47:56 +09:00
|
|
|
}
|
Rewrite logger system (#24726)
## ⚠️ Breaking
The `log.<mode>.<logger>` style config has been dropped. If you used it,
please check the new config manual & app.example.ini to make your
instance output logs as expected.
Although many legacy options still work, it's encouraged to upgrade to
the new options.
The SMTP logger is deleted because SMTP is not suitable to collect logs.
If you have manually configured Gitea log options, please confirm the
logger system works as expected after upgrading.
## Description
Close #12082 and maybe more log-related issues, resolve some related
FIXMEs in old code (which seems unfixable before)
Just like rewriting queue #24505 : make code maintainable, clear legacy
bugs, and add the ability to support more writers (eg: JSON, structured
log)
There is a new document (with examples): `logging-config.en-us.md`
This PR is safer than the queue rewriting, because it's just for
logging, it won't break other logic.
## The old problems
The logging system is quite old and difficult to maintain:
* Unclear concepts: Logger, NamedLogger, MultiChannelledLogger,
SubLogger, EventLogger, WriterLogger etc
* Some code is diffuclt to konw whether it is right:
`log.DelNamedLogger("console")` vs `log.DelNamedLogger(log.DEFAULT)` vs
`log.DelLogger("console")`
* The old system heavily depends on ini config system, it's difficult to
create new logger for different purpose, and it's very fragile.
* The "color" trick is difficult to use and read, many colors are
unnecessary, and in the future structured log could help
* It's difficult to add other log formats, eg: JSON format
* The log outputer doesn't have full control of its goroutine, it's
difficult to make outputer have advanced behaviors
* The logs could be lost in some cases: eg: no Fatal error when using
CLI.
* Config options are passed by JSON, which is quite fragile.
* INI package makes the KEY in `[log]` section visible in `[log.sub1]`
and `[log.sub1.subA]`, this behavior is quite fragile and would cause
more unclear problems, and there is no strong requirement to support
`log.<mode>.<logger>` syntax.
## The new design
See `logger.go` for documents.
## Screenshot
<details>
![image](https://github.com/go-gitea/gitea/assets/2114189/4462d713-ba39-41f5-bb08-de912e67e1ff)
![image](https://github.com/go-gitea/gitea/assets/2114189/b188035e-f691-428b-8b2d-ff7b2199b2f9)
![image](https://github.com/go-gitea/gitea/assets/2114189/132e9745-1c3b-4e00-9e0d-15eaea495dee)
</details>
## TODO
* [x] add some new tests
* [x] fix some tests
* [x] test some sub-commands (manually ....)
---------
Co-authored-by: Jason Song <i@wolfogre.com>
Co-authored-by: delvh <dev.lh@web.de>
Co-authored-by: Giteabot <teabot@gitea.io>
2023-05-22 07:35:11 +09:00
|
|
|
return fmt.Sprintf("<GitlabDownloader %s [%d]/%s>", g.baseURL, g.repoID, g.repoName)
|
2022-09-04 19:47:56 +09:00
|
|
|
}
|
|
|
|
|
2020-04-20 00:44:11 +09:00
|
|
|
// SetContext set context
|
|
|
|
func (g *GitlabDownloader) SetContext(ctx context.Context) {
|
|
|
|
g.ctx = ctx
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetRepoInfo returns a repository information
|
|
|
|
func (g *GitlabDownloader) GetRepoInfo() (*base.Repository, error) {
|
2020-09-03 02:49:25 +09:00
|
|
|
gr, _, err := g.client.Projects.GetProject(g.repoID, nil, nil, gitlab.WithContext(g.ctx))
|
2020-04-20 00:44:11 +09:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var private bool
|
|
|
|
switch gr.Visibility {
|
|
|
|
case gitlab.InternalVisibility:
|
|
|
|
private = true
|
|
|
|
case gitlab.PrivateVisibility:
|
|
|
|
private = true
|
|
|
|
}
|
|
|
|
|
|
|
|
var owner string
|
|
|
|
if gr.Owner == nil {
|
|
|
|
log.Trace("gr.Owner is nil, trying to get owner from Namespace")
|
|
|
|
if gr.Namespace != nil && gr.Namespace.Kind == "user" {
|
|
|
|
owner = gr.Namespace.Path
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
owner = gr.Owner.Username
|
|
|
|
}
|
|
|
|
|
|
|
|
// convert gitlab repo to stand Repo
|
|
|
|
return &base.Repository{
|
2020-09-15 23:37:44 +09:00
|
|
|
Owner: owner,
|
|
|
|
Name: gr.Name,
|
|
|
|
IsPrivate: private,
|
|
|
|
Description: gr.Description,
|
|
|
|
OriginalURL: gr.WebURL,
|
|
|
|
CloneURL: gr.HTTPURLToRepo,
|
|
|
|
DefaultBranch: gr.DefaultBranch,
|
2020-04-20 00:44:11 +09:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetTopics return gitlab topics
|
|
|
|
func (g *GitlabDownloader) GetTopics() ([]string, error) {
|
2020-09-03 02:49:25 +09:00
|
|
|
gr, _, err := g.client.Projects.GetProject(g.repoID, nil, nil, gitlab.WithContext(g.ctx))
|
2020-04-20 00:44:11 +09:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return gr.TagList, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetMilestones returns milestones
|
|
|
|
func (g *GitlabDownloader) GetMilestones() ([]*base.Milestone, error) {
|
2022-01-21 02:46:10 +09:00
|
|
|
perPage := g.maxPerPage
|
|
|
|
state := "all"
|
|
|
|
milestones := make([]*base.Milestone, 0, perPage)
|
2020-04-20 00:44:11 +09:00
|
|
|
for i := 1; ; i++ {
|
|
|
|
ms, _, err := g.client.Milestones.ListMilestones(g.repoID, &gitlab.ListMilestonesOptions{
|
|
|
|
State: &state,
|
|
|
|
ListOptions: gitlab.ListOptions{
|
|
|
|
Page: i,
|
|
|
|
PerPage: perPage,
|
2022-01-21 02:46:10 +09:00
|
|
|
},
|
|
|
|
}, nil, gitlab.WithContext(g.ctx))
|
2020-04-20 00:44:11 +09:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, m := range ms {
|
|
|
|
var desc string
|
|
|
|
if m.Description != "" {
|
|
|
|
desc = m.Description
|
|
|
|
}
|
2022-01-21 02:46:10 +09:00
|
|
|
state := "open"
|
2020-04-20 00:44:11 +09:00
|
|
|
var closedAt *time.Time
|
|
|
|
if m.State != "" {
|
|
|
|
state = m.State
|
|
|
|
if state == "closed" {
|
|
|
|
closedAt = m.UpdatedAt
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var deadline *time.Time
|
|
|
|
if m.DueDate != nil {
|
|
|
|
deadlineParsed, err := time.Parse("2006-01-02", m.DueDate.String())
|
|
|
|
if err != nil {
|
|
|
|
log.Trace("Error parsing Milestone DueDate time")
|
|
|
|
deadline = nil
|
|
|
|
} else {
|
|
|
|
deadline = &deadlineParsed
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
milestones = append(milestones, &base.Milestone{
|
|
|
|
Title: m.Title,
|
|
|
|
Description: desc,
|
|
|
|
Deadline: deadline,
|
|
|
|
State: state,
|
|
|
|
Created: *m.CreatedAt,
|
|
|
|
Updated: m.UpdatedAt,
|
|
|
|
Closed: closedAt,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
if len(ms) < perPage {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return milestones, nil
|
|
|
|
}
|
|
|
|
|
2020-09-10 22:04:30 +09:00
|
|
|
func (g *GitlabDownloader) normalizeColor(val string) string {
|
|
|
|
val = strings.TrimLeft(val, "#")
|
|
|
|
val = strings.ToLower(val)
|
|
|
|
if len(val) == 3 {
|
|
|
|
c := []rune(val)
|
|
|
|
val = fmt.Sprintf("%c%c%c%c%c%c", c[0], c[0], c[1], c[1], c[2], c[2])
|
|
|
|
}
|
|
|
|
if len(val) != 6 {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return val
|
|
|
|
}
|
|
|
|
|
2020-04-20 00:44:11 +09:00
|
|
|
// GetLabels returns labels
|
|
|
|
func (g *GitlabDownloader) GetLabels() ([]*base.Label, error) {
|
2022-01-21 02:46:10 +09:00
|
|
|
perPage := g.maxPerPage
|
|
|
|
labels := make([]*base.Label, 0, perPage)
|
2020-04-20 00:44:11 +09:00
|
|
|
for i := 1; ; i++ {
|
2020-09-07 00:37:53 +09:00
|
|
|
ls, _, err := g.client.Labels.ListLabels(g.repoID, &gitlab.ListLabelsOptions{ListOptions: gitlab.ListOptions{
|
2020-04-20 00:44:11 +09:00
|
|
|
Page: i,
|
|
|
|
PerPage: perPage,
|
2020-09-07 00:37:53 +09:00
|
|
|
}}, nil, gitlab.WithContext(g.ctx))
|
2020-04-20 00:44:11 +09:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for _, label := range ls {
|
|
|
|
baseLabel := &base.Label{
|
|
|
|
Name: label.Name,
|
2020-09-10 22:04:30 +09:00
|
|
|
Color: g.normalizeColor(label.Color),
|
2020-04-20 00:44:11 +09:00
|
|
|
Description: label.Description,
|
|
|
|
}
|
|
|
|
labels = append(labels, baseLabel)
|
|
|
|
}
|
|
|
|
if len(ls) < perPage {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return labels, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *GitlabDownloader) convertGitlabRelease(rel *gitlab.Release) *base.Release {
|
2020-08-28 10:36:37 +09:00
|
|
|
var zero int
|
2020-04-20 00:44:11 +09:00
|
|
|
r := &base.Release{
|
|
|
|
TagName: rel.TagName,
|
|
|
|
TargetCommitish: rel.Commit.ID,
|
|
|
|
Name: rel.Name,
|
|
|
|
Body: rel.Description,
|
|
|
|
Created: *rel.CreatedAt,
|
|
|
|
PublisherID: int64(rel.Author.ID),
|
|
|
|
PublisherName: rel.Author.Username,
|
|
|
|
}
|
|
|
|
|
2021-11-20 18:34:05 +09:00
|
|
|
httpClient := NewMigrationHTTPClient()
|
2021-08-18 22:10:39 +09:00
|
|
|
|
2020-04-20 00:44:11 +09:00
|
|
|
for k, asset := range rel.Assets.Links {
|
2023-11-03 16:56:53 +09:00
|
|
|
assetID := asset.ID // Don't optimize this, for closure we need a local variable
|
2020-12-27 12:34:19 +09:00
|
|
|
r.Assets = append(r.Assets, &base.ReleaseAsset{
|
2020-08-28 10:36:37 +09:00
|
|
|
ID: int64(asset.ID),
|
|
|
|
Name: asset.Name,
|
|
|
|
ContentType: &rel.Assets.Sources[k].Format,
|
|
|
|
Size: &zero,
|
|
|
|
DownloadCount: &zero,
|
2020-12-27 12:34:19 +09:00
|
|
|
DownloadFunc: func() (io.ReadCloser, error) {
|
2023-11-03 16:56:53 +09:00
|
|
|
link, _, err := g.client.ReleaseLinks.GetReleaseLink(g.repoID, rel.TagName, assetID, gitlab.WithContext(g.ctx))
|
2020-12-27 12:34:19 +09:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-09-04 19:47:56 +09:00
|
|
|
if !hasBaseURL(link.URL, g.baseURL) {
|
2023-11-03 16:56:53 +09:00
|
|
|
WarnAndNotice("Unexpected AssetURL for assetID[%d] in %s: %s", assetID, g, link.URL)
|
2022-09-04 19:47:56 +09:00
|
|
|
return io.NopCloser(strings.NewReader(link.URL)), nil
|
|
|
|
}
|
|
|
|
|
2020-12-27 12:34:19 +09:00
|
|
|
req, err := http.NewRequest("GET", link.URL, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
req = req.WithContext(g.ctx)
|
2021-08-18 22:10:39 +09:00
|
|
|
resp, err := httpClient.Do(req)
|
2020-12-27 12:34:19 +09:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// resp.Body is closed by the uploader
|
|
|
|
return resp.Body, nil
|
|
|
|
},
|
2020-04-20 00:44:11 +09:00
|
|
|
})
|
|
|
|
}
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetReleases returns releases
|
|
|
|
func (g *GitlabDownloader) GetReleases() ([]*base.Release, error) {
|
2022-01-21 02:46:10 +09:00
|
|
|
perPage := g.maxPerPage
|
|
|
|
releases := make([]*base.Release, 0, perPage)
|
2020-04-20 00:44:11 +09:00
|
|
|
for i := 1; ; i++ {
|
|
|
|
ls, _, err := g.client.Releases.ListReleases(g.repoID, &gitlab.ListReleasesOptions{
|
2023-01-12 17:21:16 +09:00
|
|
|
ListOptions: gitlab.ListOptions{
|
|
|
|
Page: i,
|
|
|
|
PerPage: perPage,
|
|
|
|
},
|
2020-09-03 02:49:25 +09:00
|
|
|
}, nil, gitlab.WithContext(g.ctx))
|
2020-04-20 00:44:11 +09:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, release := range ls {
|
|
|
|
releases = append(releases, g.convertGitlabRelease(release))
|
|
|
|
}
|
|
|
|
if len(ls) < perPage {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return releases, nil
|
|
|
|
}
|
|
|
|
|
2021-08-22 07:47:45 +09:00
|
|
|
type gitlabIssueContext struct {
|
|
|
|
IsMergeRequest bool
|
|
|
|
}
|
|
|
|
|
2020-04-20 00:44:11 +09:00
|
|
|
// GetIssues returns issues according start and limit
|
2022-08-31 11:15:45 +09:00
|
|
|
//
|
|
|
|
// Note: issue label description and colors are not supported by the go-gitlab library at this time
|
2020-04-20 00:44:11 +09:00
|
|
|
func (g *GitlabDownloader) GetIssues(page, perPage int) ([]*base.Issue, bool, error) {
|
|
|
|
state := "all"
|
|
|
|
sort := "asc"
|
|
|
|
|
2020-10-25 14:11:03 +09:00
|
|
|
if perPage > g.maxPerPage {
|
|
|
|
perPage = g.maxPerPage
|
|
|
|
}
|
|
|
|
|
2020-04-20 00:44:11 +09:00
|
|
|
opt := &gitlab.ListProjectIssuesOptions{
|
|
|
|
State: &state,
|
|
|
|
Sort: &sort,
|
|
|
|
ListOptions: gitlab.ListOptions{
|
|
|
|
PerPage: perPage,
|
|
|
|
Page: page,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2022-01-21 02:46:10 +09:00
|
|
|
allIssues := make([]*base.Issue, 0, perPage)
|
2020-04-20 00:44:11 +09:00
|
|
|
|
2020-09-03 02:49:25 +09:00
|
|
|
issues, _, err := g.client.Issues.ListProjectIssues(g.repoID, opt, nil, gitlab.WithContext(g.ctx))
|
2020-04-20 00:44:11 +09:00
|
|
|
if err != nil {
|
2022-10-25 04:29:17 +09:00
|
|
|
return nil, false, fmt.Errorf("error while listing issues: %w", err)
|
2020-04-20 00:44:11 +09:00
|
|
|
}
|
|
|
|
for _, issue := range issues {
|
|
|
|
|
2022-01-21 02:46:10 +09:00
|
|
|
labels := make([]*base.Label, 0, len(issue.Labels))
|
2020-04-20 00:44:11 +09:00
|
|
|
for _, l := range issue.Labels {
|
|
|
|
labels = append(labels, &base.Label{
|
|
|
|
Name: l,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
var milestone string
|
|
|
|
if issue.Milestone != nil {
|
|
|
|
milestone = issue.Milestone.Title
|
|
|
|
}
|
|
|
|
|
2023-06-03 05:35:50 +09:00
|
|
|
var reactions []*gitlab.AwardEmoji
|
2022-01-21 02:46:10 +09:00
|
|
|
awardPage := 1
|
2020-09-03 16:35:17 +09:00
|
|
|
for {
|
|
|
|
awards, _, err := g.client.AwardEmoji.ListIssueAwardEmoji(g.repoID, issue.IID, &gitlab.ListAwardEmojiOptions{Page: awardPage, PerPage: perPage}, gitlab.WithContext(g.ctx))
|
|
|
|
if err != nil {
|
2022-10-25 04:29:17 +09:00
|
|
|
return nil, false, fmt.Errorf("error while listing issue awards: %w", err)
|
2020-09-03 16:35:17 +09:00
|
|
|
}
|
2021-09-01 19:20:19 +09:00
|
|
|
|
2023-06-03 05:35:50 +09:00
|
|
|
reactions = append(reactions, awards...)
|
2021-09-01 19:20:19 +09:00
|
|
|
|
|
|
|
if len(awards) < perPage {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2020-09-03 16:35:17 +09:00
|
|
|
awardPage++
|
|
|
|
}
|
|
|
|
|
2020-04-20 00:44:11 +09:00
|
|
|
allIssues = append(allIssues, &base.Issue{
|
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-18 02:08:35 +09:00
|
|
|
Title: issue.Title,
|
|
|
|
Number: int64(issue.IID),
|
|
|
|
PosterID: int64(issue.Author.ID),
|
|
|
|
PosterName: issue.Author.Username,
|
|
|
|
Content: issue.Description,
|
|
|
|
Milestone: milestone,
|
|
|
|
State: issue.State,
|
|
|
|
Created: *issue.CreatedAt,
|
|
|
|
Labels: labels,
|
2023-06-03 05:35:50 +09:00
|
|
|
Reactions: g.awardsToReactions(reactions),
|
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-18 02:08:35 +09:00
|
|
|
Closed: issue.ClosedAt,
|
|
|
|
IsLocked: issue.DiscussionLocked,
|
|
|
|
Updated: *issue.UpdatedAt,
|
|
|
|
ForeignIndex: int64(issue.IID),
|
|
|
|
Context: gitlabIssueContext{IsMergeRequest: false},
|
2020-04-20 00:44:11 +09:00
|
|
|
})
|
|
|
|
|
2023-12-27 02:57:25 +09:00
|
|
|
// record the issue IID, to be used in GetPullRequests()
|
|
|
|
g.iidResolver.recordIssueIID(issue.IID)
|
2020-04-20 00:44:11 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
return allIssues, len(issues) < perPage, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetComments returns comments according issueNumber
|
2020-09-03 16:35:17 +09:00
|
|
|
// TODO: figure out how to transfer comment reactions
|
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-18 02:08:35 +09:00
|
|
|
func (g *GitlabDownloader) GetComments(commentable base.Commentable) ([]*base.Comment, bool, error) {
|
|
|
|
context, ok := commentable.GetContext().(gitlabIssueContext)
|
2021-08-22 07:47:45 +09: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-18 02:08:35 +09:00
|
|
|
return nil, false, fmt.Errorf("unexpected context: %+v", commentable.GetContext())
|
2021-08-22 07:47:45 +09:00
|
|
|
}
|
|
|
|
|
2022-01-21 02:46:10 +09:00
|
|
|
allComments := make([]*base.Comment, 0, g.maxPerPage)
|
2020-04-20 00:44:11 +09:00
|
|
|
|
2022-01-21 02:46:10 +09:00
|
|
|
page := 1
|
2020-04-20 00:44:11 +09:00
|
|
|
|
|
|
|
for {
|
|
|
|
var comments []*gitlab.Discussion
|
|
|
|
var resp *gitlab.Response
|
|
|
|
var err error
|
2021-08-22 07:47:45 +09:00
|
|
|
if !context.IsMergeRequest {
|
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-18 02:08:35 +09:00
|
|
|
comments, resp, err = g.client.Discussions.ListIssueDiscussions(g.repoID, int(commentable.GetForeignIndex()), &gitlab.ListIssueDiscussionsOptions{
|
2020-04-20 00:44:11 +09:00
|
|
|
Page: page,
|
2020-10-25 14:11:03 +09:00
|
|
|
PerPage: g.maxPerPage,
|
2020-09-03 02:49:25 +09:00
|
|
|
}, nil, gitlab.WithContext(g.ctx))
|
2020-04-20 00:44:11 +09:00
|
|
|
} else {
|
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-18 02:08:35 +09:00
|
|
|
comments, resp, err = g.client.Discussions.ListMergeRequestDiscussions(g.repoID, int(commentable.GetForeignIndex()), &gitlab.ListMergeRequestDiscussionsOptions{
|
2020-04-20 00:44:11 +09:00
|
|
|
Page: page,
|
2020-10-25 14:11:03 +09:00
|
|
|
PerPage: g.maxPerPage,
|
2020-09-03 02:49:25 +09:00
|
|
|
}, nil, gitlab.WithContext(g.ctx))
|
2020-04-20 00:44:11 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
2022-10-25 04:29:17 +09:00
|
|
|
return nil, false, fmt.Errorf("error while listing comments: %v %w", g.repoID, err)
|
2020-04-20 00:44:11 +09:00
|
|
|
}
|
|
|
|
for _, comment := range comments {
|
2024-02-22 16:29:03 +09:00
|
|
|
for _, note := range comment.Notes {
|
|
|
|
allComments = append(allComments, g.convertNoteToComment(commentable.GetLocalIndex(), note))
|
2020-04-20 00:44:11 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if resp.NextPage == 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
page = resp.NextPage
|
|
|
|
}
|
2024-02-26 13:08:21 +09:00
|
|
|
|
|
|
|
page = 1
|
|
|
|
for {
|
|
|
|
var stateEvents []*gitlab.StateEvent
|
|
|
|
var resp *gitlab.Response
|
|
|
|
var err error
|
|
|
|
if context.IsMergeRequest {
|
|
|
|
stateEvents, resp, err = g.client.ResourceStateEvents.ListMergeStateEvents(g.repoID, int(commentable.GetForeignIndex()), &gitlab.ListStateEventsOptions{
|
|
|
|
ListOptions: gitlab.ListOptions{
|
|
|
|
Page: page,
|
|
|
|
PerPage: g.maxPerPage,
|
|
|
|
},
|
|
|
|
}, nil, gitlab.WithContext(g.ctx))
|
|
|
|
} else {
|
|
|
|
stateEvents, resp, err = g.client.ResourceStateEvents.ListIssueStateEvents(g.repoID, int(commentable.GetForeignIndex()), &gitlab.ListStateEventsOptions{
|
|
|
|
ListOptions: gitlab.ListOptions{
|
|
|
|
Page: page,
|
|
|
|
PerPage: g.maxPerPage,
|
|
|
|
},
|
|
|
|
}, nil, gitlab.WithContext(g.ctx))
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, false, fmt.Errorf("error while listing state events: %v %w", g.repoID, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, stateEvent := range stateEvents {
|
|
|
|
comment := &base.Comment{
|
|
|
|
IssueIndex: commentable.GetLocalIndex(),
|
|
|
|
Index: int64(stateEvent.ID),
|
|
|
|
PosterID: int64(stateEvent.User.ID),
|
|
|
|
PosterName: stateEvent.User.Username,
|
|
|
|
Content: "",
|
|
|
|
Created: *stateEvent.CreatedAt,
|
|
|
|
}
|
|
|
|
switch stateEvent.State {
|
|
|
|
case gitlab.ClosedEventType:
|
|
|
|
comment.CommentType = issues_model.CommentTypeClose.String()
|
|
|
|
case gitlab.MergedEventType:
|
|
|
|
comment.CommentType = issues_model.CommentTypeMergePull.String()
|
|
|
|
case gitlab.ReopenedEventType:
|
|
|
|
comment.CommentType = issues_model.CommentTypeReopen.String()
|
|
|
|
default:
|
|
|
|
// Ignore other event types
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
allComments = append(allComments, comment)
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp.NextPage == 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
page = resp.NextPage
|
|
|
|
}
|
|
|
|
|
2021-06-30 16:23:49 +09:00
|
|
|
return allComments, true, nil
|
2020-04-20 00:44:11 +09:00
|
|
|
}
|
|
|
|
|
2024-02-24 16:49:16 +09:00
|
|
|
var targetBranchChangeRegexp = regexp.MustCompile("^changed target branch from `(.*?)` to `(.*?)`$")
|
|
|
|
|
2024-02-22 16:29:03 +09:00
|
|
|
func (g *GitlabDownloader) convertNoteToComment(localIndex int64, note *gitlab.Note) *base.Comment {
|
|
|
|
comment := &base.Comment{
|
|
|
|
IssueIndex: localIndex,
|
|
|
|
Index: int64(note.ID),
|
|
|
|
PosterID: int64(note.Author.ID),
|
|
|
|
PosterName: note.Author.Username,
|
|
|
|
PosterEmail: note.Author.Email,
|
|
|
|
Content: note.Body,
|
|
|
|
Created: *note.CreatedAt,
|
2024-02-24 16:49:16 +09:00
|
|
|
Meta: map[string]any{},
|
2024-02-22 16:29:03 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
// Try to find the underlying event of system notes.
|
|
|
|
if note.System {
|
2024-02-24 16:49:16 +09:00
|
|
|
if match := targetBranchChangeRegexp.FindStringSubmatch(note.Body); match != nil {
|
|
|
|
comment.CommentType = issues_model.CommentTypeChangeTargetBranch.String()
|
|
|
|
comment.Meta["OldRef"] = match[1]
|
|
|
|
comment.Meta["NewRef"] = match[2]
|
|
|
|
} else if strings.HasPrefix(note.Body, "enabled an automatic merge") {
|
2024-02-22 16:29:03 +09:00
|
|
|
comment.CommentType = issues_model.CommentTypePRScheduledToAutoMerge.String()
|
|
|
|
} else if note.Body == "canceled the automatic merge" {
|
|
|
|
comment.CommentType = issues_model.CommentTypePRUnScheduledToAutoMerge.String()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return comment
|
|
|
|
}
|
|
|
|
|
2020-04-20 00:44:11 +09:00
|
|
|
// GetPullRequests returns pull requests according page and perPage
|
2020-10-14 13:06:00 +09:00
|
|
|
func (g *GitlabDownloader) GetPullRequests(page, perPage int) ([]*base.PullRequest, bool, error) {
|
2020-10-25 14:11:03 +09:00
|
|
|
if perPage > g.maxPerPage {
|
|
|
|
perPage = g.maxPerPage
|
|
|
|
}
|
|
|
|
|
2023-10-29 22:29:33 +09:00
|
|
|
view := "simple"
|
2020-04-20 00:44:11 +09:00
|
|
|
opt := &gitlab.ListProjectMergeRequestsOptions{
|
|
|
|
ListOptions: gitlab.ListOptions{
|
|
|
|
PerPage: perPage,
|
|
|
|
Page: page,
|
|
|
|
},
|
2023-10-29 22:29:33 +09:00
|
|
|
View: &view,
|
2020-04-20 00:44:11 +09:00
|
|
|
}
|
|
|
|
|
2022-01-21 02:46:10 +09:00
|
|
|
allPRs := make([]*base.PullRequest, 0, perPage)
|
2020-04-20 00:44:11 +09:00
|
|
|
|
2020-09-03 02:49:25 +09:00
|
|
|
prs, _, err := g.client.MergeRequests.ListProjectMergeRequests(g.repoID, opt, nil, gitlab.WithContext(g.ctx))
|
2020-04-20 00:44:11 +09:00
|
|
|
if err != nil {
|
2022-10-25 04:29:17 +09:00
|
|
|
return nil, false, fmt.Errorf("error while listing merge requests: %w", err)
|
2020-04-20 00:44:11 +09:00
|
|
|
}
|
2023-10-29 22:29:33 +09:00
|
|
|
for _, simplePR := range prs {
|
|
|
|
// Load merge request again by itself, as not all fields are populated in the ListProjectMergeRequests endpoint.
|
|
|
|
// See https://gitlab.com/gitlab-org/gitlab/-/issues/29620
|
|
|
|
pr, _, err := g.client.MergeRequests.GetMergeRequest(g.repoID, simplePR.IID, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, false, fmt.Errorf("error while loading merge request: %w", err)
|
|
|
|
}
|
2020-04-20 00:44:11 +09:00
|
|
|
|
2022-01-21 02:46:10 +09:00
|
|
|
labels := make([]*base.Label, 0, len(pr.Labels))
|
2020-04-20 00:44:11 +09:00
|
|
|
for _, l := range pr.Labels {
|
|
|
|
labels = append(labels, &base.Label{
|
|
|
|
Name: l,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
var merged bool
|
|
|
|
if pr.State == "merged" {
|
|
|
|
merged = true
|
|
|
|
pr.State = "closed"
|
|
|
|
}
|
|
|
|
|
2022-01-21 02:46:10 +09:00
|
|
|
mergeTime := pr.MergedAt
|
2020-04-20 00:44:11 +09:00
|
|
|
if merged && pr.MergedAt == nil {
|
|
|
|
mergeTime = pr.UpdatedAt
|
|
|
|
}
|
|
|
|
|
2022-01-21 02:46:10 +09:00
|
|
|
closeTime := pr.ClosedAt
|
2020-04-20 00:44:11 +09:00
|
|
|
if merged && pr.ClosedAt == nil {
|
|
|
|
closeTime = pr.UpdatedAt
|
|
|
|
}
|
|
|
|
|
2023-10-29 09:54:58 +09:00
|
|
|
mergeCommitSHA := pr.MergeCommitSHA
|
|
|
|
if mergeCommitSHA == "" {
|
|
|
|
mergeCommitSHA = pr.SquashCommitSHA
|
|
|
|
}
|
|
|
|
|
2020-04-20 00:44:11 +09:00
|
|
|
var locked bool
|
|
|
|
if pr.State == "locked" {
|
|
|
|
locked = true
|
|
|
|
}
|
|
|
|
|
|
|
|
var milestone string
|
|
|
|
if pr.Milestone != nil {
|
|
|
|
milestone = pr.Milestone.Title
|
|
|
|
}
|
|
|
|
|
2023-06-03 05:35:50 +09:00
|
|
|
var reactions []*gitlab.AwardEmoji
|
2022-01-21 02:46:10 +09:00
|
|
|
awardPage := 1
|
2020-09-03 16:35:17 +09:00
|
|
|
for {
|
|
|
|
awards, _, err := g.client.AwardEmoji.ListMergeRequestAwardEmoji(g.repoID, pr.IID, &gitlab.ListAwardEmojiOptions{Page: awardPage, PerPage: perPage}, gitlab.WithContext(g.ctx))
|
|
|
|
if err != nil {
|
2022-10-25 04:29:17 +09:00
|
|
|
return nil, false, fmt.Errorf("error while listing merge requests awards: %w", err)
|
2020-09-03 16:35:17 +09:00
|
|
|
}
|
2021-09-01 19:20:19 +09:00
|
|
|
|
2023-06-03 05:35:50 +09:00
|
|
|
reactions = append(reactions, awards...)
|
2021-09-01 19:20:19 +09:00
|
|
|
|
|
|
|
if len(awards) < perPage {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2020-09-03 16:35:17 +09:00
|
|
|
awardPage++
|
|
|
|
}
|
|
|
|
|
2023-12-27 02:57:25 +09:00
|
|
|
// Generate new PR Numbers by the known Issue Numbers, because they share the same number space in Gitea, but they are independent in Gitlab
|
|
|
|
newPRNumber := g.iidResolver.generatePullRequestNumber(pr.IID)
|
2020-04-20 00:44:11 +09:00
|
|
|
|
|
|
|
allPRs = append(allPRs, &base.PullRequest{
|
|
|
|
Title: pr.Title,
|
2020-04-20 21:30:46 +09:00
|
|
|
Number: newPRNumber,
|
2020-04-20 00:44:11 +09:00
|
|
|
PosterName: pr.Author.Username,
|
|
|
|
PosterID: int64(pr.Author.ID),
|
|
|
|
Content: pr.Description,
|
|
|
|
Milestone: milestone,
|
|
|
|
State: pr.State,
|
|
|
|
Created: *pr.CreatedAt,
|
|
|
|
Closed: closeTime,
|
|
|
|
Labels: labels,
|
|
|
|
Merged: merged,
|
2023-10-29 09:54:58 +09:00
|
|
|
MergeCommitSHA: mergeCommitSHA,
|
2020-04-20 00:44:11 +09:00
|
|
|
MergedTime: mergeTime,
|
|
|
|
IsLocked: locked,
|
2023-06-03 05:35:50 +09:00
|
|
|
Reactions: g.awardsToReactions(reactions),
|
2020-04-20 00:44:11 +09:00
|
|
|
Head: base.PullRequestBranch{
|
|
|
|
Ref: pr.SourceBranch,
|
|
|
|
SHA: pr.SHA,
|
|
|
|
RepoName: g.repoName,
|
|
|
|
OwnerName: pr.Author.Username,
|
|
|
|
CloneURL: pr.WebURL,
|
|
|
|
},
|
|
|
|
Base: base.PullRequestBranch{
|
|
|
|
Ref: pr.TargetBranch,
|
|
|
|
SHA: pr.DiffRefs.BaseSha,
|
|
|
|
RepoName: g.repoName,
|
|
|
|
OwnerName: pr.Author.Username,
|
|
|
|
},
|
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-18 02:08:35 +09:00
|
|
|
PatchURL: pr.WebURL + ".patch",
|
|
|
|
ForeignIndex: int64(pr.IID),
|
|
|
|
Context: gitlabIssueContext{IsMergeRequest: true},
|
2020-04-20 00:44:11 +09:00
|
|
|
})
|
2022-09-04 19:47:56 +09:00
|
|
|
|
|
|
|
// SECURITY: Ensure that the PR is safe
|
|
|
|
_ = CheckAndEnsureSafePR(allPRs[len(allPRs)-1], g.baseURL, g)
|
2020-04-20 00:44:11 +09:00
|
|
|
}
|
|
|
|
|
2020-10-14 13:06:00 +09:00
|
|
|
return allPRs, len(prs) < perPage, nil
|
2020-04-20 00:44:11 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetReviews returns pull requests review
|
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-18 02:08:35 +09:00
|
|
|
func (g *GitlabDownloader) GetReviews(reviewable base.Reviewable) ([]*base.Review, error) {
|
|
|
|
approvals, resp, err := g.client.MergeRequestApprovals.GetConfiguration(g.repoID, int(reviewable.GetForeignIndex()), gitlab.WithContext(g.ctx))
|
2020-04-20 21:30:46 +09:00
|
|
|
if err != nil {
|
2022-03-23 13:54:07 +09:00
|
|
|
if resp != nil && resp.StatusCode == http.StatusNotFound {
|
2020-11-20 08:18:34 +09:00
|
|
|
log.Error(fmt.Sprintf("GitlabDownloader: while migrating a error occurred: '%s'", err.Error()))
|
|
|
|
return []*base.Review{}, nil
|
|
|
|
}
|
2020-04-20 21:30:46 +09:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-01-30 02:33:20 +09:00
|
|
|
var createdAt time.Time
|
|
|
|
if approvals.CreatedAt != nil {
|
|
|
|
createdAt = *approvals.CreatedAt
|
|
|
|
} else if approvals.UpdatedAt != nil {
|
|
|
|
createdAt = *approvals.UpdatedAt
|
|
|
|
} else {
|
|
|
|
createdAt = time.Now()
|
|
|
|
}
|
|
|
|
|
2022-01-21 02:46:10 +09:00
|
|
|
reviews := make([]*base.Review, 0, len(approvals.ApprovedBy))
|
2021-08-18 09:47:18 +09:00
|
|
|
for _, user := range approvals.ApprovedBy {
|
2020-04-20 21:30:46 +09:00
|
|
|
reviews = append(reviews, &base.Review{
|
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-18 02:08:35 +09:00
|
|
|
IssueIndex: reviewable.GetLocalIndex(),
|
2021-08-18 09:47:18 +09:00
|
|
|
ReviewerID: int64(user.User.ID),
|
|
|
|
ReviewerName: user.User.Username,
|
2022-01-30 02:33:20 +09:00
|
|
|
CreatedAt: createdAt,
|
2020-04-20 21:30:46 +09:00
|
|
|
// All we get are approvals
|
|
|
|
State: base.ReviewStateApproved,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return reviews, nil
|
2020-04-20 00:44:11 +09:00
|
|
|
}
|
2020-09-03 16:35:17 +09:00
|
|
|
|
2023-06-03 05:35:50 +09:00
|
|
|
func (g *GitlabDownloader) awardsToReactions(awards []*gitlab.AwardEmoji) []*base.Reaction {
|
|
|
|
result := make([]*base.Reaction, 0, len(awards))
|
2023-08-30 15:55:25 +09:00
|
|
|
uniqCheck := make(container.Set[string])
|
2023-06-03 05:35:50 +09:00
|
|
|
for _, award := range awards {
|
|
|
|
uid := fmt.Sprintf("%s%d", award.Name, award.User.ID)
|
2023-08-30 15:55:25 +09:00
|
|
|
if uniqCheck.Add(uid) {
|
2023-06-03 05:35:50 +09:00
|
|
|
result = append(result, &base.Reaction{
|
|
|
|
UserID: int64(award.User.ID),
|
|
|
|
UserName: award.User.Username,
|
|
|
|
Content: award.Name,
|
|
|
|
})
|
|
|
|
}
|
2020-09-03 16:35:17 +09:00
|
|
|
}
|
2023-06-03 05:35:50 +09:00
|
|
|
return result
|
2020-09-03 16:35:17 +09:00
|
|
|
}
|