tutorials|April 16, 2021|2 min read

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

TL;DR

Use GitHub's REST API to automate repository creation, configure public/private/internal visibility, and assign team read-only permissions -- saving 10+ clicks per project.

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

Mongoose - Using CRUD operations in mongodb in nodejs

Mongoose - Using CRUD operations in mongodb in nodejs

MongoDB CRUD Operations Mongoose provides a simple schema based solution to…

Kubernetes - How to Set Namespace So You Do Not Need to Mention it Again and Again in Kubectl Commands.

Kubernetes - How to Set Namespace So You Do Not Need to Mention it Again and Again in Kubectl Commands.

Introduction In some of the cases, we need to specify namespace name along with…

How to build FIPS enabled Openssl in docker

How to build FIPS enabled Openssl in docker

Introduction In this post, we will see how we can build FIPS enabled openssl in…

Solving Jboss Wildfly Oracle JDBC driver problem, with Dockerfile

Solving Jboss Wildfly Oracle JDBC driver problem, with Dockerfile

Assuming your web application is using oracle, and you are deploying your app on…

Python 3 - Format String fun

Python 3 - Format String fun

This post is dedicated for cases where we intend to append a variable value in a…

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

System Design Patterns for Scaling Writes

System Design Patterns for Scaling Writes

In the companion article on scaling reads, we covered caching, replicas, and…

Serverless vs Containers — The Decision I Keep Revisiting

Serverless vs Containers — The Decision I Keep Revisiting

Every time I start a new service, I have the same argument with myself. Lambda…

System Design Patterns for Scaling Reads

System Design Patterns for Scaling Reads

Most production systems are read-heavy. A typical web application sees 90-9…

Building a Production RAG Pipeline — From Chunking to Retrieval to Generation

Building a Production RAG Pipeline — From Chunking to Retrieval to Generation

Large Language Models are powerful, but they hallucinate. They confidently make…

Prompt Engineering Patterns That Actually Work in Production

Prompt Engineering Patterns That Actually Work in Production

Most prompt engineering advice on the internet is useless in production. “Be…

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…