From 9d5d9477b15357ac1eebd5d4f8738212cbe21b51 Mon Sep 17 00:00:00 2001 From: Michael Dowling Date: Wed, 22 Oct 2014 13:19:53 -0700 Subject: [PATCH] Automating github API releases for v2 --- Makefile | 12 +-- build/gh-release.php | 172 ++++++++++++++----------------------------- 2 files changed, 61 insertions(+), 123 deletions(-) diff --git a/Makefile b/Makefile index cb3bf52a00..7219b62ee9 100644 --- a/Makefile +++ b/Makefile @@ -57,12 +57,12 @@ tag: check_tag # "make release" to push a release. This task requires that the # OAUTH_TOKEN environment variable is available and the token has permission # to push to the repository. -release: package - @echo "Pushing code to master" +release: check_tag package git push origin master - @echo "Pushing latest tag to Github" - git push origin `git describe --abbrev=0 --tags` - echo "Creating a Github release" - php build/gh-release.php + git push origin $(TAG) + php build/gh-release.php $(TAG) + +# Tags the repo and publishes a release. +full_release: tag release .PHONY: docs burgomaster diff --git a/build/gh-release.php b/build/gh-release.php index 6c0d7240d1..79e425b967 100644 --- a/build/gh-release.php +++ b/build/gh-release.php @@ -1,126 +1,64 @@ token = $token; - $this->owner = $owner; - $this->repo = $repo; - $this->client = new \Guzzle\Http\Client(); - $this->client->setUserAgent('aws-sdk-php'); - $this->client->setDefaultOption( - 'headers/Authorization', - 'token ' . $token - ); - } - - /** - * Executes a command and returns the output as a string. If it fails it throws. - */ - public static function exec($command) - { - exec($command, $output, $returnValue); - $output = implode("\n", $output); - - if ($returnValue != 0) { - die("Error executing command: {$command}\n$output"); - } - - return $output; - } - - public function createRelease( - $tag, - $contents, - $branch = 'master', - $draft = false - ) { - $request = $this->client->createRequest( - 'POST', - "https://api.github.com/repos/$this->owner/$this->repo/releases", - array('Content-Type' => 'application/json'), - json_encode(array( - 'tag_name' => $tag, - 'target_commitish' => $branch, - 'name' => $tag . ' release', - 'body' => $contents, - 'draft' => $draft - )) - ); - return $this->client->send($request)->json(); - } - - public function uploadAsset($id, $name, $contents, $contentType) - { - $request = $this->client->createRequest( - 'POST', - sprintf( - "https://api.github.com/repos/%s/%s/releases/%s/assets?name=%s", - $this->owner, $this->repo, $id, $name - ), - array('Content-Type' => $contentType), - $contents - ); +require __DIR__ . '/../vendor/autoload.php'; - return $this->client->send($request)->json(); - } -} +use Guzzle\Http\Client; +use Guzzle\Http\Url; +use Guzzle\Stream; $owner = 'aws'; $repo = 'aws-sdk-php'; -$returnVal = shell_exec('which chag') or die('chag not found in path'); -$token = getenv('OAUTH_TOKEN') or die('OAUTH_TOKEN environment var not found!'); -$artifacts = realpath(__DIR__ . '/artifacts') or die('artifacts dir not found'); -$stageDir = realpath($artifacts . '/staging') or die('stage dir not found'); -$zip = realpath($artifacts . '/aws.zip') or die('zip not found'); -$phar = realpath($artifacts . '/aws.phar') or die('phar not found'); - -// Get the latest changelog entry -chdir($stageDir); -$tag = Release::exec('chag get'); -$contents = Release::exec('chag contents'); -echo "Found tag: {$tag}\n\n{$contents}\n\n"; - -$release = new Release($token, $owner, $repo); -$res = $release->createRelease($tag, $contents, 'master', true); -echo "Created release: {$res['id']}\n"; - -// Upload the phar -$upload = $release->uploadAsset( - $res['id'], - 'aws.phar', - file_get_contents($phar), - 'application/octet-stream' -); - -echo "Uploaded asset: {$upload['browser_download_url']}\n"; - -// Upload the zip -$upload = $release->uploadAsset( - $res['id'], - 'aws.zip', - file_get_contents($zip), - 'application/zip' -); - -echo "Uploaded asset: {$upload['browser_download_url']}\n\n"; -echo "Successfully create GitHub release\n"; +$token = getenv('OAUTH_TOKEN') or die('An OAUTH_TOKEN environment variable is required'); +isset($argv[1]) or die('Usage php gh-release.php X.Y.Z'); +$tag = $argv[1]; + +assert(file_exists(__DIR__ . '/artifacts/aws.zip')); +assert(file_exists(__DIR__ . '/artifacts/aws.phar')); + +// Grab and validate the tag annotation +chdir(".."); +$message = `chag contents -t "$tag"` or die('Chag could not find or parse the tag'); + +// Create a GitHub client. +$client = new Client('https://api.github.com/'); +$client->setDefaultOption('headers/Authorization', "token $token"); + +// Create the release +$response = $client->post( + "repos/${owner}/${repo}/releases", + array('Content-Type' => 'application/json'), + json_encode(array( + 'tag_name' => $tag, + 'name' => "Version {$tag}", + 'body' => $message, + 'prerelease' => false + )) +)->send(); + +// Grab the location of the new release +$url = (string) $response->getHeader('Location'); +// Uploads go to uploads.github.com +$uploadUrl = Url::factory($url); +$uploadUrl->setHost('uploads.github.com'); + +$client->post( + $uploadUrl . '/assets?name=aws.zip', + array('Content-Type' => 'application/zip'), + fopen(__DIR__ . '/artifacts/aws.zip', 'r') +)->send(); + +$client->post( + $uploadUrl . '/assets?name=aws.phar', + array('Content-Type' => 'application/phar'), + fopen(__DIR__ . '/artifacts/aws.phar', 'r') +)->send(); + +echo "Release successfully published to: $url\n";