@@ -51,6 +51,71 @@ public function __construct(Registry $options = null, Http $client = null)
5151 $ this ->client = $ client ?: HttpFactory::getHttp ($ options );
5252 }
5353
54+ /**
55+ * Get the HTTP client for this connector.
56+ *
57+ * @return Http
58+ *
59+ * @since 3.0.0
60+ */
61+ public function getClient ()
62+ {
63+ return $ this ->client ;
64+ }
65+
66+ /**
67+ * Get the diff for a pull request.
68+ *
69+ * @param string $user The name of the owner of the GitHub repository.
70+ * @param string $repo The name of the GitHub repository.
71+ * @param integer $pullId The pull request number.
72+ *
73+ * @return Response
74+ *
75+ * @since 3.0.0
76+ */
77+ public function getDiffForPullRequest ($ user , $ repo , $ pullId )
78+ {
79+ // Build the request path.
80+ $ path = "/repos/ $ user/ $ repo/pulls/ " . (int ) $ pullId ;
81+
82+ // Build the request headers.
83+ $ headers = array ('Accept ' => 'application/vnd.github.diff ' );
84+
85+ $ prepared = $ this ->prepareRequest ($ path , 0 , 0 , $ headers );
86+
87+ return $ this ->processResponse (
88+ $ this ->client ->get ($ prepared ['url ' ], $ prepared ['headers ' ])
89+ );
90+ }
91+
92+ /**
93+ * Method to build and return a full request URL for the request.
94+ *
95+ * This method will add appropriate pagination details if necessary and also prepend the API url to have a complete URL for the request.
96+ *
97+ * @param string $path Path to process
98+ * @param integer $page Page to request
99+ * @param integer $limit Number of results to return per page
100+ * @param array $headers The headers to send with the request
101+ *
102+ * @return array Associative array containing the prepared URL and request headers
103+ *
104+ * @since 3.0.0
105+ */
106+ protected function prepareRequest ($ path , $ page = 0 , $ limit = 0 ,
107+ array $ headers = array ()
108+ ) {
109+ $ url = $ this ->fetchUrl ($ path , $ page , $ limit );
110+
111+ if ($ token = $ this ->options ->get ('gh.token ' , false ))
112+ {
113+ $ headers ['Authorization ' ] = "token $ token " ;
114+ }
115+
116+ return array ('url ' => $ url , 'headers ' => $ headers );
117+ }
118+
54119 /**
55120 * Build and return a full request URL.
56121 *
@@ -70,27 +135,6 @@ protected function fetchUrl($path, $page = 0, $limit = 0)
70135 // Get a new Uri object using the API URL and given path.
71136 $ uri = new Uri ($ this ->options ->get ('api.url ' ) . $ path );
72137
73- // Only apply basic authentication if an access token is not set
74- if ($ this ->options ->get ('gh.token ' , false ) === false )
75- {
76- // Use basic authentication
77- if ($ this ->options ->get ('api.username ' , false ))
78- {
79- $ username = $ this ->options ->get ('api.username ' );
80- $ username = str_replace ('@ ' , '%40 ' , $ username );
81- $ username = str_replace ('# ' , '%23 ' , $ username );
82- $ uri ->setUser ($ username );
83- }
84-
85- if ($ this ->options ->get ('api.password ' , false ))
86- {
87- $ password = $ this ->options ->get ('api.password ' );
88- $ password = str_replace ('@ ' , '%40 ' , $ password );
89- $ password = str_replace ('# ' , '%23 ' , $ password );
90- $ uri ->setPass ($ password );
91- }
92- }
93-
94138 // If we have a defined page number add it to the JUri object.
95139 if ($ page > 0 )
96140 {
@@ -107,39 +151,32 @@ protected function fetchUrl($path, $page = 0, $limit = 0)
107151 }
108152
109153 /**
110- * Get the HTTP client for this connector.
111- *
112- * @return Http
113- *
114- * @since 3.0.0
115- */
116- public function getClient ()
117- {
118- return $ this ->client ;
119- }
120-
121- /**
122- * Get the diff for a pull request.
154+ * Process the response and return it.
123155 *
124- * @param string $user The name of the owner of the GitHub repository.
125- * @param string $repo The name of the GitHub repository.
126- * @param integer $pullId The pull request number.
156+ * @param Response $response The response.
157+ * @param integer $expectedCode The expected response code.
127158 *
128159 * @return Response
129160 *
130161 * @since 3.0.0
162+ * @throws Exception\UnexpectedResponse
131163 */
132- public function getDiffForPullRequest ( $ user , $ repo , $ pullId )
164+ protected function processResponse ( Response $ response , $ expectedCode = 200 )
133165 {
134- // Build the request path.
135- $ path = "/repos/ $ user/ $ repo/pulls/ " . (int ) $ pullId ;
136-
137- // Build the request headers.
138- $ headers = array ('Accept ' => 'application/vnd.github.diff ' );
166+ // Validate the response code.
167+ if ($ response ->code != $ expectedCode )
168+ {
169+ // Decode the error response and throw an exception.
170+ $ body = json_decode ($ response ->body );
171+ $ error = isset ($ body ->error ) ? $ body ->error
172+ : (isset ($ body ->message ) ? $ body ->message : 'Unknown Error ' );
139173
140- $ prepared = $ this ->prepareRequest ($ path , 0 , 0 , $ headers );
174+ throw new Exception \UnexpectedResponse (
175+ $ response , $ error , $ response ->code
176+ );
177+ }
141178
142- return $ this -> processResponse ( $ this -> client -> get ( $ prepared [ ' url ' ], $ prepared [ ' headers ' ])) ;
179+ return $ response ;
143180 }
144181
145182 /**
@@ -168,7 +205,9 @@ public function getFileContents($user, $repo, $path, $ref = null)
168205 $ prepared ['url ' ] = (string ) $ url ;
169206 }
170207
171- return $ this ->processResponse ($ this ->client ->get ($ prepared ['url ' ], $ prepared ['headers ' ]));
208+ return $ this ->processResponse (
209+ $ this ->client ->get ($ prepared ['url ' ], $ prepared ['headers ' ])
210+ );
172211 }
173212
174213 /**
@@ -189,7 +228,9 @@ public function getFilesForPullRequest($user, $repo, $pullId)
189228
190229 $ prepared = $ this ->prepareRequest ($ path );
191230
192- return $ this ->processResponse ($ this ->client ->get ($ prepared ['url ' ], $ prepared ['headers ' ]));
231+ return $ this ->processResponse (
232+ $ this ->client ->get ($ prepared ['url ' ], $ prepared ['headers ' ])
233+ );
193234 }
194235
195236 /**
@@ -206,9 +247,36 @@ public function getFilesForPullRequest($user, $repo, $pullId)
206247 */
207248 public function getOpenIssues ($ user , $ repo , $ page = 0 , $ limit = 0 )
208249 {
209- $ prepared = $ this ->prepareRequest ("/repos/ $ user/ $ repo/issues " , $ page , $ limit );
250+ $ prepared = $ this ->prepareRequest (
251+ "/repos/ $ user/ $ repo/issues " , $ page , $ limit
252+ );
253+
254+ return $ this ->processResponse (
255+ $ this ->client ->get ($ prepared ['url ' ], $ prepared ['headers ' ])
256+ );
257+ }
258+
259+ /**
260+ * Get a list of the open pull requests for a repository.
261+ *
262+ * @param string $user The name of the owner of the GitHub repository.
263+ * @param string $repo The name of the GitHub repository.
264+ * @param integer $page The page number from which to get items.
265+ * @param integer $limit The number of items on a page.
266+ *
267+ * @return Response
268+ *
269+ * @since 3.0.0
270+ */
271+ public function getOpenPulls ($ user , $ repo , $ page = 0 , $ limit = 0 )
272+ {
273+ $ prepared = $ this ->prepareRequest (
274+ "/repos/ $ user/ $ repo/pulls " , $ page , $ limit
275+ );
210276
211- return $ this ->processResponse ($ this ->client ->get ($ prepared ['url ' ], $ prepared ['headers ' ]));
277+ return $ this ->processResponse (
278+ $ this ->client ->get ($ prepared ['url ' ], $ prepared ['headers ' ])
279+ );
212280 }
213281
214282 /**
@@ -244,7 +312,9 @@ public function getPullRequest($user, $repo, $pullId)
244312
245313 $ prepared = $ this ->prepareRequest ($ path );
246314
247- return $ this ->processResponse ($ this ->client ->get ($ prepared ['url ' ], $ prepared ['headers ' ]));
315+ return $ this ->processResponse (
316+ $ this ->client ->get ($ prepared ['url ' ], $ prepared ['headers ' ])
317+ );
248318 }
249319
250320 /**
@@ -258,59 +328,9 @@ public function getRateLimit()
258328 {
259329 $ prepared = $ this ->prepareRequest ('/rate_limit ' );
260330
261- return $ this ->processResponse ($ this ->client ->get ($ prepared ['url ' ], $ prepared ['headers ' ]));
262- }
263-
264- /**
265- * Process the response and return it.
266- *
267- * @param Response $response The response.
268- * @param integer $expectedCode The expected response code.
269- *
270- * @return Response
271- *
272- * @since 3.0.0
273- * @throws Exception\UnexpectedResponse
274- */
275- protected function processResponse (Response $ response , $ expectedCode = 200 )
276- {
277- // Validate the response code.
278- if ($ response ->code != $ expectedCode )
279- {
280- // Decode the error response and throw an exception.
281- $ body = json_decode ($ response ->body );
282- $ error = isset ($ body ->error ) ? $ body ->error : (isset ($ body ->message ) ? $ body ->message : 'Unknown Error ' );
283-
284- throw new Exception \UnexpectedResponse ($ response , $ error , $ response ->code );
285- }
286-
287- return $ response ;
288- }
289-
290- /**
291- * Method to build and return a full request URL for the request.
292- *
293- * This method will add appropriate pagination details if necessary and also prepend the API url to have a complete URL for the request.
294- *
295- * @param string $path Path to process
296- * @param integer $page Page to request
297- * @param integer $limit Number of results to return per page
298- * @param array $headers The headers to send with the request
299- *
300- * @return array Associative array containing the prepared URL and request headers
301- *
302- * @since 3.0.0
303- */
304- protected function prepareRequest ($ path , $ page = 0 , $ limit = 0 , array $ headers = array ())
305- {
306- $ url = $ this ->fetchUrl ($ path , $ page , $ limit );
307-
308- if ($ token = $ this ->options ->get ('gh.token ' , false ))
309- {
310- $ headers ['Authorization ' ] = "token $ token " ;
311- }
312-
313- return array ('url ' => $ url , 'headers ' => $ headers );
331+ return $ this ->processResponse (
332+ $ this ->client ->get ($ prepared ['url ' ], $ prepared ['headers ' ])
333+ );
314334 }
315335
316336 /**
0 commit comments