Snippets
Created by
Tjerk Ameel
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 | <?php
$public_key = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC4h6SZwJIaaETjb7XIntOfBFJRYaiaHFs43hMJV8y+jYU2hskayp/YxkxrmC02/9t8b8RbykgJHiE25PFzezvudcni69PNnI6rcZe73TxvrTsXgDx2t9tIIhNp2g3ABJyddQUudzIq+A5d6nLriOdROhS798HFtXf/4cjkc7GTeyblU2PSFuZUOM4eg7XYYueTe1Y5xVeFz6lH9LugR7CXOwfOQHmuy2v21R5/jgllSr6ougUXGz5WhsMYNn8ybTEhQoEbrO1OmS9P2VW95L/L/dnkNE3eJTmr1V9aQB1GL0MTFeoFhHnlRI+hq+GY03rI93EdPm1oAPYgH/oCebUp app@5vi98s-novolab-magweb-cvps.nodes.hypernode.io";
$label = 'Byte: Novolab';
$repositories = array(
'novolab-redstage-brand',
'novolab-phoenix-navconnect',
'novolab-bluejalappeno-tieredproducts',
'novolab-trmmarketing-popupwidgets',
'novolab-vbw-punchout2go',
'module-aheadworks-searchautocomplete',
'module-aheadworks-advancednewsletter',
'module-amasty-stockstatus',
'module-amasty-groupcat',
'module-amasty-payrestriction',
'module-amasty-shiprestriction',
'module-amasty-label',
'module-amasty-base',
'module-vladimirpopov_webforms',
'module-mageworx-filedownloads',
'module-mageparts-restrictcustomer',
'module-magebuzz-shoppinglist'
);
// Detecting username for API authentication
if(!isset($argv[1])){
echo "BitBucket Username: ";
$handle = fopen ("php://stdin",'');
$username = trim(fgets($handle));
}else{
$username = trim($argv[1]);
}
// Detecting password for API authentication
echo "BitBucket password for '$username': ";
$handle = fopen ("php://stdin",'');
$password = trim(fgets($handle));
$key_pusher = new BitBucketDeploymentKeyPusher($username, $password);
//var_dump($key_pusher->deployment_key_exists($public_key, 'novolab-redstage-brand'));
//var_dump($key_pusher->deployment_key_exists('ddd', 'novolab-redstage-brand'));
$key_pusher->push_key_to_repositories($public_key, $label, $repositories);
//$key_pusher->delete_key_from_repositories($public_key, $repositories);
class BitBucketDeploymentKeyPusher {
const INDIEGROUP_ACCOUNT = 'indiegroup';
private $username = '';
private $password = '';
public function __construct($username, $password){
$this->username = $username;
$this->password = $password;
}
public function push_key_to_repositories($public_key, $label, $repositories, $account = self::INDIEGROUP_ACCOUNT){
$data = array('key' => $public_key, 'label' => $label);
foreach($repositories as $repo) {
echo "---\n";
echo "Adding deployment key '$label' to '$repo'...\n";
if(!$this->deployment_key_exists($public_key, $repo, $account)){
$deployment_key_api_url = "https://bitbucket.org/api/1.0/repositories/$account/$repo/deploy-keys";
$add_deployment_key_context = $this->_get_bb_api_post_context($data);
// TODO Interpret result
$result = file_get_contents($deployment_key_api_url, false, $add_deployment_key_context);
}else {
echo "(Skipping this repo: the deployment key already exists!)\n";
}
}
}
public function delete_key_from_repositories($public_key, $repositories, $account = self::INDIEGROUP_ACCOUNT){
foreach($repositories as $repo) {
echo "---\n";
echo "Removing deployment key from '$repo'...\n";
$key_id = $this->get_key_id($public_key, $repo, $account);
if($key_id !== false){
$deployment_key_api_url = "https://bitbucket.org/api/1.0/repositories/$account/$repo/deploy-keys/$key_id";
$delete_deployment_key_context = $this->_get_bb_api_delete_context();
// TODO Interpret result
$result = file_get_contents($deployment_key_api_url, false, $delete_deployment_key_context);
echo "Removed deployment key with id '$key_id'.\n";
}else {
echo "(Skipping this repo: couldn't find deployment key!)\n";
}
}
}
public function deployment_key_exists($public_key, $repo, $account = self::INDIEGROUP_ACCOUNT){
$deployment_key_api_url = "https://bitbucket.org/api/1.0/repositories/$account/$repo/deploy-keys";
$get_deployment_key_context = $this->_get_bb_api_get_context();
$result = file_get_contents($deployment_key_api_url, false, $get_deployment_key_context);
if($result !== false){
$keys = json_decode($result);
if(!empty($keys)){
foreach($keys as $key_data){
if($key_data->key == $public_key){
return true;
}
}
}
}
return false;
}
public function get_key_id($public_key, $repo, $account = self::INDIEGROUP_ACCOUNT){
$deployment_key_api_url = "https://bitbucket.org/api/1.0/repositories/$account/$repo/deploy-keys";
$get_deployment_key_context = $this->_get_bb_api_get_context();
$result = file_get_contents($deployment_key_api_url, false, $get_deployment_key_context);
if($result !== false){
$keys = json_decode($result);
if(!empty($keys)){
foreach($keys as $key_data){
if($key_data->key == $public_key){
return $key_data->pk;
}
}
}
}
return false;
}
/**
* @param string $username The BitBucket username.
* @param string $password The BitBucket password.
* @param array $data A keyed array containing the data for the POST request.
* @return resource
*/
private function _get_bb_api_post_context($data){
$headers = array(
"Content-type: application/x-www-form-urlencoded",
"Authorization: Basic " . base64_encode("$this->username:$this->password")
);
// use key 'http' even if you send the request to https://...
$options = array(
'http' => array(
'header' => implode("\r\n", $headers),
'method' => 'POST',
'content' => http_build_query($data),
),
);
return stream_context_create($options);
}
/**
* @param string $username The BitBucket username.
* @param string $password The BitBucket password.
* @return resource
*/
private function _get_bb_api_get_context(){
return $this->_get_bb_api_generic_context('GET');
}
/**
* @param string $username The BitBucket username.
* @param string $password The BitBucket password.
* @return resource
*/
private function _get_bb_api_delete_context(){
return $this->_get_bb_api_generic_context('DELETE');
}
/**
* @param string $username The BitBucket username.
* @param string $password The BitBucket password.
* @return resource
*/
private function _get_bb_api_generic_context($request_type){
$headers = array(
"Authorization: Basic " . base64_encode("$this->username:$this->password")
);
// use key 'http' even if you send the request to https://...
$options = array(
'http' => array(
'header' => implode("\r\n", $headers),
'method' => $request_type,
),
);
return stream_context_create($options);
}
}
|
Comments (0)
You can clone a snippet to your computer for local editing. Learn more.