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

Drupal 8 - How to create a Page with admin access and create its menu entry in Reports (No Coding)

Drupal 8 - How to create a Page with admin access and create its menu entry in Reports (No Coding)

Introduction I needed a report page, where I wanted to have some information…

Drupal 8 Rules module - How to configure Rules module to send email notification for every comment posted

Drupal 8 Rules module - How to configure Rules module to send email notification for every comment posted

Introduction In our previous post, where we saw How to configure comments module…

Microsoft Azure Just-In-Time access control

Microsoft Azure Just-In-Time access control

According to Microsoft, Therefore, they recently posted about a feature in beta…

How to Solve Circular Import Error in Python

How to Solve Circular Import Error in Python

Introduction To give some context, I have two python files. (Both in same folder…

Explaining issue: response to preflight request doesn't pass access control check

Explaining issue: response to preflight request doesn't pass access control check

You are developing a nodejs web application having some UI and backend APIs…

Azure Storage Blob - How to List and Download Blob from Azure Storage container in Python (No Azure library)

Azure Storage Blob - How to List and Download Blob from Azure Storage container in Python (No Azure library)

Introduction In this tutorial we will see, How to list and download storage…

Latest Posts

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…

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 - 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…

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…