【问题标题】:Retrieve a report with all admins for each repo in a GitHub org using GitHub API and scripting使用 GitHub API 和脚本检索 GitHub 组织中每个存储库的所有管理员的报告
【发布时间】:2021-01-24 19:42:27
【问题描述】:

目前,我没有看到任何简单的方法来生成包含 GitHub 组织中每个 repo 的管理员列表的报告。下面的示例非常适合我检索单个 repo 的管理员列表:

curl https://my-github-url/api/v3/repos/my-org/my-repo/collaborators?per_page=100 \
     -H "Authorization: Token my-valid-token" | \
     jq '[ .[] | select(.permissions.admin == true) | .login ]'

有没有办法为该组织下的所有 repos 进行迭代,然后生成一个精美的报告,可能类似于 html 表格报告或可能是 excel 类型

【问题讨论】:

    标签: github github-api


    【解决方案1】:

    您可以使用GraphQL API 来减少使用以下请求的数量:

    {
      organization(login: "your-org") {
        repositories(first: 100) {
          nodes {
            nameWithOwner
            collaborators(first: 100) {
              totalCount
              edges {
                permission
                node {
                  login
                  name
                }
              }
              pageInfo {
                endCursor
                hasNextPage
              }
            }
          }
        }
      }
    }
    

    给出:

    {
      "data": {
        "organization": {
          "repositories": {
            "nodes": [
              {
                "nameWithOwner": "Your-Org/Your-Repo",
                "collaborators": {
                  "totalCount": 18,
                  "edges": [
                    {
                      "permission": "ADMIN",
                      "node": {
                        "login": "johndoe",
                        "name": "John Doe"
                      }
                    },
                    ............................
                  ]
                }
              }
            ]
          }
        }
      }
    }
    

    使用 的示例:

    #!/bin/bash
    
    token="YOUR_TOKEN"
    org="YOUR_ORG"
    
    query='{
      organization(login: \"'$org'\") {
        repositories(first: 100) {
          nodes {
            nameWithOwner
            collaborators(first: 100) {
              totalCount
              edges {
                permission
                node {
                  login
                  name
                }
              }
              pageInfo {
                endCursor
                hasNextPage
              }
            }
          }
        }
      }
    }
    '
    curl -s -H "Authorization: token $token" \
         -H  "Content-Type:application/json" \
         -d '{ 
              "query": "'"${query//[$'\n|\r\n']}"'"
          }' https://api.github.com/graphql | jq '.data.organization.repositories.nodes[] | 
            {
                repo: .nameWithOwner, 
                users: [
                    .collaborators.edges[] | 
                    select(.permission == "ADMIN") | 
                    .node
                ]
            }'
    

    如果您有超过 100 个 repo 或超过 100 个协作者,则需要管理 pagination

    【讨论】:

    • 谢谢伯特兰。对于只有五个 repos 的组织来说,这对我来说非常有用。我得到了正确的管理员列表。但是,我需要为拥有 500 多个存储库的组织尝试此方法。我如何使用分页来做到这一点。我在这方面完全是新手?
    猜你喜欢
    • 2021-04-26
    • 1970-01-01
    • 2021-07-11
    • 1970-01-01
    • 1970-01-01
    • 2018-12-05
    • 2018-03-26
    • 1970-01-01
    • 2023-04-07
    相关资源
    最近更新 更多