[API] Pull Requests (#248)
This commit is contained in:

committed by
GitHub

parent
d7ed78a919
commit
0f05470cb8
@ -584,6 +584,28 @@ func (err ErrPullRequestNotExist) Error() string {
|
|||||||
err.ID, err.IssueID, err.HeadRepoID, err.BaseRepoID, err.HeadBarcnh, err.BaseBranch)
|
err.ID, err.IssueID, err.HeadRepoID, err.BaseRepoID, err.HeadBarcnh, err.BaseBranch)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ErrPullRequestAlreadyExists represents a "PullRequestAlreadyExists"-error
|
||||||
|
type ErrPullRequestAlreadyExists struct {
|
||||||
|
ID int64
|
||||||
|
IssueID int64
|
||||||
|
HeadRepoID int64
|
||||||
|
BaseRepoID int64
|
||||||
|
HeadBranch string
|
||||||
|
BaseBranch string
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsErrPullRequestAlreadyExists checks if an error is a ErrPullRequestAlreadyExists.
|
||||||
|
func IsErrPullRequestAlreadyExists(err error) bool {
|
||||||
|
_, ok := err.(ErrPullRequestAlreadyExists)
|
||||||
|
return ok
|
||||||
|
}
|
||||||
|
|
||||||
|
// Error does pretty-printing :D
|
||||||
|
func (err ErrPullRequestAlreadyExists) Error() string {
|
||||||
|
return fmt.Sprintf("pull request already exists for these targets [id: %d, issue_id: %d, head_repo_id: %d, base_repo_id: %d, head_branch: %s, base_branch: %s]",
|
||||||
|
err.ID, err.IssueID, err.HeadRepoID, err.BaseRepoID, err.HeadBranch, err.BaseBranch)
|
||||||
|
}
|
||||||
|
|
||||||
// _________ __
|
// _________ __
|
||||||
// \_ ___ \ ____ _____ _____ ____ _____/ |_
|
// \_ ___ \ ____ _____ _____ ____ _____/ |_
|
||||||
// / \ \/ / _ \ / \ / \_/ __ \ / \ __\
|
// / \ \/ / _ \ / \ / \_/ __ \ / \ __\
|
||||||
|
@ -170,6 +170,22 @@ func (issue *Issue) HTMLURL() string {
|
|||||||
return fmt.Sprintf("%s/%s/%d", issue.Repo.HTMLURL(), path, issue.Index)
|
return fmt.Sprintf("%s/%s/%d", issue.Repo.HTMLURL(), path, issue.Index)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// DiffURL returns the absolute URL to this diff
|
||||||
|
func (issue *Issue) DiffURL() string {
|
||||||
|
if issue.IsPull {
|
||||||
|
return fmt.Sprintf("%s/pulls/%d.diff", issue.Repo.HTMLURL(), issue.Index)
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
// PatchURL returns the absolute URL to this patch
|
||||||
|
func (issue *Issue) PatchURL() string {
|
||||||
|
if issue.IsPull {
|
||||||
|
return fmt.Sprintf("%s/pulls/%d.patch", issue.Repo.HTMLURL(), issue.Index)
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
// State returns string representation of issue status.
|
// State returns string representation of issue status.
|
||||||
func (issue *Issue) State() api.StateType {
|
func (issue *Issue) State() api.StateType {
|
||||||
if issue.IsClosed {
|
if issue.IsClosed {
|
||||||
|
@ -121,7 +121,26 @@ func (pr *PullRequest) LoadIssue() (err error) {
|
|||||||
// Required - Issue
|
// Required - Issue
|
||||||
// Optional - Merger
|
// Optional - Merger
|
||||||
func (pr *PullRequest) APIFormat() *api.PullRequest {
|
func (pr *PullRequest) APIFormat() *api.PullRequest {
|
||||||
|
|
||||||
apiIssue := pr.Issue.APIFormat()
|
apiIssue := pr.Issue.APIFormat()
|
||||||
|
baseBranch, _ := pr.BaseRepo.GetBranch(pr.BaseBranch)
|
||||||
|
baseCommit, _ := baseBranch.GetCommit()
|
||||||
|
headBranch, _ := pr.HeadRepo.GetBranch(pr.HeadBranch)
|
||||||
|
headCommit, _ := headBranch.GetCommit()
|
||||||
|
apiBaseBranchInfo := &api.PRBranchInfo{
|
||||||
|
Name: pr.BaseBranch,
|
||||||
|
Ref: pr.BaseBranch,
|
||||||
|
Sha: baseCommit.ID.String(),
|
||||||
|
RepoID: pr.BaseRepoID,
|
||||||
|
Repository: pr.BaseRepo.APIFormat(nil),
|
||||||
|
}
|
||||||
|
apiHeadBranchInfo := &api.PRBranchInfo{
|
||||||
|
Name: pr.HeadBranch,
|
||||||
|
Ref: pr.HeadBranch,
|
||||||
|
Sha: headCommit.ID.String(),
|
||||||
|
RepoID: pr.HeadRepoID,
|
||||||
|
Repository: pr.HeadRepo.APIFormat(nil),
|
||||||
|
}
|
||||||
apiPullRequest := &api.PullRequest{
|
apiPullRequest := &api.PullRequest{
|
||||||
ID: pr.ID,
|
ID: pr.ID,
|
||||||
Index: pr.Index,
|
Index: pr.Index,
|
||||||
@ -134,7 +153,12 @@ func (pr *PullRequest) APIFormat() *api.PullRequest {
|
|||||||
State: apiIssue.State,
|
State: apiIssue.State,
|
||||||
Comments: apiIssue.Comments,
|
Comments: apiIssue.Comments,
|
||||||
HTMLURL: pr.Issue.HTMLURL(),
|
HTMLURL: pr.Issue.HTMLURL(),
|
||||||
|
DiffURL: pr.Issue.DiffURL(),
|
||||||
|
PatchURL: pr.Issue.PatchURL(),
|
||||||
HasMerged: pr.HasMerged,
|
HasMerged: pr.HasMerged,
|
||||||
|
Base: apiBaseBranchInfo,
|
||||||
|
Head: apiHeadBranchInfo,
|
||||||
|
MergeBase: pr.MergeBase,
|
||||||
}
|
}
|
||||||
|
|
||||||
if pr.Status != PullRequestStatusChecking {
|
if pr.Status != PullRequestStatusChecking {
|
||||||
@ -472,6 +496,46 @@ func NewPullRequest(repo *Repository, pull *Issue, labelIDs []int64, uuids []str
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// PullRequestsOptions holds the options for PRs
|
||||||
|
type PullRequestsOptions struct {
|
||||||
|
Page int
|
||||||
|
State string
|
||||||
|
SortType string
|
||||||
|
Labels []string
|
||||||
|
MilestoneID int64
|
||||||
|
}
|
||||||
|
|
||||||
|
func listPullRequestStatement(baseRepoID int64, opts *PullRequestsOptions) *xorm.Session {
|
||||||
|
sess := x.Where("pull_request.base_repo_id=?", baseRepoID)
|
||||||
|
|
||||||
|
sess.Join("INNER", "issue", "pull_request.issue_id = issue.id")
|
||||||
|
switch opts.State {
|
||||||
|
case "closed", "open":
|
||||||
|
sess.And("issue.is_closed=?", opts.State == "closed")
|
||||||
|
}
|
||||||
|
|
||||||
|
return sess
|
||||||
|
}
|
||||||
|
|
||||||
|
// PullRequests returns all pull requests for a base Repo by the given conditions
|
||||||
|
func PullRequests(baseRepoID int64, opts *PullRequestsOptions) ([]*PullRequest, int64, error) {
|
||||||
|
if opts.Page <= 0 {
|
||||||
|
opts.Page = 1
|
||||||
|
}
|
||||||
|
|
||||||
|
countSession := listPullRequestStatement(baseRepoID, opts)
|
||||||
|
maxResults, err := countSession.Count(new(PullRequest))
|
||||||
|
if err != nil {
|
||||||
|
log.Error(4, "Count PRs", err)
|
||||||
|
return nil, maxResults, err
|
||||||
|
}
|
||||||
|
|
||||||
|
prs := make([]*PullRequest, 0, ItemsPerPage)
|
||||||
|
findSession := listPullRequestStatement(baseRepoID, opts)
|
||||||
|
findSession.Limit(ItemsPerPage, (opts.Page-1)*ItemsPerPage)
|
||||||
|
return prs, maxResults, findSession.Find(&prs)
|
||||||
|
}
|
||||||
|
|
||||||
// GetUnmergedPullRequest returnss a pull request that is open and has not been merged
|
// GetUnmergedPullRequest returnss a pull request that is open and has not been merged
|
||||||
// by given head/base and repo/branch.
|
// by given head/base and repo/branch.
|
||||||
func GetUnmergedPullRequest(headRepoID, baseRepoID int64, headBranch, baseBranch string) (*PullRequest, error) {
|
func GetUnmergedPullRequest(headRepoID, baseRepoID int64, headBranch, baseBranch string) (*PullRequest, error) {
|
||||||
@ -512,6 +576,26 @@ func GetUnmergedPullRequestsByBaseInfo(repoID int64, branch string) ([]*PullRequ
|
|||||||
Find(&prs)
|
Find(&prs)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetPullRequestByIndex returns a pull request by the given index
|
||||||
|
func GetPullRequestByIndex(repoID int64, index int64) (*PullRequest, error) {
|
||||||
|
pr := &PullRequest{
|
||||||
|
BaseRepoID: repoID,
|
||||||
|
Index: index,
|
||||||
|
}
|
||||||
|
|
||||||
|
has, err := x.Get(pr)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
} else if !has {
|
||||||
|
return nil, ErrPullRequestNotExist{0, repoID, index, 0, "", ""}
|
||||||
|
}
|
||||||
|
|
||||||
|
pr.LoadAttributes()
|
||||||
|
pr.LoadIssue()
|
||||||
|
|
||||||
|
return pr, nil
|
||||||
|
}
|
||||||
|
|
||||||
func getPullRequestByID(e Engine, id int64) (*PullRequest, error) {
|
func getPullRequestByID(e Engine, id int64) (*PullRequest, error) {
|
||||||
pr := new(PullRequest)
|
pr := new(PullRequest)
|
||||||
has, err := e.Id(id).Get(pr)
|
has, err := e.Id(id).Get(pr)
|
||||||
|
@ -8,6 +8,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"code.gitea.io/git"
|
||||||
"code.gitea.io/gitea/models"
|
"code.gitea.io/gitea/models"
|
||||||
"code.gitea.io/gitea/modules/base"
|
"code.gitea.io/gitea/modules/base"
|
||||||
"code.gitea.io/gitea/modules/log"
|
"code.gitea.io/gitea/modules/log"
|
||||||
@ -103,3 +104,24 @@ func ExtractOwnerAndRepo() macaron.Handler {
|
|||||||
ctx.Data["Repository"] = repo
|
ctx.Data["Repository"] = repo
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ReferencesGitRepo injects the GitRepo into the Context
|
||||||
|
func ReferencesGitRepo() macaron.Handler {
|
||||||
|
return func(ctx *APIContext) {
|
||||||
|
// Empty repository does not have reference information.
|
||||||
|
if ctx.Repo.Repository.IsBare {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// For API calls.
|
||||||
|
if ctx.Repo.GitRepo == nil {
|
||||||
|
repoPath := models.RepoPath(ctx.Repo.Owner.Name, ctx.Repo.Repository.Name)
|
||||||
|
gitRepo, err := git.OpenRepository(repoPath)
|
||||||
|
if err != nil {
|
||||||
|
ctx.Error(500, "RepoRef Invalid repo "+repoPath, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
ctx.Repo.GitRepo = gitRepo
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -167,6 +167,13 @@ func mustEnableIssues(ctx *context.APIContext) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func mustAllowPulls(ctx *context.Context) {
|
||||||
|
if !ctx.Repo.Repository.AllowsPulls() {
|
||||||
|
ctx.Status(404)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// RegisterRoutes registers all v1 APIs routes to web application.
|
// RegisterRoutes registers all v1 APIs routes to web application.
|
||||||
// FIXME: custom form error response
|
// FIXME: custom form error response
|
||||||
func RegisterRoutes(m *macaron.Macaron) {
|
func RegisterRoutes(m *macaron.Macaron) {
|
||||||
@ -305,6 +312,14 @@ func RegisterRoutes(m *macaron.Macaron) {
|
|||||||
Delete(reqRepoWriter(), repo.DeleteMilestone)
|
Delete(reqRepoWriter(), repo.DeleteMilestone)
|
||||||
})
|
})
|
||||||
m.Get("/editorconfig/:filename", context.RepoRef(), repo.GetEditorconfig)
|
m.Get("/editorconfig/:filename", context.RepoRef(), repo.GetEditorconfig)
|
||||||
|
m.Group("/pulls", func() {
|
||||||
|
m.Combo("").Get(bind(api.ListPullRequestsOptions{}), repo.ListPullRequests).Post(reqRepoWriter(), bind(api.CreatePullRequestOption{}), repo.CreatePullRequest)
|
||||||
|
m.Group("/:index", func() {
|
||||||
|
m.Combo("").Get(repo.GetPullRequest).Patch(reqRepoWriter(), bind(api.EditPullRequestOption{}), repo.EditPullRequest)
|
||||||
|
m.Combo("/merge").Get(repo.IsPullRequestMerged).Post(reqRepoWriter(), repo.MergePullRequest)
|
||||||
|
})
|
||||||
|
|
||||||
|
}, mustAllowPulls, context.ReferencesGitRepo())
|
||||||
}, repoAssignment())
|
}, repoAssignment())
|
||||||
}, reqToken())
|
}, reqToken())
|
||||||
|
|
||||||
|
433
routers/api/v1/repo/pull.go
Normal file
433
routers/api/v1/repo/pull.go
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user