Skip to content

Commit 003b9c5

Browse files
author
Michael Babker
committed
Finish updating rate limit checks
1 parent 4a555d1 commit 003b9c5

File tree

2 files changed

+88
-84
lines changed

2 files changed

+88
-84
lines changed

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

Lines changed: 83 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -126,122 +126,123 @@ public function apply($id)
126126
{
127127
// Get the Github object
128128
$github = Helper::initializeGithub();
129+
$rate = $github->authorization->getRateLimit();
129130

130-
// Only act if there are API hits remaining
131-
if ($github->authorization->getRateLimit()->rate->remaining > 0)
131+
// If over the API limit, we can't build this list
132+
if ($rate->resources->core->remaining == 0)
132133
{
133-
$pull = $github->pulls->get($this->getState()->get('github_user'), $this->getState()->get('github_repo'), $id);
134+
throw new \RuntimeException(
135+
\JText::sprintf('COM_PATCHTESTER_API_LIMIT_LIST', \JFactory::getDate($rate->resources->core->reset))
136+
);
137+
}
134138

135-
if (is_null($pull->head->repo))
136-
{
137-
throw new \RuntimeException(\JText::_('COM_PATCHTESTER_REPO_IS_GONE'));
138-
}
139+
$pull = $github->pulls->get($this->getState()->get('github_user'), $this->getState()->get('github_repo'), $id);
139140

140-
// Set up the JHttp object
141-
$options = new Registry;
142-
$options->set('userAgent', 'JPatchTester/2.0');
143-
$options->set('timeout', 120);
141+
if (is_null($pull->head->repo))
142+
{
143+
throw new \RuntimeException(\JText::_('COM_PATCHTESTER_REPO_IS_GONE'));
144+
}
144145

145-
// Make sure we can use the cURL driver
146-
$driver = \JHttpFactory::getAvailableDriver($options, 'curl');
146+
// Set up the JHttp object
147+
$options = new Registry;
148+
$options->set('userAgent', 'JPatchTester/2.0');
149+
$options->set('timeout', 120);
147150

148-
if (!($driver instanceof \JHttpTransportCurl))
149-
{
150-
throw new \RuntimeException('Cannot use the PHP cURL adapter in this environment, cannot use patchtester', 500);
151-
}
151+
// Make sure we can use the cURL driver
152+
$driver = \JHttpFactory::getAvailableDriver($options, 'curl');
152153

153-
$transport = new \JHttp($options, $driver);
154+
if (!($driver instanceof \JHttpTransportCurl))
155+
{
156+
throw new \RuntimeException('Cannot use the PHP cURL adapter in this environment, cannot use patchtester', 500);
157+
}
154158

155-
$patch = $transport->get($pull->diff_url)->body;
159+
$transport = new \JHttp($options, $driver);
156160

157-
$files = $this->parsePatch($patch);
161+
$patch = $transport->get($pull->diff_url)->body;
158162

159-
if (!$files)
160-
{
161-
// TODO - Should be a better way to enqueue messages without needing the application...
162-
\JFactory::getApplication()->enqueueMessage(JText::_('COM_PATCHTESTER_NO_FILES_TO_PATCH', 'message'));
163+
$files = $this->parsePatch($patch);
164+
165+
if (!$files)
166+
{
167+
// TODO - Should be a better way to enqueue messages without needing the application...
168+
\JFactory::getApplication()->enqueueMessage(JText::_('COM_PATCHTESTER_NO_FILES_TO_PATCH', 'message'));
169+
170+
return true;
171+
}
163172

164-
return true;
173+
foreach ($files as $file)
174+
{
175+
if ($file->action == 'deleted' && !file_exists(JPATH_ROOT . '/' . $file->old))
176+
{
177+
throw new \RuntimeException(sprintf(\JText::_('COM_PATCHTESTER_FILE_DELETED_DOES_NOT_EXIST_S'), $file->old));
165178
}
166179

167-
foreach ($files as $file)
180+
if ($file->action == 'added' || $file->action == 'modified')
168181
{
169-
if ($file->action == 'deleted' && !file_exists(JPATH_ROOT . '/' . $file->old))
182+
// If the backup file already exists, we can't apply the patch
183+
if (file_exists(JPATH_COMPONENT . '/backups/' . md5($file->new) . '.txt'))
170184
{
171-
throw new \RuntimeException(sprintf(\JText::_('COM_PATCHTESTER_FILE_DELETED_DOES_NOT_EXIST_S'), $file->old));
185+
throw new \RuntimeException(sprintf(\JText::_('COM_PATCHTESTER_CONFLICT_S'), $file->new));
172186
}
173187

174-
if ($file->action == 'added' || $file->action == 'modified')
188+
if ($file->action == 'modified' && !file_exists(JPATH_ROOT . '/' . $file->old))
175189
{
176-
// If the backup file already exists, we can't apply the patch
177-
if (file_exists(JPATH_COMPONENT . '/backups/' . md5($file->new) . '.txt'))
178-
{
179-
throw new \RuntimeException(sprintf(\JText::_('COM_PATCHTESTER_CONFLICT_S'), $file->new));
180-
}
181-
182-
if ($file->action == 'modified' && !file_exists(JPATH_ROOT . '/' . $file->old))
183-
{
184-
throw new \RuntimeException(sprintf(\JText::_('COM_PATCHTESTER_FILE_MODIFIED_DOES_NOT_EXIST_S'), $file->old));
185-
}
190+
throw new \RuntimeException(sprintf(\JText::_('COM_PATCHTESTER_FILE_MODIFIED_DOES_NOT_EXIST_S'), $file->old));
191+
}
186192

187-
$url = 'https://raw.github.com/' . urlencode($pull->head->user->login) . '/' . urlencode($pull->head->repo->name) . '/' . urlencode($pull->head->ref) . '/' . $file->new;
193+
$url = 'https://raw.github.com/' . urlencode($pull->head->user->login) . '/' . urlencode($pull->head->repo->name) . '/' . urlencode($pull->head->ref) . '/' . $file->new;
188194

189-
$file->body = $transport->get($url)->body;
190-
}
195+
$file->body = $transport->get($url)->body;
191196
}
197+
}
192198

193-
jimport('joomla.filesystem.file');
199+
jimport('joomla.filesystem.file');
194200

195-
// At this point, we have ensured that we have all the new files and there are no conflicts
196-
foreach ($files as $file)
201+
// At this point, we have ensured that we have all the new files and there are no conflicts
202+
foreach ($files as $file)
203+
{
204+
// We only create a backup if the file already exists
205+
if ($file->action == 'deleted' || (file_exists(JPATH_ROOT . '/' . $file->new) && $file->action == 'modified'))
197206
{
198-
// We only create a backup if the file already exists
199-
if ($file->action == 'deleted' || (file_exists(JPATH_ROOT . '/' . $file->new) && $file->action == 'modified'))
207+
if (!\JFile::copy(\JPath::clean(JPATH_ROOT . '/' . $file->old), JPATH_COMPONENT . '/backups/' . md5($file->old) . '.txt'))
200208
{
201-
if (!\JFile::copy(\JPath::clean(JPATH_ROOT . '/' . $file->old), JPATH_COMPONENT . '/backups/' . md5($file->old) . '.txt'))
202-
{
203-
throw new \RuntimeException(
204-
sprintf('Can not copy file %s to %s', JPATH_ROOT . '/' . $file->old, JPATH_COMPONENT . '/backups/' . md5($file->old) . '.txt')
205-
);
206-
}
209+
throw new \RuntimeException(
210+
sprintf('Can not copy file %s to %s', JPATH_ROOT . '/' . $file->old, JPATH_COMPONENT . '/backups/' . md5($file->old) . '.txt')
211+
);
207212
}
213+
}
208214

209-
switch ($file->action)
210-
{
211-
case 'modified':
212-
case 'added':
213-
if (!\JFile::write(\JPath::clean(JPATH_ROOT . '/' . $file->new), $file->body))
214-
{
215-
throw new \RuntimeException(sprintf('Can not write the file: %s', JPATH_ROOT . '/' . $file->new));
216-
}
215+
switch ($file->action)
216+
{
217+
case 'modified':
218+
case 'added':
219+
if (!\JFile::write(\JPath::clean(JPATH_ROOT . '/' . $file->new), $file->body))
220+
{
221+
throw new \RuntimeException(sprintf('Can not write the file: %s', JPATH_ROOT . '/' . $file->new));
222+
}
217223

218-
break;
224+
break;
219225

220-
case 'deleted':
221-
if (!\JFile::delete(\JPath::clean(JPATH_ROOT . '/' . $file->old)))
222-
{
223-
throw new \RuntimeException(sprintf('Can not delete the file: %s', JPATH_ROOT . '/' . $file->old));
224-
}
226+
case 'deleted':
227+
if (!\JFile::delete(\JPath::clean(JPATH_ROOT . '/' . $file->old)))
228+
{
229+
throw new \RuntimeException(sprintf('Can not delete the file: %s', JPATH_ROOT . '/' . $file->old));
230+
}
225231

226-
break;
227-
}
232+
break;
228233
}
234+
}
229235

230-
$table = \JTable::getInstance('TestsTable', '\\PatchTester\\Table\\');
231-
$table->pull_id = $pull->number;
232-
$table->data = json_encode($files);
233-
$table->patched_by = \JFactory::getUser()->id;
234-
$table->applied = 1;
235-
$table->applied_version = JVERSION;
236+
$table = \JTable::getInstance('TestsTable', '\\PatchTester\\Table\\');
237+
$table->pull_id = $pull->number;
238+
$table->data = json_encode($files);
239+
$table->patched_by = \JFactory::getUser()->id;
240+
$table->applied = 1;
241+
$table->applied_version = JVERSION;
236242

237-
if (!$table->store())
238-
{
239-
throw new \RuntimeException($table->getError());
240-
}
241-
}
242-
else
243+
if (!$table->store())
243244
{
244-
throw new \RuntimeException(\JText::sprintf('COM_PATCHTESTER_API_LIMIT_ACTION', \JFactory::getDate($this->rate->reset)));
245+
throw new \RuntimeException($table->getError());
245246
}
246247

247248
return true;

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -308,11 +308,14 @@ public function requestFromGithub()
308308
{
309309
// Get the Github object
310310
$github = Helper::initializeGithub();
311+
$rate = $github->authorization->getRateLimit();
311312

312313
// If over the API limit, we can't build this list
313-
if ($github->authorization->getRateLimit()->resources->core->remaining == 0)
314+
if ($rate->resources->core->remaining == 0)
314315
{
315-
throw new \RuntimeException(\JText::sprintf('COM_PATCHTESTER_API_LIMIT_LIST', \JFactory::getDate($this->rate->reset)));
316+
throw new \RuntimeException(
317+
\JText::sprintf('COM_PATCHTESTER_API_LIMIT_LIST', \JFactory::getDate($rate->resources->core->reset))
318+
);
316319
}
317320

318321
// Sanity check, ensure there aren't any applied patches

0 commit comments

Comments
 (0)