PowerShell Automation: Execute Batch GitHub CLI Commands
uyriq
Posted on November 2, 2024
Automating Routine Tasks with a PowerShell Script
Working with repositories on GitHub, especially when there are many of them, can be tiresome and time-consuming. When I needed to change the visibility of multiple repositories and manage access rights, I decided to create a cross-platform PowerShell script, batchrepoaction.ps1, which automates routine tasks and saves time and effort.
The Idea Behind the Script
The idea of writing the script came from the need to perform repetitive actions for multiple repositories. For example, changing the repository's visibility from public to private and vice versa, or adding a collaborator. These operations, if done manually through the GitHub interface, require several clicks, reading banners, and scrolling through pages. When there are dozens or even hundreds of such repositories, the task becomes tedious and time-consuming.
In real life, people like HR managers are always interested in our public information and check our repositories. Our team leads request to add or remove access to repositories for our future or former developer colleagues. These situations also motivated me to create this script.
What the Script Does
batchrepoaction.ps1 is written in PowerShell and can work both in PowerShell 5 on Windows "out of the box" and in PowerShell Core, which is available not only on Windows but also on Linux and macOS. The main idea of the script is to get a list of repositories and process them one by one by calling the GitHub CLI utility (gh). This allows executing any GitHub CLI command in the context of working with a repository.
The user must be authorized in GitHub CLI and have the gh tool installed.
The most important parameter of the script is -action. Here’s what I wrote in the help section:
.PARAMETER action
The regular `gh` CLI command to execute e.g., "gh repo edit --visibility private", "gh repo delete"
So, the idea is that the command for gh is passed in action, which will be executed for each repository from the list.
Some gh commands can be quite long and depend on several parameters. Such commands cannot be passed in the current version of the script, so, for example, for getting a list of collaborators and adding a collaborator, I made my own shorthands instead of writing a syntax parser:
list-collaborators
add-collaborator
delete-collaborator
which can be passed in action instead of their long equivalents.
The other gh commands that support one argument are passed in their syntax, so my script is still a wrapper! Moreover, if the syntax of the commands changes a bit, as promised with the upcoming release of the gh repo edit command, it will not affect the functionality of the script, just a slightly different command will be executed for the list of repositories.
It looks like I need to conclude the explanation of the script by providing usage examples step by step:
Changing Repository Visibility
First, get a list of your repositories and save it to a text file:
.\batchrepoaction.ps1 -action "gh repo list" -outputfile repofile.txt
You can now edit repofile.txt, leaving only the repositories needed for the operation.
Run the command to change the visibility of all listed repositories:
.\batchrepoaction.ps1 -repofile repofile.txt -action "gh repo edit --visibility private"
If the number of repositories to be processed is not so large, you can use the -repolist
parameter, listing the required repositories in quotes, separated by commas:
powershell
.\batchrepoaction.ps1 -repolist "user/repo1,user/repo2" -action "gh repo edit --visibility private"
Adding a Collaborator to Several Repositories
To add a collaborator to all repositories, use the command:
.\batchrepoaction.ps1 `-repolist` "user/repo1,user/repo2" -collab_permission "push" -collaborator "anygithubusername"
As they say in advertisements, emphasizing the meaning of the word "can", the batchrepoaction.ps1 script can simplify the management of multiple repositories on GitHub by automating routine tasks and allowing you to focus on more important aspects of development. I hope this script will be useful, saving your time and nerves.
Posted on November 2, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.