-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Add migrate repo archiver and packages storage support on command line #20757
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 6 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
e283d58
Add migrate repo archiver and packages storage support on command line
lunny d313929
Fix typo
lunny 76b5c41
Use stdCtx
lunny d7697ec
Use packageblob and fix command description
lunny 7fcba32
Add migrate packages unit tests
lunny ec99da1
Merge branch 'main' into lunny/migrate_more_storages
lunny 9f0e37a
Fix comment year
lunny 40a52e5
Merge branch 'main' into lunny/migrate_more_storages
lunny 15355c8
Merge branch 'main' into lunny/migrate_more_storages
6543 7cca792
Merge branch 'main' into lunny/migrate_more_storages
lunny 952e30d
Merge branch 'main' into lunny/migrate_more_storages
lunny 404a32b
Fix the migrate storage command line description
lunny 8458feb
Update cmd/migrate_storage.go
lunny 97a1ed2
Update cmd/migrate_storage.go
lunny eb08df9
Update cmd/migrate_storage.go
lunny a8424ee
Merge branch 'main' into lunny/migrate_more_storages
lunny a7ca5f6
Fix test
lunny 8e1bcde
Merge branch 'main' into lunny/migrate_more_storages
lunny 233e02d
Merge branch 'lunny/migrate_more_storages' of github.com:lunny/gitea …
lunny File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// Copyright 2020 The Gitea Authors. All rights reserved. | ||
// Use of this source code is governed by a MIT-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package cmd | ||
|
||
import ( | ||
"testing" | ||
|
||
"code.gitea.io/gitea/models/unittest" | ||
"code.gitea.io/gitea/modules/setting" | ||
) | ||
|
||
func init() { | ||
setting.SetCustomPathAndConf("", "", "") | ||
setting.LoadForTest() | ||
} | ||
|
||
func TestMain(m *testing.M) { | ||
unittest.MainTest(m, &unittest.TestOptions{ | ||
GiteaRootPath: "..", | ||
}) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
// Copyright 2020 The Gitea Authors. All rights reserved. | ||
lunny marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// Use of this source code is governed by a MIT-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package cmd | ||
|
||
import ( | ||
"context" | ||
"os" | ||
"strings" | ||
"testing" | ||
|
||
"code.gitea.io/gitea/models/packages" | ||
"code.gitea.io/gitea/models/unittest" | ||
user_model "code.gitea.io/gitea/models/user" | ||
packages_module "code.gitea.io/gitea/modules/packages" | ||
"code.gitea.io/gitea/modules/storage" | ||
packages_service "code.gitea.io/gitea/services/packages" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestMigratePackages(t *testing.T) { | ||
assert.NoError(t, unittest.PrepareTestDatabase()) | ||
|
||
creator := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1}).(*user_model.User) | ||
|
||
content := "package main\n\nfunc main() {\nfmt.Println(\"hi\")\n}\n" | ||
buf, err := packages_module.CreateHashedBufferFromReader(strings.NewReader(content), 1024) | ||
assert.NoError(t, err) | ||
defer buf.Close() | ||
|
||
v, f, err := packages_service.CreatePackageAndAddFile(&packages_service.PackageCreationInfo{ | ||
PackageInfo: packages_service.PackageInfo{ | ||
Owner: creator, | ||
PackageType: packages.TypeGeneric, | ||
Name: "test", | ||
Version: "1.0.0", | ||
}, | ||
Creator: creator, | ||
SemverCompatible: true, | ||
VersionProperties: map[string]string{}, | ||
}, &packages_service.PackageFileCreationInfo{ | ||
PackageFileInfo: packages_service.PackageFileInfo{ | ||
Filename: "a.go", | ||
}, | ||
Data: buf, | ||
IsLead: true, | ||
}) | ||
assert.NoError(t, err) | ||
assert.NotNil(t, v) | ||
assert.NotNil(t, f) | ||
|
||
ctx := context.Background() | ||
|
||
p, err := os.MkdirTemp(os.TempDir(), "migrated_packages") | ||
assert.NoError(t, err) | ||
|
||
dstStorage, err := storage.NewLocalStorage( | ||
ctx, | ||
storage.LocalStorageConfig{ | ||
Path: p, | ||
}) | ||
assert.NoError(t, err) | ||
|
||
err = migratePackages(ctx, dstStorage) | ||
assert.NoError(t, err) | ||
|
||
entries, err := os.ReadDir(p) | ||
assert.NoError(t, err) | ||
assert.EqualValues(t, 2, len(entries)) | ||
assert.EqualValues(t, "01", entries[0].Name()) | ||
assert.EqualValues(t, "tmp", entries[1].Name()) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// Copyright 2022 The Gitea Authors. All rights reserved. | ||
// Use of this source code is governed by a MIT-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package db | ||
|
||
import ( | ||
"context" | ||
|
||
"code.gitea.io/gitea/modules/setting" | ||
) | ||
|
||
// IterateObjects iterate all the Bean object | ||
func IterateObjects[Object any](ctx context.Context, f func(repo *Object) error) error { | ||
var start int | ||
batchSize := setting.Database.IterateBufferSize | ||
sess := GetEngine(ctx) | ||
for { | ||
repos := make([]*Object, 0, batchSize) | ||
if err := sess.Limit(batchSize, start).Find(&repos); err != nil { | ||
return err | ||
} | ||
if len(repos) == 0 { | ||
return nil | ||
} | ||
start += len(repos) | ||
|
||
for _, repo := range repos { | ||
if err := f(repo); err != nil { | ||
return err | ||
} | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.