Skip to content

Commit cb0f35d

Browse files
committed
fix
1 parent 04783f5 commit cb0f35d

File tree

4 files changed

+126
-267
lines changed

4 files changed

+126
-267
lines changed

modules/structs/repo_file.go

Lines changed: 16 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,16 @@ type FileOptions struct {
2222
Signoff bool `json:"signoff"`
2323
}
2424

25+
func (f *FileOptions) GetFileOptions() *FileOptions {
26+
return f
27+
}
28+
29+
type FileOptionInterface interface {
30+
GetFileOptions() *FileOptions
31+
}
32+
33+
var _ FileOptionInterface = (*FileOptions)(nil)
34+
2535
// CreateFileOptions options for creating files
2636
// 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)
2737
type CreateFileOptions struct {
@@ -31,11 +41,6 @@ type CreateFileOptions struct {
3141
ContentBase64 string `json:"content"`
3242
}
3343

34-
// Branch returns branch name
35-
func (o *CreateFileOptions) Branch() string {
36-
return o.FileOptions.BranchName
37-
}
38-
3944
// DeleteFileOptions options for deleting files (used for other File structs below)
4045
// 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)
4146
type DeleteFileOptions struct {
@@ -45,11 +50,6 @@ type DeleteFileOptions struct {
4550
SHA string `json:"sha" binding:"Required"`
4651
}
4752

48-
// Branch returns branch name
49-
func (o *DeleteFileOptions) Branch() string {
50-
return o.FileOptions.BranchName
51-
}
52-
5353
// UpdateFileOptions options for updating files
5454
// 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)
5555
type UpdateFileOptions struct {
@@ -61,25 +61,21 @@ type UpdateFileOptions struct {
6161
FromPath string `json:"from_path" binding:"MaxSize(500)"`
6262
}
6363

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.
64+
// FIXME: there is no LastCommitID in FileOptions, actually it should be an alternative to the SHA in ChangeFileOperation
7065

7166
// ChangeFileOperation for creating, updating or deleting a file
7267
type ChangeFileOperation struct {
73-
// indicates what to do with the file
68+
// indicates what to do with the file: "create" for creating a new file, "update" for updating an existing file,
69+
// "upload" for creating or updating a file, "rename" for renaming a file, and "delete" for deleting an existing file.
7470
// required: true
75-
// enum: create,update,delete
71+
// enum: create,update,upload,rename,delete
7672
Operation string `json:"operation" binding:"Required"`
7773
// path to the existing or new file
7874
// required: true
7975
Path string `json:"path" binding:"Required;MaxSize(500)"`
80-
// new or updated file content, must be base64 encoded
76+
// new or updated file content, it must be base64 encoded
8177
ContentBase64 string `json:"content"`
82-
// sha is the SHA for the file that already exists, required for update or delete
78+
// sha is the SHA for the file that already exists, required for changing existing files
8379
SHA string `json:"sha"`
8480
// old path of the file to move
8581
FromPath string `json:"from_path"`
@@ -94,16 +90,6 @@ type ChangeFilesOptions struct {
9490
Files []*ChangeFileOperation `json:"files" binding:"Required"`
9591
}
9692

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-
10793
// ApplyDiffPatchFileOptions options for applying a diff patch
10894
// 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)
10995
type ApplyDiffPatchFileOptions struct {

routers/api/v1/api.go

Lines changed: 11 additions & 17 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
}
@@ -1428,20 +1419,23 @@ func Routes() *web.Router {
14281419
m.Group("/contents", func() {
14291420
m.Get("", repo.GetContentsList)
14301421
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())
1422+
m.Group("", func() {
1423+
// "change file" operations
1424+
m.Post("", bind(api.ChangeFilesOptions{}), repo.ChangeFiles)
1425+
m.Group("/*", func() {
1426+
m.Post("", bind(api.CreateFileOptions{}), repo.CreateFile)
1427+
m.Put("", bind(api.UpdateFileOptions{}), repo.UpdateFile)
1428+
m.Delete("", bind(api.DeleteFileOptions{}), repo.DeleteFile)
1429+
})
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)