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

simplify providers

parent d3ac7bb2
Pipeline #1082 passed with stage
in 2 minutes and 34 seconds
......@@ -54,7 +54,7 @@ func migrate(ctx context.Context, downloader, uploader gofff.ForgeInterface, log
if features.Comments && !supportAllComments {
util.MaybeTerminate(ctx)
for _, issue := range issues {
uploader.CreateComments(downloader.GetComments(issue)...)
uploader.CreateComments(issue, downloader.GetComments(issue)...)
}
}
}
......@@ -66,7 +66,7 @@ func migrate(ctx context.Context, downloader, uploader gofff.ForgeInterface, log
if features.Comments && !supportAllComments {
util.MaybeTerminate(ctx)
for _, pr := range prs {
uploader.CreateComments(downloader.GetComments(pr)...)
uploader.CreateComments(pr, downloader.GetComments(pr)...)
}
}
util.MaybeTerminate(ctx)
......
......@@ -53,7 +53,7 @@ type IssueProviderInterface[IssuePtr any] interface {
type CommentProviderInterface[CommentPtr any] interface {
list.ProviderConstraint[CommentPtr, int64]
GetComments(format.Commentable) []CommentPtr
GetComments() []CommentPtr
ToFormat(CommentPtr) *format.Comment
FromFormat(*format.Comment) CommentPtr
}
......
......@@ -170,7 +170,7 @@ func (f *File) GetComments(commentable format.Commentable) []*format.Comment {
return MaybeGetBeans[format.Comment](f, filepath.Join(f.commentsPath, fmt.Sprintf("%d.json", commentable.GetID())))
}
func (f *File) CreateComments(comments ...*format.Comment) {
func (f *File) CreateComments(commentable format.Commentable, comments ...*format.Comment) {
commentsMap := make(map[int64][]*format.Comment, len(comments))
for _, comment := range comments {
commentsMap[comment.IssueIndex] = append(commentsMap[comment.IssueIndex], comment)
......
......@@ -72,7 +72,7 @@ func TestFile(t *testing.T) {
issues := original.GetIssues()
assert.NotEmpty(t, original.GetComments(issues[0]))
copy.CreateComments(original.GetComments(issues[0])...)
copy.CreateComments(issues[0], original.GetComments(issues[0])...)
assert.EqualValues(t, original.GetComments(issues[0]), copy.GetComments(issues[0]))
prs := original.GetPullRequests()
......
......@@ -470,7 +470,7 @@ func (f *Fixture) CreateProject() {
f.f.CreatePullRequests(&pr)
comment := f.CreateComment(project, &issue, user1)
f.f.CreateComments(&comment)
f.f.CreateComments(&issue, &comment)
review := f.CreateReview(project, &pr, repositoryDirectory, user1)
f.f.CreateReviews(&review)
......
......@@ -44,7 +44,7 @@ type IOConstraint[IO any, OptionsPtr any, ProjectPtr any, MilestonePtr any, Labe
GetMilestoneProvider(ProjectPtr) abstract.MilestoneProviderInterface[MilestonePtr]
GetLabelProvider(ProjectPtr) abstract.LabelProviderInterface[LabelPtr]
GetIssueProvider(ProjectPtr) abstract.IssueProviderInterface[IssuePtr]
GetCommentProvider(ProjectPtr) abstract.CommentProviderInterface[CommentPtr]
GetCommentProvider(ProjectPtr, format.Commentable) abstract.CommentProviderInterface[CommentPtr]
GetReviewProvider(ProjectPtr) abstract.ReviewProviderInterface[ReviewPtr]
GetPullRequestProvider(ProjectPtr) abstract.PullRequestProviderInterface[PullRequestPtr]
GetReleaseProvider(ProjectPtr) abstract.ReleaseProviderInterface[ReleasePtr]
......@@ -266,17 +266,17 @@ func (f *Forge[Options, IO, OptionsPtr, IOPtr, ProjectPtr, MilestonePtr, LabelPt
}
func (f *Forge[Options, IO, OptionsPtr, IOPtr, ProjectPtr, MilestonePtr, LabelPtr, IssuePtr, CommentPtr, ReviewPtr, PullRequestPtr, ReleasePtr, TopicPtr]) GetComments(commentable format.Commentable) []*format.Comment {
p := f.io.GetCommentProvider(f.project)
p := f.io.GetCommentProvider(f.project, commentable)
comments := make([]*format.Comment, 0, 100)
for _, i := range p.GetComments(commentable) {
for _, i := range p.GetComments() {
f.comments.Store(i)
comments = append(comments, CommentPtr(i).ToFormat())
}
return comments
}
func (f *Forge[Options, IO, OptionsPtr, IOPtr, ProjectPtr, MilestonePtr, LabelPtr, IssuePtr, CommentPtr, ReviewPtr, PullRequestPtr, ReleasePtr, TopicPtr]) CreateComments(comments ...*format.Comment) {
provider := f.io.GetCommentProvider(f.project)
func (f *Forge[Options, IO, OptionsPtr, IOPtr, ProjectPtr, MilestonePtr, LabelPtr, IssuePtr, CommentPtr, ReviewPtr, PullRequestPtr, ReleasePtr, TopicPtr]) CreateComments(commentable format.Commentable, comments ...*format.Comment) {
provider := f.io.GetCommentProvider(f.project, commentable)
for _, comment := range comments {
c := provider.FromFormat(comment)
list.UpsertBean[CommentPtr, int64](&f.comments, provider, c)
......
......@@ -15,10 +15,6 @@
package gitea
import (
"fmt"
"path/filepath"
"strconv"
"lab.forgefriends.org/friendlyforgeformat/gofff"
"lab.forgefriends.org/friendlyforgeformat/gofff/format"
"lab.forgefriends.org/friendlyforgeformat/gofff/util"
......@@ -89,8 +85,9 @@ func (o *Comment) FromFormat(comment format.Comment) {
}
type CommentProvider struct {
g *Gitea
project *Project
g *Gitea
project *Project
commentable format.Commentable
}
func (o *CommentProvider) ToFormat(comment *Comment) *format.Comment {
......@@ -110,18 +107,13 @@ func (o *CommentProvider) StoreForeignReference(comment *Comment, f *format.Comm
o.g.GetDb().Store(o.project.ID, gofff.ForeignReferenceComment, comment, f)
}
func (o *CommentProvider) Init(gitea *Gitea) *CommentProvider {
o.g = gitea
return o
}
func (o *CommentProvider) GetComments(commentable format.Commentable) []*Comment {
func (o *CommentProvider) GetComments() []*Comment {
allComments := make([]*Comment, 0, o.g.perPage)
for page := 1; ; page++ {
util.MaybeTerminate(o.g.ctx)
comments, _, _ := o.g.client.ListIssueComments(o.project.Owner.UserName, o.project.Name, commentable.GetID(), gitea_sdk.ListIssueCommentOptions{
comments, _, _ := o.g.client.ListIssueComments(o.project.Owner.UserName, o.project.Name, o.commentable.GetID(), gitea_sdk.ListIssueCommentOptions{
ListOptions: gitea_sdk.ListOptions{Page: page, PageSize: o.g.perPage},
})
......@@ -130,7 +122,7 @@ func (o *CommentProvider) GetComments(commentable format.Commentable) []*Comment
allComments = append(allComments, &Comment{
Comment: *comment,
CommentableIndex: commentable.GetID(),
CommentableIndex: o.commentable.GetID(),
Reactions: reactions,
})
}
......@@ -148,23 +140,9 @@ func (o *CommentProvider) Get(id int64) *Comment {
panic(err)
}
var url string
if comment.IssueURL != "" {
url = comment.IssueURL
} else if comment.PRURL != "" {
url = comment.PRURL
} else {
panic(fmt.Errorf("both IssueURL and PRURL are empty string %v", comment))
}
_, index := filepath.Split(url)
commentableIndex, err := strconv.ParseInt(index, 10, 64)
if err != nil {
panic(err)
}
return &Comment{
Comment: *comment,
CommentableIndex: commentableIndex,
CommentableIndex: o.commentable.GetID(),
}
}
......
......@@ -40,7 +40,7 @@ func TestComment(t *testing.T) {
})
assert.EqualValues(t, issue.Title, title)
commentProvider := gi.g.GetCommentProvider(project)
commentProvider := gi.g.GetCommentProvider(project, issue.ToFormat())
commentBody := "comment"
comment := commentProvider.Put(&Comment{
CommentableIndex: issue.Index,
......
......@@ -95,19 +95,10 @@ func (o *Options) GetProjectPath() string {
}
type Gitea struct {
perPage int
ctx context.Context
client *gitea_sdk.Client
options *Options
projectProvider *ProjectProvider
milestoneProvider *MilestoneProvider
labelProvider *LabelProvider
issueProvider *IssueProvider
reviewProvider *ReviewProvider
commentProvider *CommentProvider
pullRequestProvider *PullRequestProvider
releaseProvider *ReleaseProvider
topicProvider *TopicProvider
perPage int
ctx context.Context
client *gitea_sdk.Client
options *Options
}
func (g *Gitea) Init(options *Options) {
......@@ -123,15 +114,6 @@ func (g *Gitea) Init(options *Options) {
g.perPage = 100
g.client = c
g.options = options
g.projectProvider = (&ProjectProvider{}).Init(g)
g.milestoneProvider = (&MilestoneProvider{}).Init(g)
g.labelProvider = (&LabelProvider{}).Init(g)
g.issueProvider = (&IssueProvider{}).Init(g)
g.commentProvider = (&CommentProvider{}).Init(g)
g.reviewProvider = (&ReviewProvider{}).Init(g)
g.pullRequestProvider = (&PullRequestProvider{}).Init(g)
g.releaseProvider = (&ReleaseProvider{}).Init(g)
g.topicProvider = (&TopicProvider{}).Init(g)
}
func (g *Gitea) GetDb() gofff.ForeignReferenceInterface {
......@@ -171,47 +153,64 @@ func (g *Gitea) CreateRepository(directory string, repository format.Repository)
}
func (g Gitea) GetProjectProvider() abstract.ProjectProviderInterface[*Project] {
return g.projectProvider
return &ProjectProvider{g: &g}
}
func (g Gitea) GetMilestoneProvider(project *Project) abstract.MilestoneProviderInterface[*Milestone] {
g.milestoneProvider.project = project
return g.milestoneProvider
return &MilestoneProvider{
g: &g,
project: project,
}
}
func (g Gitea) GetLabelProvider(project *Project) abstract.LabelProviderInterface[*Label] {
g.labelProvider.project = project
return g.labelProvider
return &LabelProvider{
g: &g,
project: project,
}
}
func (g Gitea) GetIssueProvider(project *Project) abstract.IssueProviderInterface[*Issue] {
g.issueProvider.project = project
return g.issueProvider
return &IssueProvider{
g: &g,
project: project,
}
}
func (g Gitea) GetCommentProvider(project *Project) abstract.CommentProviderInterface[*Comment] {
g.commentProvider.project = project
return g.commentProvider
func (g Gitea) GetCommentProvider(project *Project, commentable format.Commentable) abstract.CommentProviderInterface[*Comment] {
return &CommentProvider{
g: &g,
project: project,
commentable: commentable,
}
}
func (g Gitea) GetReviewProvider(project *Project) abstract.ReviewProviderInterface[*Review] {
g.reviewProvider.project = project
return g.reviewProvider
return &ReviewProvider{
g: &g,
project: project,
}
}
func (g Gitea) GetPullRequestProvider(project *Project) abstract.PullRequestProviderInterface[*PullRequest] {
g.pullRequestProvider.project = project
return g.pullRequestProvider
return (&PullRequestProvider{
g: &g,
project: project,
}).Init()
}
func (g Gitea) GetReleaseProvider(project *Project) abstract.ReleaseProviderInterface[*Release] {
g.releaseProvider.project = project
return g.releaseProvider
return &ReleaseProvider{
g: &g,
project: project,
}
}
func (g Gitea) GetTopicProvider(project *Project) abstract.TopicProviderInterface[*Topic] {
g.topicProvider.project = project
return g.topicProvider
return &TopicProvider{
g: &g,
project: project,
}
}
func (g Gitea) Finish() {
......
......@@ -145,11 +145,6 @@ func (o *IssueProvider) FromFormat(i *format.Issue) *Issue {
func (o *IssueProvider) StoreForeignReference(issue *Issue, i *format.Issue) {
}
func (o *IssueProvider) Init(gitea *Gitea) *IssueProvider {
o.g = gitea
return o
}
func (o *IssueProvider) GetIssues() []*Issue {
allIssues := make([]*Issue, 0, o.g.perPage)
......
......@@ -78,11 +78,6 @@ func (o *LabelProvider) StoreForeignReference(label *Label, m *format.Label) {
o.g.GetDb().Store(o.project.ID, gofff.ForeignReferenceLabel, label, m)
}
func (o *LabelProvider) Init(gitea *Gitea) *LabelProvider {
o.g = gitea
return o
}
func (o *LabelProvider) GetLabels() []*Label {
allLabels := make([]*Label, 0, o.g.perPage)
......
......@@ -101,11 +101,6 @@ func (o *MilestoneProvider) StoreForeignReference(milestone *Milestone, m *forma
o.g.GetDb().Store(o.project.ID, gofff.ForeignReferenceMilestone, milestone, m)
}
func (o *MilestoneProvider) Init(gitea *Gitea) *MilestoneProvider {
o.g = gitea
return o
}
func (o *MilestoneProvider) GetObjects() []*Milestone {
allMilestones := make([]*Milestone, 0, o.g.perPage)
......
......@@ -86,11 +86,6 @@ func (o *ProjectProvider) StoreForeignReference(project *Project, f *format.Proj
o.g.GetDb().Store(0, gofff.ForeignReferenceProject, project, f)
}
func (o *ProjectProvider) Init(gitea *Gitea) *ProjectProvider {
o.g = gitea
return o
}
func (o *ProjectProvider) GetProjectByPath(path string) *Project {
if !strings.HasSuffix(o.g.options.CloneAddr, path) {
panic(fmt.Errorf(o.g.options.CloneAddr + " does not end with " + path))
......
......@@ -250,8 +250,7 @@ func (o *PullRequestProvider) FromFormat(pr *format.PullRequest) *PullRequest {
func (o *PullRequestProvider) StoreForeignReference(pullRequest *PullRequest, pr *format.PullRequest) {
}
func (o *PullRequestProvider) Init(gitea *Gitea) *PullRequestProvider {
o.g = gitea
func (o *PullRequestProvider) Init() *PullRequestProvider {
o.prHeadCache = make(map[string]struct{})
return o
}
......
......@@ -440,39 +440,15 @@ func TestUpdateGitForPullRequest(t *testing.T) {
},
}
},
// {
// name: "no fork, not found Head.SHA",
// head: headRef,
// pr: base.PullRequest{
// PatchURL: "",
// Number: 1,
// State: "open",
// Base: base.PullRequestBranch{
// CloneURL: fromRepo.RepoPath(),
// Ref: baseRef,
// SHA: baseSHA,
// RepoName: fromRepo.Name,
// OwnerName: fromRepo.OwnerName,
// },
// Head: base.PullRequestBranch{
// CloneURL: fromRepo.RepoPath(),
// Ref: headRef,
// SHA: "2697b352310fcd01cbd1f3dbd43b894080027f68",
// RepoName: fromRepo.Name,
// OwnerName: fromRepo.OwnerName,
// },
// },
// assertContent: func(t *testing.T, content string) {
// assert.Contains(t, content, "Deprecated local head")
// assert.NotContains(t, content, "Cannot remove local head")
// },
// },
} {
testCase := testCaseGenerator()
t.Run(testCase.name, func(t *testing.T) {
defer testCase.fixture.delete()
testCase.fixture.rootGitea.g.GetPullRequestProvider(testCase.fixture.fromProject)
provider := testCase.fixture.rootGitea.g.pullRequestProvider
provider := (&PullRequestProvider{
g: testCase.fixture.rootGitea.g,
project: testCase.fixture.fromProject,
}).Init()
head, messages := provider.updateGitForPullRequest(testCase.fixture.fromRepositoryDirectory, &testCase.pr)
assert.EqualValues(t, testCase.head, head)
if testCase.assertContent != nil {
......
......@@ -118,11 +118,6 @@ type ReleaseProvider struct {
project *Project
}
func (o *ReleaseProvider) Init(gitea *Gitea) *ReleaseProvider {
o.g = gitea
return o
}
func (o *ReleaseProvider) ToFormat(r *Release) *format.Release {
httpClient := o.g.GetNewMigrationHTTPClient()()
......
......@@ -151,11 +151,6 @@ func (o *ReviewProvider) StoreForeignReference(review *Review, f *format.Review)
o.g.GetDb().Store(o.project.ID, gofff.ForeignReferenceReview, (*ForeignReview)(&review.PullReview), f)
}
func (o *ReviewProvider) Init(gitea *Gitea) *ReviewProvider {
o.g = gitea
return o
}
func (o *ReviewProvider) GetReviews(reviewable format.Reviewable) []*Review {
allReviews := make([]*Review, 0, o.g.perPage)
......
......@@ -54,11 +54,6 @@ func (o *TopicProvider) FromFormat(i *format.Topic) *Topic {
return &t
}
func (o *TopicProvider) Init(gitea *Gitea) *TopicProvider {
o.g = gitea
return o
}
func (o *TopicProvider) GetTopics() []*Topic {
allTopics := make([]*Topic, 0, o.g.perPage)
......
......@@ -152,7 +152,7 @@ func (n *Null) CreateIssues(issues ...*format.Issue) {
panic(ErrNotSupported{Entity: "CreateIssues"})
}
func (n *Null) CreateComments(comments ...*format.Comment) {
func (n *Null) CreateComments(commentable format.Commentable, comments ...*format.Comment) {
panic(ErrNotSupported{Entity: "CreateComments"})
}
......
......@@ -45,8 +45,8 @@ type ForgeInterface interface {
CreateReleases(releases ...*format.Release)
CreateLabels(labels ...*format.Label)
CreateIssues(issues ...*format.Issue)
CreateComments(comments ...*format.Comment)
CreatePullRequests(prs ...*format.PullRequest)
CreateComments(commentable format.Commentable, comments ...*format.Comment)
CreateReviews(reviews ...*format.Review)
Rollback()
......
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