Last active
March 8, 2022 17:07
-
-
Save bchecketts/36e12f4747dd2d428c591214dbb856ac to your computer and use it in GitHub Desktop.
Quick script to place a single file in every repository in your Github organization
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
<?php | |
## Generate a Github user token at https://github.com/settings/tokens | |
$githubToken = 'EnterYourGithubTokenHere'; | |
## Your organization name (from https://github.com/yourOrganizationName) | |
$organization = 'yourOrganizationName'; | |
## Enter the name of the remote file you want to place | |
$remoteFile = "pull_request_template.md"; | |
## A local file that you wish to place in each repository if it doesn't already exist | |
$localFile = './pull_request_template.md'; | |
## Local working dir. Code is checked out here, updated, uploaded, then deleted | |
$workDir = __DIR__.'/working'; | |
## set $dryRun to false to actually perform the 'git push' command | |
$dryRun = true; | |
## Enter a commit message | |
$commitMessage = "Updating Pull Request Template ".date('Y-m-d'); | |
if (!file_exists($localFile)) { | |
echo "ERROR: {$localFile} must be present\n\n"; | |
exit; | |
} | |
$continue = true; | |
$page = 1; | |
while ($continue) { | |
$curl = curl_init("https://api.github.com/orgs/{$organization}/repos?page={$page}"); | |
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($curl, CURLOPT_HTTPHEADER, [ | |
"Authorization: token {$githubToken}", | |
"User-Agent: placeFileInAllGithubRepos.php v0.001" | |
]); | |
$rawResponse = curl_exec($curl); | |
$response = json_decode($rawResponse, true); | |
if (empty($response)) { | |
echo "No more pages\n"; | |
$continue = false; | |
} | |
$page++; | |
$localMd5 = md5_file($localFile); | |
echo "Github has ".count($response)." repos for the {$organization} Organization\n"; | |
foreach ($response as $repo) { | |
if ($workDir && file_exists($workDir) && is_dir($workDir)) { | |
echo "Removing old workdir at {$workDir}\n"; | |
exec("rm -Rf {$workDir}"); | |
} | |
echo "Checking out {$repo['full_name']} to {$workDir}\n"; | |
$command = "git clone {$repo['ssh_url']} {$workDir}"; | |
exec($command); | |
$placeFile = true; | |
if (file_exists("{$workDir}/{$remoteFile}")) { | |
$remoteMd5 = md5_file("{$workDir}/{$remoteFile}"); | |
if ($remoteMd5 == $localMd5) { | |
$placeFile = false; | |
} | |
} | |
if ($placeFile) { | |
echo " {$remoteFile} needs placed\n"; | |
copy($localFile, "{$workDir}/{$remoteFile}"); | |
passthru("git --git-dir={$workDir}/.git --work-tree={$workDir} add {$remoteFile}"); | |
passthru("git --git-dir={$workDir}/.git --work-tree={$workDir} commit -m \"{$commitMessage}\""); | |
echo " committed\n"; | |
if (!$dryRun) { | |
exec("git --git-dir={$workDir}/.git push"); | |
echo " git push completed\n"; | |
} | |
} else { | |
echo " {$remoteFile} is already present and correct\n"; | |
} | |
exec("rm -Rf {$workDir}"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment