How to get subfolders and files using gitlab api

冷暖自知 提交于 2019-12-10 09:24:18

问题


I am using gitlab api to get the files and folders and succeded,

But I can able to get only directory names, not its subfolders and files.

So, how can i get full tree of my repository.

Please let me know.

Thanks in advance, Mallikarjuna


回答1:


According to the API, we can use

GET /projects/:id/repository/tree

to list files and directories in a project. But we can only get the files and directories in top-level of the repo in this way, and sub-directories of directories in top-level with param path.

If you wanna get directories of script/js/components, for example, you can use

GET /projects/:id/repository/tree?path=script/js/components




回答2:


Rest API

You can use the recursive option to get the full tree using /projects/:id/repository/tree?recursive=true

For example : https://your_gitlab_host/api/v4/projects/:id/repository/tree?recursive=true&per_page=100

GraphQL API

You can also use the recently released Gitlab GraphQL API to get the trees in a recursive way :

{
  project(fullPath: "project_name_here") {
    repository {
      tree(ref: "master", recursive: true){
        blobs{
          nodes {
            name
            type
            flatPath
          }
        }
      }
    }
  }
}

You can go to the following URL : https://$gitlab_url/-/graphql-explorer and past the above query

The Graphql endpoint is a POST on "https://$gitlab_url/api/graphql" An example using curl & jq :

gitlab_url=<your gitlab host>
access_token=<your access token>
project_name=<your project name>
branch=master

curl -s -H "Authorization: Bearer $access_token" \
     -H "Content-Type:application/json" \
     -d '{ 
          "query": "{ project(fullPath: \"'$project_name'\") { repository { tree(ref: \"'$branch'\", recursive: true){ blobs{ nodes { name type flatPath }}}}}}"
      }' "https://$gitlab_url/api/graphql" | jq '.'


来源:https://stackoverflow.com/questions/18952935/how-to-get-subfolders-and-files-using-gitlab-api

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!