-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
SyncVersions.php
199 lines (160 loc) · 7.16 KB
/
SyncVersions.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
<?php
namespace AppBundle\Consumer;
use AppBundle\Entity\Repo;
use AppBundle\Entity\Version;
use AppBundle\Github\RateLimitTrait;
use AppBundle\PubSubHubbub\Publisher;
use AppBundle\Repository\RepoRepository;
use AppBundle\Repository\VersionRepository;
use Doctrine\ORM\EntityManager;
use Github\Client;
use Psr\Log\LoggerInterface;
use Swarrot\Broker\Message;
use Swarrot\Processor\ProcessorInterface;
/**
* Consumer message to sync user repo (usually happen after a successful login).
*/
class SyncVersions implements ProcessorInterface
{
use RateLimitTrait;
private $em;
private $repoRepository;
private $versionRepository;
private $pubsubhubbub;
private $logger;
private $client;
public function __construct(EntityManager $em, RepoRepository $repoRepository, VersionRepository $versionRepository, Publisher $pubsubhubbub, Client $client, LoggerInterface $logger)
{
$this->em = $em;
$this->repoRepository = $repoRepository;
$this->versionRepository = $versionRepository;
$this->pubsubhubbub = $pubsubhubbub;
$this->client = $client;
$this->logger = $logger;
}
public function process(Message $message, array $options)
{
$data = json_decode($message->getBody(), true);
$repo = $this->repoRepository->find($data['repo_id']);
if (null === $repo) {
$this->logger->error('Can not find repo', ['repo' => $data['repo_id']]);
return;
}
$this->logger->notice('Consume banditore.sync_versions message', ['repo' => $repo->getFullName()]);
$this->logger->notice('[' . $this->getRateLimits($this->client, $this->logger) . '] Check <info>' . $repo->getFullName() . '</info> … ');
try {
$nbVersions = $this->doSyncVersions($repo);
} catch (\Exception $e) {
$this->logger->error('Error while syncing repo', ['exception' => get_class($e), 'message' => $e->getMessage(), 'repo' => $repo->getFullName()]);
return;
}
// notify pubsubhubbub for that repo
if ($nbVersions > 0) {
$this->pubsubhubbub->pingHub([$data['repo_id']]);
}
$this->logger->notice('[' . $this->getRateLimits($this->client, $this->logger) . '] <comment>' . $nbVersions . '</comment> new versions for <info>' . $repo->getFullName() . '</info>');
}
/**
* Do the job to sync repo & star of a user.
*
* @param Repo $repo Repo to work on
*/
private function doSyncVersions(Repo $repo)
{
$newVersion = 0;
list($username, $repoName) = explode('/', $repo->getFullName());
// this is a simple call to retrieve at least one tag from the selected repo
// using git/refs/tags when repo has no tag throws a 404 which can't be cached
// this query return an empty array when repo has no tag and it can be cached
try {
$tags = $this->client->api('repo')->tags($username, $repoName, ['per_page' => 1, 'page' => 1]);
} catch (\Exception $e) {
$this->logger->warning('(repo/tags) <error>' . $e->getMessage() . '</error>');
return;
}
if (empty($tags)) {
return $newVersion;
}
// use git/refs/tags because tags aren't order by date creation (so we retrieve ALL tags every time …)
try {
$tags = $this->client->api('git')->tags()->all($username, $repoName);
} catch (\Exception $e) {
$this->logger->warning('(git/refs/tags) <error>' . $e->getMessage() . '</error>');
return;
}
foreach ($tags as $tag) {
// it'll be like `refs/tags/2.2.1`
$tag['name'] = str_replace('refs/tags/', '', $tag['ref']);
$version = $this->versionRepository->findOneBy([
'tagName' => $tag['name'],
'repo' => $repo->getId(),
]);
if (null !== $version) {
continue;
}
// is there an associated release?
$newRelease = [
'tag_name' => $tag['name'],
];
try {
$newRelease = $this->client->api('repo')->releases()->tag($username, $repoName, $tag['name']);
// use same key as tag to store the content of the release
$newRelease['message'] = $newRelease['body'];
} catch (\Exception $e) { // it should be `Github\Exception\RuntimeException` but I can't reproduce this exception in test :-(
// when a tag isn't a release, it'll be catched here
switch ($tag['object']['type']) {
// https://api.github.com/repos/ampproject/amphtml/git/tags/694b8cc3983f52209029605300910507bec700b4
case 'tag':
$tagInfo = $this->client->api('git')->tags()->show($username, $repoName, $tag['object']['sha']);
$newRelease += [
'name' => $tag['name'],
'prerelease' => false,
'published_at' => $tagInfo['tagger']['date'],
'message' => $tagInfo['message'],
];
break;
// https://api.github.com/repos/ampproject/amphtml/git/commits/c0a5834b32ae4b45b4bacf677c391e0f9cca82fb
case 'commit':
$commitInfo = $this->client->api('git')->commits()->show($username, $repoName, $tag['object']['sha']);
$newRelease += [
'name' => $tag['name'],
'prerelease' => false,
'published_at' => $commitInfo['author']['date'],
'message' => $commitInfo['message'],
];
}
$newRelease['message'] = $this->removePgpSignature($newRelease['message']);
}
// render markdown in plain html and use default markdown file if it fails
if (isset($newRelease['message']) && strlen(trim($newRelease['message'])) > 0) {
try {
$newRelease['message'] = $this->client->api('markdown')->render($newRelease['message'], 'gfm', $repo->getFullName());
} catch (\Exception $e) {
$this->logger->warning('<error>Failed to parse markdown: ' . $e->getMessage() . '</error>');
// it is usually a problem from the abuse detection mechanism, to avoid multiple call, we just skip to the next repo
return $newVersion;
}
}
$version = new Version($repo);
$version->hydrateFromGithub($newRelease);
$this->em->persist($version);
++$newVersion;
}
$this->em->flush();
return $newVersion;
}
/**
* Remove PGP signature from commit / tag.
*
* @param string $message
*
* @return string
*/
private function removePgpSignature($message)
{
if ($pos = stripos($message, '-----BEGIN PGP SIGNATURE-----')) {
return trim(substr($message, 0, $pos));
}
return $message;
}
}