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

Git shortcuts - Helpful shortcuts for git users

Git shortcuts - Helpful shortcuts for git users

Github is an awesome place of storing your code. Now, it also allows you to have…

Claude Code Skills — Build a Better Engineering Workflow with AI-Powered Code Reviews, Security Scans, and More

Claude Code Skills — Build a Better Engineering Workflow with AI-Powered Code Reviews, Security Scans, and More

Most developers use Claude Code like a search engine — ask a question, get an…

Building an AI Voicebot for Visitor Check-In — A Practical Guide to Handling the Messy Parts

Building an AI Voicebot for Visitor Check-In — A Practical Guide to Handling the Messy Parts

Every office lobby has the same problem: a visitor walks in, nobody’s at the…

Staff Engineer Study Plan for MAANG Interviews — The Complete 12-Week Roadmap

Staff Engineer Study Plan for MAANG Interviews — The Complete 12-Week Roadmap

If you’re a Senior Engineer (L5) preparing for Staff (L6+) roles at MAANG…

How to Be a Full-Time Freelancer: Resources, Finding Clients, and Building a Sustainable Business

How to Be a Full-Time Freelancer: Resources, Finding Clients, and Building a Sustainable Business

Making the leap from full-time employment to freelancing is one of the most…

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…

Latest Posts

Claude Code Skills — Build a Better Engineering Workflow with AI-Powered Code Reviews, Security Scans, and More

Claude Code Skills — Build a Better Engineering Workflow with AI-Powered Code Reviews, Security Scans, and More

Most developers use Claude Code like a search engine — ask a question, get an…

Building an AI Voicebot for Visitor Check-In — A Practical Guide to Handling the Messy Parts

Building an AI Voicebot for Visitor Check-In — A Practical Guide to Handling the Messy Parts

Every office lobby has the same problem: a visitor walks in, nobody’s at the…

Server Security Best Practices — Complete Hardening Guide for Production Systems

Server Security Best Practices — Complete Hardening Guide for Production Systems

Every breach post-mortem tells the same story: an unpatched service, a…

Staff Engineer Study Plan for MAANG Interviews — The Complete 12-Week Roadmap

Staff Engineer Study Plan for MAANG Interviews — The Complete 12-Week Roadmap

If you’re a Senior Engineer (L5) preparing for Staff (L6+) roles at MAANG…

XSS and CSRF Explained — The Complete Guide with Real Attack Examples and Defenses

XSS and CSRF Explained — The Complete Guide with Real Attack Examples and Defenses

XSS and CSRF have been in the OWASP Top 10 for over a decade. They’re among the…

OWASP Top 10 (2021) — Every Vulnerability Explained with Code

OWASP Top 10 (2021) — Every Vulnerability Explained with Code

The OWASP Top 10 is the industry standard for web application security risks. If…