mirror of
https://lab.forgefriends.org/friendlyforgeformat/gof3.git
synced 2025-10-06 06:52:40 +02:00
270 lines
7.3 KiB
Go
270 lines
7.3 KiB
Go
// Copyright Earl Warren <contact@earl-warren.org>
|
|
// Copyright Loïc Dachary <loic@dachary.org>
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package forgejo
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"time"
|
|
|
|
"lab.forgefriends.org/friendlyforgeformat/gof3/f3"
|
|
helper_pullrequest "lab.forgefriends.org/friendlyforgeformat/gof3/forges/helpers/pullrequest"
|
|
f3_tree "lab.forgefriends.org/friendlyforgeformat/gof3/tree/f3"
|
|
"lab.forgefriends.org/friendlyforgeformat/gof3/tree/generic"
|
|
"lab.forgefriends.org/friendlyforgeformat/gof3/util"
|
|
|
|
forgejo_sdk "lab.forgefriends.org/friendlyforgeformat/gof3/forges/forgejo/sdk"
|
|
)
|
|
|
|
type pullRequest struct {
|
|
common
|
|
|
|
forgejoPullRequest *forgejo_sdk.PullRequest
|
|
h helper_pullrequest.Interface
|
|
fetchFunc f3.PullRequestFetchFunc
|
|
}
|
|
|
|
var _ f3_tree.ForgeDriverInterface = &pullRequest{}
|
|
|
|
func newPullRequest() generic.NodeDriverInterface {
|
|
return &pullRequest{}
|
|
}
|
|
|
|
func (o *pullRequest) SetNative(pullRequest any) {
|
|
o.forgejoPullRequest = pullRequest.(*forgejo_sdk.PullRequest)
|
|
}
|
|
|
|
func (o *pullRequest) GetNativeID() string {
|
|
return fmt.Sprintf("%d", o.forgejoPullRequest.Index)
|
|
}
|
|
|
|
func (o *pullRequest) NewFormat() f3.Interface {
|
|
node := o.GetNode()
|
|
return node.GetTree().(f3_tree.TreeInterface).NewFormat(node.GetKind())
|
|
}
|
|
|
|
func (o *pullRequest) ToFormat() f3.Interface {
|
|
if o.forgejoPullRequest == nil {
|
|
return o.NewFormat()
|
|
}
|
|
|
|
var milestone string
|
|
if o.forgejoPullRequest.Milestone != nil {
|
|
milestone = o.forgejoPullRequest.Milestone.Title
|
|
}
|
|
|
|
createdAt := time.Time{}
|
|
if o.forgejoPullRequest.Created != nil {
|
|
createdAt = *o.forgejoPullRequest.Created
|
|
}
|
|
updatedAt := time.Time{}
|
|
if o.forgejoPullRequest.Created != nil {
|
|
updatedAt = *o.forgejoPullRequest.Updated
|
|
}
|
|
|
|
var (
|
|
headUserName string
|
|
headRepoName string
|
|
headRef string
|
|
headSHA string
|
|
)
|
|
if o.forgejoPullRequest.Head != nil {
|
|
if o.forgejoPullRequest.Head.Repository != nil {
|
|
headUserName = o.forgejoPullRequest.Head.Repository.Owner.UserName
|
|
headRepoName = o.forgejoPullRequest.Head.Repository.Name
|
|
}
|
|
headSHA = o.forgejoPullRequest.Head.Sha
|
|
headRef = o.forgejoPullRequest.Head.Ref
|
|
}
|
|
|
|
var (
|
|
baseUserName string
|
|
baseRepoName string
|
|
baseRef string
|
|
baseSHA string
|
|
)
|
|
if o.forgejoPullRequest.Base != nil {
|
|
if o.forgejoPullRequest.Base.Repository != nil {
|
|
baseUserName = o.forgejoPullRequest.Base.Repository.Owner.UserName
|
|
baseRepoName = o.forgejoPullRequest.Base.Repository.Name
|
|
}
|
|
baseSHA = o.forgejoPullRequest.Base.Sha
|
|
baseRef = o.forgejoPullRequest.Base.Ref
|
|
}
|
|
|
|
var mergeCommitSHA string
|
|
if o.forgejoPullRequest.MergedCommitID != nil {
|
|
mergeCommitSHA = *o.forgejoPullRequest.MergedCommitID
|
|
}
|
|
|
|
closedAt := o.forgejoPullRequest.Closed
|
|
if o.forgejoPullRequest.Merged != nil && closedAt == nil {
|
|
closedAt = o.forgejoPullRequest.Merged
|
|
}
|
|
|
|
return &f3.PullRequest{
|
|
Title: o.forgejoPullRequest.Title,
|
|
Common: f3.NewCommon(o.GetNativeID()),
|
|
PosterID: f3_tree.NewUserReference(o.forgejoPullRequest.Poster.ID),
|
|
Content: o.forgejoPullRequest.Body,
|
|
State: string(o.forgejoPullRequest.State),
|
|
Created: createdAt,
|
|
Updated: updatedAt,
|
|
Closed: closedAt,
|
|
Milestone: milestone,
|
|
Merged: o.forgejoPullRequest.HasMerged,
|
|
MergedTime: o.forgejoPullRequest.Merged,
|
|
MergeCommitSHA: mergeCommitSHA,
|
|
IsLocked: o.forgejoPullRequest.IsLocked,
|
|
Head: f3.PullRequestBranch{
|
|
Ref: headRef,
|
|
SHA: headSHA,
|
|
RepoName: headRepoName,
|
|
OwnerName: headUserName,
|
|
},
|
|
Base: f3.PullRequestBranch{
|
|
Ref: baseRef,
|
|
SHA: baseSHA,
|
|
RepoName: baseRepoName,
|
|
OwnerName: baseUserName,
|
|
},
|
|
FetchFunc: o.fetchFunc,
|
|
}
|
|
}
|
|
|
|
func (o *pullRequest) FromFormat(content f3.Interface) {
|
|
pullRequest := content.(*f3.PullRequest)
|
|
o.forgejoPullRequest = &forgejo_sdk.PullRequest{
|
|
Index: util.ParseInt(pullRequest.GetID()),
|
|
Poster: &forgejo_sdk.User{
|
|
ID: pullRequest.PosterID.GetIDAsInt(),
|
|
},
|
|
Title: pullRequest.Title,
|
|
Body: pullRequest.Content,
|
|
Milestone: &forgejo_sdk.Milestone{
|
|
Title: pullRequest.Milestone,
|
|
},
|
|
State: forgejo_sdk.StateType(pullRequest.State),
|
|
IsLocked: pullRequest.IsLocked,
|
|
HasMerged: pullRequest.Merged,
|
|
Merged: pullRequest.MergedTime,
|
|
MergedCommitID: &pullRequest.MergeCommitSHA,
|
|
Base: &forgejo_sdk.PRBranchInfo{
|
|
Ref: pullRequest.Base.Ref,
|
|
Sha: pullRequest.Base.SHA,
|
|
Repository: &forgejo_sdk.Repository{
|
|
Name: pullRequest.Base.RepoName,
|
|
Owner: &forgejo_sdk.User{
|
|
UserName: pullRequest.Base.OwnerName,
|
|
},
|
|
},
|
|
},
|
|
Head: &forgejo_sdk.PRBranchInfo{
|
|
Ref: pullRequest.Head.Ref,
|
|
Sha: pullRequest.Head.SHA,
|
|
Repository: &forgejo_sdk.Repository{
|
|
Name: pullRequest.Head.RepoName,
|
|
Owner: &forgejo_sdk.User{
|
|
UserName: pullRequest.Head.OwnerName,
|
|
},
|
|
},
|
|
},
|
|
Created: &pullRequest.Created,
|
|
Updated: &pullRequest.Updated,
|
|
Closed: pullRequest.Closed,
|
|
}
|
|
o.fetchFunc = pullRequest.FetchFunc
|
|
}
|
|
|
|
func (o *pullRequest) Get(ctx context.Context) {
|
|
node := o.GetNode()
|
|
o.Trace("%s", node.GetID())
|
|
|
|
owner := f3_tree.GetOwnerName(o.GetNode())
|
|
project := f3_tree.GetProjectName(o.GetNode())
|
|
|
|
pr, resp, err := o.getClient().GetPullRequest(owner, project, util.ParseInt(string(node.GetID())))
|
|
if resp.StatusCode == 404 {
|
|
return
|
|
}
|
|
if err != nil {
|
|
panic(fmt.Errorf("pullRequest %v %w", o, err))
|
|
}
|
|
o.forgejoPullRequest = pr
|
|
}
|
|
|
|
func (o *pullRequest) GetPullRequestHead() string {
|
|
if o.forgejoPullRequest.Head.Repository.Fork {
|
|
return o.forgejoPullRequest.Head.Repository.Owner.UserName + ":" + o.forgejoPullRequest.Head.Ref
|
|
}
|
|
return o.forgejoPullRequest.Head.Ref
|
|
}
|
|
|
|
func (o *pullRequest) GetPullRequestPushRefs() []string {
|
|
return []string{
|
|
fmt.Sprintf("refs/f3/%s/head", o.GetNativeID()),
|
|
fmt.Sprintf("refs/pull/%s/head", o.GetNativeID()),
|
|
}
|
|
}
|
|
|
|
func (o *pullRequest) GetPullRequestRef() string {
|
|
return fmt.Sprintf("refs/pull/%s/head", o.GetNativeID())
|
|
}
|
|
|
|
func (o *pullRequest) Put(ctx context.Context) generic.NodeID {
|
|
node := o.GetNode()
|
|
o.Trace("%s", node.GetID())
|
|
|
|
owner := f3_tree.GetOwnerName(o.GetNode())
|
|
project := f3_tree.GetProjectName(o.GetNode())
|
|
|
|
o.maybeSudoID(ctx, o.forgejoPullRequest.Poster.ID)
|
|
defer o.notSudo()
|
|
|
|
pr, _, err := o.getClient().CreatePullRequest(owner, project, forgejo_sdk.CreatePullRequestOption{
|
|
Head: o.GetPullRequestHead(),
|
|
Base: o.forgejoPullRequest.Base.Ref,
|
|
Title: o.forgejoPullRequest.Title,
|
|
Body: o.forgejoPullRequest.Body,
|
|
})
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
o.forgejoPullRequest = pr
|
|
return generic.NodeID(o.GetNativeID())
|
|
}
|
|
|
|
func (o *pullRequest) Patch(ctx context.Context) {
|
|
node := o.GetNode()
|
|
o.Trace("%s", node.GetID())
|
|
|
|
owner := f3_tree.GetOwnerName(o.GetNode())
|
|
project := f3_tree.GetProjectName(o.GetNode())
|
|
|
|
o.maybeSudoID(ctx, o.forgejoPullRequest.Poster.ID)
|
|
defer o.notSudo()
|
|
|
|
_, _, err := o.getClient().EditPullRequest(owner, project, util.ParseInt(string(node.GetID())), forgejo_sdk.EditPullRequestOption{
|
|
Title: o.forgejoPullRequest.Title,
|
|
Body: o.forgejoPullRequest.Body,
|
|
})
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
}
|
|
|
|
func (o *pullRequest) Delete(ctx context.Context) {
|
|
node := o.GetNode()
|
|
o.Trace("%s", node.GetID())
|
|
|
|
owner := f3_tree.GetOwnerName(o.GetNode())
|
|
project := f3_tree.GetProjectName(o.GetNode())
|
|
|
|
_, err := o.getClient().DeleteIssue(owner, project, util.ParseInt(string(node.GetID())))
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
}
|