fabic974 random (binary) wanderings in the philament empires…

Github/API: List forks of a repository + jq.sh

TL;DR: This will query the Github API (v3) endpoint using Curl, and select the SSH/Git URL as well as the fork owner username using jq, that one may process through Sed and a Bash while-read loop for adding Git remotes :

$ curl -s -u fabic -H "Accept: application/vnd.github.v3+json" \
    https://api.github.com/repos/ulli-kroll/mt7610u/forks |
      jq '.[] | [.ssh_url, .owner.login] | @sh' |
        sed -e 's/"\(.*\)"/\1/' -e "s/'//g" |
          while read url login ;
          do
            echo "~~> Adding remote '$login' for $url :";
            git remote add "$login" "$url" || (echo "ERROR!") ;
          done

We resort to Sed for removing the wrapping double-quotes; as well as single-quotes.

The most simple query using Curl and basic auth/n :

$ curl -s -u fabic -H "Accept: application/vnd.github.v3+json" \
    https://api.github.com/repos/ulli-kroll/mt7610u/forks

See developer.github.com/v3/ about the available authentication mecanisms.

Piping the output through jq, selecting data :

$ curl -s -u fabic -H "Accept: application/vnd.github.v3+json" \
    https://api.github.com/repos/ulli-kroll/mt7610u/forks |
      jq '.[] | {name: .full_name, url: .html_url, git: .ssh_url, homepage: .homepage, has_issues: .has_issues, has_wiki: .has_wiki}'

Getting just the list of Git/SSH URLs :

$ curl -s -u fabic -H "Accept: application/vnd.github.v3+json" \
    https://api.github.com/repos/ulli-kroll/mt7610u/forks |
      jq '.[] | .ssh_url'

Along with owner login, wrapped into an array :

curl -s -u fabic -H "Accept: application/vnd.github.v3+json" \
  https://api.github.com/repos/ulli-kroll/mt7610u/forks |
    jq '.[] | [.ssh_url, .owner.login]'

Filtered with |@sh provides an output that is very close to a table :

curl -s -u fabic -H "Accept: application/vnd.github.v3+json" \
  https://api.github.com/repos/ulli-kroll/mt7610u/forks |
    jq '.[] | [.ssh_url, .owner.login]'

NOTE: There’s also Bash script ok.sh, but it seems not to provide a command for listing forks.

EOF