Skip to content

Instantly share code, notes, and snippets.

@guibranco
Last active March 28, 2023 17:39
Show Gist options
  • Save guibranco/4a10858cf5815209734512b8bdf95996 to your computer and use it in GitHub Desktop.
Save guibranco/4a10858cf5815209734512b8bdf95996 to your computer and use it in GitHub Desktop.
Create labels on specific repository using Postman tests tab
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);
}
);
}
}
);
@guibranco
Copy link
Author

guibranco commented Mar 20, 2023

How to use:

  • Create a Postman request to GitHub (or anything else, doesn't matter the main request)
  • Set up environment variables (or replace directly in the script)
    • org - the organization name (or the username of the owner of the repository)
    • repository - the repository name
    • GH_PAT - GitHub Personal Access Token (with write scope to desired org/repo)
  • Copy the above code to test tab on the request
  • Run the request
  • All the labels from the dotnetdevbr/vagas repository will be copied to yours repository (with name, description and color properties)

@guibranco
Copy link
Author

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment