Last active
March 28, 2023 17:39
-
-
Save guibranco/4a10858cf5815209734512b8bdf95996 to your computer and use it in GitHub Desktop.
Create labels on specific repository using Postman tests tab
This file contains 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
const ghToken = pm.globals.get("GH_PAT"); // environment variable with GitHub PAT (Personal Access Token) with repo:write. | |
const organization = pm.collectionVariables.get("org"); // environment variable with GitHub username/organization name. | |
const repository = pm.collectionVariables.get("repository"); // environment variable with GitHub repository name. | |
// ⬆️💡 The variables above you can paste directly in the script, if you don't want to use environment variables in Postman | |
// No need to change below this line ⬇️⛔ | |
const authorizationHeader = `Authorization: Bearer ${ghToken}`; | |
const originalLabels = "https://api.github.com/repos/dotnetdevbr/vagas/labels"; // The source repository with existins labels | |
const createLabel = `https://api.github.com/repos/${organization}/${repository}/labels`; // The destination repository | |
pm.sendRequest( // ⏬ Get the existing labels of source repository | |
{ | |
url: originalLabels, | |
method: "GET", | |
header: authorizationHeader | |
}, | |
function (err, res) { | |
const labels = res.json(); | |
for (let i = 0; i < labels.length; i++) { // 🔂 Iterate over labels of source repository | |
const label = labels[i]; | |
pm.sendRequest( // ⏫ Create the new label on the destination repository | |
{ | |
url: createLabel, | |
method: "POST", | |
header: authorizationHeader, | |
body: JSON.stringify({ | |
name: label.name, | |
color: label.color, | |
description: label.description, | |
}), | |
}, | |
function (err, res) { | |
console.log(res); | |
} | |
); | |
} | |
} | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How to use:
org
- the organization name (or the username of the owner of the repository)repository
- the repository nameGH_PAT
- GitHub Personal Access Token (with write scope to desired org/repo)test
tab on the requestdotnetdevbr/vagas
repository will be copied to yours repository (with name, description and color properties)