Last active
July 7, 2022 23:37
-
-
Save WinterSilence/a45cfd0b6b470be364404b077ec5737f to your computer and use it in GitHub Desktop.
Find outdated documentation translations for Yii 2
This file contains hidden or 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
<?php | |
/** | |
* @param int $pr Identifier of Pull Request | |
* @param string $repoDir Base directory of repository | |
* @param string $package "{owner}/{repo}" | |
* @param string $docDir Repository directory of documentation | |
* @return array Files to update | |
*/ | |
function findOutdatedDocTranslations($pr, $repoDir, $package = 'yiisoft/yii2', $docDir = 'docs') | |
{ | |
$context = \stream_context_create(['http' => ['header' => 'Accept: application/vnd.github.v3+json']]); | |
$json = \file_get_contents("https://api.github.com/repos/{$package}/pulls/{$pr}/files?per_page=500", $context); | |
$files = \json_decode($json, true); | |
$files = \array_column($files, 'filename'); | |
unset($context, $json); | |
$repoDir = \realpath($repoDir) . '/'; | |
$docPatterns = []; | |
$translatedDocs = []; | |
foreach ($files as $file) { | |
$pattern = '/^' . $docDir . '\/(?<dir>\w+)(-(?<lang>[-a-zA-Z]+))*\/(?<file>[-\w]+\.md)$/'; | |
if (\preg_match($pattern, $file, $matches) === 1) { | |
if ($matches['lang'] === '') { | |
$docPatterns[] = $repoDir . $docDir . '/' . $matches['dir'] . '-*/' . $matches['file']; | |
} else { | |
$translatedDocs[] = $file; | |
} | |
} | |
} | |
if ($docPatterns === []) { | |
return []; | |
} | |
$repoDirLength = \strlen($repoDir); | |
$oldDocs = []; | |
foreach ($docPatterns as $pattern) { | |
$files = \glob($pattern, \GLOB_NOSORT); | |
foreach ($files as $file) { | |
$path =\substr($file, $repoDirLength); | |
if (!\in_array($path, $translatedDocs, true)) { | |
$oldDocs[] = $file; | |
} | |
} | |
} | |
return $oldDocs; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment