@@ -905,11 +905,71 @@ func resolveRefCommit(ctx *context.APIContext, ref string, minCommitIDLen ...int
905905 return refCommit
906906}
907907
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+
908967// GetContents Get the metadata and contents (if a file) of an entry in a repository, or a list of entries if a dir
909968func GetContents (ctx * context.APIContext ) {
910969 // swagger:operation GET /repos/{owner}/{repo}/contents/{filepath} repository repoGetContents
911970 // ---
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.
913973 // produces:
914974 // - application/json
915975 // parameters:
@@ -938,29 +998,35 @@ func GetContents(ctx *context.APIContext) {
938998 // "$ref": "#/responses/ContentsResponse"
939999 // "404":
9401000 // "$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 })
9441002 if ctx .Written () {
9451003 return
9461004 }
1005+ ctx .JSON (http .StatusOK , util .Iif [any ](ret .FileContents != nil , ret .FileContents , ret .DirContents ))
1006+ }
9471007
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 {
9491015 if git .IsErrNotExist (err ) {
9501016 ctx .APIErrorNotFound ("GetContentsOrList" , err )
951- return
1017+ return nil
9521018 }
9531019 ctx .APIErrorInternal (err )
954- } else {
955- ctx .JSON (http .StatusOK , fileList )
9561020 }
1021+ return & ret
9571022}
9581023
9591024// GetContentsList Get the metadata of all the entries of the root dir
9601025func GetContentsList (ctx * context.APIContext ) {
9611026 // swagger:operation GET /repos/{owner}/{repo}/contents repository repoGetContentsList
9621027 // ---
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.
9641030 // produces:
9651031 // - application/json
9661032 // parameters:
@@ -1084,6 +1150,6 @@ func handleGetFileContents(ctx *context.APIContext) {
10841150 if ctx .Written () {
10851151 return
10861152 }
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 )
10881154 ctx .JSON (http .StatusOK , util .SliceNilAsEmpty (filesResponse ))
10891155}
0 commit comments