Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A script to get the commit message and use it as the note to trigger a speedcurve deploy (magento 1)
<?php
class Skywire_Speedcurve_Alert extends Mage_Shell_Abstract
{
const GIT_COMMIT_API = 'https://api.github.com/repos/%s/%s/git/commits/%s';
const SPEEDCURVE_API = 'https://api.speedcurve.com/v1/deploys';
protected $hash;
protected $repo;
protected $owner;
protected $githubUser;
protected $githubToken;
protected $scToken;
protected $siteId;
protected $message;
public function run()
{
$this->hash = $this->getArg('hash');
$this->repo = $this->getArg('repo');
$this->owner = $this->getArg('owner');
$this->githubUser = $this->getArg('githubUser');
$this->githubToken = $this->getArg('githubToken');
$this->scToken = $this->getArg('SCToken');
$this->siteId = $this->getArg('siteId');
if ( !$this->hash
|| !$this->repo
|| !$this->owner
|| !$this->scToken
|| !$this->siteId
|| !$this->githubUser
|| !$this->githubToken)
{
die("Missing required parameters");
}
try {
// $this->authenticate();
$this->getCommitMessage();
} catch (Exception $e) {
printf ("Error getting commit message, will use hash as fallback: " . $e->getMessage());
$this->message = $this->hash;
}
try {
$this->alertSpeedcurve();
} catch (Exception $e) {
die ("Error alerting speedcurve: " . $e->getMessage());
}
}
/**
* Gets the git commit message for provided hash
*
* @return mixed
* @throws Exception
*/
protected function getCommitMessage()
{
$ch = curl_init();
$url = sprintf(self::GIT_COMMIT_API, $this->owner, $this->repo, $this->hash);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch,CURLOPT_USERAGENT, 'speedcurveAlert');
curl_setopt($ch, CURLOPT_USERPWD, sprintf('%s:%s', $this->githubUser, $this->githubToken));
$output = curl_exec($ch);
$output = json_decode($output);
curl_close($ch);
if (!property_exists($output, 'message')) {
Throw new Exception ('Speedcurve alert failed');
}
return $this->message = $output->message;
}
/**
* Triggers Speedcurve deployment test using commit message as the note
*
* @throws Exception
*/
protected function alertSpeedcurve()
{
$ch = curl_init();
$array = [
'site_id' => $this->siteId,
'note' => $this->message
];
curl_setopt($ch, CURLOPT_URL, self::SPEEDCURVE_API);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERPWD, $this->scToken . ":x");
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($array));
$output = curl_exec($ch);
curl_close($ch);
$output = json_decode($output);
if ($output->status != 'success') {
Throw new Exception ('Speedcurve alert failed');
} else {
printf($output->message);
}
}
/**
* Output help text
*
* @return string
*/
public function usageHelp()
{
return <<<USAGE
Usage: php -f speedcurveAlert.php [options]
Github:
--hash Commit hash to get message from
--repo Repo commit belongs to
--owner Repo owner
--githubUser User token is linked to
--githubToken Auth token
Speedcurve:
--SCToken Speedcurve token
--siteId Speedcurve site id
--help This help message
USAGE;
}
}
$shell = new Skywire_Speedcurve_Alert();
$shell->run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment