2020-01-10 07:53:53 +00:00
|
|
|
// Copyright 2020 The Gitea Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package convert
|
|
|
|
|
|
|
|
import (
|
2020-01-31 21:13:51 +00:00
|
|
|
"fmt"
|
|
|
|
|
2020-01-10 07:53:53 +00:00
|
|
|
"code.gitea.io/gitea/models"
|
|
|
|
"code.gitea.io/gitea/modules/git"
|
|
|
|
"code.gitea.io/gitea/modules/log"
|
2020-01-14 03:38:04 +00:00
|
|
|
repo_module "code.gitea.io/gitea/modules/repository"
|
2020-01-10 07:53:53 +00:00
|
|
|
api "code.gitea.io/gitea/modules/structs"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ToAPIPullRequest assumes following fields have been assigned with valid values:
|
|
|
|
// Required - Issue
|
|
|
|
// Optional - Merger
|
|
|
|
func ToAPIPullRequest(pr *models.PullRequest) *api.PullRequest {
|
|
|
|
var (
|
|
|
|
baseBranch *git.Branch
|
|
|
|
headBranch *git.Branch
|
|
|
|
baseCommit *git.Commit
|
|
|
|
err error
|
|
|
|
)
|
2020-01-31 21:13:51 +00:00
|
|
|
|
2020-01-10 07:53:53 +00:00
|
|
|
if err = pr.Issue.LoadRepo(); err != nil {
|
2020-01-31 21:13:51 +00:00
|
|
|
log.Error("pr.Issue.LoadRepo[%d]: %v", pr.ID, err)
|
2020-01-10 07:53:53 +00:00
|
|
|
return nil
|
|
|
|
}
|
2020-01-31 21:13:51 +00:00
|
|
|
|
2020-02-29 02:49:50 +00:00
|
|
|
apiIssue := ToAPIIssue(pr.Issue)
|
2020-03-02 22:31:55 +00:00
|
|
|
if err := pr.LoadBaseRepo(); err != nil {
|
|
|
|
log.Error("GetRepositoryById[%d]: %v", pr.ID, err)
|
|
|
|
return nil
|
2020-01-10 07:53:53 +00:00
|
|
|
}
|
|
|
|
|
2020-03-02 22:31:55 +00:00
|
|
|
if err := pr.LoadHeadRepo(); err != nil {
|
|
|
|
log.Error("GetRepositoryById[%d]: %v", pr.ID, err)
|
|
|
|
return nil
|
2020-01-10 07:53:53 +00: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 21:01:55 +00:00
|
|
|
IsLocked: apiIssue.IsLocked,
|
2020-01-10 07:53:53 +00:00
|
|
|
Comments: apiIssue.Comments,
|
|
|
|
HTMLURL: pr.Issue.HTMLURL(),
|
|
|
|
DiffURL: pr.Issue.DiffURL(),
|
|
|
|
PatchURL: pr.Issue.PatchURL(),
|
|
|
|
HasMerged: pr.HasMerged,
|
|
|
|
MergeBase: pr.MergeBase,
|
|
|
|
Deadline: apiIssue.Deadline,
|
|
|
|
Created: pr.Issue.CreatedUnix.AsTimePtr(),
|
|
|
|
Updated: pr.Issue.UpdatedUnix.AsTimePtr(),
|
2020-03-02 22:31:55 +00:00
|
|
|
|
|
|
|
Base: &api.PRBranchInfo{
|
2020-01-10 07:53:53 +00:00
|
|
|
Name: pr.BaseBranch,
|
|
|
|
Ref: pr.BaseBranch,
|
|
|
|
RepoID: pr.BaseRepoID,
|
2020-12-02 21:38:30 +00:00
|
|
|
Repository: ToRepo(pr.BaseRepo, models.AccessModeNone),
|
2020-03-02 22:31:55 +00:00
|
|
|
},
|
|
|
|
Head: &api.PRBranchInfo{
|
|
|
|
Name: pr.HeadBranch,
|
|
|
|
Ref: fmt.Sprintf("refs/pull/%d/head", pr.Index),
|
|
|
|
RepoID: -1,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
baseBranch, err = repo_module.GetBranch(pr.BaseRepo, pr.BaseBranch)
|
|
|
|
if err != nil && !git.IsErrBranchNotExist(err) {
|
|
|
|
log.Error("GetBranch[%s]: %v", pr.BaseBranch, err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if err == nil {
|
2020-01-10 07:53:53 +00:00
|
|
|
baseCommit, err = baseBranch.GetCommit()
|
2020-03-02 22:31:55 +00: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 07:53:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-31 21:13:51 +00:00
|
|
|
if pr.HeadRepo != nil {
|
2020-03-02 22:31:55 +00:00
|
|
|
apiPullRequest.Head.RepoID = pr.HeadRepo.ID
|
2020-12-02 21:38:30 +00:00
|
|
|
apiPullRequest.Head.Repository = ToRepo(pr.HeadRepo, models.AccessModeNone)
|
2020-03-02 22:31:55 +00:00
|
|
|
|
|
|
|
headGitRepo, err := git.OpenRepository(pr.HeadRepo.RepoPath())
|
2020-01-10 07:53:53 +00:00
|
|
|
if err != nil {
|
2020-03-02 22:31:55 +00: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 07:06:38 +00:00
|
|
|
log.Error("GetCommit[%s]: %v", pr.HeadBranch, err)
|
2020-01-10 07:53:53 +00:00
|
|
|
return nil
|
|
|
|
}
|
2020-03-02 22:31:55 +00:00
|
|
|
if err == nil {
|
|
|
|
apiPullRequest.Head.Sha = headCommitID
|
|
|
|
}
|
2020-01-10 07:53:53 +00:00
|
|
|
} else {
|
2020-03-02 22:31:55 +00:00
|
|
|
commit, err := headBranch.GetCommit()
|
|
|
|
if err != nil && !git.IsErrNotExist(err) {
|
|
|
|
log.Error("GetCommit[%s]: %v", headBranch.Name, err)
|
|
|
|
return nil
|
2020-01-31 21:13:51 +00:00
|
|
|
}
|
2020-03-02 22:31:55 +00:00
|
|
|
if err == nil {
|
|
|
|
apiPullRequest.Head.Ref = pr.HeadBranch
|
|
|
|
apiPullRequest.Head.Sha = commit.ID.String()
|
2020-01-31 21:13:51 +00:00
|
|
|
}
|
2020-01-10 07:53:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-08 20:48:31 +00:00
|
|
|
if len(apiPullRequest.Head.Sha) == 0 && len(apiPullRequest.Head.Ref) != 0 {
|
|
|
|
baseGitRepo, err := git.OpenRepository(pr.BaseRepo.RepoPath())
|
|
|
|
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 07:53:53 +00:00
|
|
|
if pr.Status != models.PullRequestStatusChecking {
|
|
|
|
mergeable := !(pr.Status == models.PullRequestStatusConflict || pr.Status == models.PullRequestStatusError) && !pr.IsWorkInProgress()
|
|
|
|
apiPullRequest.Mergeable = mergeable
|
|
|
|
}
|
|
|
|
if pr.HasMerged {
|
|
|
|
apiPullRequest.Merged = pr.MergedUnix.AsTimePtr()
|
|
|
|
apiPullRequest.MergedCommitID = &pr.MergedCommitID
|
2021-03-27 16:45:26 +00:00
|
|
|
apiPullRequest.MergedBy = ToUser(pr.Merger, nil)
|
2020-01-10 07:53:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return apiPullRequest
|
|
|
|
}
|