@@ -121,7 +121,26 @@ func (pr *PullRequest) LoadIssue() (err error) {
121121// Required - Issue
122122// Optional - Merger
123123func (pr * PullRequest ) APIFormat () * api.PullRequest {
124+
124125 apiIssue := pr .Issue .APIFormat ()
126+ baseBranch , _ := pr .BaseRepo .GetBranch (pr .BaseBranch )
127+ baseCommit , _ := baseBranch .GetCommit ()
128+ headBranch , _ := pr .HeadRepo .GetBranch (pr .HeadBranch )
129+ headCommit , _ := headBranch .GetCommit ()
130+ apiBaseBranchInfo := & api.PRBranchInfo {
131+ Name : pr .BaseBranch ,
132+ Ref : pr .BaseBranch ,
133+ Sha : baseCommit .ID .String (),
134+ RepoID : pr .BaseRepoID ,
135+ Repository : pr .BaseRepo .APIFormat (nil ),
136+ }
137+ apiHeadBranchInfo := & api.PRBranchInfo {
138+ Name : pr .HeadBranch ,
139+ Ref : pr .HeadBranch ,
140+ Sha : headCommit .ID .String (),
141+ RepoID : pr .HeadRepoID ,
142+ Repository : pr .HeadRepo .APIFormat (nil ),
143+ }
125144 apiPullRequest := & api.PullRequest {
126145 ID : pr .ID ,
127146 Index : pr .Index ,
@@ -134,7 +153,12 @@ func (pr *PullRequest) APIFormat() *api.PullRequest {
134153 State : apiIssue .State ,
135154 Comments : apiIssue .Comments ,
136155 HTMLURL : pr .Issue .HTMLURL (),
156+ DiffURL : pr .Issue .DiffURL (),
157+ PatchURL : pr .Issue .PatchURL (),
137158 HasMerged : pr .HasMerged ,
159+ Base : apiBaseBranchInfo ,
160+ Head : apiHeadBranchInfo ,
161+ MergeBase : pr .MergeBase ,
138162 }
139163
140164 if pr .Status != PullRequestStatusChecking {
@@ -472,6 +496,46 @@ func NewPullRequest(repo *Repository, pull *Issue, labelIDs []int64, uuids []str
472496 return nil
473497}
474498
499+ // PullRequestsOptions holds the options for PRs
500+ type PullRequestsOptions struct {
501+ Page int
502+ State string
503+ SortType string
504+ Labels []string
505+ MilestoneID int64
506+ }
507+
508+ func listPullRequestStatement (baseRepoID int64 , opts * PullRequestsOptions ) * xorm.Session {
509+ sess := x .Where ("pull_request.base_repo_id=?" , baseRepoID )
510+
511+ sess .Join ("INNER" , "issue" , "pull_request.issue_id = issue.id" )
512+ switch opts .State {
513+ case "closed" , "open" :
514+ sess .And ("issue.is_closed=?" , opts .State == "closed" )
515+ }
516+
517+ return sess
518+ }
519+
520+ // PullRequests returns all pull requests for a base Repo by the given conditions
521+ func PullRequests (baseRepoID int64 , opts * PullRequestsOptions ) ([]* PullRequest , int64 , error ) {
522+ if opts .Page <= 0 {
523+ opts .Page = 1
524+ }
525+
526+ countSession := listPullRequestStatement (baseRepoID , opts )
527+ maxResults , err := countSession .Count (new (PullRequest ))
528+ if err != nil {
529+ log .Error (4 , "Count PRs" , err )
530+ return nil , maxResults , err
531+ }
532+
533+ prs := make ([]* PullRequest , 0 , ItemsPerPage )
534+ findSession := listPullRequestStatement (baseRepoID , opts )
535+ findSession .Limit (ItemsPerPage , (opts .Page - 1 )* ItemsPerPage )
536+ return prs , maxResults , findSession .Find (& prs )
537+ }
538+
475539// GetUnmergedPullRequest returnss a pull request that is open and has not been merged
476540// by given head/base and repo/branch.
477541func GetUnmergedPullRequest (headRepoID , baseRepoID int64 , headBranch , baseBranch string ) (* PullRequest , error ) {
@@ -512,6 +576,26 @@ func GetUnmergedPullRequestsByBaseInfo(repoID int64, branch string) ([]*PullRequ
512576 Find (& prs )
513577}
514578
579+ // GetPullRequestByIndex returns a pull request by the given index
580+ func GetPullRequestByIndex (repoID int64 , index int64 ) (* PullRequest , error ) {
581+ pr := & PullRequest {
582+ BaseRepoID : repoID ,
583+ Index : index ,
584+ }
585+
586+ has , err := x .Get (pr )
587+ if err != nil {
588+ return nil , err
589+ } else if ! has {
590+ return nil , ErrPullRequestNotExist {0 , repoID , index , 0 , "" , "" }
591+ }
592+
593+ pr .LoadAttributes ()
594+ pr .LoadIssue ()
595+
596+ return pr , nil
597+ }
598+
515599func getPullRequestByID (e Engine , id int64 ) (* PullRequest , error ) {
516600 pr := new (PullRequest )
517601 has , err := e .Id (id ).Get (pr )
0 commit comments