/
SpressGithubMetadataPlugin.php
144 lines (123 loc) · 5.79 KB
/
SpressGithubMetadataPlugin.php
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<?php
use Yosymfony\Spress\Core\Plugin\PluginInterface;
use Yosymfony\Spress\Core\Plugin\EventSubscriber;
use Yosymfony\Spress\Core\Plugin\Event\EnvironmentEvent;
class SpressGithubMetadataPlugin implements PluginInterface
{
private $io;
public function initialize(EventSubscriber $subscriber)
{
$subscriber->addEventListener('spress.start', 'onStart');
}
public function getMetas()
{
return [
'name' => 'spress/github-metadata-plugin',
'description' => 'Github metadata of a repository',
'author' => 'Victor Puertas',
'license' => 'MIT',
];
}
public function onStart(EnvironmentEvent $event)
{
$io = $event->getIO();
$configValues = $event->getConfigValues();
$io->write('Getting Github metadata...');
$repository = $this->getRepositoryComponents($configValues['repository']);
if (2 != count($repository)) {
$io->write('<error>Invalid repository name.</error>');
return;
}
$client = new \Github\Client();
$metadataRepo = $client->api('repo')->show($repository['vendor'], $repository['name']);
$metadata = [
'name' => $metadataRepo['name'],
'full_name' => $metadataRepo['full_name'],
'description' => $metadataRepo['description'],
'fork' => $metadataRepo['fork'],
'html_url' => $metadataRepo['html_url'],
'clone_url' => $metadataRepo['clone_url'],
'git_url' => $metadataRepo['git_url'],
'ssh_url' => $metadataRepo['ssh_url'],
'mirror_url' => $metadataRepo['mirror_url'],
'homepage' => $metadataRepo['homepage'],
'forks_count' => $metadataRepo['forks_count'],
'stargazers_count' => $metadataRepo['stargazers_count'],
'watchers_count' => $metadataRepo['watchers_count'],
'size' => $metadataRepo['size'],
'default_branch' => $metadataRepo['default_branch'],
'open_issues_count' => $metadataRepo['open_issues_count'],
'has_issues' => $metadataRepo['has_issues'],
'has_wiki' => $metadataRepo['has_wiki'],
'has_pages' => $metadataRepo['has_pages'],
'has_downloads' => $metadataRepo['has_downloads'],
'pushed_at' => $metadataRepo['pushed_at'],
'created_at' => $metadataRepo['created_at'],
'updated_at' => $metadataRepo['updated_at'],
'owner' => [
'login' => $metadataRepo['owner']['login'],
'avatar_url' => $metadataRepo['owner']['avatar_url'],
'gravatar_id' => $metadataRepo['owner']['gravatar_id'],
'html_url' => $metadataRepo['owner']['html_url'],
],
'organization' => [],
'source' => [],
'contributors' => [],
];
if (isset($metadataRepo['organization'])) {
$metadata['organization'] = [
'login' => $metadataRepo['organization']['login'],
'avatar_url' => $metadataRepo['organization']['avatar_url'],
'html_url' => $metadataRepo['organization']['html_url'],
];
}
if (isset($metadataRepo['source'])) {
$metadata['source'] = [
'name' => $metadataRepo['source']['name'],
'full_name' => $metadataRepo['source']['full_name'],
'description' => $metadataRepo['source']['description'],
'html_url' => $metadataRepo['source']['html_url'],
'clone_url' => $metadataRepo['source']['clone_url'],
'git_url' => $metadataRepo['source']['git_url'],
'ssh_url' => $metadataRepo['source']['ssh_url'],
'svn_url' => $metadataRepo['source']['svn_url'],
'mirror_url' => $metadataRepo['source']['mirror_url'],
'homepage' => $metadataRepo['source']['homepage'],
'forks_count' => $metadataRepo['source']['forks_count'],
'stargazers_count' => $metadataRepo['source']['stargazers_count'],
'watchers_count' => $metadataRepo['source']['watchers_count'],
'size' => $metadataRepo['source']['size'],
'default_branch' => $metadataRepo['source']['default_branch'],
'open_issues_count' => $metadataRepo['source']['open_issues_count'],
'has_issues' => $metadataRepo['source']['has_issues'],
'has_wiki' => $metadataRepo['source']['has_wiki'],
'has_pages' => $metadataRepo['source']['has_pages'],
'has_downloads' => $metadataRepo['source']['has_downloads'],
'pushed_at' => $metadataRepo['source']['pushed_at'],
'created_at' => $metadataRepo['source']['created_at'],
'updated_at' => $metadataRepo['source']['updated_at'],
];
}
$contributors = $client->api('repo')->contributors($repository['vendor'], $repository['name']);
foreach ($contributors as $contributor) {
$metadata['contributors'][] = [
'login' => $contributor['login'],
'avatar_url' => $contributor['avatar_url'],
'html_url' => $contributor['html_url'],
'type' => $contributor['type'],
'contributions' => $contributor['contributions'],
];
}
$configValues['github'] = $metadata;
$event->setConfigValues($configValues);
}
private function getRepositoryComponents($name)
{
$result = [];
if (preg_match('/^([a-z0-9_.-]+)\/([a-z0-9_.-]+)$/i', $name, $matches) && 3 == count($matches)) {
$result['vendor'] = $matches[1];
$result['name'] = $matches[2];
}
return $result;
}
}