Skip to content

Commit 0fdf3e9

Browse files
committed
Merge remote-tracking branch 'origin/master'
2 parents 2b39f0e + 8a1eb29 commit 0fdf3e9

File tree

17 files changed

+255
-195
lines changed

17 files changed

+255
-195
lines changed

administrator/components/com_patchtester/PatchTester/GitHub/GitHub.php

Lines changed: 123 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,71 @@ public function __construct(Registry $options = null, Http $client = null)
5151
$this->client = $client ?: HttpFactory::getHttp($options);
5252
}
5353

54+
/**
55+
* Get the HTTP client for this connector.
56+
*
57+
* @return Http
58+
*
59+
* @since 3.0.0
60+
*/
61+
public function getClient()
62+
{
63+
return $this->client;
64+
}
65+
66+
/**
67+
* Get the diff for a pull request.
68+
*
69+
* @param string $user The name of the owner of the GitHub repository.
70+
* @param string $repo The name of the GitHub repository.
71+
* @param integer $pullId The pull request number.
72+
*
73+
* @return Response
74+
*
75+
* @since 3.0.0
76+
*/
77+
public function getDiffForPullRequest($user, $repo, $pullId)
78+
{
79+
// Build the request path.
80+
$path = "/repos/$user/$repo/pulls/" . (int) $pullId;
81+
82+
// Build the request headers.
83+
$headers = array('Accept' => 'application/vnd.github.diff');
84+
85+
$prepared = $this->prepareRequest($path, 0, 0, $headers);
86+
87+
return $this->processResponse(
88+
$this->client->get($prepared['url'], $prepared['headers'])
89+
);
90+
}
91+
92+
/**
93+
* Method to build and return a full request URL for the request.
94+
*
95+
* This method will add appropriate pagination details if necessary and also prepend the API url to have a complete URL for the request.
96+
*
97+
* @param string $path Path to process
98+
* @param integer $page Page to request
99+
* @param integer $limit Number of results to return per page
100+
* @param array $headers The headers to send with the request
101+
*
102+
* @return array Associative array containing the prepared URL and request headers
103+
*
104+
* @since 3.0.0
105+
*/
106+
protected function prepareRequest($path, $page = 0, $limit = 0,
107+
array $headers = array()
108+
) {
109+
$url = $this->fetchUrl($path, $page, $limit);
110+
111+
if ($token = $this->options->get('gh.token', false))
112+
{
113+
$headers['Authorization'] = "token $token";
114+
}
115+
116+
return array('url' => $url, 'headers' => $headers);
117+
}
118+
54119
/**
55120
* Build and return a full request URL.
56121
*
@@ -107,39 +172,32 @@ protected function fetchUrl($path, $page = 0, $limit = 0)
107172
}
108173

109174
/**
110-
* Get the HTTP client for this connector.
111-
*
112-
* @return Http
113-
*
114-
* @since 3.0.0
115-
*/
116-
public function getClient()
117-
{
118-
return $this->client;
119-
}
120-
121-
/**
122-
* Get the diff for a pull request.
175+
* Process the response and return it.
123176
*
124-
* @param string $user The name of the owner of the GitHub repository.
125-
* @param string $repo The name of the GitHub repository.
126-
* @param integer $pullId The pull request number.
177+
* @param Response $response The response.
178+
* @param integer $expectedCode The expected response code.
127179
*
128180
* @return Response
129181
*
130182
* @since 3.0.0
183+
* @throws Exception\UnexpectedResponse
131184
*/
132-
public function getDiffForPullRequest($user, $repo, $pullId)
185+
protected function processResponse(Response $response, $expectedCode = 200)
133186
{
134-
// Build the request path.
135-
$path = "/repos/$user/$repo/pulls/" . (int) $pullId;
136-
137-
// Build the request headers.
138-
$headers = array('Accept' => 'application/vnd.github.diff');
187+
// Validate the response code.
188+
if ($response->code != $expectedCode)
189+
{
190+
// Decode the error response and throw an exception.
191+
$body = json_decode($response->body);
192+
$error = isset($body->error) ? $body->error
193+
: (isset($body->message) ? $body->message : 'Unknown Error');
139194

140-
$prepared = $this->prepareRequest($path, 0, 0, $headers);
195+
throw new Exception\UnexpectedResponse(
196+
$response, $error, $response->code
197+
);
198+
}
141199

142-
return $this->processResponse($this->client->get($prepared['url'], $prepared['headers']));
200+
return $response;
143201
}
144202

145203
/**
@@ -168,7 +226,9 @@ public function getFileContents($user, $repo, $path, $ref = null)
168226
$prepared['url'] = (string) $url;
169227
}
170228

171-
return $this->processResponse($this->client->get($prepared['url'], $prepared['headers']));
229+
return $this->processResponse(
230+
$this->client->get($prepared['url'], $prepared['headers'])
231+
);
172232
}
173233

174234
/**
@@ -189,7 +249,9 @@ public function getFilesForPullRequest($user, $repo, $pullId)
189249

190250
$prepared = $this->prepareRequest($path);
191251

192-
return $this->processResponse($this->client->get($prepared['url'], $prepared['headers']));
252+
return $this->processResponse(
253+
$this->client->get($prepared['url'], $prepared['headers'])
254+
);
193255
}
194256

195257
/**
@@ -206,9 +268,36 @@ public function getFilesForPullRequest($user, $repo, $pullId)
206268
*/
207269
public function getOpenIssues($user, $repo, $page = 0, $limit = 0)
208270
{
209-
$prepared = $this->prepareRequest("/repos/$user/$repo/issues", $page, $limit);
271+
$prepared = $this->prepareRequest(
272+
"/repos/$user/$repo/issues", $page, $limit
273+
);
274+
275+
return $this->processResponse(
276+
$this->client->get($prepared['url'], $prepared['headers'])
277+
);
278+
}
279+
280+
/**
281+
* Get a list of the open pull requests for a repository.
282+
*
283+
* @param string $user The name of the owner of the GitHub repository.
284+
* @param string $repo The name of the GitHub repository.
285+
* @param integer $page The page number from which to get items.
286+
* @param integer $limit The number of items on a page.
287+
*
288+
* @return Response
289+
*
290+
* @since 3.0.0
291+
*/
292+
public function getOpenPulls($user, $repo, $page = 0, $limit = 0)
293+
{
294+
$prepared = $this->prepareRequest(
295+
"/repos/$user/$repo/pulls", $page, $limit
296+
);
210297

211-
return $this->processResponse($this->client->get($prepared['url'], $prepared['headers']));
298+
return $this->processResponse(
299+
$this->client->get($prepared['url'], $prepared['headers'])
300+
);
212301
}
213302

214303
/**
@@ -244,7 +333,9 @@ public function getPullRequest($user, $repo, $pullId)
244333

245334
$prepared = $this->prepareRequest($path);
246335

247-
return $this->processResponse($this->client->get($prepared['url'], $prepared['headers']));
336+
return $this->processResponse(
337+
$this->client->get($prepared['url'], $prepared['headers'])
338+
);
248339
}
249340

250341
/**
@@ -258,59 +349,9 @@ public function getRateLimit()
258349
{
259350
$prepared = $this->prepareRequest('/rate_limit');
260351

261-
return $this->processResponse($this->client->get($prepared['url'], $prepared['headers']));
262-
}
263-
264-
/**
265-
* Process the response and return it.
266-
*
267-
* @param Response $response The response.
268-
* @param integer $expectedCode The expected response code.
269-
*
270-
* @return Response
271-
*
272-
* @since 3.0.0
273-
* @throws Exception\UnexpectedResponse
274-
*/
275-
protected function processResponse(Response $response, $expectedCode = 200)
276-
{
277-
// Validate the response code.
278-
if ($response->code != $expectedCode)
279-
{
280-
// Decode the error response and throw an exception.
281-
$body = json_decode($response->body);
282-
$error = isset($body->error) ? $body->error : (isset($body->message) ? $body->message : 'Unknown Error');
283-
284-
throw new Exception\UnexpectedResponse($response, $error, $response->code);
285-
}
286-
287-
return $response;
288-
}
289-
290-
/**
291-
* Method to build and return a full request URL for the request.
292-
*
293-
* This method will add appropriate pagination details if necessary and also prepend the API url to have a complete URL for the request.
294-
*
295-
* @param string $path Path to process
296-
* @param integer $page Page to request
297-
* @param integer $limit Number of results to return per page
298-
* @param array $headers The headers to send with the request
299-
*
300-
* @return array Associative array containing the prepared URL and request headers
301-
*
302-
* @since 3.0.0
303-
*/
304-
protected function prepareRequest($path, $page = 0, $limit = 0, array $headers = array())
305-
{
306-
$url = $this->fetchUrl($path, $page, $limit);
307-
308-
if ($token = $this->options->get('gh.token', false))
309-
{
310-
$headers['Authorization'] = "token $token";
311-
}
312-
313-
return array('url' => $url, 'headers' => $headers);
352+
return $this->processResponse(
353+
$this->client->get($prepared['url'], $prepared['headers'])
354+
);
314355
}
315356

316357
/**

administrator/components/com_patchtester/PatchTester/Model/PullsModel.php

Lines changed: 46 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -508,7 +508,7 @@ public function requestFromGithub($page)
508508
// TODO - Option to configure the batch size
509509
$batchSize = 100;
510510

511-
$pullsResponse = Helper::initializeGithub()->getOpenIssues(
511+
$pullsResponse = Helper::initializeGithub()->getOpenPulls(
512512
$this->getState()->get('github_user'),
513513
$this->getState()->get('github_repo'),
514514
$page,
@@ -573,59 +573,56 @@ public function requestFromGithub($page)
573573

574574
foreach ($pulls as $pull)
575575
{
576-
if (isset($pull->pull_request))
577-
{
578-
// Check if this PR is RTC and has a `PR-` branch label
579-
$isRTC = false;
580-
$isNPM = false;
581-
$branch = '';
576+
// Check if this PR is RTC and has a `PR-` branch label
577+
$isRTC = false;
578+
$isNPM = false;
579+
$branch = '';
582580

583-
foreach ($pull->labels as $label)
581+
foreach ($pull->labels as $label)
582+
{
583+
if (strtolower($label->name) === 'rtc')
584584
{
585-
if (strtolower($label->name) === 'rtc')
586-
{
587-
$isRTC = true;
588-
}
589-
elseif (strpos($label->name, 'PR-') === 0)
590-
{
591-
$branch = substr($label->name, 3);
592-
}
593-
elseif (in_array(
594-
strtolower($label->name),
595-
['npm resource changed', 'composer dependency changed'],
596-
true
597-
))
598-
{
599-
$isNPM = true;
600-
}
601-
602-
$labels[] = implode(
603-
',',
604-
[
605-
(int) $pull->number,
606-
$this->getDb()->quote($label->name),
607-
$this->getDb()->quote($label->color),
608-
]
609-
);
585+
$isRTC = true;
586+
}
587+
elseif (strpos($label->name, 'PR-') === 0)
588+
{
589+
$branch = substr($label->name, 3);
590+
}
591+
elseif (in_array(
592+
strtolower($label->name),
593+
['npm resource changed', 'composer dependency changed'],
594+
true
595+
))
596+
{
597+
$isNPM = true;
610598
}
611599

612-
// Build the data object to store in the database
613-
$pullData = [
614-
(int) $pull->number,
615-
$this->getDb()->quote(
616-
HTMLHelper::_('string.truncate', $pull->title, 150)
617-
),
618-
$this->getDb()->quote(
619-
HTMLHelper::_('string.truncate', $pull->body, 100)
620-
),
621-
$this->getDb()->quote($pull->pull_request->html_url),
622-
(int) $isRTC,
623-
(int) $isNPM,
624-
$this->getDb()->quote($branch),
625-
];
626-
627-
$data[] = implode(',', $pullData);
600+
$labels[] = implode(
601+
',',
602+
[
603+
(int) $pull->number,
604+
$this->getDb()->quote($label->name),
605+
$this->getDb()->quote($label->color),
606+
]
607+
);
628608
}
609+
610+
// Build the data object to store in the database
611+
$pullData = [
612+
(int) $pull->number,
613+
$this->getDb()->quote(
614+
HTMLHelper::_('string.truncate', $pull->title, 150)
615+
),
616+
$this->getDb()->quote(
617+
HTMLHelper::_('string.truncate', $pull->body, 100)
618+
),
619+
$this->getDb()->quote($pull->html_url),
620+
(int) $isRTC,
621+
(int) $isNPM,
622+
$this->getDb()->quote($branch),
623+
];
624+
625+
$data[] = implode(',', $pullData);
629626
}
630627

631628
// If there are no pulls to insert then bail, assume we're finished

0 commit comments

Comments
 (0)