2020-01-10 13:23:53 +05:30
|
|
|
// 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-02-01 02:43:51 +05:30
|
|
|
"fmt"
|
|
|
|
|
2020-01-10 13:23:53 +05:30
|
|
|
"code.gitea.io/gitea/models"
|
|
|
|
"code.gitea.io/gitea/modules/git"
|
|
|
|
"code.gitea.io/gitea/modules/log"
|
2020-01-14 09:08:04 +05:30
|
|
|
repo_module "code.gitea.io/gitea/modules/repository"
|
2020-01-10 13:23:53 +05:30
|
|
|
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
|
|
|
|
headCommit *git.Commit
|
|
|
|
err error
|
|
|
|
)
|
2020-02-01 02:43:51 +05:30
|
|
|
|
2020-01-10 13:23:53 +05:30
|
|
|
if err = pr.Issue.LoadRepo(); err != nil {
|
2020-02-01 02:43:51 +05:30
|
|
|
log.Error("pr.Issue.LoadRepo[%d]: %v", pr.ID, err)
|
2020-01-10 13:23:53 +05:30
|
|
|
return nil
|
|
|
|
}
|
2020-02-01 02:43:51 +05:30
|
|
|
|
2020-02-29 08:19:50 +05:30
|
|
|
apiIssue := ToAPIIssue(pr.Issue)
|
2020-01-10 13:23:53 +05:30
|
|
|
if pr.BaseRepo == nil {
|
|
|
|
pr.BaseRepo, err = models.GetRepositoryByID(pr.BaseRepoID)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("GetRepositoryById[%d]: %v", pr.ID, err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
2020-02-01 02:43:51 +05:30
|
|
|
if pr.HeadRepoID != 0 && pr.HeadRepo == nil {
|
2020-01-10 13:23:53 +05:30
|
|
|
pr.HeadRepo, err = models.GetRepositoryByID(pr.HeadRepoID)
|
2020-02-01 02:43:51 +05:30
|
|
|
if err != nil && !models.IsErrRepoNotExist(err) {
|
2020-01-10 13:23:53 +05:30
|
|
|
log.Error("GetRepositoryById[%d]: %v", pr.ID, err)
|
|
|
|
return nil
|
|
|
|
|
2020-02-01 02:43:51 +05:30
|
|
|
}
|
2020-01-10 13:23:53 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
|
|
|
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-01-14 09:08:04 +05:30
|
|
|
baseBranch, err = repo_module.GetBranch(pr.BaseRepo, pr.BaseBranch)
|
2020-01-10 13:23:53 +05:30
|
|
|
if err != nil {
|
|
|
|
if git.IsErrBranchNotExist(err) {
|
|
|
|
apiPullRequest.Base = nil
|
|
|
|
} else {
|
|
|
|
log.Error("GetBranch[%s]: %v", pr.BaseBranch, err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
apiBaseBranchInfo := &api.PRBranchInfo{
|
|
|
|
Name: pr.BaseBranch,
|
|
|
|
Ref: pr.BaseBranch,
|
|
|
|
RepoID: pr.BaseRepoID,
|
|
|
|
Repository: pr.BaseRepo.APIFormat(models.AccessModeNone),
|
|
|
|
}
|
|
|
|
baseCommit, err = baseBranch.GetCommit()
|
|
|
|
if err != nil {
|
|
|
|
if git.IsErrNotExist(err) {
|
|
|
|
apiBaseBranchInfo.Sha = ""
|
|
|
|
} else {
|
|
|
|
log.Error("GetCommit[%s]: %v", baseBranch.Name, err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
apiBaseBranchInfo.Sha = baseCommit.ID.String()
|
|
|
|
}
|
|
|
|
apiPullRequest.Base = apiBaseBranchInfo
|
|
|
|
}
|
|
|
|
|
2020-02-01 02:43:51 +05:30
|
|
|
if pr.HeadRepo != nil {
|
|
|
|
headBranch, err = repo_module.GetBranch(pr.HeadRepo, pr.HeadBranch)
|
2020-01-10 13:23:53 +05:30
|
|
|
if err != nil {
|
2020-02-01 02:43:51 +05:30
|
|
|
if git.IsErrBranchNotExist(err) {
|
|
|
|
apiPullRequest.Head = nil
|
2020-01-10 13:23:53 +05:30
|
|
|
} else {
|
2020-02-01 02:43:51 +05:30
|
|
|
log.Error("GetBranch[%s]: %v", pr.HeadBranch, err)
|
2020-01-10 13:23:53 +05:30
|
|
|
return nil
|
|
|
|
}
|
|
|
|
} else {
|
2020-02-01 02:43:51 +05:30
|
|
|
apiHeadBranchInfo := &api.PRBranchInfo{
|
|
|
|
Name: pr.HeadBranch,
|
|
|
|
Ref: pr.HeadBranch,
|
|
|
|
RepoID: pr.HeadRepoID,
|
|
|
|
Repository: pr.HeadRepo.APIFormat(models.AccessModeNone),
|
|
|
|
}
|
|
|
|
headCommit, err = headBranch.GetCommit()
|
|
|
|
if err != nil {
|
|
|
|
if git.IsErrNotExist(err) {
|
|
|
|
apiHeadBranchInfo.Sha = ""
|
|
|
|
} else {
|
|
|
|
log.Error("GetCommit[%s]: %v", headBranch.Name, err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
apiHeadBranchInfo.Sha = headCommit.ID.String()
|
|
|
|
}
|
|
|
|
apiPullRequest.Head = apiHeadBranchInfo
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
apiPullRequest.Head = &api.PRBranchInfo{
|
|
|
|
Name: pr.HeadBranch,
|
|
|
|
Ref: fmt.Sprintf("refs/pull/%d/head", pr.Index),
|
|
|
|
RepoID: -1,
|
2020-01-10 13:23:53 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
apiPullRequest.MergedBy = pr.Merger.APIFormat()
|
|
|
|
}
|
|
|
|
|
|
|
|
return apiPullRequest
|
|
|
|
}
|