Last active
March 2, 2018 11:02
-
-
Save pineapplemachine/71d1cadea8fedeb07de828df89ed6061 to your computer and use it in GitHub Desktop.
Copy github labels with descriptions 01/03/2018
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Usage: | |
| // This script can be used to copy the labels from one github repository to another. | |
| // Enter the source and destination repository information toward the bottom of this | |
| // source file, as well as an API access token with permission to view the source | |
| // repository and to modify the target repository. | |
| // WARNING: By default, the script will delete all the labels in the destination | |
| // repository before copying them over from the source repository. | |
| const axios = require("axios"); | |
| class Github{ | |
| constructor(options){ | |
| this.owner = options.owner; | |
| this.repository = options.repository; | |
| this.apiAccessToken = options.apiAccessToken; | |
| } | |
| getApiUrl(endpoint){ | |
| const host = `https://api.github.com`; | |
| const url = `${host}/repos/${this.owner}/${this.repository}/${endpoint}`; | |
| return `${url}?access_token=${this.apiAccessToken}`; | |
| } | |
| async getLabels(){ | |
| return (await axios({ | |
| method: "get", | |
| url: this.getApiUrl("labels"), | |
| headers: { | |
| "Accept": "application/vnd.github.symmetra-preview+json", | |
| }, | |
| })).data; | |
| } | |
| async deleteLabel(labelName){ | |
| return await axios({ | |
| method: "delete", | |
| url: this.getApiUrl(`labels/${labelName}`), | |
| headers: { | |
| "Accept": "application/vnd.github.symmetra-preview+json", | |
| }, | |
| }); | |
| } | |
| async deleteLabels(labelNames){ | |
| return await Promise.all(labelNames.map(name => this.deleteLabel(name))); | |
| } | |
| async deleteAllLabels(){ | |
| return await this.deleteLabels( | |
| (await this.getLabels()).map(label => label.name) | |
| ); | |
| } | |
| async addLabel(label){ | |
| return await axios({ | |
| method: "post", | |
| url: this.getApiUrl("labels"), | |
| headers: { | |
| "Accept": "application/vnd.github.symmetra-preview+json", | |
| }, | |
| data: label, | |
| }); | |
| } | |
| async addLabels(labels){ | |
| return await Promise.all(labels.map(label => this.addLabel(label))); | |
| } | |
| } | |
| (async () => { | |
| const source = new Github({ | |
| owner: "pineapplemachine", // Owner of repo to copy labels from | |
| repository: "higher", // Name of repo to copy labels from | |
| apiAccessToken: "[YOUR PERSONAL ACCESS TOKEN]", | |
| }); | |
| const target = new Github({ | |
| owner: "mapita", // Owner of repo to write labels to | |
| repository: "shorter", // Name of repo to write labels to | |
| apiAccessToken: "[YOUR PERSONAL ACCESS TOKEN]", | |
| }); | |
| try{ | |
| // WARNING: This deletes any and all existing labels | |
| await target.deleteAllLabels(); | |
| // Copy the labels from the source to the destination repo | |
| await target.addLabels(await source.getLabels()); | |
| }catch(error){ | |
| console.error("Encountered an error."); | |
| console.error(error); | |
| } | |
| })(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment