@@ -514,6 +514,7 @@ func ChangeFiles(ctx *context.APIContext) {
514514}
515515
516516// CreateFile handles API call for creating a file
517+ // Deprecated: Use CreateOrUpdateFileOptions instead
517518func CreateFile (ctx * context.APIContext ) {
518519 // swagger:operation POST /repos/{owner}/{repo}/contents/{filepath} repository repoCreateFile
519520 // ---
@@ -611,11 +612,11 @@ func CreateFile(ctx *context.APIContext) {
611612 }
612613}
613614
614- // UpdateFile handles API call for updating a file
615- func UpdateFile (ctx * context.APIContext ) {
616- // swagger:operation PUT /repos/{owner}/{repo}/contents/{filepath} repository repoUpdateFile
615+ // CreateOrUpdateFile handles API call for creating or updating a file
616+ func CreateOrUpdateFile (ctx * context.APIContext ) {
617+ // swagger:operation PUT /repos/{owner}/{repo}/contents/{filepath} repository repoCreateOrUpdateFile
617618 // ---
618- // summary: Update a file in a repository
619+ // summary: Create or update a file in a repository
619620 // consumes:
620621 // - application/json
621622 // produces:
@@ -633,17 +634,19 @@ func UpdateFile(ctx *context.APIContext) {
633634 // required: true
634635 // - name: filepath
635636 // in: path
636- // description: path of the file to update
637+ // description: path of the file to create or update
637638 // type: string
638639 // required: true
639640 // - name: body
640641 // in: body
641642 // required: true
642643 // schema:
643- // "$ref": "#/definitions/UpdateFileOptions "
644+ // "$ref": "#/definitions/CreateOrUpdateFileOptions "
644645 // responses:
645646 // "200":
646647 // "$ref": "#/responses/FileResponse"
648+ // "201":
649+ // "$ref": "#/responses/FileResponse"
647650 // "403":
648651 // "$ref": "#/responses/error"
649652 // "404":
@@ -652,10 +655,7 @@ func UpdateFile(ctx *context.APIContext) {
652655 // "$ref": "#/responses/error"
653656 // "423":
654657 // "$ref": "#/responses/repoArchivedError"
655- apiOpts := web .GetForm (ctx ).(* api.UpdateFileOptions )
656- if ctx .Repo .Repository .IsEmpty {
657- ctx .Error (http .StatusUnprocessableEntity , "RepoIsEmpty" , fmt .Errorf ("repo is empty" ))
658- }
658+ apiOpts := web .GetForm (ctx ).(* api.CreateOrUpdateFileOptions )
659659
660660 if apiOpts .BranchName == "" {
661661 apiOpts .BranchName = ctx .Repo .Repository .DefaultBranch
@@ -667,16 +667,30 @@ func UpdateFile(ctx *context.APIContext) {
667667 return
668668 }
669669
670+ var changeRepoFile files_service.ChangeRepoFile
671+ if apiOpts .SHA == "" {
672+ changeRepoFile = files_service.ChangeRepoFile {
673+ Operation : "create" ,
674+ TreePath : ctx .Params ("*" ),
675+ ContentReader : contentReader ,
676+ }
677+ } else {
678+ if ctx .Repo .Repository .IsEmpty {
679+ ctx .Error (http .StatusUnprocessableEntity , "RepoIsEmpty" , fmt .Errorf ("repo is empty" ))
680+ return
681+ }
682+
683+ changeRepoFile = files_service.ChangeRepoFile {
684+ Operation : "update" ,
685+ TreePath : ctx .Params ("*" ),
686+ ContentReader : contentReader ,
687+ SHA : apiOpts .SHA ,
688+ FromTreePath : apiOpts .FromPath ,
689+ }
690+ }
691+
670692 opts := & files_service.ChangeRepoFilesOptions {
671- Files : []* files_service.ChangeRepoFile {
672- {
673- Operation : "update" ,
674- ContentReader : contentReader ,
675- SHA : apiOpts .SHA ,
676- FromTreePath : apiOpts .FromPath ,
677- TreePath : ctx .Params ("*" ),
678- },
679- },
693+ Files : []* files_service.ChangeRepoFile {& changeRepoFile },
680694 Message : apiOpts .Message ,
681695 OldBranch : apiOpts .BranchName ,
682696 NewBranch : apiOpts .NewBranchName ,
@@ -709,7 +723,11 @@ func UpdateFile(ctx *context.APIContext) {
709723 handleCreateOrUpdateFileError (ctx , err )
710724 } else {
711725 fileResponse := files_service .GetFileResponseFromFilesResponse (filesResponse , 0 )
712- ctx .JSON (http .StatusOK , fileResponse )
726+ if apiOpts .SHA == "" {
727+ ctx .JSON (http .StatusCreated , fileResponse )
728+ } else {
729+ ctx .JSON (http .StatusOK , fileResponse )
730+ }
713731 }
714732}
715733
@@ -728,10 +746,10 @@ func handleCreateOrUpdateFileError(ctx *context.APIContext, err error) {
728746 return
729747 }
730748
731- ctx .Error (http .StatusInternalServerError , "UpdateFile " , err )
749+ ctx .Error (http .StatusInternalServerError , "CreateOrUpdateFile " , err )
732750}
733751
734- // Called from both CreateFile or UpdateFile to handle both
752+ // Called from both CreateFile or CreateOrUpdateFile to handle both
735753func createOrUpdateFiles (ctx * context.APIContext , opts * files_service.ChangeRepoFilesOptions ) (* api.FilesResponse , error ) {
736754 if ! canWriteFiles (ctx , opts .OldBranch ) {
737755 return nil , repo_model.ErrUserDoesNotHaveAccessToRepo {
0 commit comments