【Github】GitHubのリポジトリをコマンドから作成する

■ はじめに

小ネタ

https://dk521123.hatenablog.com/entry/2023/02/15/173618

で、リポジトリを別のGitに移行するシェルを作ったが
前提条件として「[2] 移動先のリポジトリは、事前に作っておくこと」
とあったが、これも含めてシェル化すると大分楽になる。

そこで、リポジトリをコマンドで作るやり方を調べてみた
なお、Token発行を含めて、以下のサイトが大変参考になった。感謝、、、

https://rainbow-engine.com/github-create-repo/

目次

【1】GitHubのリポジトリをコマンドから作成するには
【2】Token発行手順
 1)使用上の注意
【3】コマンド例
【4】リポジトリを別のGitに移行するシェル(リポジトリ自動作成版)

【1】GitHubリポジトリをコマンドから作成するには

* 以下の公式サイトより

https://docs.github.com/en/rest/repos/repos?apiVersion=2022-11-28#create-a-repository-for-the-authenticated-user

curl \
  -X POST \
  -H "Accept: application/vnd.github+json" \
  -H "Authorization: Bearer <YOUR-TOKEN>"\
  -H "X-GitHub-Api-Version: 2022-11-28" \
  https://api.github.com/user/repos \
  -d '{"name":"YOUR-REPOSITORY","description":"This is your first repo!","homepage":"https://github.com","private":false,"is_template":true}'

【2】Token発行手順

* Tokenを発行しておくこと(<YOUR-TOKEN>の部分)
 => 今回は、Classic で発行した

発行手順

[1] [Userアイコン]-[Settings]-[Developer settings]
 -[Personal Access Token]-[Tokens(classic)]を選択
[2] [Generate new token]-[Generate new token(classic)]を選択
[3] 以下を入力し、「Generate token」ボタン押下
 + Name: test-token (任意の文字列)
 + Select scopes: とりあえず、全部チェック(ちゃんとやりたい場合は参考文献参照)
[4] Tokenが発行されるので、テキストエディタなどにコピペしておく

1)使用上の注意

* 「Fine Grained-Token」で発行しても
 エラー「Resource not accessible by personal access token」になった
 => Beta版だったから?

【3】コマンド例

# Token(classic)を発行しておく(以下の「xxxxxxxx」部分)
curl \
  -X POST \
  -H "Accept: application/vnd.github+json" \
  -H "Authorization: Bearer xxxxxxxx"\
  -H "X-GitHub-Api-Version: 2022-11-28" \
  https://api.github.com/user/repos \
  -d '{"name":"test-repo","description":"This is my test repo!","homepage":"https://dk521123.hatenablog.com/","private":false,"is_template":true}'

【3】リポジトリを別のGitに移行するシェル(リポジトリ自動作成版)

以上を踏まえて、以下の関連記事のシェルを書き換えてみた

https://dk521123.hatenablog.com/entry/2023/02/15/173618

#!/bin/bash

# !! Change your source repository URLs
declare -a source_repo_list=(
  "https://github.com/your-account/your-repo-1.git"
  "https://github.com/your-account/your-repo-2.git"
  "https://github.com/your-account/your-repo-3.git"
)
# !! Change your params
destination_github_access_token="xxxxx"
your_homepage="https://dk521123.hatenablog.com/"

for source_repo in ${source_repo_list[@]};
do
  echo "**** Start to copy ${source_repo} ****"

  # Step1: Git Clone with mirror option
  git clone --mirror ${source_repo}

  # Step2: Move to repository directory
  source_repo_name=$(basename ${source_repo})
  echo "Repository name: ${source_repo_name} (e.g. your-repo-1.git)"
  cd ${source_repo_name}

  # Step3: Create a destination repository !! Add here !!
  destination_repo_name="your_${source_repo_name}"
  curl \
    -X POST \
    -H "Accept: application/vnd.github+json" \
    -H "Authorization: Bearer ${destination_github_access_token}"\
    -H "X-GitHub-Api-Version: 2022-11-28" \
    https://api.github.com/user/repos \
    -d '{"name":"${destination_repo_name}","description":"This is for ${destination_repo_name}","homepage":"${your_homepage}","private":false,"is_template":true}'

  # Step4: Git push with mirror option
  # !! Change your destination repository URL. 
  destination_git_url="https://github.com/destination/${destination_repo_name}"
  echo "Move to ${destination_git_url}"
  git push --mirror ${destination_git_url}

  cd ..

  echo "**** Done for ${source_repo_name} ****"
done

echo "DONE..."

参考文献

https://rainbow-engine.com/github-create-repo/

関連記事

Github ~ 入門編 ~
https://dk521123.hatenablog.com/entry/2019/07/18/234652
リポジトリを別のGitに移行するシェル
https://dk521123.hatenablog.com/entry/2023/02/15/173618