John Siu Blog

Tech - Business Tool, Personal Toys

Use Github/Gogs Api To Create Repo

☰ Table of Content

Gogs implemented Github api. The only different is the URL.

Token

No matter you are using Gogs or Github, you have to login your account and generate a token.

Gogs

Go into Settings -> Applications, then click Generate New Token.

Github

Go into Settings -> Developer settings -> Personal access tokens, then click Generate new token.

In both cases, copy&paste the token into a file right away. We will need it in our script/function below.

zsh function

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
create_new_repo(){

  REPO=$1

  TOK="Your token here"

  # Gogs url
  #URL="https://<your gogs domain>/api/v1"

  # Github url
  #URL="https://api.github.com"

  REQ="{\"name\":\"${REPO}\"}"

  curl \
  -H "Content-Type: application/json" \
  -H "Authorization: token ${TOK}" \
  -d "${REQ}" \
  ${URL}/user/repos
}

The only difference between gogs and github is the URL. Gogs need /api/v1 in its url, while github uses api sub-domain. Uncomment the one you want to use.

Add function in .zshrc and use as follow:

1
create_new_repo <repo_name>

Implementation

I have two fully functional implementations, please check them out:

LanguageProject
bashmygit
golanggo-mygit

John Siu

Update: 2022-05-25
Tag
[api]
[zsh]
comments powered by Disqus