While developing Mass Action Scheduler, I came up with my own custom GitHub labels to help me better organize issues and feature requests, as well as be a way to better communicate to users on the status of their feedback.

Of all the labels for Mass Action Scheduler, there’s at least 12 of them that I would like to reuse in my other projects. As of July 2019, GitHub does not provide an “easy” way to copy labels from one repository to another. However, after searching the interwebs I did eventually discover multiple ways to do it with varying levels of effort. I thought it’d be nice to share the different approaches I came across and which I ended up using.

My Favorite Solutions

My two favorite solutions are (1) my own approach using the command line and the GitHub API, and (2) some JavaScript functions that you run in your browser’s console to automate clicks on GitHub.com.

Command Line Automation with Bash

The solution I ultimately went with was a ~50 line bash script that uses curl, jq, and the GitHub API. What I liked about my custom script is that I could have it do exactly what I wanted, haha. It supports copying the label’s name, color, and description; and will create or update labels in the destination repo. The other solutions I explored would not overwrite labels in the destination org. I don’t blame them, that’s probably a safe choice. However, for my purposes I did want to update labels so that I can keep the colors and descriptions in sync across the projects if and when the labels change. Because this uses the GitHub API, you do need to provide a personal access token.

Browser Automation with JavaScript

In contrast to using the GitHub API to copy labels from one repo to another, another trick I found was simply using JavaScript in your browser’s console to automate clicks on the GitHub website.

Early in my search I came across this gist by Max Thirouin for exporting labels and this gist by Chang-Hsi Hsieh for importing labels. The original scripts didn’t support label descriptions, but folks in the comments of both gists provided updates.

Compared to the “one command and you’re done” approach of the Command Line Automation with Bash solution, the browser automation technique requires you to manually run JavaScript in your browser’s console to export labels and then run some other JavaScript to import labels. This is because the scripts work by assuming you have already navigated in your browser to the labels page of the repository whose labels you want to export from or import into. However, the tradeoff is that you don’t have to provide your GitHub password or access token to third-party code. Everything happens in your browser while already securely logged in to GitHub.

Honorable Mentions

These are the other options that I considered but ultimately didn’t use.

Probot logo

Probot Settings App

Probot apps are GitHub apps to automate and improve your workflow. One such app is the Settings app. It reads configuration from a .github/settings.yml on your default branch then applies the changes to your GitHub repo.

For example, you can specify whether the repo should be public or private, what the repo’s name and description should be, who should be collaborators, what labels to use, and much more.

The following screenshot is a snippet from .github/settings.yml that defines labels, their colors, and even how to rename a label.

However, a major caveat with the Probot Settings app is that it inherently escalates anyone with push permissions to the admin role, since they can push changes to the settings.yml file to the master branch, which will then apply changes to the repo itself.

In the end, I chose not to use the Probot Settings app due to this security concern and because I wanted an even more light-weight solution than installing a third-party app to my GitHub account.

destan/github-label-manager

GitHub Label Maker is an open source web app to create, update, delete, and copy labels from one repository to another. It’s developed by Destan Sarpkaya. At the time of writing, this app uses the GitHub REST API but only supports managing label names and colors. It has not been updated to support managing label descriptions.

The web page is easy enough to use, but I chose not to use it because I want my label descriptions and I didn’t feel comfortable entering my GitHub password into the website.

jvandemo/copy-github-labels

Copy GitHub Labels is an open source Node.js CLI module to copy labels from one repository to another. It’s developed by Jurgen Van de Moere. At the time of writing, this app uses the official GitHub Node.js library, octokit/rest.js, to interact with the GitHub API. Feature wise, the app only creates new labels, it won’t update existing labels in the destination repo. The project is well documented and has lots of examples to follow.

In the end, I chose not to use this solution because I wanted something even more light-weight than installing a bunch of node modules.

thetutlage/gh-copy-labels

GitHub Copy Labels is an open source Node.js CLI module to copy labels from one repository to another. It’s developed by Harminder Virk. At the time of writing, this app uses a Node.js GitHub library, octonode, for interacting with the GitHub API. Feature wise, the app only creates new labels, it won’t update existing labels in the destination repo.

I actually couldn’t get this app to install or work. Attempting to install the package via npm per the README gave an error about missing file build/bin.js. The demo video looks promising though and ironically uses the build/bin.js file that npm complained about not finding, so your mileage may vary.