LoginSignup
2
3

More than 5 years have passed since last update.

GitHubの自分が所有しているリポジトリをcURLで取得する

Last updated at Posted at 2018-11-07

GitHubのAPIを使います。

TOKENの取得

こちらから取得します。

Screen Shot 2018-11-07 at 11.51.19.png

権限はrepoを出しておきます。(inviteとかいらないかも)

Screen Shot 2018-11-07 at 11.51.10.png

取得したTOKENとGitHubユーザー名を環境変数に設定しておきます。

$ export TOKEN=xxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
$ export USER=yousan

実行

$ curl -s -u $USER:$TOKEN "https://api.github.com/user/repos?per_page=100&visibility=all&page=1"

こんな感じで出てきます。

image.png

オプション

指定できるオプションです。

オプション名 説明
per_page 一回当たりの取得件数。100が最大値。
visibility パブリック(public)、プライベート(private)リポジトリを指定。両方はall。
page ペジネーション。1以上の数字で指定する。

jq

出力結果はjqを入れておくと見やすいです。

参考: jq コマンドを使う日常のご紹介

htmlのURL取得

リポジトリのURLを取得する絞り込み。

$ curl -s -u $USER:$TOKEN "https://api.github.com/user/repos?page=1&per_page=1000&visibility=public" | jq '.[].html_url'

image.png

grepと組み合わせれば簡易的な検索ができます。

クォーテーションなし

$ curl -s  -u $USER:$TOKEN "https://api.github.com/user/repos?page=1&per_page=1000&visibility=public" | jq -r '.[].html_url' | head -n 20

image.png

SSHのURL取得

自分のリポジトリを手元にクローンして作業ときはSSHのURLが便利です。

image.png

検索してクローン

応用して検索してクローンします。

git clone $(curl -s  -u $USER:$TOKEN "https://api.github.com/user/repos?page=1&per_page=1000&visibility=public" | jq -r '.[].ssh_url' | grep factorio)

ペジネーションと複数引っかかってしまうことに注意が必要です。

image.png

その他

GitHubに限定ですがhubコマンドがすごく便利です。
参考: GitHubユーザーのためのhubコマンド

そこに検索が加わるとすごく便利だなぁと思ってます。
https://github.com/github/hub/issues/833

というかすでに良い感じのパッケージがあるっぽかったです。
コマンドラインからgithubを検索してgit cloneをする with ghs, peco and ghq

2
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3