Skip to content

Instantly share code, notes, and snippets.

@symm
Created April 13, 2015 21:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save symm/ba69f2b715558c61b1a2 to your computer and use it in GitHub Desktop.
Save symm/ba69f2b715558c61b1a2 to your computer and use it in GitHub Desktop.
Github Label Manager
{
"require": {
"guzzlehttp/guzzle": "~5.2"
}
}
<?php
require_once(__DIR__ . '/vendor/autoload.php');
$githubOrganization = 'symm';
$authToken = 'AUTH_TOKEN_HERE'; // Create a personal access token @ https://github.com/settings/applications
$repos = [
'Geo',
'kata-poker'
];
$desiredLabels = [
'✔' => '89f786',
'✔✔' => '4be324',
'✔✔✔' => '2d9e11',
'✘' => 'ffb8b8',
'✘✘' => 'fc4e4e',
'✘✘✘' => 'ff0000',
'[A] Security' => 'ff7600',
'[A] View' => '2a4380',
'[A] Entity' => '2a4380',
'[A] Deployment' => '2a4380',
'[A] Front End' => '2a4380',
'[D] Documentation' => '01939a',
'[D] Examples' => '01939a',
'[P] Accepted' => 'ffe940',
'[P] Awaiting Action' => 'ffe940',
'[P] Design' => 'ffe940',
'[P] Implementation' => 'ffe940',
'[P] Integration' => 'ffe940',
'[P] Pending Review' => 'ffe940',
'[P] Refactor' => 'ffe940',
'[S] Blocked' => '000000',
'[S] Complete' => '000000',
'[S] Discuss' => '000000',
'[S] Duplicate' => '000000',
'[S] Fixed Elsewhere' => '000000',
'[S] Hold' => '000000',
'[S] Rejected' => '000000',
'[S] Won\'t Fix' => '000000',
'[T] User Story' => '6c8ad5',
'[T] Code Test' => '6c8ad5',
];
$client = new GuzzleHttp\Client([
'base_url' => 'https://api.github.com/',
'defaults' => [
'headers' => [
'Authorization' => 'token ' . $authToken
]
]
]);
foreach ($repos as $repo) {
echo 'Working on repo: ' . $repo . PHP_EOL;
$existingLabels = [];
$labelsToRemove = [];
$labelsToAdd = [];
$response = $client->get('repos/' . $githubOrganization . '/'. $repo . '/labels');
$response = $response->json();
foreach ($response as $label) {
$name = $label['name'];
$existingLabels[$name] = $label['color'];
}
$labelsToRemove = array_diff($existingLabels, $desiredLabels);
$labelsToAdd = array_diff($desiredLabels, $existingLabels);
foreach ($labelsToRemove as $name => $color) {
echo 'Removing Label: ' . $name . PHP_EOL;
$client->delete('repos/' . $githubOrganization . '/' . $repo .'/labels/' . $name);
}
foreach ($labelsToAdd as $name => $color) {
echo 'Adding Label: ' . $name . PHP_EOL;
$client->post('repos/' . $githubOrganization . '/'. $repo . '/labels', [
'body' => json_encode([
'name' => $name,
'color' => $color
])
]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment