ljharb / repo-report

CLI to list all repos a user has access to, and report on their configuration in aggregate.
MIT License
24 stars 11 forks source link

Fetch all repositories using the graphQL API #1

Closed sladyn98 closed 3 years ago

sladyn98 commented 3 years ago

We could search for all of the repositories owned by a user and then list out the parameters we would require to query a) Default Branch b) Pull Requests c) Default Permissions d) Branch Protection

sladyn98 commented 3 years ago

One of the queries I tried running to get repositories is

{
  viewer {
    repositories(first: 100) {
      totalCount
      nodes {
        nameWithOwner
      }
      pageInfo {
        endCursor
        hasNextPage
      }
    }
  }
}
sladyn98 commented 3 years ago

This is one query I used to get all repos under jordans username

{
  user(login: "ljharb") {
    repositories(first: 50, isFork: false) {
      nodes {
        name
        url
      }
    }
  }
}

You can get the default branch name by adding a defaultBranchRef

{
  user(login: "ljharb") {
    repositories(first: 50, isFork: false) {
      nodes {
        name
        url
        defaultBranchRef {
          name
        }
      }  
    }
  }

}

sladyn98 commented 3 years ago
{
  user(login: "ljharb") {
    repositories(first: 50, isFork: false) {
      nodes {
        name
        url
        defaultBranchRef {
          name
          branchProtectionRule {
            isAdminEnforced
            requiresStatusChecks
            allowsForcePushes
          }
        }
      }  
    }
  }
}

This returns data in this format. We can then parse this information about default branch name and group it

{
  "data": {
    "user": {
      "repositories": {
        "nodes": [
          {
            "name": "compat-table",
            "url": "https://github.com/kangax/compat-table",
            "defaultBranchRef": {
              "name": "gh-pages",
              "branchProtectionRule": null
            }
          },
          {
            "name": "nvm",
            "url": "https://github.com/nvm-sh/nvm",
            "defaultBranchRef": {
              "name": "master",
              "branchProtectionRule": null
            }
          },
          {
            "name": "forms",
            "url": "https://github.com/caolan/forms",
            "defaultBranchRef": {
              "name": "master",
              "branchProtectionRule": null
            }
          },
          {
            "name": "testling",
            "url": "https://github.com/substack/testling",
            "defaultBranchRef": {
              "name": "master",
              "branchProtectionRule": null
            }
          },
          {
            "name": "isitteatimeyet",
            "url": "https://github.com/attaboy/isitteatimeyet",
            "defaultBranchRef": {
              "name": "master",
              "branchProtectionRule": null
            }
          },
          {
            "name": "travis-cookbooks",
            "url": "https://github.com/travis-ci/travis-cookbooks",
            "defaultBranchRef": {
              "name": "master",
              "branchProtectionRule": null
            }
          },
          {
            "name": "resolve",
            "url": "https://github.com/browserify/resolve",
            "defaultBranchRef": {
              "name": "master",
              "branchProtectionRule": null
            }
          },
          {
            "name": "TcoExpand",
            "url": "https://github.com/ljharb/TcoExpand",
            "defaultBranchRef": {
              "name": "master",
              "branchProtectionRule": null
            }
          },
          {
            "name": "is",
            "url": "https://github.com/enricomarino/is",
            "defaultBranchRef": {
              "name": "master",
              "branchProtectionRule": null
            }
          },
thehanimo commented 3 years ago

Here's the one I used:

query {
  viewer {
    repositories(
      first: 100
      affiliations: [OWNER, ORGANIZATION_MEMBER, COLLABORATOR]
      ${endCursor ? `after: "${endCursor}"` : ""}
    ) {
      totalCount
      pageInfo {
        endCursor
        hasNextPage
      }
      nodes {
        name
        owner {
          login
        }
        isPrivate
        defaultBranchRef {
            name
        }
        viewerPermission
      }
    }
  }
  rateLimit {
    cost
    remaining
  }
}

Some sample outputs I got with the feature I pushed here

Screenshot 2021-02-13 at 9 05 30 PM Screenshot 2021-02-13 at 9 06 33 PM
diananova commented 3 years ago

This looks great! A few thoughts:

sladyn98 commented 3 years ago

We can close this since we have already initial research