├── .gitignore ├── README.md └── lib └── beanstalkapi.class.php /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled source # 2 | ################### 3 | *.com 4 | *.class 5 | *.dll 6 | *.exe 7 | *.o 8 | *.so 9 | 10 | # Packages # 11 | ############ 12 | # it's better to unpack these files and commit the raw source 13 | # git has its own built in compression methods 14 | *.7z 15 | *.dmg 16 | *.gz 17 | *.iso 18 | *.jar 19 | *.rar 20 | *.tar 21 | *.zip 22 | 23 | # Logs and databases # 24 | ###################### 25 | *.log 26 | *.sql 27 | *.sqlite 28 | 29 | # OS generated files # 30 | ###################### 31 | .DS_Store? 32 | ehthumbs.db 33 | Icon? 34 | Thumbs.db 35 | 36 | # IDE generated files # 37 | ####################### 38 | .idea 39 | .idea/ 40 | .project -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Beanstalk PHP API v0.10.1 Documentation # 2 | 3 | ## Installation ## 4 | Requires PHP 5, libcurl library and SimpleXML extension 5 | 6 | ### *Either* download source ### 7 | Download the most recent tag from https://github.com/chrisbarr/Beanstalk-PHP-API/tags 8 | 9 | ### *Or* clone via GitHub ### 10 | Clone the most recent copy of the repository 11 | 12 | git clone git://github.com/chrisbarr/Beanstalk-PHP-API.git 13 | cd ./Beanstalk-PHP-API 14 | 15 | ### Then include file ### 16 | 17 | Include beanstalkapi.class.php in the php file you wish to use it in, using 18 | 19 | ```php 20 | require_once('lib/beanstalkapi.class.php'); 21 | ``` 22 | 23 | ## Usage ## 24 | Before using any of the following methods, you must first call the following: 25 | 26 | ```php 27 | $Beanstalk = new BeanstalkAPI('ACCOUNT_NAME_HERE', 'USERNAME_HERE', 'PASSWORD_HERE'); 28 | ``` 29 | 30 | Make sure to put your account details in the appropriate places. 31 | 32 | Now call the API functions using the `$Beanstalk` variable, ie. `$Beanstalk->find_all_users();` 33 | 34 | The BeanstalkAPI object can use either XML or JSON to communicate with your Beanstalk account. It will use JSON by default and return an array as the response. If you want to use XML just provide 'xml' as the 4th parameter: 35 | 36 | ```php 37 | $Beanstalk = new BeanstalkAPI('ACCOUNT_NAME_HERE', 'USERNAME_HERE', 'PASSWORD_HERE', 'xml'); 38 | ``` 39 | 40 | and it will return a SimpleXMLElement. 41 | 42 | ### API Methods ### 43 | List of available function calls: 44 | 45 | #### Account Details #### 46 | * `get_account_details();` 47 | * `update_account_details();` 48 | 49 | #### Plans #### 50 | * `find_all_plans();` 51 | 52 | #### Users #### 53 | * `find_all_users();` 54 | * `find_single_user(user_id);` 55 | * `find_current_user();` 56 | * `create_user(login, email, first_name, last_name, password);` 57 | * `update_user(user_id, params);` 58 | * `delete_user(user_id);` 59 | 60 | #### Invitations #### 61 | * `find_invitation(invitation_id);` 62 | * `create_invitation(email, first_name, last_name);` 63 | 64 | #### Public Keys #### 65 | * `find_all_public_keys();` 66 | * `find_single_public_key(key_id);` 67 | * `create_public_key(content);` 68 | * `update_public_key(key_id, params);` 69 | * `delete_public_key(key_id);` 70 | 71 | #### Feed Keys #### 72 | * `find_current_user_feed_key();` 73 | 74 | #### Repositories #### 75 | * `find_all_repositories();` 76 | * `find_single_repository(repo_id);` 77 | * `find_repository_branches(repo_id);` 78 | * `find_repository_tags(repo_id);` 79 | * `create_repository(name, type_id, title);` 80 | * `update_repository(repo_id, params);` 81 | 82 | #### Imports #### 83 | * `find_import(import_id);` 84 | * `create_import(repo_id, import_url);` 85 | 86 | #### User Permissions #### 87 | * `find_user_permissions(user_id);` 88 | * `create_user_permissions(user_id, repo_id, read, write, full_deployments_access);` 89 | * `delete_user_permissions(user_id);` 90 | 91 | #### Integrations #### 92 | * `find_all_integrations(repo_id);` 93 | * `find_single_integration(repo_id, integration_id);` 94 | 95 | #### Changesets #### 96 | * `find_all_changesets();` 97 | * `find_single_repository_changesets(repo_id);` 98 | * `find_single_changeset(repo_id, revision);` 99 | * `find_changeset_diffs(repo_id, revision);` 100 | 101 | #### Comments #### 102 | * `find_all_comments(repo_id);` 103 | * `find_all_changeset_comments(repo_id, revision);` 104 | * `find_single_user_comments(user_id);` 105 | * `find_single_comment(repo_id, comment_id);` 106 | * `create_comment(repo_id, revision_id, body, file_path, line_number);` 107 | 108 | #### Server Environments #### 109 | * `find_all_server_environments(repo_id);` 110 | * `find_single_server_environment(repo_id, environment_id);` 111 | * `create_server_environment(repo_id, name, automatic);` 112 | * `update_server_environment(repo_id, environment_id, params);` 113 | 114 | #### Release Servers #### 115 | * `find_all_release_servers(repo_id, environment_id);` 116 | * `find_single_release_server(repo_id, server_id);` 117 | * `create_release_server(repo_id, environment_id, name, local_path, remote_path, remote_addr, protocol, port, login, password);` 118 | * `update_release_server(repo_id, server_id, params);` 119 | * `delete_release_server(repo_id, server_id);` 120 | 121 | #### Releases #### 122 | * `find_all_releases();` 123 | * `find_all_repository_releases(repo_id);` 124 | * `find_single_release(repo_id, release_id);` 125 | * `create_release(repo_id, revision_id);` 126 | * `retry_release(repo_id, release_id);` 127 | 128 | ### Examples ### 129 | Display account details: 130 | 131 | ```php 132 | get_account_details(); 137 | 138 | print_r($account_details); 139 | ?> 140 | ``` 141 | 142 | Fetch a list of repositories: 143 | 144 | ```php 145 | find_all_repositories(); 150 | 151 | print_r($repositories); 152 | ?> 153 | ``` 154 | 155 | If there is a problem connecting to the API, the function will throw an APIException: 156 | 157 | ```php 158 | find_all_users(); 165 | 166 | // This will only be executed if find_all_users() ran correctly 167 | print_r($users); 168 | } 169 | catch(APIException $e) 170 | { 171 | echo 'Oops, there was a problem ' . $e->getMessage(); 172 | // Use $e->getCode() to get the returned HTTP status code of the exception 173 | } 174 | ?> 175 | ``` 176 | 177 | ## Further info ## 178 | Detailed documentation about the API can be found on the Beanstalk website at http://api.beanstalkapp.com/ 179 | -------------------------------------------------------------------------------- /lib/beanstalkapi.class.php: -------------------------------------------------------------------------------- 1 | account_name = $account_name; 43 | 44 | if(!is_null($username)) 45 | $this->username = $username; 46 | 47 | if(!is_null($password)) 48 | $this->password = $password; 49 | 50 | if(empty($this->account_name) || empty($this->username) || empty($this->password)) 51 | throw new InvalidArgumentException("Account name, username and password required"); 52 | 53 | $this->format = strtolower($format) == 'json' ? 'json' : 'xml'; 54 | } 55 | 56 | 57 | // 58 | // Account 59 | // 60 | 61 | /** 62 | * Returns Beanstalk account details. 63 | * 64 | * @link http://api.beanstalkapp.com/account.html 65 | * @return SimpleXMLElement|array 66 | */ 67 | public function get_account_details() { 68 | return $this->_execute_curl("account." . $this->format); 69 | } 70 | 71 | /** 72 | * Allows a user to update their account details by sending specific parameters 73 | * 74 | * @link http://api.beanstalkapp.com/account.html 75 | * @param array $params Accepts - name, timezone 76 | * @return SimpleXMLElement|array 77 | */ 78 | public function update_account_details($params = array()) { 79 | if(count($params) == 0) 80 | throw new InvalidArgumentException("Nothing to update"); 81 | 82 | if($this->format == 'xml') 83 | { 84 | $xml = new SimpleXMLElement(""); 85 | 86 | if(isset($params['name'])) 87 | $xml->addChild('name', $params['name']); 88 | 89 | if(isset($params['timezone'])) 90 | $xml->addChild('time_zone', $params['timezone']); // Inconsistency in API? 91 | 92 | $data = $xml->asXml(); 93 | } 94 | else 95 | { 96 | $data_array = array('account' => array()); 97 | 98 | if(isset($params['name'])) 99 | $data_array['account']['name'] = $params['name']; 100 | 101 | if(isset($params['timezone'])) 102 | $data_array['account']['time_zone'] = $params['timezone']; 103 | 104 | $data = json_encode($data_array); 105 | } 106 | 107 | return $this->_execute_curl("account." . $this->format, NULL, "PUT", $data); 108 | } 109 | 110 | 111 | // 112 | // Plans 113 | // 114 | 115 | /** 116 | * Returns Beanstalk account plans 117 | * 118 | * @link http://api.beanstalkapp.com/plan.html 119 | * @return SimpleXMLElement|array 120 | */ 121 | public function find_all_plans() { 122 | return $this->_execute_curl("plans." . $this->format); 123 | } 124 | 125 | 126 | // 127 | // Users 128 | // 129 | 130 | /** 131 | * Returns Beanstalk account user list. 132 | * 133 | * @link http://api.beanstalkapp.com/user.html 134 | * @param integer $page [optional] Current page of results 135 | * @param integer $per_page [optional] Results per page - default 30, max 50 136 | * @return SimpleXMLElement|array 137 | */ 138 | public function find_all_users($page = 1, $per_page = 30) { 139 | $per_page = intval($per_page) > 50 ? 50 : intval($per_page); 140 | 141 | return $this->_execute_curl("users." . $this->format . "?page=" . $page . "&per_page=" . $per_page); 142 | } 143 | 144 | /** 145 | * Returns a Beanstalk account user based on a specific user ID 146 | * 147 | * @link http://api.beanstalkapp.com/user.html 148 | * @param integer $user_id required 149 | * @return SimpleXMLElement|array 150 | */ 151 | public function find_single_user($user_id) { 152 | if(empty($user_id)) 153 | throw new InvalidArgumentException("User ID required"); 154 | else 155 | return $this->_execute_curl("users", $user_id . "." . $this->format); 156 | } 157 | 158 | /** 159 | * Returns Beanstalk user currently being used to access the API 160 | * 161 | * @link http://api.beanstalkapp.com/user.html 162 | * @return SimpleXMLElement|array 163 | */ 164 | public function find_current_user() { 165 | return $this->_execute_curl("users", "current." . $this->format); 166 | } 167 | 168 | /** 169 | * Create a new Beanstalk user 170 | * 171 | * @link http://api.beanstalkapp.com/user.html 172 | * @param string $login 173 | * @param string $email 174 | * @param string $first_name 175 | * @param string $last_name 176 | * @param string $password 177 | * @param int $admin [optional] 178 | * @param string $timezone [optional] 179 | * @return SimpleXMLElement|array 180 | */ 181 | public function create_user($login, $email, $first_name, $last_name, $password, $admin = 0, $timezone = NULL) { 182 | if(empty($login) || empty($email) || empty($first_name) || empty($last_name) || empty($password)) 183 | throw new InvalidArgumentException("Some required fields missing"); 184 | 185 | if($this->format == 'xml') 186 | { 187 | $xml = new SimpleXMLElement(''); 188 | 189 | $xml->addChild('login', $login); 190 | $xml->addChild('email', $email); 191 | $xml->addChild('first_name', $first_name); 192 | $xml->addChild('last_name', $last_name); 193 | $xml->addChild('password', $password); 194 | $xml->addChild('admin', $admin); // Should change to optional? 195 | 196 | if(!is_null($timezone)) 197 | $xml->addChild('timezone', $timezone); 198 | 199 | $data = $xml->asXml(); 200 | } 201 | else 202 | { 203 | $data_array = array('user' => array()); 204 | 205 | $data_array['user']['login'] = $login; 206 | $data_array['user']['email'] = $email; 207 | $data_array['user']['first_name'] = $first_name; 208 | $data_array['user']['last_name'] = $last_name; 209 | $data_array['user']['password'] = $password; 210 | $data_array['user']['admin'] = $admin; 211 | 212 | if(isset($timezone)) 213 | $data_array['user']['timezone'] = $timezone; 214 | 215 | $data = json_encode($data_array); 216 | } 217 | 218 | return $this->_execute_curl("users." . $this->format, NULL, "POST", $data); 219 | } 220 | 221 | /** 222 | * Update an existing user 223 | * 224 | * @link http://api.beanstalkapp.com/user.html 225 | * @param integer $user_id 226 | * @param array $params Accepts - email, first_name, last_name, password, admin, timezone 227 | * @return SimpleXMLElement|array 228 | */ 229 | public function update_user($user_id, $params = array()) { 230 | if(empty($user_id)) 231 | throw new InvalidArgumentException("User ID required"); 232 | 233 | if(count($params) == 0) 234 | throw new InvalidArgumentException("Nothing to update"); 235 | 236 | if($this->format == 'xml') 237 | { 238 | $xml = new SimpleXMLElement(''); 239 | 240 | if(isset($params['email'])) 241 | $xml->addChild('email', $params['email']); 242 | 243 | if(isset($params['first_name'])) 244 | $xml->addChild('first_name', $params['first_name']); 245 | 246 | if(isset($params['last_name'])) 247 | $xml->addChild('last_name', $params['last_name']); 248 | 249 | if(isset($params['password'])) 250 | $xml->addChild('password', $params['password']); 251 | 252 | if(isset($params['admin'])) 253 | $xml->addChild('admin', $params['admin']); 254 | 255 | if(isset($params['timezone'])) 256 | $xml->addChild('timezone', $params['timezone']); 257 | 258 | $data = $xml->asXml(); 259 | } 260 | else 261 | { 262 | $data_array = array('user' => array()); 263 | 264 | if(isset($params['email'])) 265 | $data_array['user']['email'] = $params['email']; 266 | 267 | if(isset($params['first_name'])) 268 | $data_array['user']['first_name'] = $params['first_name']; 269 | 270 | if(isset($params['last_name'])) 271 | $data_array['user']['last_name'] = $params['last_name']; 272 | 273 | if(isset($params['password'])) 274 | $data_array['user']['password'] = $params['password']; 275 | 276 | if(isset($params['admin'])) 277 | $data_array['user']['admin'] = $params['admin']; 278 | 279 | if(isset($params['timezone'])) 280 | $data_array['user']['timezone'] = $params['timezone']; 281 | 282 | $data = json_encode($data_array); 283 | } 284 | 285 | return $this->_execute_curl("users", $user_id . "." . $this->format, "PUT", $data); 286 | } 287 | 288 | /** 289 | * Delete a user 290 | * 291 | * @link http://api.beanstalkapp.com/user.html 292 | * @param integer $user_id 293 | * @return SimpleXMLElement|array 294 | */ 295 | public function delete_user($user_id) { 296 | if(empty($user_id)) 297 | throw new InvalidArgumentException("User ID required"); 298 | 299 | return $this->_execute_curl("users", $user_id . "." . $this->format, "DELETE"); 300 | } 301 | 302 | 303 | // 304 | // Invitations 305 | // 306 | 307 | /** 308 | * Return an invitation 309 | * 310 | * @link http://api.beanstalkapp.com/invitation.html 311 | * @param integer $invitation_id 312 | * @return SimpleXMLElement|array 313 | */ 314 | public function find_invitation($invitation_id) 315 | { 316 | if(empty($invitation_id)) 317 | throw new InvalidArgumentException("Invitation ID required"); 318 | 319 | return $this->_execute_curl("invitations", $invitation_id . "." . $this->format); 320 | } 321 | 322 | /** 323 | * Create an invitation - creates a User and Invitation 324 | * 325 | * @link http://api.beanstalkapp.com/invitation.html 326 | * @param string $email 327 | * @param string $first_name 328 | * @param string $last_name 329 | * @return SimpleXMLElement|array 330 | */ 331 | public function create_invitation($email, $first_name, $last_name) 332 | { 333 | if(empty($email) || empty($first_name) || empty($last_name)) 334 | throw new InvalidArgumentException("Some required fields missing"); 335 | 336 | if($this->format == 'xml') 337 | { 338 | $xml = new SimpleXMLElement(''); 339 | 340 | $user = $xml->addChild('user'); 341 | 342 | $user->addChild('email', $email); 343 | $user->addChild('first_name', $first_name); 344 | $user->addChild('last_name', $last_name); 345 | 346 | $data = $xml->asXml(); 347 | } 348 | else 349 | { 350 | $data_array = array('invitation' => array()); 351 | 352 | $data_array['invitation']['user'] = array(); 353 | 354 | $data_array['invitation']['user']['email'] = $email; 355 | $data_array['invitation']['user']['first_name'] = $first_name; 356 | $data_array['invitation']['user']['last_name'] = $last_name; 357 | 358 | $data = json_encode($data_array); 359 | } 360 | 361 | return $this->_execute_curl("invitations." . $this->format, NULL, "POST", $data); 362 | } 363 | 364 | 365 | // 366 | // Public Keys 367 | // 368 | 369 | /** 370 | * Return all public keys for current user - or for a specified user (if using admin account) 371 | * 372 | * @link http://api.beanstalkapp.com/public_key.html 373 | * @param integer $user_id [optional] 374 | * @return SimpleXMLElement|array 375 | */ 376 | public function find_all_public_keys($user_id = NULL) { 377 | if(!is_null($user_id)) 378 | return $this->_execute_curl("public_keys." . $this->format . "?user_id=" . $user_id); 379 | else 380 | return $this->_execute_curl("public_keys." . $this->format); 381 | } 382 | 383 | /** 384 | * Return a single public key 385 | * 386 | * @link http://api.beanstalkapp.com/public_key.html 387 | * @param integer $key_id 388 | * @return SimpleXMLElement|array 389 | */ 390 | public function find_single_public_key($key_id) { 391 | if(empty($key_id)) 392 | throw new InvalidArgumentException("Public key ID required"); 393 | 394 | return $this->_execute_curl("public_keys", $key_id . "." . $this->format); 395 | } 396 | 397 | /** 398 | * Create a new public key - creates for current user unless specified (must be admin) 399 | * 400 | * @link http://api.beanstalkapp.com/public_key.html 401 | * @param string $content 402 | * @param string $name [optional] 403 | * @param integer $user_id [optional] Defaults to current user 404 | * @return SimpleXMLElement|array 405 | */ 406 | public function create_public_key($content, $name = NULL, $user_id = NULL) { 407 | if(empty($content)) 408 | throw new InvalidArgumentException("Key content required"); 409 | 410 | if($this->format == 'xml') 411 | { 412 | $xml = new SimpleXMLElement(''); 413 | 414 | $xml->addChild('content', $content); 415 | 416 | if(!is_null($name)) 417 | $xml->addChild('name', $name); 418 | 419 | if(!is_null($user_id)) 420 | $xml->addChild('user_id', $user_id); 421 | 422 | $data = $xml->asXml(); 423 | } 424 | else 425 | { 426 | $data_array = array('public_key' => array()); 427 | 428 | $data_array['public_key']['content'] = $content; 429 | 430 | if(!is_null($name)) 431 | $data_array['public_key']['name'] = $name; 432 | 433 | if(!is_null($user_id)) 434 | $data_array['public_key']['user_id'] = $user_id; 435 | 436 | $data = json_encode($data_array); 437 | } 438 | 439 | return $this->_execute_curl("public_keys." . $this->format, NULL, "POST", $data); 440 | } 441 | 442 | /** 443 | * Update a public key - can only update own keys unless admin 444 | * 445 | * @link http://api.beanstalkapp.com/public_key.html 446 | * @param integer $key_id 447 | * @param array $params Accepts - content, name 448 | * @return SimpleXMLElement|array 449 | */ 450 | public function update_public_key($key_id, $params = array()) { 451 | if(empty($key_id)) 452 | throw new InvalidArgumentException("Public key ID required"); 453 | 454 | if(count($params) == 0) 455 | throw new InvalidArgumentException("Nothing to update"); 456 | 457 | if($this->format == 'xml') 458 | { 459 | $xml = new SimpleXMLElement(''); 460 | 461 | if(!is_null($params['content'])) 462 | $xml->addChild('content', $params['content']); 463 | 464 | if(!is_null($params['name'])) 465 | $xml->addChild('name', $params['name']); 466 | 467 | $data = $xml->asXml(); 468 | } 469 | else 470 | { 471 | $data_array = array('public_key' => array()); 472 | 473 | if(!is_null($params['content'])) 474 | $data_array['public_key']['content'] = $params['content']; 475 | 476 | if(!is_null($params['name'])) 477 | $data_array['public_key']['name'] = $params['name']; 478 | 479 | $data = json_encode($data_array); 480 | } 481 | 482 | return $this->_execute_curl("public_keys", $key_id . "." . $this->format, "PUT", $data); 483 | } 484 | 485 | /** 486 | * Delete a public key - can only delete own keys unless admin 487 | * 488 | * @link http://api.beanstalkapp.com/public_key.html 489 | * @param integer $key_id 490 | * @return SimpleXMLElement|array 491 | */ 492 | public function delete_public_key($key_id) { 493 | if(empty($key_id)) 494 | throw new InvalidArgumentException("Public key ID required"); 495 | 496 | return $this->_execute_curl("public_keys", $key_id . "." . $this->format, "DELETE"); 497 | } 498 | 499 | 500 | // 501 | // Feed Keys 502 | // 503 | 504 | /** 505 | * Find the Feed Key for current API user 506 | * Used to construct address to RSS/Atom feed for account/repo 507 | * 508 | * @link http://api.beanstalkapp.com/feed_key.html 509 | * @return SimpleXMLElement|array 510 | */ 511 | public function find_current_user_feed_key() 512 | { 513 | return $this->_execute_curl("feed_key." . $this->format); 514 | } 515 | 516 | 517 | // 518 | // Repositories 519 | // 520 | 521 | /** 522 | * Returns Beanstalk account repository list 523 | * 524 | * @link http://api.beanstalkapp.com/repository.html 525 | * @param integer $page [optional] Current page of results 526 | * @param integer $per_page [optional] Results per page - default 30, max 50 527 | * @return SimpleXMLElement|array 528 | */ 529 | public function find_all_repositories($page = 1, $per_page = 30) { 530 | $per_page = intval($per_page) > 50 ? 50 : intval($per_page); 531 | 532 | return $this->_execute_curl("repositories." . $this->format . "?page=" . $page . "&per_page=" . $per_page); 533 | } 534 | 535 | /** 536 | * Returns a Beanstalk account repository based on a specific repository ID 537 | * 538 | * @link http://api.beanstalkapp.com/repository.html 539 | * @param integer $repo_id required 540 | * @return SimpleXMLElement|array 541 | */ 542 | public function find_single_repository($repo_id) { 543 | if(empty($repo_id)) 544 | throw new InvalidArgumentException("Repository ID required"); 545 | else 546 | return $this->_execute_curl("repositories", $repo_id . "." . $this->format); 547 | } 548 | 549 | /** 550 | * Returns an array of the repository's branches - git only 551 | * 552 | * @link http://api.beanstalkapp.com/repository.html 553 | * @param integer $repo_id 554 | * @return SimpleXMLElement|array 555 | */ 556 | public function find_repository_branches($repo_id) { 557 | if(empty($repo_id)) 558 | throw new InvalidArgumentException("Repository ID required"); 559 | else 560 | return $this->_execute_curl("repositories", $repo_id . "/branches." . $this->format); 561 | } 562 | 563 | /** 564 | * Returns an array of the repository's tags - git only 565 | * 566 | * @link http://api.beanstalkapp.com/repository.html 567 | * @param integer $repo_id 568 | * @return SimpleXMLElement|array 569 | */ 570 | public function find_repository_tags($repo_id) { 571 | if(empty($repo_id)) 572 | throw new InvalidArgumentException("Repository ID required"); 573 | else 574 | return $this->_execute_curl("repositories", $repo_id . "/tags." . $this->format); 575 | } 576 | 577 | /** 578 | * Create a repository 579 | * 580 | * @link http://api.beanstalkapp.com/repository.html 581 | * @param string $name 582 | * @param string $type_id [optional] Can be git or subversion 583 | * @param string $title 584 | * @param bool $create_structure [optional] 585 | * @param string $color_label [optional] Accepts - red, orange, yellow, green, blue, pink, grey 586 | * @return SimpleXMLElement|array 587 | */ 588 | public function create_repository($name, $type_id = "subversion", $title, $create_structure = true, $color_label = "grey") { 589 | if(empty($name) || empty($title)) 590 | throw new InvalidArgumentException("Repository name and title required"); 591 | 592 | if($this->format == 'xml') 593 | { 594 | $xml = new SimpleXMLElement(''); 595 | 596 | $xml->addChild('name', $name); 597 | 598 | if(!is_null($type_id)) 599 | $xml->addChild('type_id', $type_id); 600 | 601 | $xml->addChild('title', $title); 602 | 603 | // 'create_structure' option applies to SVN only 604 | if ($type_id == 'subversion' && !is_null($create_structure)) 605 | $xml->addChild('create_structure', $create_structure); 606 | 607 | if(!is_null($color_label)) 608 | $xml->addChild('color_label', "label-" . $color_label); 609 | 610 | $data = $xml->asXml(); 611 | } 612 | else 613 | { 614 | $data_array = array('repository' => array()); 615 | 616 | $data_array['repository']['name'] = $name; 617 | 618 | if(!is_null($type_id)) 619 | $data_array['repository']['type_id'] = $type_id; 620 | 621 | $data_array['repository']['title'] = $title; 622 | 623 | // 'create_structure' option applies to SVN only 624 | if ($type_id == 'subversion' && !is_null($create_structure)) 625 | $data_array['repository']['create_structure'] = $create_structure; 626 | 627 | if(!is_null($color_label)) 628 | $data_array['repository']['color_label'] = "label-" . $color_label; 629 | 630 | $data = json_encode($data_array); 631 | } 632 | 633 | return $this->_execute_curl("repositories." . $this->format, NULL, "POST", $data); 634 | } 635 | 636 | /** 637 | * Update an existing repository 638 | * 639 | * @link http://api.beanstalkapp.com/repository.html 640 | * @param integer $repo_id 641 | * @param array $params Accepts - name, title, color_label (red, orange, yellow, green, blue, pink, grey) 642 | * @return SimpleXMLElement|array 643 | */ 644 | public function update_repository($repo_id, $params = array()) { 645 | if(empty($repo_id)) 646 | throw new InvalidArgumentException("Repository ID required"); 647 | 648 | if(count($params) == 0) 649 | throw new InvalidArgumentException("Nothing to update"); 650 | 651 | if($this->format == 'xml') 652 | { 653 | $xml = new SimpleXMLElement(''); 654 | 655 | if(isset($params['name'])) 656 | $xml->addChild('name', $params['name']); 657 | 658 | if(isset($params['title'])) 659 | $xml->addChild('title', $params['title']); 660 | 661 | if(isset($params['color-label'])) 662 | $xml->addChild('color_label', "label-" . $params['color_label']); 663 | 664 | $data = $xml->asXml(); 665 | } 666 | else 667 | { 668 | $data_array = array('repository' => array()); 669 | 670 | if(isset($params['name'])) 671 | $data_array['repository']['name'] = $params['name']; 672 | 673 | if(isset($params['title'])) 674 | $data_array['repository']['title'] = $params['title']; 675 | 676 | if(isset($params['color_label'])) 677 | $data_array['repository']['color_label'] = "label-" . $params['color_label']; 678 | 679 | $data = json_encode($data_array); 680 | } 681 | 682 | return $this->_execute_curl("repositories", $repo_id . "." . $this->format, "PUT", $data); 683 | } 684 | 685 | 686 | // 687 | // Repository Import 688 | // 689 | 690 | /** 691 | * Find an import - also returns the status of the import 692 | * 693 | * @link http://api.beanstalkapp.com/repository_import.html 694 | * @return SimpleXMLElement|array 695 | */ 696 | public function find_import($import_id) 697 | { 698 | if(empty($import_id)) 699 | throw new Exception("Import ID required"); 700 | 701 | return $this->_execute_curl("repository_imports", $import_id . "." . $this->format); 702 | } 703 | 704 | /** 705 | * Import an SVN dump into a repository 706 | * 707 | * @link http://api.beanstalkapp.com/repository_import.html 708 | * @param integer $repo_id 709 | * @param string $import_url 710 | * @return SimpleXMLElement|array 711 | */ 712 | public function create_import($repo_id, $import_url) 713 | { 714 | if(empty($repo_id) || empty($import_url)) 715 | throw new InvalidArgumentException("Repository ID and import URL required"); 716 | 717 | if($this->format == 'xml') 718 | { 719 | $xml = new SimpleXMLElement(''); 720 | 721 | $xml->addChild('uri', $import_url); 722 | 723 | $data = $xml->asXml(); 724 | } 725 | else 726 | { 727 | $data_array = array('repository_import' => array()); 728 | 729 | $data_array['repository_import']['uri'] = $import_url; 730 | 731 | $data = json_encode($data_array); 732 | } 733 | 734 | return $this->_execute_curl($repo_id, "repository_imports." . $this->format, "POST", $data); 735 | } 736 | 737 | 738 | // 739 | // User Permissions 740 | // 741 | 742 | /** 743 | * Find permissions for a user 744 | * 745 | * @link http://api.beanstalkapp.com/permissions.html 746 | * @param integer $user_id 747 | * @return SimpleXMLElement|array 748 | */ 749 | public function find_user_permissions($user_id) { 750 | if(empty($user_id)) 751 | throw new InvalidArgumentException("User ID required"); 752 | 753 | return $this->_execute_curl("permissions", $user_id . "." . $this->format); 754 | } 755 | 756 | /** 757 | * Create permissions for a user for a repository - overwrites existing 758 | * 759 | * @link http://api.beanstalkapp.com/permissions.html 760 | * @param integer $user_id 761 | * @param integer $repo_id 762 | * @param bool $read [optional] 763 | * @param bool $write [optional] 764 | * @param bool $full_deployments_access [optional] Gives full deployment access to a repository 765 | * @param integer $server_environment_id [optional] Give deployment access only to a specific server environment 766 | * @return SimpleXMLElement|array 767 | */ 768 | public function create_user_permissions($user_id, $repo_id, $read = false, $write = false, $full_deployments_access = false, $server_environment_id = NULL) { 769 | if(empty($user_id) || empty($repo_id)) 770 | throw new InvalidArgumentException("Some required fields missing"); 771 | 772 | if($this->format == 'xml') 773 | { 774 | $xml = new SimpleXMLElement(''); 775 | 776 | $user_xml = $xml->addChild('user_id', $user_id); 777 | $user_xml->addAttribute('type', 'integer'); 778 | 779 | $repo_xml = $xml->addChild('repository_id', $repo_id); 780 | $repo_xml->addAttribute('type', 'integer'); 781 | 782 | if($read === true) 783 | $read_xml = $xml->addChild('read', 'true'); 784 | else 785 | $read_xml = $xml->addChild('read', 'false'); 786 | 787 | $read_xml->addAttribute('type', 'boolean'); 788 | 789 | if($write === true) 790 | $write_xml = $xml->addChild('write', 'true'); 791 | else 792 | $write_xml = $xml->addChild('write', 'false'); 793 | 794 | $write_xml->addAttribute('type', 'boolean'); 795 | 796 | if($full_deployments_access === true) 797 | $full_deploy_xml = $xml->addChild('full_deployments_access', 'true'); 798 | else 799 | $full_deploy_xml = $xml->addChild('full_deployments_access', 'false'); 800 | 801 | $full_deploy_xml->addAttribute('type', 'boolean'); 802 | 803 | if(!is_null($server_environment_id)) { 804 | $environment_xml = $xml->addChild('server_environment_id', $server_environment_id); 805 | $environment_xml->addAttribute('type', 'integer'); 806 | } 807 | 808 | $data = $xml->asXml(); 809 | } 810 | else 811 | { 812 | $data_array = array('permission' => array()); 813 | 814 | $data_array['permission']['user_id'] = $user_id; 815 | $data_array['permission']['repository_id'] = $repo_id; 816 | 817 | if($read === true) 818 | $data_array['permission']['read'] = true; 819 | else 820 | $data_array['permission']['read'] = false; 821 | 822 | if($write === true) 823 | $data_array['permission']['write'] = true; 824 | else 825 | $data_array['permission']['write'] = false; 826 | 827 | if($full_deployments_access === true) 828 | $data_array['permission']['full_deployments_access'] = true; 829 | else 830 | $data_array['permission']['full_deployments_access'] = false; 831 | 832 | if(!is_null($server_environment_id)) 833 | $data_array['permission']['server_environment_id'] = $server_environment_id; 834 | 835 | $data = json_encode($data_array); 836 | } 837 | 838 | return $this->_execute_curl("permissions." . $this->format, NULL, "POST", $data); 839 | } 840 | 841 | /** 842 | * Strip a user of a set of permissions for a repository 843 | * 844 | * @link http://api.beanstalkapp.com/permissions.html 845 | * @param integer $permission_id 846 | * @return SimpleXMLElement|array 847 | */ 848 | public function delete_user_permissions($permission_id) { 849 | if(empty($permission_id)) 850 | throw new InvalidArgumentException("Permission ID required"); 851 | 852 | return $this->_execute_curl("permissions", $permission_id . "." . $this->format, "DELETE"); 853 | } 854 | 855 | 856 | // 857 | // Integrations 858 | // 859 | 860 | /** 861 | * Find all integrations for a repository 862 | * 863 | * @link http://api.beanstalkapp.com/integration.html 864 | * @param integer $repo_id 865 | * @return SimpleXMLElement|array 866 | */ 867 | public function find_all_integrations($repo_id) { 868 | if(empty($repo_id)) 869 | throw new InvalidArgumentException("Repository ID required"); 870 | 871 | return $this->_execute_curl("repositories", $repo_id . "/integrations." . $this->format); 872 | } 873 | 874 | /** 875 | * Find a single integration from a repository 876 | * 877 | * @link http://api.beanstalkapp.com/integration.html 878 | * @param integer $repo_id 879 | * @param integer $integration_id 880 | * @return SimpleXMLElement|array 881 | */ 882 | public function find_single_integration($repo_id, $integration_id) { 883 | if(empty($repo_id) || empty($integration_id)) 884 | throw new InvalidArgumentException("Repository ID and Integration ID required"); 885 | 886 | return $this->_execute_curl("repositories", $repo_id . "/integrations/" . $integration_id . "." . $this->format); 887 | } 888 | 889 | 890 | // 891 | // Changesets 892 | // 893 | 894 | /** 895 | * Returns Beanstalk account changeset list 896 | * 897 | * @link http://api.beanstalkapp.com/changeset.html 898 | * @param integer $page [optional] Current page of results 899 | * @param integer $per_page [optional] Results per page - default 15, max 30 900 | * @param string $order_field [optioanl] Order results by a field - default 'time' 901 | * @param string $order [optional] Order direction - can be ASC or DESC - default 'DESC' 902 | * @return SimpleXMLElement|array 903 | */ 904 | public function find_all_changesets($page = 1, $per_page = 15, $order_field = 'time', $order = 'DESC') { 905 | $per_page = intval($per_page) > 30 ? 30 : intval($per_page); 906 | $order = strtoupper($order) == 'ASC' ? 'ASC' : 'DESC'; 907 | 908 | return $this->_execute_curl("changesets." . $this->format . "?page=" . $page . "&per_page=" . $per_page . "&order_field" . $order_field . "&order=" . $order); 909 | } 910 | 911 | /** 912 | * Returns a Beanstalk repository changesets based on a specific repository ID 913 | * 914 | * @link http://api.beanstalkapp.com/changeset.html 915 | * @param integer $repo_id required 916 | * @param integer $page [optional] 917 | * @param integer $per_page [optional] Set results per page - default 15 918 | * @param string $order_field [optioanl] Order results by a field - default 'time' 919 | * @param string $order [optional] Order direction - can be ASC or DESC - default 'DESC' 920 | * @return SimpleXMLElement|array 921 | */ 922 | public function find_single_repository_changesets($repo_id, $page = 1, $per_page = 15, $order_field = 'time', $order = 'DESC') { 923 | if(empty($repo_id)) 924 | throw new InvalidArgumentException("Repository ID required"); 925 | 926 | $per_page = intval($per_page) > 30 ? 30 : intval($per_page); 927 | $order = strtoupper($order) == 'ASC' ? 'ASC' : 'DESC'; 928 | 929 | return $this->_execute_curl("changesets", "repository." . $this->format . "?repository_id=" . $repo_id . "&page=" . $page . "&per_page=" . $per_page . "&order_field" . $order_field . "&order=" . $order); 930 | } 931 | 932 | /** 933 | * Returns a Beanstalk repository's specific changeset based on a specific repository ID and changeset ID 934 | * 935 | * @link http://api.beanstalkapp.com/changeset.html 936 | * @param integer $repo_id required 937 | * @param integer $revision required 938 | * @return SimpleXMLElement|array 939 | */ 940 | public function find_single_changeset($repo_id, $revision) { 941 | if(empty($repo_id) || empty($revision)) 942 | throw new InvalidArgumentException("Changeset ID and repository ID required"); 943 | else 944 | return $this->_execute_curl("changesets", $revision . "." . $this->format . "?repository_id=" . $repo_id); 945 | } 946 | 947 | /** 948 | * Return the diff for a specified repository ID and changeset ID 949 | * 950 | * @link http://api.beanstalkapp.com/changeset.html 951 | * @param integer $repo_id required 952 | * @param integer $revision required 953 | * @return SimpleXMLElement|array 954 | */ 955 | public function find_changeset_diffs($repo_id, $revision) { 956 | if(empty($repo_id) || empty($revision)) 957 | throw new InvalidArgumentException("Changeset ID and repository ID required"); 958 | else 959 | return $this->_execute_curl("changesets", $revision . "/differences." . $this->format . "?repository_id=" . $repo_id); 960 | } 961 | 962 | 963 | // 964 | // Comments 965 | // 966 | 967 | /** 968 | * Returns a Beanstalk repository's comment listing 969 | * 970 | * @link http://api.beanstalkapp.com/comment.html 971 | * @param integer $repo_id required 972 | * @param integer $page [optional] Current page of results 973 | * @param integer $per_page [optional] Results per page - default 15, max 50 974 | * @return SimpleXMLElement|array 975 | */ 976 | public function find_all_comments($repo_id, $page = 1, $per_page = 15) { 977 | if(empty($repo_id)) 978 | throw new InvalidArgumentException("Repository ID required"); 979 | 980 | $per_page = intval($per_page) > 50 ? 50 : intval($per_page); 981 | 982 | return $this->_execute_curl($repo_id, "comments." . $this->format . "?page=" . $page . "&per_page=" . $per_page); 983 | } 984 | 985 | /** 986 | * Returns a Beanstalk repository's comment listing for a specific changeset 987 | * 988 | * @link http://api.beanstalkapp.com/comment.html 989 | * @param integer $repo_id required 990 | * @param integer $revision required 991 | * @param integer $page [optional] Current page of results 992 | * @param integer $per_page [optional] Results per page - default 15, max 50 993 | * @return SimpleXMLElement|array 994 | */ 995 | public function find_all_changeset_comments($repo_id, $revision, $page = 1, $per_page = 15) { 996 | if(empty($repo_id) || empty($revision)) 997 | throw new InvalidArgumentException("Repository ID and revision ID required"); 998 | 999 | $per_page = intval($per_page) > 50 ? 50 : intval($per_page); 1000 | 1001 | return $this->_execute_curl($repo_id, "comments." . $this->format . "?revision=" . $revision . "&page=" . $page . "&per_page=" . $per_page); 1002 | } 1003 | 1004 | /** 1005 | * Return comments from a specific user 1006 | * 1007 | * @link http://api.beanstalkapp.com/comment.html 1008 | * @param integer $user_id 1009 | * @param integer $page [optional] Current page of results 1010 | * @param integer $per_page [optional] Results per page - default 15, max 50 1011 | * @return SimpleXMLElement|array 1012 | */ 1013 | public function find_single_user_comments($user_id, $page = 1, $per_page = 15) { 1014 | if(empty($user_id)) 1015 | throw new InvalidArgumentException("User ID required"); 1016 | 1017 | $per_page = intval($per_page) > 50 ? 50 : intval($per_page); 1018 | 1019 | return $this->_execute_curl("comments", "user." . $this->format . "?user_id=" . $user_id . "&page=" . $page . "&per_page=" . $per_page); 1020 | } 1021 | 1022 | /** 1023 | * Returns a Beanstalk repository's comment based on a specific comment ID 1024 | * 1025 | * @link http://api.beanstalkapp.com/comment.html 1026 | * @param integer $repo_id required 1027 | * @param integer $revision required 1028 | * @return SimpleXMLElement|array 1029 | */ 1030 | public function find_single_comment($repo_id, $comment_id) { 1031 | if(empty($repo_id) || empty($comment_id)) 1032 | throw new InvalidArgumentException("Repository ID and comment ID required"); 1033 | else 1034 | return $this->_execute_curl($repo_id, "comments/" . $comment_id . "." . $this->format); 1035 | } 1036 | 1037 | /** 1038 | * Create new comment - unclear from docs which parameters are required 1039 | * 1040 | * @link http://api.beanstalkapp.com/comment.html 1041 | * @param integer $repo_id 1042 | * @param integer $revision_id 1043 | * @param string $body 1044 | * @param string $file_path 1045 | * @param integer $line_number 1046 | * @return SimpleXMLElement|array 1047 | */ 1048 | public function create_comment($repo_id, $revision_id, $body, $file_path, $line_number) { 1049 | if(empty($repo_id) || empty($revision_id) || empty($body) || empty($file_path) || empty($line_number)) 1050 | throw new InvalidArgumentException("Some required fields missing"); 1051 | 1052 | if($this->format == 'xml') 1053 | { 1054 | $xml = new SimpleXMLElement(''); 1055 | 1056 | $revision_xml = $xml->addChild('revision', $revision_id); 1057 | $revision_xml->addAttribute('type', 'integer'); 1058 | 1059 | $xml->addChild('body', $body); 1060 | $xml->addChild('file_path', $file_path); 1061 | $xml->addChild('line_number', $line_number); // Should this have type attribute set as well? 1062 | 1063 | $data = $xml->asXml(); 1064 | } 1065 | else 1066 | { 1067 | $data_array = array('comment' => array()); 1068 | 1069 | $data_array['comment']['revision'] = $revision_id; 1070 | $data_array['comment']['body'] = $body; 1071 | $data_array['comment']['file_path'] = $file_path; 1072 | $data_array['comment']['line_number'] = $line_number; 1073 | 1074 | $data = json_encode($data_array); 1075 | } 1076 | 1077 | return $this->_execute_curl($repo_id, "comments." . $this->format, "POST", $data); 1078 | } 1079 | 1080 | 1081 | // 1082 | // Server Environments 1083 | // 1084 | 1085 | /** 1086 | * Returns a Beanstalk repository's server environment listing 1087 | * 1088 | * @link http://api.beanstalkapp.com/server_environment.html 1089 | * @param integer $repo_id required 1090 | * @return SimpleXMLElement|array 1091 | */ 1092 | public function find_all_server_environments($repo_id) { 1093 | if(empty($repo_id)) 1094 | throw new InvalidArgumentException("Repository ID required"); 1095 | else 1096 | return $this->_execute_curl($repo_id, "server_environments." . $this->format); 1097 | } 1098 | 1099 | /** 1100 | * Returns a Beanstalk repository's server environment listing based on a specific environment ID 1101 | * 1102 | * @link http://api.beanstalkapp.com/server_environment.html 1103 | * @param integer $repo_id required 1104 | * @param integer $environment_id required 1105 | * @return SimpleXMLElement|array 1106 | */ 1107 | public function find_single_server_environment($repo_id, $environment_id) { 1108 | if(empty($repo_id) || empty($environment_id)) 1109 | throw new InvalidArgumentException("Repository ID required"); 1110 | else 1111 | return $this->_execute_curl($repo_id, "server_environments/" . $environment_id . "." . $this->format); 1112 | } 1113 | 1114 | /** 1115 | * Create a new server environment 1116 | * 1117 | * @link http://api.beanstalkapp.com/server_environment.html 1118 | * @param integer $repo_id 1119 | * @param string $name 1120 | * @param bool $automatic [optional] 1121 | * @param string $branch_name [optional] Git only 1122 | * @param string $color_label [optional] Accepts - red, orange, yellow, green, blue, pink, grey 1123 | * @return SimpleXMLElement|array 1124 | */ 1125 | public function create_server_environment($repo_id, $name, $automatic = false, $branch_name = NULL, $color_label = NULL) { 1126 | if(empty($repo_id) || empty($name) || ($automatic !== false && $automatic !== true)) 1127 | throw new InvalidArgumentException("Repository ID, name and deploy automatically required"); 1128 | 1129 | if($this->format == 'xml') 1130 | { 1131 | $xml = new SimpleXMLElement(''); 1132 | 1133 | $xml->addChild('name', $name); 1134 | $xml->addChild('automatic', $automatic); 1135 | 1136 | if(!is_null($branch_name)) 1137 | $xml->addChild('branch_name', $branch_name); 1138 | 1139 | if(!is_null($color_label)) 1140 | $xml->addChild('color_label', 'color-' . $color_label); 1141 | 1142 | $data = $xml->asXml(); 1143 | } 1144 | else 1145 | { 1146 | $data_array = array('server_environment' => array()); 1147 | 1148 | $data_array['server_environment']['name'] = $name; 1149 | $data_array['server_environment']['automatic'] = $automatic; 1150 | 1151 | if(!is_null($branch_name)) 1152 | $data_array['server_environment']['branch_name'] = $branch_name; 1153 | 1154 | if(!is_null($color_label)) 1155 | $data_array['server_environment']['color_label'] = 'color-' . $color_label; 1156 | 1157 | $data = json_encode($data_array); 1158 | } 1159 | 1160 | return $this->_execute_curl($repo_id, "server_environments." . $this->format, "POST", $data); 1161 | } 1162 | 1163 | /** 1164 | * Update a server environment 1165 | * 1166 | * @link http://api.beanstalkapp.com/server_environment.html 1167 | * @param integer $repo_id 1168 | * @param integer $environment_id 1169 | * @param array $params Accepts - name, automatic, branch_name 1170 | * @return SimpleXMLElement|array 1171 | */ 1172 | public function update_server_environment($repo_id, $environment_id, $params = array()) { 1173 | if(empty($repo_id) || empty($environment_id)) 1174 | throw new InvalidArgumentException("Repository ID and server environment ID requried"); 1175 | 1176 | if(count($params) == 0) 1177 | throw new InvalidArgumentException("Nothing to update"); 1178 | 1179 | if($this->format == 'xml') 1180 | { 1181 | $xml = new SimpleXMLElement(''); 1182 | 1183 | if(isset($params['name'])) 1184 | $xml->addChild('name', $params['name']); 1185 | 1186 | if(isset($params['automatic'])) 1187 | $xml->addChild('automatic', $params['automatic']); 1188 | 1189 | if(isset($params['branch_name'])) 1190 | $xml->addChild('branch_name', $params['branch_name']); 1191 | 1192 | $data = $xml->asXml(); 1193 | } 1194 | else 1195 | { 1196 | $data_array = array('server_environment' => array()); 1197 | 1198 | if(isset($params['name'])) 1199 | $data_array['server_environment']['name'] = $params['name']; 1200 | 1201 | if(isset($params['automatic'])) 1202 | $data_array['server_environment']['automatic'] = $params['automatic']; 1203 | 1204 | if(isset($params['branch_name'])) 1205 | $data_array['server_environment']['branch_name'] = $params['branch_name']; 1206 | 1207 | $data = json_encode($data_array); 1208 | } 1209 | 1210 | return $this->_execute_curl($repo_id, "server_environments/" . $environment_id . "." . $this->format, "PUT", $data); 1211 | } 1212 | 1213 | 1214 | // 1215 | // Release Servers 1216 | // 1217 | 1218 | /** 1219 | * Returns a Beanstalk repository's release server listing 1220 | * 1221 | * @link http://api.beanstalkapp.com/release_server.html 1222 | * @param integer $repo_id required 1223 | * @param integer $environment_id required 1224 | * @return SimpleXMLElement|array 1225 | */ 1226 | function find_all_release_servers($repo_id, $environment_id) { 1227 | if(empty($repo_id) || empty($environment_id)) 1228 | throw new InvalidArgumentException("Repository ID and environment ID required"); 1229 | else 1230 | return $this->_execute_curl($repo_id, "release_servers." . $this->format . "?environment_id=" . $environment_id); 1231 | } 1232 | 1233 | /** 1234 | * Returns a Beanstalk repository's release server listing based on a specific server ID 1235 | * 1236 | * @link http://api.beanstalkapp.com/release_server.html 1237 | * @param integer $repo_id required 1238 | * @param integer $server_id required 1239 | * @return SimpleXMLElement|array 1240 | */ 1241 | public function find_single_release_server($repo_id, $server_id) { 1242 | if(empty($repo_id) || empty($server_id)) 1243 | throw new InvalidArgumentException("Repository ID and server ID required"); 1244 | else 1245 | return $this->_execute_curl($repo_id, "release_servers/" . $server_id . "." . $this->format); 1246 | } 1247 | 1248 | /** 1249 | * Create a release server 1250 | * 1251 | * @link http://api.beanstalkapp.com/release_server.html 1252 | * @param integer $repo_id 1253 | * @param integer $environment_id 1254 | * @param string $name 1255 | * @param string $local_path 1256 | * @param string $remote_path 1257 | * @param string $remote_addr 1258 | * @param string $protocol [optional] Accepts - ftp, sftp 1259 | * @param integer $port [optional] 1260 | * @param string $login 1261 | * @param string $password 1262 | * @param bool $use_active_mode [optional] 1263 | * @param bool $authenticate_by_key [optional] 1264 | * @param bool $use_feat [optional] Defaults to true 1265 | * @param string $pre_release_hook [optional] 1266 | * @param string $post_release_hook [optional] 1267 | * @return SimpleXMLElement|array 1268 | */ 1269 | public function create_release_server($repo_id, $environment_id, $name, $local_path, $remote_path, $remote_addr, $protocol = 'ftp', $port = 21, $login, $password, $use_active_mode = NULL, $authenticate_by_key = NULL, $use_feat = true, $pre_release_hook = NULL, $post_release_hook = NULL) { 1270 | if(empty($repo_id) || empty($environment_id) || empty($name) || empty($local_path) || empty($remote_path) || empty($remote_addr) || empty($protocol) || empty($port) || empty($login)) 1271 | throw new InvalidArgumentException("Some required fields missing"); 1272 | 1273 | if($this->format == 'xml') 1274 | { 1275 | $xml = new SimpleXMLElement(''); 1276 | 1277 | $xml->addChild('name', $name); 1278 | $xml->addChild('local_path', $local_path); 1279 | $xml->addChild('remote_path', $remote_path); 1280 | $xml->addChild('remote_addr', $remote_addr); 1281 | 1282 | $xml->addChild('login', $login); 1283 | 1284 | if($protocol == 'sftp') { 1285 | $xml->addChild('protocol', 'sftp'); 1286 | 1287 | if($authenticate_by_key == true) { 1288 | $xml->addChild('authenticate_by_key', true); 1289 | } 1290 | else { 1291 | $xml->addChild('password', $password); 1292 | } 1293 | } 1294 | else { 1295 | $xml->addChild('protocol', 'ftp'); 1296 | $xml->addChild('password', $password); 1297 | } 1298 | 1299 | $xml->addChild('port', $port); 1300 | 1301 | if(!is_null($use_active_mode)) 1302 | $xml->addChild('use_active_mode', $use_active_mode); 1303 | 1304 | if(!is_null($use_feat)) 1305 | $xml->addChild('use_feat', $use_feat); // True by default 1306 | 1307 | if(!is_null($pre_release_hook)) 1308 | $xml->addChild('pre_release_hook', $pre_release_hook); 1309 | 1310 | if(!is_null($post_release_hook)) 1311 | $xml->addChild('post_release_hook', $post_release_hook); 1312 | 1313 | $data = $xml->asXml(); 1314 | } 1315 | else 1316 | { 1317 | $data_array = array('release_server' => array()); 1318 | 1319 | $data_array['release_server']['name'] = $name; 1320 | $data_array['release_server']['local_path'] = $local_path; 1321 | $data_array['release_server']['remote_path'] = $remote_path; 1322 | $data_array['release_server']['remote_addr'] = $remote_addr; 1323 | 1324 | $data_array['release_server']['login'] = $login; 1325 | 1326 | if($protocol == 'sftp') { 1327 | $data_array['release_server']['protocol'] = 'sftp'; 1328 | 1329 | if($authenticate_by_key == true) { 1330 | $data_array['release_server']['authenticate_by_key'] = true; 1331 | } 1332 | else { 1333 | $data_array['release_server']['password'] = $password; 1334 | } 1335 | } 1336 | else { 1337 | $data_array['release_server']['protocol'] = 'ftp'; 1338 | $data_array['release_server']['password'] = $password; 1339 | } 1340 | 1341 | $data_array['release_server']['port'] = $port; 1342 | 1343 | if(!is_null($use_active_mode)) 1344 | $data_array['release_server']['use_active_mode'] = $use_active_mode; 1345 | 1346 | if(!is_null($use_feat)) 1347 | $data_array['release_server']['use_feat'] = $use_feat; // True by default 1348 | 1349 | if(!is_null($pre_release_hook)) 1350 | $data_array['release_server']['pre_release_hook'] = $pre_release_hook; 1351 | 1352 | if(!is_null($post_release_hook)) 1353 | $data_array['release_server']['post_release_hook'] = $post_release_hook; 1354 | 1355 | $data = json_encode($data_array); 1356 | } 1357 | 1358 | return $this->_execute_curl($repo_id, "release_servers." . $this->format . "?environment_id=" . $environment_id, "POST", $data); 1359 | } 1360 | 1361 | /** 1362 | * Update a release server 1363 | * 1364 | * @link http://api.beanstalkapp.com/release_server.html 1365 | * @param integer $repo_id 1366 | * @param integer $server_id 1367 | * @param array $params Accepts - name, local_path, remote_path, remote_addr, protocol, port, login, password, use_active_mode, authenticate_by_key, use_feat, pre_release_hook, post_release_hook 1368 | * @return SimpleXMLElement|array 1369 | */ 1370 | public function update_release_server($repo_id, $server_id, $params = array()) { 1371 | if(empty($repo_id) || empty($server_id)) 1372 | throw new InvalidArgumentException("Repository ID and release server ID required"); 1373 | 1374 | if(count($params) == 0) 1375 | throw new InvalidArgumentException("Nothing to update"); 1376 | 1377 | if($this->format == 'xml') 1378 | { 1379 | $xml = new SimpleXMLElement(''); 1380 | 1381 | if(!is_null($params['name'])) 1382 | $xml->addChild('name', $params['name']); 1383 | 1384 | if(!is_null($params['local_path'])) 1385 | $xml->addChild('local_path', $params['local_path']); 1386 | 1387 | if(!is_null($params['remote_path'])) 1388 | $xml->addChild('remote_path', $params['remote_path']); 1389 | 1390 | if(!is_null($params['remote_addr'])) 1391 | $xml->addChild('remote_addr', $params['remote_addr']); 1392 | 1393 | if(!is_null($params['protocol'])) 1394 | $xml->addChild('protocol', $params['protocol']); 1395 | 1396 | if(!is_null($params['port'])) 1397 | $xml->addChild('port', $params['port']); 1398 | 1399 | if(!is_null($params['login'])) 1400 | $xml->addChild('login', $params['login']); 1401 | 1402 | if(!is_null($params['password'])) 1403 | $xml->addChild('password', $params['password']); 1404 | 1405 | if(!is_null($params['use_active_mode'])) 1406 | $xml->addChild('use_active_mode', $params['use_active_mode']); 1407 | 1408 | if(!is_null($params['authenticate_by_key'])) 1409 | $xml->addChild('authenticate_by_key', $params['authenticate_by_key']); 1410 | 1411 | if(!is_null($params['use_feat'])) 1412 | $xml->addChild('use_feat', $params['use_feat']); 1413 | 1414 | if(!is_null($params['pre_release_hook'])) 1415 | $xml->addChild('pre_release_hook', $params['pre_release_hook']); 1416 | 1417 | if(!is_null($params['post_release_hook'])) 1418 | $xml->addChild('post_release_hook', $params['post_release_hook']); 1419 | 1420 | $data = $xml->asXml(); 1421 | } 1422 | else 1423 | { 1424 | $data_array = array('release_server' => array()); 1425 | 1426 | if(!is_null($params['name'])) 1427 | $data_array['release_server']['name'] = $params['name']; 1428 | 1429 | if(!is_null($params['local_path'])) 1430 | $data_array['release_server']['local_path'] = $params['local_path']; 1431 | 1432 | if(!is_null($params['remote_path'])) 1433 | $data_array['release_server']['remote_path'] = $params['remote_path']; 1434 | 1435 | if(!is_null($params['remote_addr'])) 1436 | $data_array['release_server']['remote_addr'] = $params['remote_addr']; 1437 | 1438 | if(!is_null($params['protocol'])) 1439 | $data_array['release_server']['protocol'] = $params['protocol']; 1440 | 1441 | if(!is_null($params['port'])) 1442 | $data_array['release_server']['port'] = $params['port']; 1443 | 1444 | if(!is_null($params['login'])) 1445 | $data_array['release_server']['login'] = $params['login']; 1446 | 1447 | if(!is_null($params['password'])) 1448 | $data_array['release_server']['password'] = $params['password']; 1449 | 1450 | if(!is_null($params['use_active_mode'])) 1451 | $data_array['release_server']['use_active_mode'] = $params['use_active_mode']; 1452 | 1453 | if(!is_null($params['authenticate_by_key'])) 1454 | $data_array['release_server']['authenticate_by_key'] = $params['authenticate_by_key']; 1455 | 1456 | if(!is_null($params['use_feat'])) 1457 | $data_array['release_server']['use_feat'] = $params['use_feat']; 1458 | 1459 | if(!is_null($params['pre_release_hook'])) 1460 | $data_array['release_server']['pre_release_hook'] = $params['pre_release_hook']; 1461 | 1462 | if(!is_null($params['post_release_hook'])) 1463 | $data_array['release_server']['post_release_hook'] = $params['post_release_hook']; 1464 | 1465 | $data = json_encode($data_array); 1466 | } 1467 | 1468 | return $this->_execute($repo_id, "release_servers/" . $server_id . "." . $this->format, "PUT", $data); 1469 | } 1470 | 1471 | /** 1472 | * Delete a release server 1473 | * 1474 | * @link http://api.beanstalkapp.com/release_server.html 1475 | * @param integer $repo_id 1476 | * @param integer $server_id 1477 | * @return SimpleXMLElement|array 1478 | */ 1479 | public function delete_release_server($repo_id, $server_id) { 1480 | if(empty($repo_id) || empty($server_id)) 1481 | throw new InvalidArgumentException("Repository ID and release server ID required"); 1482 | 1483 | return $this->_execute_curl($repo_id, "release_servers/" . $server_id . "." . $this->format, "DELETE"); 1484 | } 1485 | 1486 | 1487 | // 1488 | // Releases 1489 | // 1490 | 1491 | /** 1492 | * Returns a listing of releases for an account, or for a Beanstalk repository if specified 1493 | * 1494 | * @link http://api.beanstalkapp.com/release.html 1495 | * @param integer $repo_id [optional] Releases from specified repository 1496 | * @param integer $page [optional] Current page of results 1497 | * @param integer $per_page [optional] Results per page - default 10, max 50 1498 | * @return SimpleXMLElement|array 1499 | */ 1500 | public function find_all_releases($repo_id = NULL, $page = 1, $per_page = 10) { 1501 | $per_page = intval($per_page) > 50 ? 50 : intval($per_page); 1502 | 1503 | if(empty($repo_id)) 1504 | { 1505 | return $this->_execute_curl("releases." . $this->format . "?page=" . $page . "&per_page=" . $per_page, NULL); 1506 | } 1507 | else 1508 | { 1509 | return $this->_execute_curl($repo_id, "releases." . $this->format . "?page=" . $page . "&per_page=" . $per_page); 1510 | } 1511 | } 1512 | 1513 | /** 1514 | * Returns a listing of releases for a specific repos, and optionally environment 1515 | * @param integer $repo_id 1516 | * @param integer $environment_id [optional] Optional server environment filtering 1517 | * @param integer $page [optional] Current page of results 1518 | * @param integer $per_page [optional] Results per page - default 10, max 50 1519 | * @return SimpleXmlElement|array 1520 | */ 1521 | public function find_all_repository_releases($repo_id, $environment_id = NULL, $page = 1, $per_page = 10) { 1522 | if(empty($repo_id)) 1523 | throw new InvalidArgumentException("Repository ID required"); 1524 | 1525 | $per_page = intval($per_page) > 50 ? 50 : intval($per_page); 1526 | 1527 | // Should this be changed to array of query string params and use http_build_query() ? 1528 | if(is_null($environment_id)) 1529 | { 1530 | return $this->_execute_curl($repo_id, "releases." . $this->format . "?page=" . $page . "&per_page=" . $per_page); 1531 | } 1532 | else 1533 | { 1534 | return $this->_execute_curl($repo_id, "releases." . $this->format . "?environment_id=" . $environment_id . "&page=" . $page . "&per_page=" . $per_page); 1535 | } 1536 | } 1537 | 1538 | /** 1539 | * Returns a Beanstalk repository's release based on a specific release id 1540 | * 1541 | * @link http://api.beanstalkapp.com/release.html 1542 | * @param integer $repo_id required 1543 | * @param integer $release_id required 1544 | * @return SimpleXMLElement|array 1545 | */ 1546 | public function find_single_release($repo_id, $release_id) { 1547 | if(empty($repo_id) || empty($release_id)) 1548 | throw new InvalidArgumentException("Repository ID and release ID required"); 1549 | 1550 | return $this->_execute_curl($repo_id, "releases/" . $release_id . "." . $this->format); 1551 | } 1552 | 1553 | /** 1554 | * Create a new release - ie. deploy to a server environment 1555 | * 1556 | * @link http://api.beanstalkapp.com/release.html 1557 | * @param integer $repo_id 1558 | * @param integer $environment_id 1559 | * @param integer $revision_id 1560 | * @param string $comment [optional] 1561 | * @param bool $deploy_from_scratch [optional] 1562 | * @return SimpleXMLElement|array 1563 | */ 1564 | public function create_release($repo_id, $environment_id, $revision_id, $comment = '', $deploy_from_scratch = false) { 1565 | if(empty($repo_id) || empty($environment_id) || empty($revision_id)) 1566 | throw new InvalidArgumentException("Repository ID, server environment ID and revision required"); 1567 | 1568 | if($this->format == 'xml') 1569 | { 1570 | $xml = new SimpleXMLElement(''); 1571 | 1572 | $revision_xml = $xml->addChild('revision', $revision_id); 1573 | $revision_xml->addAttribute('type', 'integer'); 1574 | 1575 | $xml->addChild('comment', $comment); 1576 | $xml->addChild('deploy_from_scratch', $deploy_from_scratch); 1577 | 1578 | $data = $xml->asXml(); 1579 | } 1580 | else 1581 | { 1582 | $data_array = array('release' => array()); 1583 | 1584 | $data_array['release']['revision'] = $revision_id; 1585 | $data_array['release']['comment'] = $comment; 1586 | $data_array['release']['deploy_from_scratch'] = $deploy_from_scratch; 1587 | 1588 | $data = json_encode($data_array); 1589 | } 1590 | 1591 | return $this->_execute_curl($repo_id, "releases." . $this->format . "?environment_id=" . $environment_id, "POST", $data); 1592 | } 1593 | 1594 | /** 1595 | * Retry a failed release 1596 | * 1597 | * @link http://api.beanstalkapp.com/release.html 1598 | * @param integer $repo_id 1599 | * @param integer $release_id 1600 | * @return SimpleXMLElement|array 1601 | */ 1602 | public function retry_release($repo_id, $release_id) { 1603 | if(empty($repo_id) || empty($release_id)) 1604 | throw new InvalidArgumentException("Repository ID and release ID required"); 1605 | 1606 | return $this->_execute_curl($repo_id, "releases/" . $release_id . "/retry." . $this->format, "PUT"); 1607 | } 1608 | 1609 | 1610 | // 1611 | // Utility functions 1612 | // 1613 | 1614 | /** 1615 | * Sets up and executes the cURL requests and returns the response 1616 | * 1617 | * @param string $api_name 1618 | * @param string $api_params [optional] 1619 | * @param string $curl_verb [optional] 1620 | * @param string $write_data [optional] 1621 | * @return SimpleXMLElement Returns false on error 1622 | */ 1623 | private function _execute_curl($api_name, $api_params = NULL, $curl_verb = "GET", $write_data = NULL) { 1624 | if( ! isset($api_params)) 1625 | $ch = curl_init("https://" . $this->account_name . ".beanstalkapp.com/api/" . $api_name); 1626 | else 1627 | $ch = curl_init("https://" . $this->account_name . ".beanstalkapp.com/api/" . $api_name . "/" . $api_params); 1628 | 1629 | $headers = array('Content-type: application/' . $this->format); 1630 | curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 1631 | curl_setopt($ch, CURLOPT_USERPWD, $this->username . ':' . $this->password); 1632 | curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1); 1633 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 1634 | 1635 | curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 1636 | 1637 | if(!is_null($write_data)) 1638 | curl_setopt($ch, CURLOPT_POSTFIELDS, $write_data); 1639 | 1640 | // Special processing for DELETE requests 1641 | if($curl_verb == 'DELETE') { 1642 | $curl_verb = 'POST'; 1643 | curl_setopt($ch, CURLOPT_HTTPHEADER, array('X-HTTP-Method-Override: DELETE')); 1644 | } 1645 | 1646 | curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $curl_verb); 1647 | 1648 | $data = curl_exec($ch); 1649 | 1650 | $this->curl_info = curl_getinfo($ch); 1651 | 1652 | // Check return code is in 2xx range 1653 | if(floor($this->curl_info['http_code'] / 100) != 2) { 1654 | $this->error_code = $this->curl_info['http_code']; 1655 | $this->error_string = "Curl request failed"; 1656 | throw new APIException($this->error_code . ": ".$this->error_string, $this->error_code); 1657 | } 1658 | 1659 | // Request failed 1660 | if ($data === FALSE) { 1661 | $this->error_code = curl_errno($ch); 1662 | $this->error_string = curl_error($ch); 1663 | curl_close($ch); 1664 | throw new APIException($this->error_code . ": " . $this->error_string, $this->error_code); 1665 | } 1666 | 1667 | curl_close($ch); 1668 | 1669 | // API can return empty responses, just return true 1670 | if(empty($data)) { 1671 | return true; 1672 | } 1673 | 1674 | if($this->format == 'xml') 1675 | { 1676 | // Process XML into SimpleXMLElement 1677 | return simplexml_load_string($data); 1678 | } 1679 | else 1680 | { 1681 | // Process JSON 1682 | return json_decode($data); 1683 | } 1684 | } 1685 | } 1686 | 1687 | // Exception thrown if there's a problem with the API 1688 | class APIException extends Exception {} 1689 | --------------------------------------------------------------------------------