【问题标题】:How to get subfolders and files using gitlab api如何使用 gitlab api 获取子文件夹和文件
【发布时间】:2013-09-23 06:25:10
【问题描述】:

我正在使用gitlab api获取文件和文件夹并成功,

但我只能获取目录名称,而不是其子文件夹和文件。

那么,我怎样才能获得我的存储库的完整树。

请告诉我。

提前致谢, 马利卡朱那

【问题讨论】:

  • 您当前使用什么命令来获取目录名称?
  • 嗨@VonC,我正在使用GET /projects之类的get方法来获取与auth token相关的项目

标签: gitlab


【解决方案1】:

根据API,我们可以使用

GET /projects/:id/repository/tree

列出项目中的文件和目录。但是我们只能通过这种方式获取repo顶层的文件和目录,以及参数path的顶层目录的子目录。

如果你想获取script/js/components的目录,例如,你可以使用

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

【讨论】:

  • @rwking 谢谢你的提醒。我找到了遍历树的方法并更新了我的答案。
【解决方案2】:

休息 API

您可以使用 recursive option 获取完整的树,使用 /projects/:id/repository/tree?recursive=true

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

GraphQL API

您还可以使用最近发布的Gitlab GraphQL API 以递归方式获取树:

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

您可以转到以下 URL:https://$gitlab_url/-/graphql-explorer 并通过上述查询

Graphql 端点是“https://$gitlab_url/api/graphql”上的 POST 使用 & 的示例:

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 '.'

【讨论】:

  • 你知道一次调用可以返回多少个树项有限制吗?
【解决方案3】:

您应该对文件的完整路径进行 url 编码。例如,以免假设您的存储库下的文件路径是:javascript/header.js

那么你可以使用: curl --head --header "PRIVATE-TOKEN: " "https:///api/v4/projects//repository/files/javascript%2Fheader%2Ejs"

【讨论】:

    【解决方案4】:

    当然,正如其他回复中提到的,您错过了 gitlab repositories APIpath 属性,该属性可让您浏览文件层次结构。

    另外,为简单起见,python gitlab 项目通过projects API 公开它。示例:

    # list the content of the root directory for the default branch
    items = project.repository_tree()
    
    # list the content of a subdirectory on a specific branch
    items = project.repository_tree(path='docs', ref='branch1')
    

    【讨论】:

      猜你喜欢
      • 2021-10-31
      • 2020-12-19
      • 1970-01-01
      • 2018-04-02
      • 2013-04-11
      • 1970-01-01
      • 2022-12-06
      • 1970-01-01
      • 2023-04-11
      相关资源
      最近更新 更多