GitLab.class.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. class GitLab{
  3. public $token,$host;
  4. public function projects($id = null){
  5. $projects = $this->rest('GET','/projects');
  6. return $projects;
  7. }
  8. public function groups($id = null){
  9. $projects = $this->rest('GET','/groups?per_page=1000');
  10. return $projects;
  11. }
  12. public function project_save($group,$label,$slug){
  13. $body = array(
  14. "name"=> $slug,
  15. "path"=> $slug,
  16. "description"=> $label,
  17. 'namespace_id' => $group
  18. );
  19. $project = $this->rest('POST','/projects',json_encode($body));
  20. return $project;
  21. }
  22. public function groupe_save($label,$slug){
  23. $body = array(
  24. "name"=> $slug,
  25. "path"=> $slug,
  26. "description"=> $label,
  27. "visibility"=> "internal",
  28. "share_with_group_lock"=> false,
  29. "require_two_factor_authentication"=> false,
  30. "two_factor_grace_period"=> 48,
  31. "project_creation_level"=> "developer",
  32. "auto_devops_enabled"=> null,
  33. "subgroup_creation_level"=> "owner",
  34. "emails_disabled"=> null,
  35. "mentions_disabled"=> null,
  36. "lfs_enabled"=> true,
  37. "default_branch_protection"=> 1,
  38. "avatar_url"=> null,
  39. "request_access_enabled"=> false,
  40. "parent_id"=> null
  41. );
  42. $project = $this->rest('POST','/groups',json_encode($body));
  43. return $project;
  44. }
  45. //Requete rest
  46. public function rest($method,$action,$body='',$headers=array()){
  47. $url = $this->host.$action;
  48. $ch = curl_init();
  49. $options[CURLOPT_URL] = $url;
  50. $options[CURLOPT_RETURNTRANSFER] = true;
  51. $options[CURLOPT_SSL_VERIFYPEER] = false;
  52. $options[CURLOPT_FOLLOWLOCATION] = true;
  53. $options[CURLOPT_SSL_VERIFYPEER] = false;
  54. $options[CURLOPT_USERAGENT] = 'Awesome erp';
  55. $options[CURLOPT_CUSTOMREQUEST] = $method;
  56. if(!empty($body)) $options[CURLOPT_POSTFIELDS] = $body;
  57. $headers = array();
  58. $headers[] = 'Content-Type: application/json';
  59. $headers[] = 'Accept: application/json';
  60. $headers[] = 'Private-Token: '.$this->token;
  61. $options[CURLOPT_HTTPHEADER] = $headers;
  62. curl_setopt_array($ch,$options);
  63. $response = curl_exec($ch);
  64. if($response === false) throw new Exception(curl_error($ch));
  65. curl_close($ch);
  66. return json_decode($response,true);
  67. }
  68. }
  69. ?>