]> source.dussan.org Git - gitea.git/commitdiff
Merge endpoints for pull diff/patch (#17104)
authorqwerty287 <80460567+qwerty287@users.noreply.github.com>
Tue, 21 Sep 2021 23:04:53 +0000 (01:04 +0200)
committerGitHub <noreply@github.com>
Tue, 21 Sep 2021 23:04:53 +0000 (01:04 +0200)
this merges the two API endpoints for the PR diff/patch in to one

routers/api/v1/api.go
routers/api/v1/repo/pull.go
templates/swagger/v1_json.tmpl

index 90189701c01a638c3cc0ead36ffbd0f790952152..082381d31c0ec6a781c0ba169e423f130d0f6f19 100644 (file)
@@ -897,8 +897,7 @@ func Routes(sessioner func(http.Handler) http.Handler) *web.Route {
                                        m.Group("/{index}", func() {
                                                m.Combo("").Get(repo.GetPullRequest).
                                                        Patch(reqToken(), bind(api.EditPullRequestOption{}), repo.EditPullRequest)
-                                               m.Get(".diff", repo.DownloadPullDiff)
-                                               m.Get(".patch", repo.DownloadPullPatch)
+                                               m.Get(".{diffType:diff|patch}", repo.DownloadPullDiffOrPatch)
                                                m.Post("/update", reqToken(), repo.UpdatePullRequest)
                                                m.Get("/commits", repo.GetPullRequestCommits)
                                                m.Combo("/merge").Get(repo.IsPullRequestMerged).
index dee9a94bca6bd3b46686856472111c5fb718bc7d..b9767b413d7b631fee0ed1bc483e0778c8a5d23d 100644 (file)
@@ -174,11 +174,11 @@ 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
+// DownloadPullDiffOrPatch render a pull's raw diff or patch
+func DownloadPullDiffOrPatch(ctx *context.APIContext) {
+       // swagger:operation GET /repos/{owner}/{repo}/pulls/{index}.{diffType} repository repoDownloadPullDiffOrPatch
        // ---
-       // summary: Get a pull request diff
+       // summary: Get a pull request diff or patch
        // produces:
        // - text/plain
        // parameters:
@@ -198,48 +198,17 @@ func DownloadPullDiff(ctx *context.APIContext) {
        //   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
+       // - name: diffType
        //   in: path
-       //   description: name of the repo
+       //   description: whether the output is diff or patch
        //   type: string
-       //   required: true
-       // - name: index
-       //   in: path
-       //   description: index of the pull request to get
-       //   type: integer
-       //   format: int64
+       //   enum: [diff, patch]
        //   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) {
@@ -249,6 +218,12 @@ func DownloadPullDiffOrPatch(ctx *context.APIContext, patch bool) {
                }
                return
        }
+       var patch bool
+       if ctx.Params(":diffType") == "diff" {
+               patch = false
+       } else {
+               patch = true
+       }
 
        if err := pull_service.DownloadDiffOrPatch(pr, ctx, patch); err != nil {
                ctx.InternalServerError(err)
index 77c89aea3af9f5d3f5f2b9afd34dfdb6670e9d12..63eba4583288effbc411aa6d7a1027943a80aeb9 100644 (file)
         }
       }
     },
-    "/repos/{owner}/{repo}/pulls/{index}.diff": {
+    "/repos/{owner}/{repo}/pulls/{index}.{diffType}": {
       "get": {
         "produces": [
           "text/plain"
         "tags": [
           "repository"
         ],
-        "summary": "Get a pull request diff",
-        "operationId": "repoDownloadPullDiff",
+        "summary": "Get a pull request diff or patch",
+        "operationId": "repoDownloadPullDiffOrPatch",
         "parameters": [
           {
             "type": "string",
             "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
           },
           {
+            "enum": [
+              "diff",
+              "patch"
+            ],
             "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",
+            "description": "whether the output is diff or patch",
+            "name": "diffType",
             "in": "path",
             "required": true
           }