GitHub AppsでOrganizationにリポジトリを新規作成する / magicien 

GitHub Appsでは、今のところユーザ用リポジトリの作成ができない。Organization用であれば作成できるようなので、作成までの手順をメモしておく。

事前準備

Appの作成とJWT生成の準備は「GitHub Appsでリポジトリにファイル追加・更新」と同様に済ませておく。

操作対象のOrganizationを決める

これも前回とほぼ同じなので流れだけ簡単に書いておく。

ユーザを判別する

ユーザに下記URLへアクセスしてもらう。
https://github.com/login/oauth/authorize?child_id=ChildID&redirect_uri=自分のサイトのどこか&state=ランダムな文字列

戻ってきたリダイレクト先でcodeを取得する。
https://自分のサイト/どこかのページ.html?code=1234567890abcdef&state=先ほどのstate

アクセストークン生成

curl -X POST -d "code=先ほどのcode" -d "client_id=ClientID" -d "client_secret=ClientSecret" https://github.com/login/oauth/access_token

応答からアクセストークン取得
access_token=アクセストークン&scope=&token_type=bearer

ユーザが管理していてAppをインストール済みのOrganizationを取得する

取得方法はユーザのリポジトリを見つけるときと同じ。
curl -H "Authorization: token アクセストークン" -H "Accept: application/vnd.github.machine-man-preview+json" https://api.github.com/user/installations

Organization用のInstallationは、target_typeがOrganizationになっている(ユーザ用はUserになっている)。
{
  "total_count": 1,
  "installations": [
    {
      "id": 56789,
      "account": {
        "login": "Organization名",
        中略
      },
      中略
      "integration_id": 1234,
      "app_id": 1234,
      "target_type": "Organization",
      中略
    }
  ],
  "integration_installations": [
    {
      "id": 56789,
      "account": {
        "login": "Organization名",
        中略
      },
      中略
      "integration_id": 1234,
      "app_id": 1234,
      "target_type": "Organization",
      中略
    }
  ]
}
installations[i].app_id が一致し、target_typeがOrganizationのものを見つける。 必要なのは、id(installationのid)とaccount.login(Organization名)。

Installationとしての認証

ここは前回と同じ。
curl -X POST -H "Authorization: Bearer JWTのトークン" -H "Accept: application/vnd.github.machine-man-preview+json" https://api.github.com/installations/先ほど取得したinstallationのid/access_tokens

応答も同じ。
{
  "token": "アクセストークンその2",
  "expires_at": "2017-01-01T00:00:00Z"
}

リポジトリを生成する

公式の説明はここ。
最低限必要なパラメータはnameだけ。
curl -X POST -H "Authorization: token アクセストークンその2" -H "Accept: application/vnd.github.machine-man-preview+json" -d '{"name": "リポジトリ名"}' https://api.github.com/orgs/Organization名/repos

おしまい!

2017/09/10(Sun) 17:09:58