Verified Commit 4ba1c670 authored by Loïc Dachary's avatar Loïc Dachary
Browse files

reorganize the API and introduce a logger

parent 39b0c9d7
Pipeline #1005 failed with stage
in 1 minute and 11 seconds
......@@ -12,7 +12,7 @@
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
package abstract
package gofff
type Configuration struct {
Directory string
......
......@@ -17,20 +17,22 @@ package domain
import (
"context"
"lab.forgefriends.org/friendlyforgeformat/gofff/forges/abstract"
"lab.forgefriends.org/friendlyforgeformat/gofff"
"lab.forgefriends.org/friendlyforgeformat/gofff/util"
)
// forgefriends/services/migrations/migrate.go migrateRepository
func migrate(ctx context.Context, downloader, uploader abstract.ForgeInterface, features abstract.Features) {
func migrate(ctx context.Context, downloader, uploader gofff.ForgeInterface, logger gofff.Logger, features gofff.Features) {
downloader.SetContext(ctx)
uploader.SetContext(ctx)
supportAllComments := downloader.SupportGetRepoComments()
util.MaybeTerminate(ctx)
logger.Message("Migrating project")
uploader.CreateProject(downloader.GetProject())
logger.Message("Migrating git repositories")
uploader.CreateRepositories(downloader.GetRepositories()...)
if features.Issues {
util.MaybeTerminate(ctx)
......@@ -64,6 +66,20 @@ func migrate(ctx context.Context, downloader, uploader abstract.ForgeInterface,
uploader.Finish()
}
func Migrate(ctx context.Context, downloader, uploader abstract.ForgeInterface, features abstract.Features) {
util.CatchTerminate(func() { migrate(ctx, downloader, uploader, features) })
func Migrate(ctx context.Context, downloader, uploader gofff.ForgeInterface, logger gofff.Logger, features gofff.Features) (err error) {
err = nil
defer func() {
if r := recover(); r != nil {
var ok bool
err, ok = r.(error)
if !ok {
panic(r)
}
}
}()
util.CatchTerminate(func() { migrate(ctx, downloader, uploader, logger, features) })
return
}
......@@ -16,12 +16,13 @@ package domain
import (
"context"
"fmt"
"os"
"path/filepath"
"testing"
"lab.forgefriends.org/friendlyforgeformat/gofff"
"lab.forgefriends.org/friendlyforgeformat/gofff/forges"
"lab.forgefriends.org/friendlyforgeformat/gofff/forges/abstract"
"lab.forgefriends.org/friendlyforgeformat/gofff/forges/file"
"lab.forgefriends.org/friendlyforgeformat/gofff/forges/gitea"
......@@ -29,7 +30,7 @@ import (
)
func TestMigrateFile2File(t *testing.T) {
features := abstract.Features{
features := gofff.Features{
Issues: true,
Comments: true,
Releases: true,
......@@ -41,13 +42,13 @@ func TestMigrateFile2File(t *testing.T) {
downloader := fixture.GetFile()
uploader := fixture.CopyFile()
Migrate(context.Background(), downloader, uploader, features)
assert.NoError(t, Migrate(context.Background(), downloader, uploader, gofff.NewLogger(), features))
fixture.AssertEquals(downloader, uploader)
}
func TestMigrateCancel(t *testing.T) {
fixture := file.NewFixture(t, abstract.AllFeatures)
fixture := file.NewFixture(t, gofff.AllFeatures)
fixture.CreateProject()
downloader := fixture.GetFile()
......@@ -55,14 +56,14 @@ func TestMigrateCancel(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
cancel()
Migrate(ctx, downloader, uploader, abstract.AllFeatures)
assert.NoError(t, Migrate(ctx, downloader, uploader, gofff.NewLogger(), gofff.AllFeatures))
assert.FileExists(t, filepath.Join(downloader.GetDirectory(), "issue.json"))
assert.NoFileExists(t, filepath.Join(uploader.GetDirectory(), "issue.json"))
}
func TestMigrateFile2Gitea(t *testing.T) {
features := abstract.Features{
features := gofff.Features{
Issues: true,
Comments: true,
}
......@@ -80,7 +81,7 @@ func TestMigrateFile2Gitea(t *testing.T) {
projectname := "projectname"
cloneAddr := "http://" + ip + ":8781/" + username + "/" + projectname
uploaderOptions := gitea.Options{
Configuration: abstract.Configuration{
Configuration: gofff.Configuration{
Directory: t.TempDir(),
},
Features: features,
......@@ -91,11 +92,26 @@ func TestMigrateFile2Gitea(t *testing.T) {
uploader := forges.New(uploaderOptions)
uploader.DeleteProject()
Migrate(context.Background(), downloader, uploader, features)
assert.NoError(t, Migrate(context.Background(), downloader, uploader, gofff.NewLogger(), features))
issues := uploader.GetIssues()
assert.EqualValues(t, 1, len(issues))
uploader = forges.New(uploaderOptions)
Migrate(context.Background(), downloader, uploader, features)
assert.NoError(t, Migrate(context.Background(), downloader, uploader, gofff.NewLogger(), features))
assert.EqualValues(t, 1, len(issues))
}
func TestMigrateError(t *testing.T) {
fixture := file.NewFixture(t, gofff.AllFeatures)
fixture.CreateProject()
downloader := fixture.GetFile()
uploader := fixture.CopyFile()
logger := gofff.NewLogger()
err := fmt.Errorf("ERROR")
logger.Message = func(string) {
panic(err)
}
assert.EqualValues(t, Migrate(context.Background(), downloader, uploader, logger, gofff.AllFeatures), err)
}
......@@ -12,7 +12,7 @@
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
package abstract
package gofff
type Features struct {
Wiki bool
......
......@@ -20,15 +20,16 @@ import (
"os"
"path/filepath"
"lab.forgefriends.org/friendlyforgeformat/gofff/forges/abstract"
"lab.forgefriends.org/friendlyforgeformat/gofff"
"lab.forgefriends.org/friendlyforgeformat/gofff/forges/null"
"lab.forgefriends.org/friendlyforgeformat/gofff/format"
"lab.forgefriends.org/friendlyforgeformat/gofff/util"
)
type Options struct {
abstract.Configuration
abstract.Features
gofff.Logger
gofff.Configuration
gofff.Features
Validation bool
}
......@@ -40,7 +41,7 @@ func (o Options) GetProjectPath() string {
return o.Directory
}
func (o Options) GetConfiguration() abstract.Configuration {
func (o Options) GetConfiguration() gofff.Configuration {
return o.Configuration
}
......
......@@ -17,13 +17,13 @@ package file
import (
"testing"
"lab.forgefriends.org/friendlyforgeformat/gofff/forges/abstract"
"lab.forgefriends.org/friendlyforgeformat/gofff"
"github.com/stretchr/testify/assert"
)
func TestFile(t *testing.T) {
fixture := NewFixture(t, abstract.AllFeatures)
fixture := NewFixture(t, gofff.AllFeatures)
fixture.CreateProject()
original := fixture.GetFile()
......
......@@ -21,7 +21,7 @@ import (
"testing"
"time"
"lab.forgefriends.org/friendlyforgeformat/gofff/forges/abstract"
"lab.forgefriends.org/friendlyforgeformat/gofff"
"lab.forgefriends.org/friendlyforgeformat/gofff/format"
"lab.forgefriends.org/friendlyforgeformat/gofff/util"
......@@ -40,11 +40,11 @@ type User struct {
Email string
}
func NewFixture(t *testing.T, features abstract.Features) *Fixture {
func NewFixture(t *testing.T, features gofff.Features) *Fixture {
directory := t.TempDir()
f := &File{}
f.Init(Options{
Configuration: abstract.Configuration{
Configuration: gofff.Configuration{
Directory: directory,
},
Features: features,
......@@ -59,7 +59,7 @@ func NewFixture(t *testing.T, features abstract.Features) *Fixture {
func (f *Fixture) CopyFile() *File {
c := &File{}
c.Init(Options{
Configuration: abstract.Configuration{
Configuration: gofff.Configuration{
Directory: f.t.TempDir(),
},
Features: f.f.Options.Features,
......
......@@ -19,6 +19,7 @@ import (
"fmt"
"path/filepath"
"lab.forgefriends.org/friendlyforgeformat/gofff"
"lab.forgefriends.org/friendlyforgeformat/gofff/forges/abstract"
"lab.forgefriends.org/friendlyforgeformat/gofff/forges/db"
"lab.forgefriends.org/friendlyforgeformat/gofff/forges/file"
......@@ -31,7 +32,7 @@ import (
type OptionsConstraint interface {
Verbose() bool
GetProjectPath() string
GetConfiguration() abstract.Configuration
GetConfiguration() gofff.Configuration
}
type IOConstraint[IO any, Options OptionsConstraint, ProjectProviderPtr any, ProjectPtr any, IssueProviderPtr any, CommentProviderPtr any, PullRequestProviderPtr any] interface {
......@@ -331,7 +332,7 @@ func (f *Forge[Options, IO, Project, ProjectProvider, Issue, IssueProvider, Comm
}
}
func New(options OptionsConstraint) abstract.ForgeInterface {
func New(options OptionsConstraint) gofff.ForgeInterface {
switch o := options.(type) {
case file.Options:
f := &file.File{}
......@@ -346,3 +347,21 @@ func New(options OptionsConstraint) abstract.ForgeInterface {
panic(fmt.Errorf("unsupported type %T", options))
}
}
func NewForge(options OptionsConstraint) (forge gofff.ForgeInterface, err error) {
err = nil
defer func() {
if r := recover(); r != nil {
var ok bool
err, ok = r.(error)
if !ok {
panic(r)
}
}
}()
forge = New(options)
return
}
......@@ -18,7 +18,7 @@ import (
"os"
"testing"
"lab.forgefriends.org/friendlyforgeformat/gofff/forges/abstract"
"lab.forgefriends.org/friendlyforgeformat/gofff"
"lab.forgefriends.org/friendlyforgeformat/gofff/forges/file"
"lab.forgefriends.org/friendlyforgeformat/gofff/forges/gitea"
......@@ -40,7 +40,7 @@ func TestNewGitea(t *testing.T) {
projectname := "projectname"
cloneAddr := "http://" + ip + ":8781/" + username + "/" + projectname
uploader := New(gitea.Options{
Configuration: abstract.Configuration{
Configuration: gofff.Configuration{
Directory: tmpDir,
},
CloneAddr: cloneAddr,
......
......@@ -20,7 +20,7 @@ import (
"net/url"
"strings"
"lab.forgefriends.org/friendlyforgeformat/gofff/forges/abstract"
"lab.forgefriends.org/friendlyforgeformat/gofff"
"lab.forgefriends.org/friendlyforgeformat/gofff/forges/null"
"lab.forgefriends.org/friendlyforgeformat/gofff/format"
"lab.forgefriends.org/friendlyforgeformat/gofff/util"
......@@ -29,15 +29,16 @@ import (
)
type Options struct {
abstract.Configuration
abstract.Features
gofff.Logger
gofff.Configuration
gofff.Features
CloneAddr string
AuthUsername string
AuthPassword string
AuthToken string
}
func (o Options) GetConfiguration() abstract.Configuration {
func (o Options) GetConfiguration() gofff.Configuration {
return o.Configuration
}
......
......@@ -18,7 +18,7 @@ import (
"path/filepath"
"testing"
"lab.forgefriends.org/friendlyforgeformat/gofff/forges/abstract"
"lab.forgefriends.org/friendlyforgeformat/gofff"
"lab.forgefriends.org/friendlyforgeformat/gofff/forges/file"
"lab.forgefriends.org/friendlyforgeformat/gofff/format"
......@@ -31,7 +31,7 @@ func TestRepository(t *testing.T) {
assert.NotNil(t, project)
defer gi.g.GetProjectProvider().DeleteProject(gi.repoPath)
fixture := file.NewFixture(t, abstract.AllFeatures)
fixture := file.NewFixture(t, gofff.AllFeatures)
fixture.CreateRepositories()
repository := fixture.GetDefaultRepositoryDirectory()
_, _ = fixture.CreateRepositoryContent(repository)
......
......@@ -19,7 +19,7 @@ import (
"testing"
"time"
"lab.forgefriends.org/friendlyforgeformat/gofff/forges/abstract"
"lab.forgefriends.org/friendlyforgeformat/gofff"
"lab.forgefriends.org/friendlyforgeformat/gofff/forges/file"
"lab.forgefriends.org/friendlyforgeformat/gofff/format"
"lab.forgefriends.org/friendlyforgeformat/gofff/util"
......@@ -34,7 +34,7 @@ func TestPullRequests(t *testing.T) {
assert.NotNil(t, project)
// defer gi.g.GetProjectProvider().DeleteProject(gi.repoPath)
fixture := file.NewFixture(t, abstract.AllFeatures)
fixture := file.NewFixture(t, gofff.AllFeatures)
pr := &PullRequest{}
pr.FromFormat(fixture.CreatePullRequest(project.ToFormat(), getTestUser(t, gi)))
gi.g.CreateRepository(format.Repository{
......
......@@ -18,7 +18,7 @@ import (
"context"
"fmt"
"lab.forgefriends.org/friendlyforgeformat/gofff/forges/abstract"
"lab.forgefriends.org/friendlyforgeformat/gofff"
"lab.forgefriends.org/friendlyforgeformat/gofff/format"
)
......@@ -43,7 +43,7 @@ func (err ErrNotSupported) Error() string {
type Null struct{}
var _ abstract.ForgeInterface = &Null{}
var _ gofff.ForgeInterface = &Null{}
// Context sets the context
func (n Null) SetContext(context.Context) {
......
......@@ -12,7 +12,7 @@
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
package abstract
package gofff
import (
"context"
......
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
package gofff
type Logger struct {
Message func(string)
Log func(string)
}
func NewLogger() Logger {
return Logger{
Message: func(string) {},
Log: func(string) {},
}
}
......@@ -37,7 +37,7 @@ func Retry(fun func(), tries int) {
return
}
errors = append(errors, err)
time.Sleep(1 * time.Second)
<-time.After(1 * time.Second)
}
panic(fmt.Errorf("Retry: failed %v", errors))
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment