Skip to content

Commit b8e5e2a

Browse files
Fix diffpatch API endpoint (#35610)
Fix the swagger documentation for the `diffpatch` API endpoint, and fix the wrong API path caused by a refactoring change. Closes #35602 --------- Co-authored-by: wxiaoguang <[email protected]>
1 parent 0bc1294 commit b8e5e2a

File tree

5 files changed

+143
-3
lines changed

5 files changed

+143
-3
lines changed

routers/api/v1/api.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1423,6 +1423,7 @@ func Routes() *web.Router {
14231423
m.Get("/tags/{sha}", repo.GetAnnotatedTag)
14241424
m.Get("/notes/{sha}", repo.GetNote)
14251425
}, context.ReferencesGitRepo(true), reqRepoReader(unit.TypeCode))
1426+
m.Post("/diffpatch", mustEnableEditor, reqToken(), bind(api.ApplyDiffPatchFileOptions{}), repo.ReqChangeRepoFileOptionsAndCheck, repo.ApplyDiffPatch)
14261427
m.Group("/contents", func() {
14271428
m.Get("", repo.GetContentsList)
14281429
m.Get("/*", repo.GetContents)
@@ -1434,7 +1435,6 @@ func Routes() *web.Router {
14341435
m.Put("", bind(api.UpdateFileOptions{}), repo.ReqChangeRepoFileOptionsAndCheck, repo.UpdateFile)
14351436
m.Delete("", bind(api.DeleteFileOptions{}), repo.ReqChangeRepoFileOptionsAndCheck, repo.DeleteFile)
14361437
})
1437-
m.Post("/diffpatch", bind(api.ApplyDiffPatchFileOptions{}), repo.ReqChangeRepoFileOptionsAndCheck, repo.ApplyDiffPatch)
14381438
}, mustEnableEditor, reqToken())
14391439
}, reqRepoReader(unit.TypeCode), context.ReferencesGitRepo())
14401440
m.Group("/contents-ext", func() {

routers/api/v1/repo/patch.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func ApplyDiffPatch(ctx *context.APIContext) {
3636
// in: body
3737
// required: true
3838
// schema:
39-
// "$ref": "#/definitions/UpdateFileOptions"
39+
// "$ref": "#/definitions/ApplyDiffPatchFileOptions"
4040
// responses:
4141
// "200":
4242
// "$ref": "#/responses/FileResponse"

routers/api/v1/swagger/options.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,9 @@ type swaggerParameterBodies struct {
121121
// in:body
122122
GetFilesOptions api.GetFilesOptions
123123

124+
// in:body
125+
ApplyDiffPatchFileOptions api.ApplyDiffPatchFileOptions
126+
124127
// in:body
125128
ChangeFilesOptions api.ChangeFilesOptions
126129

templates/swagger/v1_json.tmpl

Lines changed: 49 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
// Copyright 2025 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package integration
5+
6+
import (
7+
"fmt"
8+
"net/http"
9+
"net/url"
10+
"testing"
11+
12+
auth_model "code.gitea.io/gitea/models/auth"
13+
repo_model "code.gitea.io/gitea/models/repo"
14+
"code.gitea.io/gitea/models/unittest"
15+
user_model "code.gitea.io/gitea/models/user"
16+
api "code.gitea.io/gitea/modules/structs"
17+
18+
"github.com/stretchr/testify/assert"
19+
)
20+
21+
func getApplyDiffPatchFileOptions() *api.ApplyDiffPatchFileOptions {
22+
return &api.ApplyDiffPatchFileOptions{
23+
FileOptions: api.FileOptions{
24+
BranchName: "master",
25+
},
26+
Content: `diff --git a/patch-file-1.txt b/patch-file-1.txt
27+
new file mode 100644
28+
index 0000000000..aaaaaaaaaa
29+
--- /dev/null
30+
+++ b/patch-file-1.txt
31+
@@ -0,0 +1 @@
32+
+File 1
33+
`,
34+
}
35+
}
36+
37+
func TestAPIApplyDiffPatchFileOptions(t *testing.T) {
38+
onGiteaRun(t, func(t *testing.T, u *url.URL) {
39+
user2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}) // owner of the repo1 & repo16
40+
org3 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 3}) // owner of the repo3, is an org
41+
user4 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 4}) // owner of neither repos
42+
repo1 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1}) // public repo
43+
repo3 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 3}) // public repo
44+
repo16 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 16}) // private repo
45+
46+
session2 := loginUser(t, user2.Name)
47+
token2 := getTokenForLoggedInUser(t, session2, auth_model.AccessTokenScopeWriteRepository, auth_model.AccessTokenScopeWriteUser)
48+
session4 := loginUser(t, user4.Name)
49+
token4 := getTokenForLoggedInUser(t, session4, auth_model.AccessTokenScopeWriteRepository, auth_model.AccessTokenScopeWriteUser)
50+
51+
req := NewRequestWithJSON(t, "POST", "/api/v1/repos/user2/repo1/diffpatch", getApplyDiffPatchFileOptions()).AddTokenAuth(token2)
52+
resp := MakeRequest(t, req, http.StatusCreated)
53+
var fileResponse api.FileResponse
54+
DecodeJSON(t, resp, &fileResponse)
55+
assert.Nil(t, fileResponse.Content)
56+
assert.NotEmpty(t, fileResponse.Commit.HTMLURL)
57+
req = NewRequest(t, "GET", "/api/v1/repos/user2/repo1/raw/patch-file-1.txt")
58+
resp = MakeRequest(t, req, http.StatusOK)
59+
assert.Equal(t, "File 1\n", resp.Body.String())
60+
61+
// Test creating a file in repo1 by user4 who does not have write access
62+
req = NewRequestWithJSON(t, "POST", fmt.Sprintf("/api/v1/repos/%s/%s/diffpatch", user2.Name, repo16.Name), getApplyDiffPatchFileOptions()).
63+
AddTokenAuth(token4)
64+
MakeRequest(t, req, http.StatusNotFound)
65+
66+
// Tests a repo with no token given so will fail
67+
req = NewRequestWithJSON(t, "POST", fmt.Sprintf("/api/v1/repos/%s/%s/diffpatch", user2.Name, repo16.Name), getApplyDiffPatchFileOptions())
68+
MakeRequest(t, req, http.StatusNotFound)
69+
70+
// Test using access token for a private repo that the user of the token owns
71+
req = NewRequestWithJSON(t, "POST", fmt.Sprintf("/api/v1/repos/%s/%s/diffpatch", user2.Name, repo16.Name), getApplyDiffPatchFileOptions()).
72+
AddTokenAuth(token2)
73+
MakeRequest(t, req, http.StatusCreated)
74+
75+
// Test using org repo "org3/repo3" where user2 is a collaborator
76+
req = NewRequestWithJSON(t, "POST", fmt.Sprintf("/api/v1/repos/%s/%s/diffpatch", org3.Name, repo3.Name), getApplyDiffPatchFileOptions()).
77+
AddTokenAuth(token2)
78+
MakeRequest(t, req, http.StatusCreated)
79+
80+
// Test using org repo "org3/repo3" with no user token
81+
req = NewRequestWithJSON(t, "POST", fmt.Sprintf("/api/v1/repos/%s/%s/diffpatch", org3.Name, repo3.Name), getApplyDiffPatchFileOptions())
82+
MakeRequest(t, req, http.StatusNotFound)
83+
84+
// Test using repo "user2/repo1" where user4 is a NOT collaborator
85+
req = NewRequestWithJSON(t, "POST", fmt.Sprintf("/api/v1/repos/%s/%s/diffpatch", user2.Name, repo1.Name), getApplyDiffPatchFileOptions()).
86+
AddTokenAuth(token4)
87+
MakeRequest(t, req, http.StatusForbidden)
88+
})
89+
}

0 commit comments

Comments
 (0)