2020-01-10 02:53:53 -05:00
|
|
|
// Copyright 2020 The Gitea Authors. All rights reserved.
|
2022-11-27 13:20:29 -05:00
|
|
|
// SPDX-License-Identifier: MIT
|
2020-01-10 02:53:53 -05:00
|
|
|
|
|
|
|
package convert
|
|
|
|
|
|
|
|
import (
|
2022-01-19 18:26:57 -05:00
|
|
|
"context"
|
2020-01-31 16:13:51 -05:00
|
|
|
"fmt"
|
|
|
|
|
2022-06-13 05:37:59 -04:00
|
|
|
issues_model "code.gitea.io/gitea/models/issues"
|
2021-11-28 06:58:28 -05:00
|
|
|
"code.gitea.io/gitea/models/perm"
|
2022-05-11 06:09:36 -04:00
|
|
|
access_model "code.gitea.io/gitea/models/perm/access"
|
2021-11-24 04:49:20 -05:00
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2020-01-10 02:53:53 -05:00
|
|
|
"code.gitea.io/gitea/modules/git"
|
|
|
|
"code.gitea.io/gitea/modules/log"
|
|
|
|
api "code.gitea.io/gitea/modules/structs"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ToAPIPullRequest assumes following fields have been assigned with valid values:
|
|
|
|
// Required - Issue
|
|
|
|
// Optional - Merger
|
2022-06-13 05:37:59 -04:00
|
|
|
func ToAPIPullRequest(ctx context.Context, pr *issues_model.PullRequest, doer *user_model.User) *api.PullRequest {
|
2020-01-10 02:53:53 -05:00
|
|
|
var (
|
|
|
|
baseBranch *git.Branch
|
|
|
|
headBranch *git.Branch
|
|
|
|
baseCommit *git.Commit
|
|
|
|
err error
|
|
|
|
)
|
2020-01-31 16:13:51 -05:00
|
|
|
|
2022-04-08 05:11:15 -04:00
|
|
|
if err = pr.Issue.LoadRepo(ctx); err != nil {
|
2020-01-31 16:13:51 -05:00
|
|
|
log.Error("pr.Issue.LoadRepo[%d]: %v", pr.ID, err)
|
2020-01-10 02:53:53 -05:00
|
|
|
return nil
|
|
|
|
}
|
2020-01-31 16:13:51 -05:00
|
|
|
|
2022-11-19 03:12:33 -05:00
|
|
|
apiIssue := ToAPIIssue(ctx, pr.Issue)
|
|
|
|
if err := pr.LoadBaseRepo(ctx); err != nil {
|
2020-03-02 17:31:55 -05:00
|
|
|
log.Error("GetRepositoryById[%d]: %v", pr.ID, err)
|
|
|
|
return nil
|
2020-01-10 02:53:53 -05:00
|
|
|
}
|
|
|
|
|
2022-11-19 03:12:33 -05:00
|
|
|
if err := pr.LoadHeadRepo(ctx); err != nil {
|
2020-03-02 17:31:55 -05:00
|
|
|
log.Error("GetRepositoryById[%d]: %v", pr.ID, err)
|
|
|
|
return nil
|
2020-01-10 02:53:53 -05:00
|
|
|
}
|
|
|
|
|
2022-05-11 06:09:36 -04:00
|
|
|
p, err := access_model.GetUserRepoPermission(ctx, pr.BaseRepo, doer)
|
2021-10-06 20:03:37 -04:00
|
|
|
if err != nil {
|
|
|
|
log.Error("GetUserRepoPermission[%d]: %v", pr.BaseRepoID, err)
|
2021-11-28 06:58:28 -05:00
|
|
|
p.AccessMode = perm.AccessModeNone
|
2021-10-06 20:03:37 -04:00
|
|
|
}
|
|
|
|
|
2020-01-10 02:53:53 -05:00
|
|
|
apiPullRequest := &api.PullRequest{
|
|
|
|
ID: pr.ID,
|
|
|
|
URL: pr.Issue.HTMLURL(),
|
|
|
|
Index: pr.Index,
|
|
|
|
Poster: apiIssue.Poster,
|
|
|
|
Title: apiIssue.Title,
|
|
|
|
Body: apiIssue.Body,
|
|
|
|
Labels: apiIssue.Labels,
|
|
|
|
Milestone: apiIssue.Milestone,
|
|
|
|
Assignee: apiIssue.Assignee,
|
|
|
|
Assignees: apiIssue.Assignees,
|
|
|
|
State: apiIssue.State,
|
2020-06-01 17:01:55 -04:00
|
|
|
IsLocked: apiIssue.IsLocked,
|
2020-01-10 02:53:53 -05:00
|
|
|
Comments: apiIssue.Comments,
|
|
|
|
HTMLURL: pr.Issue.HTMLURL(),
|
|
|
|
DiffURL: pr.Issue.DiffURL(),
|
|
|
|
PatchURL: pr.Issue.PatchURL(),
|
|
|
|
HasMerged: pr.HasMerged,
|
|
|
|
MergeBase: pr.MergeBase,
|
2022-04-21 17:55:45 -04:00
|
|
|
Mergeable: pr.Mergeable(),
|
2020-01-10 02:53:53 -05:00
|
|
|
Deadline: apiIssue.Deadline,
|
|
|
|
Created: pr.Issue.CreatedUnix.AsTimePtr(),
|
|
|
|
Updated: pr.Issue.UpdatedUnix.AsTimePtr(),
|
2020-03-02 17:31:55 -05:00
|
|
|
|
2022-04-28 11:45:33 -04:00
|
|
|
AllowMaintainerEdit: pr.AllowMaintainerEdit,
|
|
|
|
|
2020-03-02 17:31:55 -05:00
|
|
|
Base: &api.PRBranchInfo{
|
2020-01-10 02:53:53 -05:00
|
|
|
Name: pr.BaseBranch,
|
|
|
|
Ref: pr.BaseBranch,
|
|
|
|
RepoID: pr.BaseRepoID,
|
2022-12-02 21:48:26 -05:00
|
|
|
Repository: ToRepo(ctx, pr.BaseRepo, p.AccessMode),
|
2020-03-02 17:31:55 -05:00
|
|
|
},
|
|
|
|
Head: &api.PRBranchInfo{
|
|
|
|
Name: pr.HeadBranch,
|
2021-12-02 14:36:50 -05:00
|
|
|
Ref: fmt.Sprintf("%s%d/head", git.PullPrefix, pr.Index),
|
2020-03-02 17:31:55 -05:00
|
|
|
RepoID: -1,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2023-01-17 06:42:32 -05:00
|
|
|
if pr.Issue.ClosedUnix != 0 {
|
|
|
|
apiPullRequest.Closed = pr.Issue.ClosedUnix.AsTimePtr()
|
|
|
|
}
|
|
|
|
|
2022-03-29 15:13:41 -04:00
|
|
|
gitRepo, err := git.OpenRepository(ctx, pr.BaseRepo.RepoPath())
|
2021-11-24 02:56:24 -05:00
|
|
|
if err != nil {
|
|
|
|
log.Error("OpenRepository[%s]: %v", pr.BaseRepo.RepoPath(), err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
defer gitRepo.Close()
|
|
|
|
|
|
|
|
baseBranch, err = gitRepo.GetBranch(pr.BaseBranch)
|
2020-03-02 17:31:55 -05:00
|
|
|
if err != nil && !git.IsErrBranchNotExist(err) {
|
|
|
|
log.Error("GetBranch[%s]: %v", pr.BaseBranch, err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if err == nil {
|
2020-01-10 02:53:53 -05:00
|
|
|
baseCommit, err = baseBranch.GetCommit()
|
2020-03-02 17:31:55 -05:00
|
|
|
if err != nil && !git.IsErrNotExist(err) {
|
|
|
|
log.Error("GetCommit[%s]: %v", baseBranch.Name, err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
apiPullRequest.Base.Sha = baseCommit.ID.String()
|
2020-01-10 02:53:53 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-13 05:37:59 -04:00
|
|
|
if pr.Flow == issues_model.PullRequestFlowAGit {
|
2022-03-29 15:13:41 -04:00
|
|
|
gitRepo, err := git.OpenRepository(ctx, pr.BaseRepo.RepoPath())
|
2021-07-28 05:42:56 -04:00
|
|
|
if err != nil {
|
|
|
|
log.Error("OpenRepository[%s]: %v", pr.GetGitRefName(), err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
defer gitRepo.Close()
|
|
|
|
|
|
|
|
apiPullRequest.Head.Sha, err = gitRepo.GetRefCommitID(pr.GetGitRefName())
|
|
|
|
if err != nil {
|
|
|
|
log.Error("GetRefCommitID[%s]: %v", pr.GetGitRefName(), err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
apiPullRequest.Head.RepoID = pr.BaseRepoID
|
|
|
|
apiPullRequest.Head.Repository = apiPullRequest.Base.Repository
|
|
|
|
apiPullRequest.Head.Name = ""
|
|
|
|
}
|
|
|
|
|
2022-06-13 05:37:59 -04:00
|
|
|
if pr.HeadRepo != nil && pr.Flow == issues_model.PullRequestFlowGithub {
|
2022-05-11 06:09:36 -04:00
|
|
|
p, err := access_model.GetUserRepoPermission(ctx, pr.HeadRepo, doer)
|
2021-10-06 20:03:37 -04:00
|
|
|
if err != nil {
|
|
|
|
log.Error("GetUserRepoPermission[%d]: %v", pr.HeadRepoID, err)
|
2021-11-28 06:58:28 -05:00
|
|
|
p.AccessMode = perm.AccessModeNone
|
2021-10-06 20:03:37 -04:00
|
|
|
}
|
|
|
|
|
2020-03-02 17:31:55 -05:00
|
|
|
apiPullRequest.Head.RepoID = pr.HeadRepo.ID
|
2022-12-02 21:48:26 -05:00
|
|
|
apiPullRequest.Head.Repository = ToRepo(ctx, pr.HeadRepo, p.AccessMode)
|
2020-03-02 17:31:55 -05:00
|
|
|
|
2022-03-29 15:13:41 -04:00
|
|
|
headGitRepo, err := git.OpenRepository(ctx, pr.HeadRepo.RepoPath())
|
2020-01-10 02:53:53 -05:00
|
|
|
if err != nil {
|
2020-03-02 17:31:55 -05:00
|
|
|
log.Error("OpenRepository[%s]: %v", pr.HeadRepo.RepoPath(), err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
defer headGitRepo.Close()
|
|
|
|
|
|
|
|
headBranch, err = headGitRepo.GetBranch(pr.HeadBranch)
|
|
|
|
if err != nil && !git.IsErrBranchNotExist(err) {
|
|
|
|
log.Error("GetBranch[%s]: %v", pr.HeadBranch, err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if git.IsErrBranchNotExist(err) {
|
|
|
|
headCommitID, err := headGitRepo.GetRefCommitID(apiPullRequest.Head.Ref)
|
|
|
|
if err != nil && !git.IsErrNotExist(err) {
|
2020-03-09 03:06:38 -04:00
|
|
|
log.Error("GetCommit[%s]: %v", pr.HeadBranch, err)
|
2020-01-10 02:53:53 -05:00
|
|
|
return nil
|
|
|
|
}
|
2020-03-02 17:31:55 -05:00
|
|
|
if err == nil {
|
|
|
|
apiPullRequest.Head.Sha = headCommitID
|
|
|
|
}
|
2020-01-10 02:53:53 -05:00
|
|
|
} else {
|
2020-03-02 17:31:55 -05:00
|
|
|
commit, err := headBranch.GetCommit()
|
|
|
|
if err != nil && !git.IsErrNotExist(err) {
|
|
|
|
log.Error("GetCommit[%s]: %v", headBranch.Name, err)
|
|
|
|
return nil
|
2020-01-31 16:13:51 -05:00
|
|
|
}
|
2020-03-02 17:31:55 -05:00
|
|
|
if err == nil {
|
|
|
|
apiPullRequest.Head.Ref = pr.HeadBranch
|
|
|
|
apiPullRequest.Head.Sha = commit.ID.String()
|
2020-01-31 16:13:51 -05:00
|
|
|
}
|
2020-01-10 02:53:53 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-08 15:48:31 -05:00
|
|
|
if len(apiPullRequest.Head.Sha) == 0 && len(apiPullRequest.Head.Ref) != 0 {
|
2022-03-29 15:13:41 -04:00
|
|
|
baseGitRepo, err := git.OpenRepository(ctx, pr.BaseRepo.RepoPath())
|
2021-03-08 15:48:31 -05:00
|
|
|
if err != nil {
|
|
|
|
log.Error("OpenRepository[%s]: %v", pr.BaseRepo.RepoPath(), err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
defer baseGitRepo.Close()
|
|
|
|
refs, err := baseGitRepo.GetRefsFiltered(apiPullRequest.Head.Ref)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("GetRefsFiltered[%s]: %v", apiPullRequest.Head.Ref, err)
|
|
|
|
return nil
|
|
|
|
} else if len(refs) == 0 {
|
|
|
|
log.Error("unable to resolve PR head ref")
|
|
|
|
} else {
|
|
|
|
apiPullRequest.Head.Sha = refs[0].Object.String()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-10 02:53:53 -05:00
|
|
|
if pr.HasMerged {
|
|
|
|
apiPullRequest.Merged = pr.MergedUnix.AsTimePtr()
|
|
|
|
apiPullRequest.MergedCommitID = &pr.MergedCommitID
|
2021-03-27 12:45:26 -04:00
|
|
|
apiPullRequest.MergedBy = ToUser(pr.Merger, nil)
|
2020-01-10 02:53:53 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return apiPullRequest
|
|
|
|
}
|