Skip to content

Commit f9175ff

Browse files
author
Michael Babker
committed
Initial cut at renamed file support (Ref #172)
1 parent 340ab0a commit f9175ff

File tree

1 file changed

+106
-38
lines changed
  • administrator/components/com_patchtester/PatchTester/Model

1 file changed

+106
-38
lines changed

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

Lines changed: 106 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -94,20 +94,33 @@ protected function parseFileList($files)
9494
}
9595

9696
// Sometimes the repo filename is not the production file name
97-
$prodFileName = $file->filename;
98-
$filePath = explode('/', $prodFileName);
97+
$prodFileName = $file->filename;
98+
$prodRenamedFileName = isset($file->previous_filename) ? $file->previous_filename : false;
99+
$filePath = explode('/', $prodFileName);
99100

100101
// Remove the `src` here to match the CMS paths if needed
101102
if ($filePath[0] === 'src')
102103
{
103104
$prodFileName = str_replace('src/', '', $prodFileName);
104105
}
105106

107+
if ($prodRenamedFileName)
108+
{
109+
$filePath = explode('/', $prodRenamedFileName);
110+
111+
// Remove the `src` here to match the CMS paths if needed
112+
if ($filePath[0] === 'src')
113+
{
114+
$prodRenamedFileName = str_replace('src/', '', $prodRenamedFileName);
115+
}
116+
}
117+
106118
$parsedFiles[] = (object) array(
107119
'action' => $file->status,
108120
'filename' => $prodFileName,
109121
'repofilename' => $file->filename,
110122
'fileurl' => $file->contents_url,
123+
'originalFile' => $prodRenamedFileName,
111124
);
112125
}
113126

@@ -181,48 +194,56 @@ public function apply($id)
181194

182195
foreach ($parsedFiles as $file)
183196
{
184-
if ($file->action == 'deleted' && !file_exists(JPATH_ROOT . '/' . $file->filename))
185-
{
186-
throw new \RuntimeException(\JText::sprintf('COM_PATCHTESTER_FILE_DELETED_DOES_NOT_EXIST_S', $file->old));
187-
}
188-
189-
if ($file->action == 'added' || $file->action == 'modified')
197+
switch ($file->action)
190198
{
191-
// If the backup file already exists, we can't apply the patch
192-
if (file_exists(JPATH_COMPONENT . '/backups/' . md5($file->filename) . '.txt'))
193-
{
194-
throw new \RuntimeException(\JText::sprintf('COM_PATCHTESTER_CONFLICT_S', $file->filename));
195-
}
199+
case 'deleted':
200+
if (!file_exists(JPATH_ROOT . '/' . $file->filename))
201+
{
202+
throw new \RuntimeException(\JText::sprintf('COM_PATCHTESTER_FILE_DELETED_DOES_NOT_EXIST_S', $file->filename));
203+
}
196204

197-
if ($file->action == 'modified' && !file_exists(JPATH_ROOT . '/' . $file->filename))
198-
{
199-
throw new \RuntimeException(\JText::sprintf('COM_PATCHTESTER_FILE_MODIFIED_DOES_NOT_EXIST_S', $file->filename));
200-
}
205+
break;
201206

202-
try
203-
{
204-
$contentsResponse = $github->getFileContents(
205-
$pull->head->user->login, $this->getState()->get('github_repo'), $file->repofilename, urlencode($pull->head->ref)
206-
);
207+
case 'added':
208+
case 'modified':
209+
case 'renamed':
210+
// If the backup file already exists, we can't apply the patch
211+
if (file_exists(JPATH_COMPONENT . '/backups/' . md5($file->filename) . '.txt'))
212+
{
213+
throw new \RuntimeException(\JText::sprintf('COM_PATCHTESTER_CONFLICT_S', $file->filename));
214+
}
207215

208-
$contents = json_decode($contentsResponse->body);
216+
if ($file->action == 'modified' && !file_exists(JPATH_ROOT . '/' . $file->filename))
217+
{
218+
throw new \RuntimeException(\JText::sprintf('COM_PATCHTESTER_FILE_MODIFIED_DOES_NOT_EXIST_S', $file->filename));
219+
}
209220

210-
// In case encoding type ever changes
211-
switch ($contents->encoding)
221+
try
212222
{
213-
case 'base64':
214-
$file->body = base64_decode($contents->content);
223+
$contentsResponse = $github->getFileContents(
224+
$pull->head->user->login, $this->getState()->get('github_repo'), $file->repofilename, urlencode($pull->head->ref)
225+
);
215226

216-
break;
227+
$contents = json_decode($contentsResponse->body);
217228

218-
default:
219-
throw new \RuntimeException(\JText::_('COM_PATCHTESTER_ERROR_UNSUPPORTED_ENCODING'));
229+
// In case encoding type ever changes
230+
switch ($contents->encoding)
231+
{
232+
case 'base64':
233+
$file->body = base64_decode($contents->content);
234+
235+
break;
236+
237+
default:
238+
throw new \RuntimeException(\JText::_('COM_PATCHTESTER_ERROR_UNSUPPORTED_ENCODING'));
239+
}
220240
}
221-
}
222-
catch (UnexpectedResponse $e)
223-
{
224-
throw new \RuntimeException(\JText::sprintf('COM_PATCHTESTER_COULD_NOT_CONNECT_TO_GITHUB', $e->getMessage()), $e->getCode(), $e);
225-
}
241+
catch (UnexpectedResponse $e)
242+
{
243+
throw new \RuntimeException(\JText::sprintf('COM_PATCHTESTER_COULD_NOT_CONNECT_TO_GITHUB', $e->getMessage()), $e->getCode(), $e);
244+
}
245+
246+
break;
226247
}
227248
}
228249

@@ -233,10 +254,12 @@ public function apply($id)
233254
foreach ($parsedFiles as $file)
234255
{
235256
// We only create a backup if the file already exists
236-
if ($file->action == 'deleted' || (file_exists(JPATH_ROOT . '/' . $file->filename) && $file->action == 'modified'))
257+
if ($file->action == 'deleted' || (file_exists(JPATH_ROOT . '/' . $file->filename) && $file->action == 'modified')
258+
|| (file_exists(JPATH_ROOT . '/' . $file->originalFile) && $file->action == 'renamed'))
237259
{
238-
$src = JPATH_ROOT . '/' . $file->filename;
239-
$dest = JPATH_COMPONENT . '/backups/' . md5($file->filename) . '.txt';
260+
$filename = $file->action == 'renamed' ? $file->originalFile : $file->filename;
261+
$src = JPATH_ROOT . '/' . $filename;
262+
$dest = JPATH_COMPONENT . '/backups/' . md5($filename) . '.txt';
240263

241264
if (!\JFile::copy(\JPath::clean($src), $dest))
242265
{
@@ -261,6 +284,19 @@ public function apply($id)
261284
throw new \RuntimeException(\JText::sprintf('COM_PATCHTESTER_ERROR_CANNOT_DELETE_FILE', JPATH_ROOT . '/' . $file->filename));
262285
}
263286

287+
break;
288+
289+
case 'renamed':
290+
if (!\JFile::delete(\JPath::clean(JPATH_ROOT . '/' . $file->originalFile)))
291+
{
292+
throw new \RuntimeException(\JText::sprintf('COM_PATCHTESTER_ERROR_CANNOT_DELETE_FILE', JPATH_ROOT . '/' . $file->originalFile));
293+
}
294+
295+
if (!\JFile::write(\JPath::clean(JPATH_ROOT . '/' . $file->filename), $file->body))
296+
{
297+
throw new \RuntimeException(\JText::sprintf('COM_PATCHTESTER_ERROR_CANNOT_WRITE_FILE', JPATH_ROOT . '/' . $file->filename));
298+
}
299+
264300
break;
265301
}
266302

@@ -366,6 +402,38 @@ public function revert($id)
366402
}
367403
}
368404

405+
break;
406+
407+
case 'renamed':
408+
$originalSrc = JPATH_COMPONENT . '/backups/' . md5($file->originalFile) . '.txt';
409+
$newSrc = JPATH_ROOT . '/' . $file->filename;
410+
$dest = JPATH_ROOT . '/' . $file->originalFile;
411+
412+
if (!\JFile::copy($originalSrc, $dest))
413+
{
414+
throw new \RuntimeException(\JText::sprintf('COM_PATCHTESTER_ERROR_CANNOT_COPY_FILE', $originalSrc, $dest));
415+
}
416+
417+
if (file_exists($originalSrc))
418+
{
419+
if (!\JFile::delete($originalSrc))
420+
{
421+
throw new \RuntimeException(
422+
\JText::sprintf('COM_PATCHTESTER_ERROR_CANNOT_DELETE_FILE', $originalSrc)
423+
);
424+
}
425+
}
426+
427+
if (file_exists($newSrc))
428+
{
429+
if (!\JFile::delete($newSrc))
430+
{
431+
throw new \RuntimeException(
432+
\JText::sprintf('COM_PATCHTESTER_ERROR_CANNOT_DELETE_FILE', $newSrc)
433+
);
434+
}
435+
}
436+
369437
break;
370438
}
371439
}

0 commit comments

Comments
 (0)