@@ -6,7 +6,9 @@ package repo
6
6
7
7
import (
8
8
"fmt"
9
+ "math"
9
10
"net/http"
11
+ "strconv"
10
12
"strings"
11
13
"time"
12
14
@@ -1101,3 +1103,122 @@ func UpdatePullRequest(ctx *context.APIContext) {
1101
1103
1102
1104
ctx .Status (http .StatusOK )
1103
1105
}
1106
+
1107
+ // GetPullRequestCommits gets all commits associated with a given PR
1108
+ func GetPullRequestCommits (ctx * context.APIContext ) {
1109
+ // swagger:operation GET /repos/{owner}/{repo}/pulls/{index}/commits repository repoGetPullRequestCommits
1110
+ // ---
1111
+ // summary: Get commits for a pull request
1112
+ // produces:
1113
+ // - application/json
1114
+ // parameters:
1115
+ // - name: owner
1116
+ // in: path
1117
+ // description: owner of the repo
1118
+ // type: string
1119
+ // required: true
1120
+ // - name: repo
1121
+ // in: path
1122
+ // description: name of the repo
1123
+ // type: string
1124
+ // required: true
1125
+ // - name: index
1126
+ // in: path
1127
+ // description: index of the pull request to get
1128
+ // type: integer
1129
+ // format: int64
1130
+ // required: true
1131
+ // - name: page
1132
+ // in: query
1133
+ // description: page number of results to return (1-based)
1134
+ // type: integer
1135
+ // - name: limit
1136
+ // in: query
1137
+ // description: page size of results
1138
+ // type: integer
1139
+ // responses:
1140
+ // "200":
1141
+ // "$ref": "#/responses/CommitList"
1142
+ // "404":
1143
+ // "$ref": "#/responses/notFound"
1144
+
1145
+ pr , err := models .GetPullRequestByIndex (ctx .Repo .Repository .ID , ctx .ParamsInt64 (":index" ))
1146
+ if err != nil {
1147
+ if models .IsErrPullRequestNotExist (err ) {
1148
+ ctx .NotFound ()
1149
+ } else {
1150
+ ctx .Error (http .StatusInternalServerError , "GetPullRequestByIndex" , err )
1151
+ }
1152
+ return
1153
+ }
1154
+
1155
+ if err := pr .LoadBaseRepo (); err != nil {
1156
+ ctx .InternalServerError (err )
1157
+ return
1158
+ }
1159
+
1160
+ var prInfo * git.CompareInfo
1161
+ baseGitRepo , err := git .OpenRepository (pr .BaseRepo .RepoPath ())
1162
+ if err != nil {
1163
+ ctx .ServerError ("OpenRepository" , err )
1164
+ return
1165
+ }
1166
+ defer baseGitRepo .Close ()
1167
+ if pr .HasMerged {
1168
+ prInfo , err = baseGitRepo .GetCompareInfo (pr .BaseRepo .RepoPath (), pr .MergeBase , pr .GetGitRefName ())
1169
+ } else {
1170
+ prInfo , err = baseGitRepo .GetCompareInfo (pr .BaseRepo .RepoPath (), pr .BaseBranch , pr .GetGitRefName ())
1171
+ }
1172
+ if err != nil {
1173
+ ctx .ServerError ("GetCompareInfo" , err )
1174
+ return
1175
+ }
1176
+ commits := prInfo .Commits
1177
+
1178
+ listOptions := utils .GetListOptions (ctx )
1179
+
1180
+ totalNumberOfCommits := commits .Len ()
1181
+ totalNumberOfPages := int (math .Ceil (float64 (totalNumberOfCommits ) / float64 (listOptions .PageSize )))
1182
+
1183
+ userCache := make (map [string ]* models.User )
1184
+
1185
+ start , end := listOptions .GetStartEnd ()
1186
+
1187
+ if end > totalNumberOfCommits {
1188
+ end = totalNumberOfCommits
1189
+ }
1190
+
1191
+ apiCommits := make ([]* api.Commit , end - start )
1192
+
1193
+ i := 0
1194
+ addedCommitsCount := 0
1195
+ for commitPointer := commits .Front (); commitPointer != nil ; commitPointer = commitPointer .Next () {
1196
+ if i < start {
1197
+ i ++
1198
+ continue
1199
+ }
1200
+ if i >= end {
1201
+ break
1202
+ }
1203
+
1204
+ commit := commitPointer .Value .(* git.Commit )
1205
+
1206
+ // Create json struct
1207
+ apiCommits [addedCommitsCount ], err = convert .ToCommit (ctx .Repo .Repository , commit , userCache )
1208
+ addedCommitsCount ++
1209
+ if err != nil {
1210
+ ctx .ServerError ("toCommit" , err )
1211
+ return
1212
+ }
1213
+ i ++
1214
+ }
1215
+
1216
+ ctx .SetLinkHeader (int (totalNumberOfCommits ), listOptions .PageSize )
1217
+
1218
+ ctx .Header ().Set ("X-Page" , strconv .Itoa (listOptions .Page ))
1219
+ ctx .Header ().Set ("X-PerPage" , strconv .Itoa (listOptions .PageSize ))
1220
+ ctx .Header ().Set ("X-Total-Count" , fmt .Sprintf ("%d" , totalNumberOfCommits ))
1221
+ ctx .Header ().Set ("X-PageCount" , strconv .Itoa (totalNumberOfPages ))
1222
+ ctx .Header ().Set ("X-HasMore" , strconv .FormatBool (listOptions .Page < totalNumberOfPages ))
1223
+ ctx .JSON (http .StatusOK , & apiCommits )
1224
+ }
0 commit comments