Hub: Listing all public repositories of a github user/organization

Created on 18 Feb 2017  路  7Comments  路  Source: github/hub

I really enjoy using hub, this is such a great tool.

I was wondering if it's possible to use hub to list all the github repos associated with a particular user - either through a command or autocompletion at the command line. I use bash on Linux if that makes a difference.

Cheers,
C

Most helpful comment

In case you want to show repositories of local user(that was logged in via hub):

repos() {
  local user="${1?}"
  shift 1
  paginate hub api -t graphql -f user="$user" "$@" -f query='
    query($user: String!, $per_page: Int = 100, $after: String) {
      user(login: $user) {
        repositories(first: $per_page, after: $after) {
          nodes {
            nameWithOwner
          }
          pageInfo {
            hasNextPage
            endCursor
          }
        }
      }
    }
  '
}

paginate() {
  local output cursor
  output="$("$@")"
  cursor="$(awk '/\.hasNextPage/ { has_next=$2 } /\.endCursor/ { if (has_next=="true") print $2 }' <<<"$output")"
  printf "%s\n" "$output"
  [ -z "$cursor" ] || paginate "$@" -f after="$cursor"
}

user() {
  hub api --flat user | grep .login | awk '/.login/ {print $2}'
}

repos "$(user)" | awk '/\.nameWithOwner\t/ { print $2 }'

All 7 comments

Hi, thanks for writing in with a feature idea.

Listing repositories isn't something that most git users will perform on the command-line. It seems that this would be more suited for a generic "data dump" command that could output raw data from the API for script consumption: https://github.com/github/hub/issues/1407

Once that is done, we could tweak our shell completion scripts to autocomplete hub clone github/hu to github/hubot (or offer choices if there are multiple matches), but this is a long way down the road from now, and I'm not sure when we could get to that.

I'm going to close this as a duplicate. This will be possible when we have a generic API command for fetching arbitrary data.

Think this would still be a really good feature to have! Feels out-of-place to be able to create and delete repositories from the command line, but not view them. Doing this using the generic API command becomes less trivial when you want to view private repositories.

Still hope to see this feature one day!

Until then, fetching all your repositories can be done with hub api:

repos() {
  local user="${1?}"
  shift 1
  paginate hub api -t graphql -f user="$user" "$@" -f query='
    query($user: String!, $per_page: Int = 100, $after: String) {
      user(login: $user) {
        repositories(first: $per_page, after: $after) {
          nodes {
            nameWithOwner
          }
          pageInfo {
            hasNextPage
            endCursor
          }
        }
      }
    }
  '
}

paginate() {
  local output cursor
  output="$("$@")"
  cursor="$(awk '/\.hasNextPage/ { has_next=$2 } /\.endCursor/ { if (has_next=="true") print $2 }' <<<"$output")"
  printf "%s\n" "$output"
  [ -z "$cursor" ] || paginate "$@" -f after="$cursor"
}

repos "tfcat" | awk '/\.nameWithOwner\t/ { print $2 }'

In case you want to show repositories of local user(that was logged in via hub):

repos() {
  local user="${1?}"
  shift 1
  paginate hub api -t graphql -f user="$user" "$@" -f query='
    query($user: String!, $per_page: Int = 100, $after: String) {
      user(login: $user) {
        repositories(first: $per_page, after: $after) {
          nodes {
            nameWithOwner
          }
          pageInfo {
            hasNextPage
            endCursor
          }
        }
      }
    }
  '
}

paginate() {
  local output cursor
  output="$("$@")"
  cursor="$(awk '/\.hasNextPage/ { has_next=$2 } /\.endCursor/ { if (has_next=="true") print $2 }' <<<"$output")"
  printf "%s\n" "$output"
  [ -z "$cursor" ] || paginate "$@" -f after="$cursor"
}

user() {
  hub api --flat user | grep .login | awk '/.login/ {print $2}'
}

repos "$(user)" | awk '/\.nameWithOwner\t/ { print $2 }'

I use this to list my repositories hub api -X GET /user/repos | jq -r '.[] .name'

This helps me list the public repositories from from other users hub api -X GET /users/{username}/repos | jq -r '.[] .name'

hub api -X GET /orgs/{orgname}/repos | jq -r '.[] .name'

Great find @cesarnorena - thanks!

A more efficient way to list _all_ repositories _owned_ by you:

 hub api --paginate -XGET user/repos -f affiliation=owner -f per_page=100 | jq -r '.[].name'

Explanation:

  • uses--paginate to fetch all pages of results
  • uses per_page=100 to ensure the minimum amount of HTTP requests are necessary
  • uses the affiliation filter to exclude repositories you have access to but are not the owner of

The same with GraphQL:

# repos.graphql
query($per_page: Int = 100, $endCursor: String) {
  viewer {
    repositories(first: $per_page, after: $endCursor, ownerAffiliations: OWNER) {
      nodes {
        nameWithOwner
      }
      pageInfo {
        hasNextPage
        endCursor
      }
    }
  }
}
hub api graphql --paginate -F [email protected] | jq -r '.data.viewer.repositories.nodes[].nameWithOwner'

See https://github.com/mislav/hub-api-utils/blob/master/bin/hub-repos

Was this page helpful?
0 / 5 - 0 ratings