@@ -905,11 +905,71 @@ func resolveRefCommit(ctx *context.APIContext, ref string, minCommitIDLen ...int
905
905
return refCommit
906
906
}
907
907
908
+ func GetContentsExt (ctx * context.APIContext ) {
909
+ // swagger:operation GET /repos/{owner}/{repo}/contents-ext/{filepath} repository repoGetContentsExt
910
+ // ---
911
+ // summary: The extended "contents" API, to get file metadata and/or content, or list a directory.
912
+ // description: It guarantees that only one of the response fields is set if the request succeeds.
913
+ // Users can pass "includes=file_content" or "includes=lfs_metadata" to retrieve more fields.
914
+ // produces:
915
+ // - application/json
916
+ // parameters:
917
+ // - name: owner
918
+ // in: path
919
+ // description: owner of the repo
920
+ // type: string
921
+ // required: true
922
+ // - name: repo
923
+ // in: path
924
+ // description: name of the repo
925
+ // type: string
926
+ // required: true
927
+ // - name: filepath
928
+ // in: path
929
+ // description: path of the dir, file, symlink or submodule in the repo
930
+ // type: string
931
+ // required: true
932
+ // - name: ref
933
+ // in: query
934
+ // description: the name of the commit/branch/tag, default to the repository’s default branch.
935
+ // type: string
936
+ // required: false
937
+ // - name: includes
938
+ // in: query
939
+ // description: By default this API's response only contains file's metadata. Use comma-separated "includes" options to retrieve more fields.
940
+ // Option "file_content" will try to retrieve the file content, option "lfs_metadata" will try to retrieve LFS metadata.
941
+ // type: string
942
+ // required: false
943
+ // responses:
944
+ // "200":
945
+ // "$ref": "#/responses/ContentsExtResponse"
946
+ // "404":
947
+ // "$ref": "#/responses/notFound"
948
+
949
+ opts := files_service.GetContentsOrListOptions {TreePath : ctx .PathParam ("*" )}
950
+ for includeOpt := range strings .SplitSeq (ctx .FormString ("includes" ), "," ) {
951
+ if includeOpt == "" {
952
+ continue
953
+ }
954
+ switch includeOpt {
955
+ case "file_content" :
956
+ opts .IncludeSingleFileContent = true
957
+ case "lfs_metadata" :
958
+ opts .IncludeLfsMetadata = true
959
+ default :
960
+ ctx .APIError (http .StatusBadRequest , fmt .Sprintf ("unknown include option %q" , includeOpt ))
961
+ return
962
+ }
963
+ }
964
+ ctx .JSON (http .StatusOK , getRepoContents (ctx , opts ))
965
+ }
966
+
908
967
// GetContents Get the metadata and contents (if a file) of an entry in a repository, or a list of entries if a dir
909
968
func GetContents (ctx * context.APIContext ) {
910
969
// swagger:operation GET /repos/{owner}/{repo}/contents/{filepath} repository repoGetContents
911
970
// ---
912
- // summary: Gets the metadata and contents (if a file) of an entry in a repository, or a list of entries if a dir
971
+ // summary: Gets the metadata and contents (if a file) of an entry in a repository, or a list of entries if a dir.
972
+ // description: This API follows GitHub's design, and it is not easy to use. Recommend to use our "contents-ext" API instead.
913
973
// produces:
914
974
// - application/json
915
975
// parameters:
@@ -938,29 +998,35 @@ func GetContents(ctx *context.APIContext) {
938
998
// "$ref": "#/responses/ContentsResponse"
939
999
// "404":
940
1000
// "$ref": "#/responses/notFound"
941
-
942
- treePath := ctx .PathParam ("*" )
943
- refCommit := resolveRefCommit (ctx , ctx .FormTrim ("ref" ))
1001
+ ret := getRepoContents (ctx , files_service.GetContentsOrListOptions {TreePath : ctx .PathParam ("*" ), IncludeSingleFileContent : true })
944
1002
if ctx .Written () {
945
1003
return
946
1004
}
1005
+ ctx .JSON (http .StatusOK , util .Iif [any ](ret .FileContents != nil , ret .FileContents , ret .DirContents ))
1006
+ }
947
1007
948
- if fileList , err := files_service .GetContentsOrList (ctx , ctx .Repo .Repository , refCommit , treePath ); err != nil {
1008
+ func getRepoContents (ctx * context.APIContext , opts files_service.GetContentsOrListOptions ) * api.ContentsExtResponse {
1009
+ refCommit := resolveRefCommit (ctx , ctx .FormTrim ("ref" ))
1010
+ if ctx .Written () {
1011
+ return nil
1012
+ }
1013
+ ret , err := files_service .GetContentsOrList (ctx , ctx .Repo .Repository , ctx .Repo .GitRepo , refCommit , opts )
1014
+ if err != nil {
949
1015
if git .IsErrNotExist (err ) {
950
1016
ctx .APIErrorNotFound ("GetContentsOrList" , err )
951
- return
1017
+ return nil
952
1018
}
953
1019
ctx .APIErrorInternal (err )
954
- } else {
955
- ctx .JSON (http .StatusOK , fileList )
956
1020
}
1021
+ return & ret
957
1022
}
958
1023
959
1024
// GetContentsList Get the metadata of all the entries of the root dir
960
1025
func GetContentsList (ctx * context.APIContext ) {
961
1026
// swagger:operation GET /repos/{owner}/{repo}/contents repository repoGetContentsList
962
1027
// ---
963
- // summary: Gets the metadata of all the entries of the root dir
1028
+ // summary: Gets the metadata of all the entries of the root dir.
1029
+ // description: This API follows GitHub's design, and it is not easy to use. Recommend to use our "contents-ext" API instead.
964
1030
// produces:
965
1031
// - application/json
966
1032
// parameters:
@@ -1084,6 +1150,6 @@ func handleGetFileContents(ctx *context.APIContext) {
1084
1150
if ctx .Written () {
1085
1151
return
1086
1152
}
1087
- filesResponse := files_service .GetContentsListFromTreePaths (ctx , ctx .Repo .Repository , refCommit , opts .Files )
1153
+ filesResponse := files_service .GetContentsListFromTreePaths (ctx , ctx .Repo .Repository , ctx . Repo . GitRepo , refCommit , opts .Files )
1088
1154
ctx .JSON (http .StatusOK , util .SliceNilAsEmpty (filesResponse ))
1089
1155
}
0 commit comments