Automating your Github using Shell Script

Rebecca Oliveira
4 min readFeb 6, 2019

--

Github

The problem

A few days ago I had a “small problem” with my Github account…I’ll explain very quickly, to understanding how I discover Shell Script.

My Github account was a little bit old (not so old), was created when I used to be a project manager and zero codes. In 2018, when I started to study programming languages, I back to use, now to create my repositories and codes.

So last week I decided to format my computer, with confidence that all the things that I did were completely safe in my Github. But, after to reboot my computer, trying to access my Github, started a series of 💩:

  1. Was necessary to insert Two Factor Authentication (Ohh nooo! I didn’t remember that I had activated this 2FA)
  2. The cellphone that I used in that time (2012) it’s not the same that I use today and… of course, I don’t have the 2FA of my Github here.
  3. Well, there’s a Recovery Codes, and where it is? Yes…I don’t know, probably I saved in the machine that I worked in my last job… I lost!
  4. I did contact with the support and the answer: “Sorry, there’s nothing that we can do. It’s impossible to cancel 2FA of your account, it’s our recurity rules.”

Ok… my Github account was blocked. Fortunately, I had only public repositories — a lesson for you, beginner as a developer like me and had some private repositories, always check if everything is safe.

Solution: To create a new Github account and to clone all repositories from my old account to this new.

So, I call my Twitter’s friends:

Frieeeennnddd…..help meeeee!

Thanks to Our Lady of the Internet and Programming Communities the Celso Fernandes introduce me Shell Script, and I didn’t need to clone all repositories manually.

But, what is a Shell Script?

Shell Script is an instruction automation tool, which executes a sequence of commands.

It is the shell that interprets the command line entered by the user on the terminal and calls the desired programs.

Because these are the characteristics of a programming language, shell is a very powerful tool for developing quick scripts and scripts to automate day-to-day tasks.

I met the blog of Aurelio, he is really good with Shell Script, wrote a book about this technology — in Portuguese (and I have this book at home — thanks to my husband to bought this book a few years ago…rsrsrs), is possible to solve a lot of doubts about this subject in his website.

Creating a repository using terminal

Let’s start to create repositories automatically.

Using terminal is possible to create a repository, using the Github API:

curl -u 'username' https://api.github.com/user/repos -d '{"name":"repository name"}'

Where 'username' is your Github username, and in the field"repository name" is to include the name of the repository to be created. In sequence, it is necessary to fill with your Github password, and it's ready your new repository.

But, how can I do if I have to create a 100 repositories? It’s here where Sell Script starts:

All the files in this following explanation were created inside the same directory. Well, let’s create a file with the name .secrets.shthat will be saved our Github user and password.

#!/bin/bashexport GITHUB_PASSWORD='AdventureTime'export GITHUB_USERNAME='Finn'

Here, we create a new file .sh named of create_repo.sh with the command to create a new repository, importing user and password.

#!/bin/bash curlcurl -u "$GITHUB_USERNAME:$GITHUB_PASSWORD" https://api.github.com/user/repos -d "{\"name\":\"${1}\"}"

And to create many repositories, and use another file.sh withthe name ofcreate_repos.sh:

#!/bin/bashfor repo in repo1 repo2 repo2
do
./create_repo.sh $repo
done

Files created, run in the terminal this two command lines:

$ source ~/.secrets.sh 
$ ./create_repos.sh

Cloning repositories from the same account

In my case, I had to clone all repositories from my old account to the new one.

After to create the new repositories (I choose the same name for my new account), I add a new file clone_repos.sh to clone the repositories of my old account.

#!/bin/bashAPP_ROOT=`pwd`
for repo in repoA repoB repoC repoD repoE repoF
do
cd ~/src
git clone https://github.com/PrincessBubblegum/${repo}.git
cd ~/src/${repo}
git remote rm origin
git remote add origin git@github.com:Finn/${repo}.git
git push --set-upstream origin master
done
cd $APP_ROOT

In this part of the code for repo in repoA repoB... is necessary to insert the same name of the repositories that you are cloning, this example above, the name of the repositories will be cloned from ‘PrincessBubblegum’ account.

File create, now run in the terminal:

$ ./clone_repos.sh

And your Github will be with all cloned repositories, using only one line of code.

Lessons learned

Some lessons that I learned in this process:

  1. You have to understand your work tools: The computer, the files, your Github. Was good for me to know that I had Two Factor Authentication in my Github account that I had never imagined before. And it exists public and private keys, and to save Recovery Codes in a safe place is very important.
  2. If you are doing some repetitive job in computing, for example: creating many repositories manually, or using a lot Crtl C + Ctrl V, stop and do research, because for sure someone created a way to automate this process.
  3. Não subestime sua capacidade, mesmo sendo iniciante em programação. Pesquise, leia, pergunte para desenvolvedores experientes, participe de comunidades, é assim que se constrói conhecimento.

And you who read until here, do you use Shell Script to automate something? How do you usually automate your tasks? Share with us here. :D

Você pode ler este artigo em Português aqui!

--

--