73 lines
2 KiB
Go
73 lines
2 KiB
Go
// Copyright Earl Warren <contact@earl-warren.org>
|
|
// Copyright Loïc Dachary <loic@dachary.org>
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package pullrequest
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"code.forgejo.org/f3/gof3/v3/f3"
|
|
helpers_repository "code.forgejo.org/f3/gof3/v3/forges/helpers/repository"
|
|
"code.forgejo.org/f3/gof3/v3/id"
|
|
"code.forgejo.org/f3/gof3/v3/logger"
|
|
f3_tree "code.forgejo.org/f3/gof3/v3/tree/f3"
|
|
"code.forgejo.org/f3/gof3/v3/tree/generic"
|
|
)
|
|
|
|
type Interface interface {
|
|
Upsert(context.Context) id.NodeID
|
|
Get(context.Context) bool
|
|
}
|
|
|
|
type prInterface interface {
|
|
logger.MessageInterface
|
|
f3_tree.PullRequestDriverInterface
|
|
GetNode() generic.NodeInterface
|
|
SetFetchFunc(func(ctx context.Context, url, ref string))
|
|
ToFormat() f3.Interface
|
|
}
|
|
|
|
type helper struct {
|
|
pr prInterface
|
|
r helpers_repository.Interface
|
|
}
|
|
|
|
func (o *helper) getRepository(ctx context.Context) helpers_repository.Interface {
|
|
if o.r == nil {
|
|
project := f3_tree.GetFirstNodeKind(o.pr.GetNode(), f3_tree.KindProject)
|
|
r := project.Find(generic.NewPathFromString("repositories/vcs"))
|
|
if r == generic.NilNode {
|
|
panic(fmt.Errorf("no repository found for %s", project.GetCurrentPath()))
|
|
}
|
|
o.r = r.GetDriver().(f3_tree.ForgeDriverInterface).GetHelper().(helpers_repository.Interface)
|
|
}
|
|
return o.r
|
|
}
|
|
|
|
func (o *helper) Get(ctx context.Context) bool {
|
|
headURL := o.getRepository(ctx).GetRepositoryURL()
|
|
headRef := o.pr.GetPullRequestHead()
|
|
o.pr.SetFetchFunc(func(ctx context.Context, url, ref string) {
|
|
helpers_repository.GitMirrorRef(ctx, o.pr, headURL, headRef, url, ref)
|
|
})
|
|
return true
|
|
}
|
|
|
|
func (o *helper) Upsert(ctx context.Context) id.NodeID {
|
|
f := o.pr.ToFormat().(*f3.PullRequest)
|
|
if f.FetchFunc != nil {
|
|
pushURL := o.getRepository(ctx).GetRepositoryPushURL()
|
|
for _, pushRef := range o.pr.GetPullRequestPushRefs() {
|
|
f.FetchFunc(ctx, pushURL, pushRef)
|
|
}
|
|
}
|
|
return o.pr.GetNode().GetID()
|
|
}
|
|
|
|
func NewHelper(ctx context.Context, pr prInterface) Interface {
|
|
return &helper{
|
|
pr: pr,
|
|
}
|
|
}
|