Skip to content

Instantly share code, notes, and snippets.

@andrewstobbe
Created May 30, 2017 02:38
Show Gist options
  • Save andrewstobbe/df14d84f6e4527894d379aad1d441b85 to your computer and use it in GitHub Desktop.
Save andrewstobbe/df14d84f6e4527894d379aad1d441b85 to your computer and use it in GitHub Desktop.
<?php
/*
* This file is part of Status.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace App\Status\Integrations\GitHub;
use App\Status\Integrations\Contracts\Releases as ReleasesContract;
use GuzzleHttp\Client;
use Illuminate\Contracts\Cache\Repository;
class Releases implements ReleasesContract
{
/**
* The default url.
*
* @var string
*/
const URL = 'https://api.github.com/repos/foo/bar/releases/latest';
/**
* The failed status indicator.
*
* @var int
*/
const FAILED = 1;
/**
* The cache repository instance.
*
* @var \Illuminate\Contracts\Cache\Repository
*/
protected $cache;
/**
* The github authentication token.
*
* @var string|null
*/
protected $token;
/**
* The url to use.
*
* @var string|null
*/
protected $url;
/**
* Creates a new releases instance.
*
* @param \Illuminate\Contracts\Cache\Repository $cache
* @param string|null $token
* @param string|null $url
*
* @return void
*/
public function __construct(Repository $cache, $token = null, $url = null)
{
$this->cache = $cache;
$this->token = $token;
$this->url = $url ?: static::URL;
}
/**
* Returns the latest release.
*
* @return string
*/
public function latest()
{
$release = $this->cache->remember('release.latest', 720, function () {
$headers = ['Accept' => 'application/vnd.github.v3+json', 'User-Agent' => defined('STATUS_VERSION') ? 'flow/'.constant('STATUS_VERSION') : 'flow'];
if ($this->token) {
$headers['OAUTH-TOKEN'] = $this->token;
}
return json_decode((new Client())->get($this->url, [
'headers' => $headers,
])->getBody(), true);
});
return [
'tag_name' => $release['tag_name'],
'prelease' => $release['prerelease'],
'draft' => $release['draft'],
];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment