-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #244 from dxw/feature/warn-user-on-archived-github…
…-repos Feature/warn user on archived GitHub repos
- Loading branch information
Showing
4 changed files
with
219 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,8 +39,69 @@ public function is_repo() | |
return file_exists("{$this->repo_path}/.git"); | ||
} | ||
|
||
private function is_github_repository($repository) | ||
{ | ||
$pos = strpos($repository, 'github.com'); | ||
return $pos !== false; | ||
} | ||
|
||
/** Issue a warning to the user if a GitHub repository is archived. | ||
* | ||
* Note that we specifically ignore any non-GitHub repository for now, | ||
* which is why we have not factored this code into its own class structure. | ||
* | ||
* See: https://docs.github.com/en/rest/repos/repos?get-a-repository | ||
*/ | ||
public function check_is_archived_github_repository($repository) | ||
{ | ||
if (!$this->is_github_repository($repository)) { | ||
return; | ||
} | ||
$baseurl = 'https://api.github.com/repos'; # Must not have a trailing slash. | ||
$substrings = explode('/', $repository); | ||
$num_substrings = count($substrings); | ||
# If the URL is http formatted: ['https', 'github.com', 'org', 'repo'] | ||
# If the URL is ssh formatted: ['[email protected]:org', 'repo'] | ||
if ($num_substrings < 2) { | ||
return false; | ||
} | ||
$repo = $substrings[$num_substrings - 1]; | ||
if (false !== strpos($repo, '.git')) { # repo.git | ||
$repo = str_replace('.git', '', $repo); | ||
} | ||
|
||
if (false !== strpos($repository, '@')) { | ||
# ssh formatted... | ||
$org = explode(':', $substrings[$num_substrings - 2])[1]; | ||
} else { | ||
# http formatted... | ||
$org = $substrings[$num_substrings - 2]; | ||
} | ||
$api_url = join('/', [$baseurl, $org, $repo]); | ||
|
||
$curl = curl_init(); | ||
curl_setopt($curl, CURLOPT_URL, $api_url); | ||
curl_setopt($curl, CURLOPT_USERAGENT, 'Whippet'); | ||
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); | ||
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); | ||
$raw_json = curl_exec($curl); | ||
$json = json_decode($raw_json); | ||
curl_close($curl); | ||
if (!is_null($json) && property_exists($json, 'archived') && $json->archived) { | ||
echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n"; | ||
echo "!! WARNING: GitHub repo is archived. This dependency !!\n"; | ||
echo "!! should be replaced before the repo is removed. !!\n"; | ||
echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n"; | ||
} | ||
} | ||
|
||
public function checkout($revision) | ||
{ | ||
list($output, $return) = $this->run_command(['git', 'remote', 'get-url', 'origin']); | ||
if ($return === 0) { | ||
$this->check_is_archived_github_repository($output[0]); | ||
} | ||
|
||
list($output, $return) = $this->run_command(['git', 'fetch', '-a', '--force', '&&', 'git', 'checkout', $revision]); | ||
|
||
return $this->check_git_return('Checkout failed', $return, $output); | ||
|
@@ -62,6 +123,8 @@ public function mixed_reset($revision = 'HEAD') | |
|
||
public function clone_repo($repository) | ||
{ | ||
$this->check_is_archived_github_repository($repository); | ||
|
||
list($output, $return) = $this->run_command(['git', 'clone', $repository, $this->repo_path], false); | ||
|
||
if (!$this->check_git_return('Clone failed', $return, $output)) { | ||
|
@@ -73,6 +136,8 @@ public function clone_repo($repository) | |
|
||
public function clone_no_checkout($repository) | ||
{ | ||
$this->check_is_archived_github_repository($repository); | ||
|
||
$tmpdir = $this->get_tmpdir(); | ||
|
||
list($output, $return) = $this->run_command(['git', 'clone', '--no-checkout', $repository, $tmpdir], false); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -161,6 +161,65 @@ public function testInspectionsApiUnavailable() | |
$this->assertEquals($expectedOutput, $output); | ||
} | ||
|
||
public function testInstallArchiveRepo() | ||
{ | ||
$dir = $this->getDir(); | ||
file_put_contents($dir.'/whippet.json', 'foobar'); | ||
file_put_contents($dir.'/whippet.lock', 'foobar'); | ||
|
||
$my_theme = [ | ||
'name' => 'my-theme', | ||
'src' => '[email protected]:wordpress-themes/my-theme', | ||
'revision' => '27ba906', | ||
]; | ||
|
||
$whippetLock = $this->getWhippetLock(sha1('foobar'), [ | ||
'themes' => [ | ||
$my_theme, | ||
], | ||
'plugins' => [], | ||
]); | ||
$this->addFactoryCallStatic('\\Dxw\\Whippet\\Files\\WhippetLock', 'fromFile', $dir.'/whippet.lock', \Result\Result::ok($whippetLock)); | ||
|
||
$gitMyTheme = $this->getGit(false, '[email protected]:wordpress-themes/my-theme', '27ba906', true); | ||
$this->addFactoryNewInstance('\\Dxw\\Whippet\\Git\\Git', $dir.'/wp-content/themes/my-theme', $gitMyTheme); | ||
|
||
$inspection_check_results = function ($type, $dep) { | ||
return [ | ||
'themes' => [ | ||
'my-theme' => \Result\Result::ok('') | ||
], | ||
][$type][$dep['name']]; | ||
}; | ||
|
||
$dependencies = new \Dxw\Whippet\Dependencies\Installer( | ||
$this->getFactory(), | ||
$this->getProjectDirectory($dir), | ||
$this->fakeInspectionCheckerWithResults($inspection_check_results) | ||
); | ||
|
||
ob_start(); | ||
$result = $dependencies->installAll(); | ||
$output = ob_get_clean(); | ||
|
||
$this->assertFalse($result->isErr()); | ||
$expectedOutput = <<<'EOT' | ||
[Adding themes/my-theme] | ||
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! | ||
!! WARNING: GitHub repo is archived. This dependency !! | ||
!! should be replaced before the repo is removed. !! | ||
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! | ||
git clone output | ||
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! | ||
!! WARNING: GitHub repo is archived. This dependency !! | ||
!! should be replaced before the repo is removed. !! | ||
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! | ||
git checkout output | ||
|
||
|
||
EOT; | ||
$this->assertEquals($expectedOutput, $output); | ||
} | ||
|
||
public function testInstallAllThemeAlreadyCloned() | ||
{ | ||
|
@@ -468,6 +527,67 @@ public function testInstallSingleAlreadyCloned() | |
$this->assertFalse($result->isErr()); | ||
} | ||
|
||
public function testInstallSingleAlreadyClonedAndArchived() | ||
{ | ||
$dir = $this->getDir(); | ||
file_put_contents($dir.'/whippet.json', 'foobar'); | ||
file_put_contents($dir.'/whippet.lock', 'foobar'); | ||
|
||
$whippetLock = $this->getWhippetLock(sha1('foobar'), [ | ||
'themes' => [ | ||
[ | ||
'name' => 'my-theme', | ||
'src' => '[email protected]:wordpress-themes/my-theme', | ||
'revision' => '27ba906', | ||
], | ||
], | ||
'plugins' => [ | ||
[ | ||
'name' => 'my-plugin', | ||
'src' => '[email protected]:wordpress-plugins/my-plugin', | ||
'revision' => '123456', | ||
], | ||
[ | ||
'name' => 'another-plugin', | ||
'src' => '[email protected]:wordpress-plugins/another-plugin', | ||
'revision' => '789abc', | ||
], | ||
], | ||
]); | ||
$this->addFactoryCallStatic('\\Dxw\\Whippet\\Files\\WhippetLock', 'fromFile', $dir.'/whippet.lock', \Result\Result::ok($whippetLock)); | ||
|
||
$gitMyPlugin = $this->getGit(false, '[email protected]:wordpress-plugins/my-plugin', '123456', true); | ||
$this->addFactoryNewInstance('\\Dxw\\Whippet\\Git\\Git', $dir.'/wp-content/plugins/my-plugin', $gitMyPlugin); | ||
|
||
$dependencies = new \Dxw\Whippet\Dependencies\Installer( | ||
$this->getFactory(), | ||
$this->getProjectDirectory($dir), | ||
$this->fakeInspectionChecker() | ||
); | ||
ob_start(); | ||
$result = $dependencies->installSingle('plugins/my-plugin'); | ||
$output = ob_get_clean(); | ||
$expectedOutput = <<<'EOT' | ||
[Adding plugins/my-plugin] | ||
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! | ||
!! WARNING: GitHub repo is archived. This dependency !! | ||
!! should be replaced before the repo is removed. !! | ||
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! | ||
git clone output | ||
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! | ||
!! WARNING: GitHub repo is archived. This dependency !! | ||
!! should be replaced before the repo is removed. !! | ||
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! | ||
git checkout output | ||
|
||
|
||
EOT; | ||
|
||
|
||
$this->assertEquals($expectedOutput, $output); | ||
$this->assertFalse($result->isErr()); | ||
} | ||
|
||
public function testInstallSingleCloneFails() | ||
{ | ||
$dir = $this->getDir(); | ||
|