tutorials|April 16, 2021|2 min read

How to create Repository using Github Rest API, Configure Visibility and Assign a Team as Readonly

How to create Repository using Github Rest API, Configure Visibility and Assign a Team as Readonly

Introduction

I had to create many repositories in an Github organization. I created an organization, and I had around 40 projects which I need to migrate from perforce to git.

We can repository from Github portal(UI), and it requires at least 6 clicks:

  1. Click on ’+’ button on left top
  2. Click on repository
  3. On left side, click your organization (by default your username is selected)
  4. Type project name
  5. Change visibility (public/private or internal)
  6. Click on checkbox for README.md
  7. Click create
  8. After creating, assign team permissions (3 clicks)

So total of 7+3=10 clicks per project So, for 40 projects I had to perform 40*10 = 400 clicks, which was unmanageable.

Requirements

My requirements are:

  • Create Repositories (with above mentioned settings)
  • Commit and Push project files

Pre-requisite

I synced those 40 folders to my local directory. Lets call it mylibs_ And, I have a team in github which I will assign permission to these projects.

Fetch Team_id

curl  -u USERNAME:TOKEN https://github.com/api/v3/repos/USERNAME/PROJECT/teams

Shell Script

Assumming following folders:

  • Target folder: mylibs
  • Original Project folder: mylibs_
AUTH="USERNAME:TOKEN"
for d in *;
do
  echo "Working on ${d}"
  curl -s -o /dev/null -w "%{http_code}" \
    -u ${AUTH} \
    -X POST \
    -H "Accept: application/vnd.github.v3+json" \
    https://github.com/api/v3/orgs/mylibs/repos \
    -d '{"name": "'${d}'", "visibility": "private", "private": "true", "auto_init": "true", "team_id": "TEAM_ID"}'

  echo "";
  git clone [email protected]:mylibs/${d}.git ../mylibs/${d}

  cp -R ${d}/* ../mylibs/${d}/

  cd ../mylibs/${d}/
  
  git add .
  git commit -m "Migration from P4 to git"
  git push
  echo ""
  echo "Done with ${d}"
  cd -
done

Create Github Authentication Token

Visit github page for How to create oAuth token{:target=“_blank”}

Note

Please note that the repositories created will be with visibility=private. And the team assigned to each repository will be having readonly access.

Related Posts

How to Renew Lets Encrypt SSL Certificate

How to Renew Lets Encrypt SSL Certificate

Introduction to problem This post is applicable for those who has already an SSL…

Shell script to extract single file from a bunch of jar files

Shell script to extract single file from a bunch of jar files

Each jar file has a manifest file in META_INF/MANIFEST.MF I have to read each…

Youtube API in NodeJs, Usage with Example

Youtube API in NodeJs, Usage with Example

This library is ES6, promise compatible. Or, in your package.json file, include…

How to Protect Git branch and Enforce Restrictions

How to Protect Git branch and Enforce Restrictions

In previous article: Effective Git Branching Strategy, I discussed Git branching…

A Practical Guide on how to to create your own git command alias

A Practical Guide on how to to create your own git command alias

Introduction In this guide, We will learn on how to create some handy command…

Curl - Your friend for Rest APIs/Calls - Basic Commands

Curl - Your friend for Rest APIs/Calls - Basic Commands

Curl is a wonderful tool for initiate REST APIs or calls. Or, you can literally…

Latest Posts

Jenkins Pipeline with Jenkinsfile - How To Schedule Job on Cron and Not on Code Commit

Jenkins Pipeline with Jenkinsfile - How To Schedule Job on Cron and Not on Code Commit

Introduction In this post we will see following: How to schedule a job on cron…

How to Git Clone Another Repository from Jenkin Pipeline in Jenkinsfile

How to Git Clone Another Repository from Jenkin Pipeline in Jenkinsfile

Introduction There are some cases, where I need another git repository while…

How to Fetch Multiple Credentials and Expose them in Environment using Jenkinsfile pipeline

How to Fetch Multiple Credentials and Expose them in Environment using Jenkinsfile pipeline

Introduction In this post, we will see how to fetch multiple credentials and…

Jenkins Pipeline - How to run Automation on Different Environment (Dev/Stage/Prod), with Credentials

Jenkins Pipeline - How to run Automation on Different Environment (Dev/Stage/Prod), with Credentials

Introduction I have an automation script, that I want to run on different…

Jenkinsfile - How to Create UI Form Text fields, Drop-down and Run for Different Conditions

Jenkinsfile - How to Create UI Form Text fields, Drop-down and Run for Different Conditions

Introduction I had to write a CICD system for one of our project. I had to…

Java Log4j Logger - Programmatically Initialize JSON logger with customized keys in json logs

Java Log4j Logger - Programmatically Initialize JSON logger with customized keys in json logs

Introduction Java log4j has many ways to initialize and append the desired…