summaryrefslogtreecommitdiffstats
path: root/routers
diff options
context:
space:
mode:
authorsebastian-sauer <sauer.sebastian@gmail.com>2021-07-02 14:19:57 +0200
committerGitHub <noreply@github.com>2021-07-02 14:19:57 +0200
commit92328a3394be51e6200f69c91c402aa15ff6e06e (patch)
treec75096a6e31386435ee250c021cd3c6d7131002c /routers
parenta3476e5ad5ee87d4e985b9a3e914bf5348216745 (diff)
downloadgitea-92328a3394be51e6200f69c91c402aa15ff6e06e.tar.gz
gitea-92328a3394be51e6200f69c91c402aa15ff6e06e.zip
Add API to get commits of PR (#16300)
* Add API to get commits of PR fixes #10918 Co-authored-by: Andrew Bezold <andrew.bezold@gmail.com> Co-authored-by: 6543 <6543@obermui.de>
Diffstat (limited to 'routers')
-rw-r--r--routers/api/v1/api.go1
-rw-r--r--routers/api/v1/repo/pull.go121
2 files changed, 122 insertions, 0 deletions
diff --git a/routers/api/v1/api.go b/routers/api/v1/api.go
index c6b4ff04de..b6913ea1bc 100644
--- a/routers/api/v1/api.go
+++ b/routers/api/v1/api.go
@@ -905,6 +905,7 @@ func Routes() *web.Route {
m.Get(".diff", repo.DownloadPullDiff)
m.Get(".patch", repo.DownloadPullPatch)
m.Post("/update", reqToken(), repo.UpdatePullRequest)
+ m.Get("/commits", repo.GetPullRequestCommits)
m.Combo("/merge").Get(repo.IsPullRequestMerged).
Post(reqToken(), mustNotBeArchived, bind(forms.MergePullRequestForm{}), repo.MergePullRequest)
m.Group("/reviews", func() {
diff --git a/routers/api/v1/repo/pull.go b/routers/api/v1/repo/pull.go
index eff998ee99..0c09a9a86b 100644
--- a/routers/api/v1/repo/pull.go
+++ b/routers/api/v1/repo/pull.go
@@ -6,7 +6,9 @@ package repo
import (
"fmt"
+ "math"
"net/http"
+ "strconv"
"strings"
"time"
@@ -1101,3 +1103,122 @@ func UpdatePullRequest(ctx *context.APIContext) {
ctx.Status(http.StatusOK)
}
+
+// GetPullRequestCommits gets all commits associated with a given PR
+func GetPullRequestCommits(ctx *context.APIContext) {
+ // swagger:operation GET /repos/{owner}/{repo}/pulls/{index}/commits repository repoGetPullRequestCommits
+ // ---
+ // summary: Get commits for a pull request
+ // produces:
+ // - application/json
+ // parameters:
+ // - name: owner
+ // in: path
+ // description: owner of the repo
+ // type: string
+ // required: true
+ // - name: repo
+ // in: path
+ // description: name of the repo
+ // type: string
+ // required: true
+ // - name: index
+ // in: path
+ // description: index of the pull request to get
+ // type: integer
+ // format: int64
+ // required: true
+ // - name: page
+ // in: query
+ // description: page number of results to return (1-based)
+ // type: integer
+ // - name: limit
+ // in: query
+ // description: page size of results
+ // type: integer
+ // responses:
+ // "200":
+ // "$ref": "#/responses/CommitList"
+ // "404":
+ // "$ref": "#/responses/notFound"
+
+ pr, err := models.GetPullRequestByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index"))
+ if err != nil {
+ if models.IsErrPullRequestNotExist(err) {
+ ctx.NotFound()
+ } else {
+ ctx.Error(http.StatusInternalServerError, "GetPullRequestByIndex", err)
+ }
+ return
+ }
+
+ if err := pr.LoadBaseRepo(); err != nil {
+ ctx.InternalServerError(err)
+ return
+ }
+
+ var prInfo *git.CompareInfo
+ baseGitRepo, err := git.OpenRepository(pr.BaseRepo.RepoPath())
+ if err != nil {
+ ctx.ServerError("OpenRepository", err)
+ return
+ }
+ defer baseGitRepo.Close()
+ if pr.HasMerged {
+ prInfo, err = baseGitRepo.GetCompareInfo(pr.BaseRepo.RepoPath(), pr.MergeBase, pr.GetGitRefName())
+ } else {
+ prInfo, err = baseGitRepo.GetCompareInfo(pr.BaseRepo.RepoPath(), pr.BaseBranch, pr.GetGitRefName())
+ }
+ if err != nil {
+ ctx.ServerError("GetCompareInfo", err)
+ return
+ }
+ commits := prInfo.Commits
+
+ listOptions := utils.GetListOptions(ctx)
+
+ totalNumberOfCommits := commits.Len()
+ totalNumberOfPages := int(math.Ceil(float64(totalNumberOfCommits) / float64(listOptions.PageSize)))
+
+ userCache := make(map[string]*models.User)
+
+ start, end := listOptions.GetStartEnd()
+
+ if end > totalNumberOfCommits {
+ end = totalNumberOfCommits
+ }
+
+ apiCommits := make([]*api.Commit, end-start)
+
+ i := 0
+ addedCommitsCount := 0
+ for commitPointer := commits.Front(); commitPointer != nil; commitPointer = commitPointer.Next() {
+ if i < start {
+ i++
+ continue
+ }
+ if i >= end {
+ break
+ }
+
+ commit := commitPointer.Value.(*git.Commit)
+
+ // Create json struct
+ apiCommits[addedCommitsCount], err = convert.ToCommit(ctx.Repo.Repository, commit, userCache)
+ addedCommitsCount++
+ if err != nil {
+ ctx.ServerError("toCommit", err)
+ return
+ }
+ i++
+ }
+
+ ctx.SetLinkHeader(int(totalNumberOfCommits), listOptions.PageSize)
+
+ ctx.Header().Set("X-Page", strconv.Itoa(listOptions.Page))
+ ctx.Header().Set("X-PerPage", strconv.Itoa(listOptions.PageSize))
+ ctx.Header().Set("X-Total-Count", fmt.Sprintf("%d", totalNumberOfCommits))
+ ctx.Header().Set("X-PageCount", strconv.Itoa(totalNumberOfPages))
+ ctx.Header().Set("X-HasMore", strconv.FormatBool(listOptions.Page < totalNumberOfPages))
+ ctx.JSON(http.StatusOK, &apiCommits)
+}