2 namespace Robo\Task\Development;
4 use Robo\Exception\TaskException;
5 use Robo\Task\BaseTask;
7 abstract class GitHub extends BaseTask
9 const GITHUB_URL = 'https://api.github.com';
19 protected $password = '';
34 protected $accessToken;
41 public function repo($repo)
48 * @param string $owner
52 public function owner($owner)
54 $this->owner = $owner;
63 public function uri($uri)
65 list($this->owner, $this->repo) = explode('/', $uri);
72 protected function getUri()
74 return $this->owner . '/' . $this->repo;
82 public function user($user)
93 public function password($password)
95 $this->password = $password;
100 * @param $accessToken
104 public function accessToken($token)
106 $this->accessToken = $token;
112 * @param array $params
113 * @param string $method
117 * @throws \Robo\Exception\TaskException
119 protected function sendRequest($uri, $params = [], $method = 'POST')
121 if (!$this->owner or !$this->repo) {
122 throw new TaskException($this, 'Repo URI is not set');
126 $url = sprintf('%s/repos/%s/%s', self::GITHUB_URL, $this->getUri(), $uri);
127 $this->printTaskInfo($url);
128 $this->printTaskInfo('{method} {url}', ['method' => $method, 'url' => $url]);
130 if (!empty($this->user)) {
131 curl_setopt($ch, CURLOPT_USERPWD, $this->user . ':' . $this->password);
134 if (!empty($this->accessToken)) {
135 $url .= "?access_token=" . $this->accessToken;
142 CURLOPT_RETURNTRANSFER => true,
143 CURLOPT_POST => $method != 'GET',
144 CURLOPT_POSTFIELDS => json_encode($params),
145 CURLOPT_FOLLOWLOCATION => true,
146 CURLOPT_USERAGENT => "Robo"
150 $output = curl_exec($ch);
151 $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
152 $response = json_decode($output);
154 $this->printTaskInfo($output);
155 return [$code, $response];