bench-forgejo/services/migrations/codebase_test.go

151 lines
4.2 KiB
Go
Raw Permalink Normal View History

// Copyright 2021 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package migrations
import (
"context"
"net/url"
"os"
"testing"
"time"
base "code.gitea.io/gitea/modules/migration"
"github.com/stretchr/testify/assert"
)
func TestCodebaseDownloadRepo(t *testing.T) {
// Skip tests if Codebase token is not found
cloneUser := os.Getenv("CODEBASE_CLONE_USER")
clonePassword := os.Getenv("CODEBASE_CLONE_PASSWORD")
apiUser := os.Getenv("CODEBASE_API_USER")
apiPassword := os.Getenv("CODEBASE_API_TOKEN")
if apiUser == "" || apiPassword == "" {
t.Skip("skipped test because a CODEBASE_ variable was not in the environment")
}
cloneAddr := "https://gitea-test.codebasehq.com/gitea-test/test.git"
u, _ := url.Parse(cloneAddr)
if cloneUser != "" {
u.User = url.UserPassword(cloneUser, clonePassword)
}
factory := &CodebaseDownloaderFactory{}
downloader, err := factory.New(context.Background(), base.MigrateOptions{
CloneAddr: u.String(),
AuthUsername: apiUser,
AuthPassword: apiPassword,
})
if err != nil {
t.Fatalf("Error creating Codebase downloader: %v", err)
}
repo, err := downloader.GetRepoInfo()
assert.NoError(t, err)
assertRepositoryEqual(t, &base.Repository{
Name: "test",
Owner: "",
Description: "Repository Description",
CloneURL: "git@codebasehq.com:gitea-test/gitea-test/test.git",
OriginalURL: cloneAddr,
}, repo)
milestones, err := downloader.GetMilestones()
assert.NoError(t, err)
assertMilestonesEqual(t, []*base.Milestone{
{
Title: "Milestone1",
Deadline: timePtr(time.Date(2021, time.September, 16, 0, 0, 0, 0, time.UTC)),
},
{
Title: "Milestone2",
Deadline: timePtr(time.Date(2021, time.September, 17, 0, 0, 0, 0, time.UTC)),
Closed: timePtr(time.Date(2021, time.September, 17, 0, 0, 0, 0, time.UTC)),
State: "closed",
},
}, milestones)
labels, err := downloader.GetLabels()
assert.NoError(t, err)
assert.Len(t, labels, 4)
issues, isEnd, err := downloader.GetIssues(1, 2)
assert.NoError(t, err)
assert.True(t, isEnd)
assertIssuesEqual(t, []*base.Issue{
{
Number: 2,
Title: "Open Ticket",
Content: "Open Ticket Message",
PosterName: "gitea-test-43",
PosterEmail: "gitea-codebase@smack.email",
State: "open",
Created: time.Date(2021, time.September, 26, 19, 19, 14, 0, time.UTC),
Updated: time.Date(2021, time.September, 26, 19, 19, 34, 0, time.UTC),
Labels: []*base.Label{
{
Name: "Feature",
},
},
},
{
Number: 1,
Title: "Closed Ticket",
Content: "Closed Ticket Message",
PosterName: "gitea-test-43",
PosterEmail: "gitea-codebase@smack.email",
State: "closed",
Milestone: "Milestone1",
Created: time.Date(2021, time.September, 26, 19, 18, 33, 0, time.UTC),
Updated: time.Date(2021, time.September, 26, 19, 18, 55, 0, time.UTC),
Labels: []*base.Label{
{
Name: "Bug",
},
},
},
}, issues)
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 22:38:35 +05:30
comments, _, err := downloader.GetComments(issues[0])
assert.NoError(t, err)
assertCommentsEqual(t, []*base.Comment{
{
IssueIndex: 2,
PosterName: "gitea-test-43",
PosterEmail: "gitea-codebase@smack.email",
Created: time.Date(2021, time.September, 26, 19, 19, 34, 0, time.UTC),
Updated: time.Date(2021, time.September, 26, 19, 19, 34, 0, time.UTC),
Content: "open comment",
},
}, comments)
prs, _, err := downloader.GetPullRequests(1, 1)
assert.NoError(t, err)
assertPullRequestsEqual(t, []*base.PullRequest{
{
Number: 3,
Title: "Readme Change",
Content: "Merge Request comment",
PosterName: "gitea-test-43",
PosterEmail: "gitea-codebase@smack.email",
State: "open",
Created: time.Date(2021, time.September, 26, 20, 25, 47, 0, time.UTC),
Updated: time.Date(2021, time.September, 26, 20, 25, 47, 0, time.UTC),
Head: base.PullRequestBranch{
Ref: "readme-mr",
SHA: "1287f206b888d4d13540e0a8e1c07458f5420059",
RepoName: "test",
},
Base: base.PullRequestBranch{
Ref: "master",
SHA: "f32b0a9dfd09a60f616f29158f772cedd89942d2",
RepoName: "test",
},
},
}, prs)
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 22:38:35 +05:30
rvs, err := downloader.GetReviews(prs[0])
assert.NoError(t, err)
assert.Empty(t, rvs)
}