]> source.dussan.org Git - gitea.git/commitdiff
Provide diff and patch API endpoints (#11751)
authorzeripath <art27@cantab.net>
Fri, 5 Jun 2020 11:03:12 +0000 (12:03 +0100)
committerGitHub <noreply@github.com>
Fri, 5 Jun 2020 11:03:12 +0000 (14:03 +0300)
* Provide diff and patch API endpoints

The diff and patch endpoints on the main routes are not accessible by token
therefore we provide new API based endpoints for these

Fix #10923

Signed-off-by: Andrew Thornton <art27@cantab.net>
* placate swagger

Signed-off-by: Andrew Thornton <art27@cantab.net>
* Make the response an actual string

Signed-off-by: Andrew Thornton <art27@cantab.net>
Co-authored-by: techknowlogick <techknowlogick@gitea.io>
Co-authored-by: Lauris BH <lauris@nix.lv>
modules/context/api.go
routers/api/v1/api.go
routers/api/v1/repo/pull.go
templates/swagger/v1_json.tmpl

index 5d91ac49a360945663375488a76ac83941fc250c..acecf8f2647c384de19f010eaaef04fcb8927cd7 100644 (file)
@@ -65,6 +65,10 @@ type APINotFound struct{}
 // swagger:response redirect
 type APIRedirect struct{}
 
+//APIString is a string response
+// swagger:response string
+type APIString string
+
 // Error responds with an error message to client with given obj as the message.
 // If status is 500, also it prints error to log.
 func (ctx *APIContext) Error(status int, title string, obj interface{}) {
index 0f8a0326e98fff717ba6e1754c0c62e98902b58f..1ae4e7a58f5be752c22dd3873f5042e327c8ae32 100644 (file)
@@ -796,6 +796,8 @@ func RegisterRoutes(m *macaron.Macaron) {
                                        m.Group("/:index", func() {
                                                m.Combo("").Get(repo.GetPullRequest).
                                                        Patch(reqToken(), reqRepoWriter(models.UnitTypePullRequests), bind(api.EditPullRequestOption{}), repo.EditPullRequest)
+                                               m.Get(".diff", repo.DownloadPullDiff)
+                                               m.Get(".patch", repo.DownloadPullPatch)
                                                m.Combo("/merge").Get(repo.IsPullRequestMerged).
                                                        Post(reqToken(), mustNotBeArchived, bind(auth.MergePullRequestForm{}), repo.MergePullRequest)
                                                m.Group("/reviews", func() {
index bddf4e48f7067d1e8737dcdcacdb99046414e177..284c231628bb8f18898e76528bcbb093e33f8ba6 100644 (file)
@@ -169,6 +169,88 @@ func GetPullRequest(ctx *context.APIContext) {
        ctx.JSON(http.StatusOK, convert.ToAPIPullRequest(pr))
 }
 
+// DownloadPullDiff render a pull's raw diff
+func DownloadPullDiff(ctx *context.APIContext) {
+       // swagger:operation GET /repos/{owner}/{repo}/pulls/{index}.diff repository repoDownloadPullDiff
+       // ---
+       // summary: Get a pull request diff
+       // produces:
+       // - text/plain
+       // 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
+       // responses:
+       //   "200":
+       //     "$ref": "#/responses/string"
+       //   "404":
+       //     "$ref": "#/responses/notFound"
+       DownloadPullDiffOrPatch(ctx, false)
+}
+
+// DownloadPullPatch render a pull's raw patch
+func DownloadPullPatch(ctx *context.APIContext) {
+       // swagger:operation GET /repos/{owner}/{repo}/pulls/{index}.patch repository repoDownloadPullPatch
+       // ---
+       // summary: Get a pull request patch file
+       // produces:
+       // - text/plain
+       // 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
+       // responses:
+       //   "200":
+       //     "$ref": "#/responses/string"
+       //   "404":
+       //     "$ref": "#/responses/notFound"
+       DownloadPullDiffOrPatch(ctx, true)
+}
+
+// DownloadPullDiffOrPatch render a pull's raw diff or patch
+func DownloadPullDiffOrPatch(ctx *context.APIContext, patch bool) {
+       pr, err := models.GetPullRequestByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index"))
+       if err != nil {
+               if models.IsErrPullRequestNotExist(err) {
+                       ctx.NotFound()
+               } else {
+                       ctx.InternalServerError(err)
+               }
+               return
+       }
+
+       if err := pull_service.DownloadDiffOrPatch(pr, ctx, patch); err != nil {
+               ctx.InternalServerError(err)
+               return
+       }
+}
+
 // CreatePullRequest does what it says
 func CreatePullRequest(ctx *context.APIContext, form api.CreatePullRequestOption) {
        // swagger:operation POST /repos/{owner}/{repo}/pulls repository repoCreatePullRequest
index 687a6bc5e4cc606c181c5dbb8406f2a4e1aa2fe3..d90fafd75fe43e095a5d775957b9d9d2d7dbc577 100644 (file)
         }
       }
     },
+    "/repos/{owner}/{repo}/pulls/{index}.diff": {
+      "get": {
+        "produces": [
+          "text/plain"
+        ],
+        "tags": [
+          "repository"
+        ],
+        "summary": "Get a pull request diff",
+        "operationId": "repoDownloadPullDiff",
+        "parameters": [
+          {
+            "type": "string",
+            "description": "owner of the repo",
+            "name": "owner",
+            "in": "path",
+            "required": true
+          },
+          {
+            "type": "string",
+            "description": "name of the repo",
+            "name": "repo",
+            "in": "path",
+            "required": true
+          },
+          {
+            "type": "integer",
+            "format": "int64",
+            "description": "index of the pull request to get",
+            "name": "index",
+            "in": "path",
+            "required": true
+          }
+        ],
+        "responses": {
+          "200": {
+            "$ref": "#/responses/string"
+          },
+          "404": {
+            "$ref": "#/responses/notFound"
+          }
+        }
+      }
+    },
+    "/repos/{owner}/{repo}/pulls/{index}.patch": {
+      "get": {
+        "produces": [
+          "text/plain"
+        ],
+        "tags": [
+          "repository"
+        ],
+        "summary": "Get a pull request patch file",
+        "operationId": "repoDownloadPullPatch",
+        "parameters": [
+          {
+            "type": "string",
+            "description": "owner of the repo",
+            "name": "owner",
+            "in": "path",
+            "required": true
+          },
+          {
+            "type": "string",
+            "description": "name of the repo",
+            "name": "repo",
+            "in": "path",
+            "required": true
+          },
+          {
+            "type": "integer",
+            "format": "int64",
+            "description": "index of the pull request to get",
+            "name": "index",
+            "in": "path",
+            "required": true
+          }
+        ],
+        "responses": {
+          "200": {
+            "$ref": "#/responses/string"
+          },
+          "404": {
+            "$ref": "#/responses/notFound"
+          }
+        }
+      }
+    },
     "/repos/{owner}/{repo}/pulls/{index}/merge": {
       "get": {
         "produces": [
     "redirect": {
       "description": "APIRedirect is a redirect response"
     },
+    "string": {
+      "description": "APIString is a string response",
+      "schema": {
+        "type": "string"
+      }
+    },
     "validationError": {
       "description": "APIValidationError is error format response related to input validation",
       "headers": {