Skip to content

Commit 17260e4

Browse files
committed
fix
1 parent 04783f5 commit 17260e4

File tree

9 files changed

+188
-359
lines changed

9 files changed

+188
-359
lines changed

modules/structs/repo_file.go

Lines changed: 20 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,20 @@ type FileOptions struct {
2020
Dates CommitDateOptions `json:"dates"`
2121
// Add a Signed-off-by trailer by the committer at the end of the commit log message.
2222
Signoff bool `json:"signoff"`
23+
// the blob ID (SHA) for the file that already exists, it is required for changing existing files
24+
SHA string `json:"sha"`
25+
}
26+
27+
func (f *FileOptions) GetFileOptions() *FileOptions {
28+
return f
29+
}
30+
31+
type FileOptionsInterface interface {
32+
GetFileOptions() *FileOptions
2333
}
2434

35+
var _ FileOptionsInterface = (*FileOptions)(nil)
36+
2537
// CreateFileOptions options for creating files
2638
// Note: `author` and `committer` are optional (if only one is given, it will be used for the other, otherwise the authenticated user will be used)
2739
type CreateFileOptions struct {
@@ -31,55 +43,38 @@ type CreateFileOptions struct {
3143
ContentBase64 string `json:"content"`
3244
}
3345

34-
// Branch returns branch name
35-
func (o *CreateFileOptions) Branch() string {
36-
return o.FileOptions.BranchName
37-
}
38-
3946
// DeleteFileOptions options for deleting files (used for other File structs below)
4047
// Note: `author` and `committer` are optional (if only one is given, it will be used for the other, otherwise the authenticated user will be used)
4148
type DeleteFileOptions struct {
4249
FileOptions
43-
// sha is the SHA for the file that already exists
44-
// required: true
45-
SHA string `json:"sha" binding:"Required"`
46-
}
47-
48-
// Branch returns branch name
49-
func (o *DeleteFileOptions) Branch() string {
50-
return o.FileOptions.BranchName
5150
}
5251

5352
// UpdateFileOptions options for updating files
5453
// Note: `author` and `committer` are optional (if only one is given, it will be used for the other, otherwise the authenticated user will be used)
5554
type UpdateFileOptions struct {
56-
DeleteFileOptions
55+
FileOptions
5756
// content must be base64 encoded
5857
// required: true
5958
ContentBase64 string `json:"content"`
6059
// from_path (optional) is the path of the original file which will be moved/renamed to the path in the URL
6160
FromPath string `json:"from_path" binding:"MaxSize(500)"`
6261
}
6362

64-
// Branch returns branch name
65-
func (o *UpdateFileOptions) Branch() string {
66-
return o.FileOptions.BranchName
67-
}
68-
69-
// FIXME: ChangeFileOperation.SHA is NOT required for update or delete if last commit is provided in the options.
63+
// FIXME: there is no LastCommitID in FileOptions, actually it should be an alternative to the SHA in ChangeFileOperation
7064

7165
// ChangeFileOperation for creating, updating or deleting a file
7266
type ChangeFileOperation struct {
73-
// indicates what to do with the file
67+
// indicates what to do with the file: "create" for creating a new file, "update" for updating an existing file,
68+
// "upload" for creating or updating a file, "rename" for renaming a file, and "delete" for deleting an existing file.
7469
// required: true
75-
// enum: create,update,delete
70+
// enum: create,update,upload,rename,delete
7671
Operation string `json:"operation" binding:"Required"`
7772
// path to the existing or new file
7873
// required: true
7974
Path string `json:"path" binding:"Required;MaxSize(500)"`
80-
// new or updated file content, must be base64 encoded
75+
// new or updated file content, it must be base64 encoded
8176
ContentBase64 string `json:"content"`
82-
// sha is the SHA for the file that already exists, required for update or delete
77+
// sha is the SHA for the file that already exists, required for changing existing files
8378
SHA string `json:"sha"`
8479
// old path of the file to move
8580
FromPath string `json:"from_path"`
@@ -94,20 +89,10 @@ type ChangeFilesOptions struct {
9489
Files []*ChangeFileOperation `json:"files" binding:"Required"`
9590
}
9691

97-
// Branch returns branch name
98-
func (o *ChangeFilesOptions) Branch() string {
99-
return o.FileOptions.BranchName
100-
}
101-
102-
// FileOptionInterface provides a unified interface for the different file options
103-
type FileOptionInterface interface {
104-
Branch() string
105-
}
106-
10792
// ApplyDiffPatchFileOptions options for applying a diff patch
10893
// Note: `author` and `committer` are optional (if only one is given, it will be used for the other, otherwise the authenticated user will be used)
10994
type ApplyDiffPatchFileOptions struct {
110-
DeleteFileOptions
95+
FileOptions
11196
// required: true
11297
Content string `json:"content"`
11398
}

routers/api/v1/api.go

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -455,15 +455,6 @@ func reqRepoWriter(unitTypes ...unit.Type) func(ctx *context.APIContext) {
455455
}
456456
}
457457

458-
// reqRepoBranchWriter user should have a permission to write to a branch, or be a site admin
459-
func reqRepoBranchWriter(ctx *context.APIContext) {
460-
options, ok := web.GetForm(ctx).(api.FileOptionInterface)
461-
if !ok || (!ctx.Repo.CanWriteToBranch(ctx, ctx.Doer, options.Branch()) && !ctx.IsUserSiteAdmin()) {
462-
ctx.APIError(http.StatusForbidden, "user should have a permission to write to this branch")
463-
return
464-
}
465-
}
466-
467458
// reqRepoReader user should have specific read permission or be a repo admin or a site admin
468459
func reqRepoReader(unitType unit.Type) func(ctx *context.APIContext) {
469460
return func(ctx *context.APIContext) {
@@ -746,7 +737,7 @@ func mustEnableWiki(ctx *context.APIContext) {
746737

747738
func mustNotBeArchived(ctx *context.APIContext) {
748739
if ctx.Repo.Repository.IsArchived {
749-
ctx.APIError(http.StatusLocked, fmt.Errorf("%s is archived", ctx.Repo.Repository.LogString()))
740+
ctx.APIError(http.StatusLocked, fmt.Errorf("%s is archived", ctx.Repo.Repository.FullName()))
750741
return
751742
}
752743
}
@@ -1424,24 +1415,27 @@ func Routes() *web.Router {
14241415
m.Get("/tags/{sha}", repo.GetAnnotatedTag)
14251416
m.Get("/notes/{sha}", repo.GetNote)
14261417
}, context.ReferencesGitRepo(true), reqRepoReader(unit.TypeCode))
1427-
m.Post("/diffpatch", reqRepoWriter(unit.TypeCode), reqToken(), bind(api.ApplyDiffPatchFileOptions{}), mustNotBeArchived, repo.ApplyDiffPatch)
14281418
m.Group("/contents", func() {
14291419
m.Get("", repo.GetContentsList)
14301420
m.Get("/*", repo.GetContents)
1431-
m.Post("", reqToken(), bind(api.ChangeFilesOptions{}), reqRepoBranchWriter, mustNotBeArchived, repo.ChangeFiles)
1432-
m.Group("/*", func() {
1433-
m.Post("", bind(api.CreateFileOptions{}), reqRepoBranchWriter, mustNotBeArchived, repo.CreateFile)
1434-
m.Put("", bind(api.UpdateFileOptions{}), reqRepoBranchWriter, mustNotBeArchived, repo.UpdateFile)
1435-
m.Delete("", bind(api.DeleteFileOptions{}), reqRepoBranchWriter, mustNotBeArchived, repo.DeleteFile)
1436-
}, reqToken())
1421+
m.Group("", func() {
1422+
// "change file" operations
1423+
m.Post("", bind(api.ChangeFilesOptions{}), repo.ChangeFiles)
1424+
m.Group("/*", func() {
1425+
m.Post("", bind(api.CreateFileOptions{}), repo.CreateFile)
1426+
m.Put("", bind(api.UpdateFileOptions{}), repo.UpdateFile)
1427+
m.Delete("", bind(api.DeleteFileOptions{}), repo.DeleteFile)
1428+
})
1429+
m.Post("/diffpatch", bind(api.ApplyDiffPatchFileOptions{}), repo.ApplyDiffPatch)
1430+
}, reqToken(), repo.ReqRepoChangeFileOptions) // need permission to write to the branch
14371431
}, reqRepoReader(unit.TypeCode), context.ReferencesGitRepo())
14381432
m.Group("/contents-ext", func() {
14391433
m.Get("", repo.GetContentsExt)
14401434
m.Get("/*", repo.GetContentsExt)
14411435
}, reqRepoReader(unit.TypeCode), context.ReferencesGitRepo())
14421436
m.Combo("/file-contents", reqRepoReader(unit.TypeCode), context.ReferencesGitRepo()).
14431437
Get(repo.GetFileContentsGet).
1444-
Post(bind(api.GetFilesOptions{}), repo.GetFileContentsPost) // POST method requires "write" permission, so we also support "GET" method above
1438+
Post(bind(api.GetFilesOptions{}), repo.GetFileContentsPost) // the POST method requires "write" permission, so we also support "GET" method above
14451439
m.Get("/signing-key.gpg", misc.SigningKeyGPG)
14461440
m.Get("/signing-key.pub", misc.SigningKeySSH)
14471441
m.Group("/topics", func() {

0 commit comments

Comments
 (0)