Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@manuinsfx
Last active May 3, 2021 19:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save manuinsfx/d974a17457f8a7d6ec888bd8592c239a to your computer and use it in GitHub Desktop.
Save manuinsfx/d974a17457f8a7d6ec888bd8592c239a to your computer and use it in GitHub Desktop.
#!/usr/bin/env php
<?php
require_once 'vendor/autoload.php';
$options = getopt('f:t:');
if (empty($csvFilename = $options['f'])) {
die("Please provide CSV filename: -f some-file");
}
if (empty($apiToken = $options['t'])) {
die("Please provide GitHub API Token: -t some-token");
}
if (!file_exists($csvFilename)) {
die("File $csvFilename not found.");
}
if (!$fp = fopen($csvFilename, 'r')) {
die("Error opening CSV file");
}
function updateRepository(\Github\Client $client, string $oldRepoName, string $newRepoName, bool $shouldArchive)
{
echo "Rename $oldRepoName to $newRepoName" . ($shouldArchive ? ' and archive it' : '') . "\n";
try {
$client->api('repo')->update('inserteffect', $oldRepoName, [
'name' => $newRepoName,
'archived' => $shouldArchive,
// TODO private?
// 'private' => true,
]);
} catch (Exception $e) {
if ($e->getCode() == 404) {
echo "- $oldRepoName not found. Skipping.\n";
} else {
throw $e;
}
}
}
// TODO
$COLUMN_INDEX_OLD_REPO_NAME = 6;
$COLUMN_INDEX_NEW_REPO_NAME = 14;
$COLUMN_INDEX_ARCHIVE = 13;
try {
$client = new \Github\Client();
$client->authenticate($apiToken, '', \Github\Client::AUTH_ACCESS_TOKEN);
while ($row = fgetcsv($fp)) {
$oldRepoName = $row[$COLUMN_INDEX_OLD_REPO_NAME];
$newRepoName = $row[$COLUMN_INDEX_NEW_REPO_NAME];
$shouldArchive = boolval($row[$COLUMN_INDEX_ARCHIVE]);
// Skip first row as well as empty rows
if (strlen($oldRepoName) > 0 && $oldRepoName != 'name') {
updateRepository($client, $oldRepoName, $newRepoName, $shouldArchive);
}
}
} catch (Exception $e) {
die($e->getMessage());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment