-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented GitHub Sync for shared documentation files
- Loading branch information
0 parents
commit 55d01b1
Showing
5 changed files
with
1,492 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/vendor/ |
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 |
---|---|---|
@@ -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"); | ||
} | ||
} | ||
}); |
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 |
---|---|---|
@@ -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" | ||
} | ||
} | ||
} |
Oops, something went wrong.