mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2025-06-19 23:50:51 +00:00
API: enforce sha requirement on POST /repos/{owner}/{repo}/contents
(#8139)
Currently the `POST /repos/{owner}/{repo}/contents` API endpoint accepts request without any `ChangeFileOperation.SHA`, unlike stated by the doc:
33eee199cf/modules/structs/repo_file.go (L80-L81)
This PR adds:
- some more (already passing) tests around this function
- a new (failing) test to show this wrong behavior
- a fix (note that this is a breaking change for clients exploiting this bug)
- an update for all the existing tests
<!--start release-notes-assistant-->
## Release notes
<!--URL:https://codeberg.org/forgejo/forgejo-->
- Breaking bug fixes
- [PR](https://codeberg.org/forgejo/forgejo/pulls/8139): <!--number 8139 --><!--line 0 --><!--description QVBJOiBlbmZvcmNlIHNoYSByZXF1aXJlbWVudCBvbiBgUE9TVCAvcmVwb3Mve293bmVyfS97cmVwb30vY29udGVudHNg-->API: enforce sha requirement on `POST /repos/{owner}/{repo}/contents`<!--description-->
<!--end release-notes-assistant-->
Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/8139
Reviewed-by: Gusted <gusted@noreply.codeberg.org>
Co-authored-by: oliverpool <git@olivier.pfad.fr>
Co-committed-by: oliverpool <git@olivier.pfad.fr>
This commit is contained in:
parent
d3bc095d0c
commit
c93eb1f927
13 changed files with 170 additions and 86 deletions
|
@ -10,6 +10,7 @@ import (
|
|||
repo_model "forgejo.org/models/repo"
|
||||
user_model "forgejo.org/models/user"
|
||||
"forgejo.org/modules/git"
|
||||
"forgejo.org/modules/gitrepo"
|
||||
api "forgejo.org/modules/structs"
|
||||
files_service "forgejo.org/services/repository/files"
|
||||
)
|
||||
|
@ -30,7 +31,12 @@ func createFileInBranch(user *user_model.User, repo *repo_model.Repository, tree
|
|||
return files_service.ChangeRepoFiles(git.DefaultContext, repo, user, opts)
|
||||
}
|
||||
|
||||
func deleteFileInBranch(user *user_model.User, repo *repo_model.Repository, treePath, branchName string) (*api.FilesResponse, error) {
|
||||
func deleteFileInBranch(user *user_model.User, repo *repo_model.Repository, treePath, branchName string) error {
|
||||
commitID, err := gitrepo.GetBranchCommitID(git.DefaultContext, repo, branchName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
opts := &files_service.ChangeRepoFilesOptions{
|
||||
Files: []*files_service.ChangeRepoFile{
|
||||
{
|
||||
|
@ -38,16 +44,17 @@ func deleteFileInBranch(user *user_model.User, repo *repo_model.Repository, tree
|
|||
TreePath: treePath,
|
||||
},
|
||||
},
|
||||
OldBranch: branchName,
|
||||
Author: nil,
|
||||
Committer: nil,
|
||||
OldBranch: branchName,
|
||||
Author: nil,
|
||||
Committer: nil,
|
||||
LastCommitID: commitID,
|
||||
}
|
||||
return files_service.ChangeRepoFiles(git.DefaultContext, repo, user, opts)
|
||||
_, err = files_service.ChangeRepoFiles(git.DefaultContext, repo, user, opts)
|
||||
return err
|
||||
}
|
||||
|
||||
func createOrReplaceFileInBranch(user *user_model.User, repo *repo_model.Repository, treePath, branchName, content string) error {
|
||||
_, err := deleteFileInBranch(user, repo, treePath, branchName)
|
||||
|
||||
err := deleteFileInBranch(user, repo, treePath, branchName)
|
||||
if err != nil && !models.IsErrRepoFileDoesNotExist(err) {
|
||||
return err
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue