Skip to content

Commit

Permalink
Implemented GitHub Sync for shared documentation files
Browse files Browse the repository at this point in the history
  • Loading branch information
kelunik committed Sep 16, 2017
0 parents commit 55d01b1
Show file tree
Hide file tree
Showing 5 changed files with 1,492 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/vendor/
93 changes: 93 additions & 0 deletions bin/github-sync-website-shared
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
#!/usr/bin/env php
<?php

use Amp\Artax\Client;
use Amp\Artax\DefaultClient;
use Amp\ByteStream\ResourceInputStream;
use Amp\ByteStream\ResourceOutputStream;
use Amp\Loop;
use Amp\WebsiteTools\GitHubClient;
use Kelunik\OAuth\Providers\GitHub;
use function Kelunik\OAuthCli\authenticate;

require __DIR__ . "/../vendor/autoload.php";

$clientId = getenv("AMPHP_GITHUB_SYNC_APP_ID");
$clientSecret = getenv("AMPHP_GITHUB_SYNC_APP_SECRET");

Loop::run(function () use ($clientId, $clientSecret) {
$stdin = new ResourceInputStream(STDIN);
$stdout = new ResourceOutputStream(STDOUT);

$httpClient = new DefaultClient;
$httpClient->setOption(Client::OP_DEFAULT_HEADERS, [
"user-agent" => "amphp/website-tools",
]);

$github = new GitHub($httpClient, "http://127.0.0.1:1337/oauth", $clientId, $clientSecret, ["repo"]);
$oauthToken = yield authenticate($github);

$githubClient = new GitHubClient($oauthToken, $httpClient);

$reference = yield $githubClient->getHead("amphp/website-shared", "master");

yield $stdout->write("amphp/website-shared is at {$reference}\r\n");
yield $stdout->write("Do you want to update all repositories to that reference? [y/N]: ");
$answer = yield $stdin->read();

if (trim($answer) !== "y") {
exit(0);
}

$repositories = $githubClient->getRepositories("amphp");

while (yield $repositories->advance()) {
$repository = $repositories->getCurrent();
$repository = $repository["full_name"];

$currentVersion = yield $githubClient->getSubmoduleVersion($repository, "docs/.shared");

if ($currentVersion !== null && $currentVersion !== $reference) {
$stdout->write("{$repository} is not up-to-date, should I update it? [Y/n]: ");

$answer = trim(yield $stdin->read());

if ($answer !== "y" && $answer !== "") {
exit(0);
}

$master = yield $githubClient->getHead($repository, "master");
$baseTree = yield $githubClient->getCommitTree($repository, $master);
$baseTree = yield $githubClient->getTree($repository, $baseTree);

foreach ($baseTree["tree"] as $baseTreeKey => $baseTreeChild) {
if ($baseTreeChild["path"] === "docs") {
$docsTree = yield $githubClient->getTree($repository, $baseTreeChild["sha"]);

foreach ($docsTree["tree"] as $docsTreeKey => $docsTreeChild) {
if ($docsTreeChild["path"] === ".shared") {
$docsTree["tree"][$docsTreeKey]["sha"] = $reference;
}
}

$docsTree = yield $githubClient->createTree($repository, $docsTree["sha"], $docsTree["tree"]);
$baseTree["tree"][$baseTreeKey]["sha"] = $docsTree["sha"];
}
}

$newTree = yield $githubClient->createTree($repository, $baseTree["sha"], $baseTree["tree"]);

$commitMessage = <<<MESSAGE
Update shared documentation files
This is an automated commit. Please report any issues to https://github.com/amphp/website-tools.
MESSAGE;

$commit = yield $githubClient->createCommit($repository, $commitMessage, $newTree["sha"], [$master]);
yield $stdout->write("Created {$commit["sha"]} in {$repository}.\r\n");

$ref = yield $githubClient->updateHead($repository, "master", $commit["sha"]);
yield $stdout->write("Updated {$ref["ref"]} in {$repository} to {$commit["sha"]}.\r\n");
}
}
});
23 changes: 23 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "amphp/website-tools",
"description": "Website tools for amphp.org.",
"type": "project",
"require": {
"amphp/amp": "^2.0",
"amphp/artax": "^3.0",
"kelunik/oauth-cli": "^1.0",
"kelunik/link-header-rfc5988": "^1.0"
},
"license": "MIT",
"authors": [
{
"name": "Niklas Keller",
"email": "[email protected]"
}
],
"autoload": {
"psr-4": {
"Amp\\WebsiteTools\\": "src"
}
}
}
Loading

0 comments on commit 55d01b1

Please sign in to comment.