Welcome to mirror list, hosted at ThFree Co, Russian Federation.

run.php « label-updater - github.com/nextcloud/github_helper.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: ffe7502026ced16f3b89679021542a3027ed6154 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<?php

require_once 'vendor/autoload.php';

$COLOR_GRAY = "\033[0;37m";
$COLOR_RED = "\033[0;31m";
$NO_COLOR = "\033[0m";
$STRIKE_THROUGH = "\033[9m";
$BOLD = "\033[1m";

$client = new \Github\Client(
	new \Github\HttpClient\CachedHttpClient([
		'cache_dir' => '/tmp/github-api-cache'
	])
);

if(!file_exists('credentials.json')) {
	print 'Please create the file credentials.json and provide your apikey.' . PHP_EOL;
	print '  cp credentials.dist.json credentials.json' . PHP_EOL;
	exit(1);
}

$authentication = json_decode(file_get_contents('credentials.json'));

$client->authenticate($authentication->apikey, Github\Client::AUTH_HTTP_TOKEN);
$paginator = new Github\ResultPager($client);

$response = $client->getHttpClient()->get("rate_limit");
print("Remaining requests to GitHub this hour: " . \Github\HttpClient\Message\ResponseMediator::getContent($response)['rate']['remaining'] . PHP_EOL);

$repos = $argv;
$options = getopt('', ['init']);
$config = json_decode(file_get_contents('config.json'), true);

$init = isset($options['init']);
unset($repos[0]);
if ($init) {
	unset($repos[array_search('--init', $repos, true)]);
}

if (empty($repos)) {
	$repos = $config['repos'];
}

/** @var \Github\Api\Issue\Labels $api */
$api = $client->api('issue')->labels();

$organizationApi = $client->api('organization');

function getAllLabels($client, $owner, $repo) {
	$paginator = new Github\ResultPager($client);
	return $paginator->fetchAll($client->api('issue')->labels(), 'all', [$owner, $repo]);
}

$masterLabels = getAllLabels($client, $config['org'], $config['master']);

foreach ($repos as $repo) {
	$labels = getAllLabels($client, $config['org'], $repo);
	if ($init) {
		foreach ($labels as $label) {
			$api->deleteLabel($config['org'], $repo, $label['name']);
		}
		$labels = [];
	}
	print($BOLD . $config['org'] . '/' . $repo . $NO_COLOR . PHP_EOL);

	foreach ($masterLabels as $masterLabel) {
		foreach ($config['exclude'] as $exclude) {
			if (preg_match($exclude, $masterLabel['name'])) {
				print(' - ' . $config['org'] . '/' . $repo . ': ' . $STRIKE_THROUGH . $masterLabel['name'] . $NO_COLOR . ' ignoring because of patter ' . $exclude . PHP_EOL);
				continue 2;
			}
		}

		foreach ($labels as $label) {
			if ($label['name'] === $masterLabel['name']) {
				if ($label['color'] !== $masterLabel['color']) {
					print(' - ' . $config['org'] . '/' . $repo . ': Updating color of ' . $masterLabel['name'] . $NO_COLOR . PHP_EOL);
					$api->update($config['org'], $repo, $label['name'], $label['name'], $masterLabel['color']);
				} else {
					print(' - ' . $config['org'] . '/' . $repo . ': Skipping ' . $masterLabel['name'] . $NO_COLOR . PHP_EOL);
				}
				continue 2;
			}
		}

		print(' - ' . $config['org'] . '/' . $repo . ': Adding ' . $masterLabel['name'] . $NO_COLOR . PHP_EOL);
		$api->create($config['org'], $repo, [
			'name' => $masterLabel['name'],
			'color' => $masterLabel['color'],
		]);
	}
}