1818
1919namespace App \Controller \Component ;
2020
21+ use App \Github \GithubApi ;
2122use Cake \Controller \Component ;
2223use Cake \Core \Configure ;
23- use Cake \Http \Client ;
2424use Cake \Http \Client \Request as ClientRequest ;
25- use Cake \Http \Client \Response as ClientResponse ;
26- use Cake \Log \Log ;
2725use Cake \Routing \Router ;
2826
29- use function array_merge ;
30- use function assert ;
3127use function http_build_query ;
32- use function in_array ;
33- use function is_array ;
34- use function is_string ;
3528use function json_encode ;
36- use function strtoupper ;
3729
3830/**
3931 * Github api component handling comunication with github.
32+ *
33+ * The API itself lives in App\Github\GithubApi, which the middleware also
34+ * uses: this component only gives it to a controller.
4035 */
4136class GithubApiComponent extends Component
4237{
38+ /**
39+ * The shared API, built when it is first used.
40+ */
41+ private GithubApi |null $ api = null ;
42+
43+ /**
44+ * The API of the component.
45+ */
46+ public function getApi (): GithubApi
47+ {
48+ return $ this ->api ??= new GithubApi ();
49+ }
50+
51+ /**
52+ * Replace the API, for a test.
53+ *
54+ * @param GithubApi $api The API to use
55+ */
56+ public function setApi (GithubApi $ api ): void
57+ {
58+ $ this ->api = $ api ;
59+ }
60+
4361 /**
4462 * perform an api request given a path, the data to send, the method and whether
4563 * or not to return a status.
4664 *
47- * @see GithubApiComponent ::sendRequest()
65+ * @see GithubApi ::sendRequest()
4866 *
4967 * @param string $path the api path to preform the request to
5068 * @param array|string $data the data to send in the request. This works with both GET
@@ -56,7 +74,7 @@ class GithubApiComponent extends Component
5674 * @phpstan-param ClientRequest::METHOD_* $method
5775 *
5876 * @return array the returned response decoded and optionally the status code,
59- * see GithubApiComponent::sendRequest()
77+ * eg: array($decodedResponse, $statusCode) or just $decodedResponse
6078 */
6179 public function apiRequest (
6280 string $ path ,
@@ -65,63 +83,24 @@ public function apiRequest(
6583 bool $ returnStatus = false ,
6684 string $ access_token = '' ,
6785 ): array {
68- $ path = 'https://api.github.com/ ' . $ path ;
69- if (strtoupper ($ method ) === ClientRequest::METHOD_GET ) {
70- $ path .= '? ' . http_build_query ($ data );
71- $ data = [];
72- }
73-
74- return $ this ->sendRequest ($ path , $ data , $ method , $ returnStatus , $ access_token );
75- }
76-
77- /**
78- * retrieve an access token using a code that has been authorized by a user.
79- *
80- * @param string $code the code returned by github to the callback url
81- *
82- * @return string|null the access token
83- */
84- public function getAccessToken (string |null $ code ): string |null
85- {
86- $ url = 'https://github.com/login/oauth/access_token ' ;
87- $ data = array_merge (
88- Configure ::read ('GithubConfig ' , []),
89- ['code ' => $ code ],
90- );
91- $ decodedResponse = $ this ->sendRequest ($ url , http_build_query ($ data ), ClientRequest::METHOD_POST );
92-
93- return $ decodedResponse ['access_token ' ] ?? null ;
94- }
95-
96- /**
97- * retrieve the github info stored on a user by his access token.
98- *
99- * @param string $accessToken the access token belonging to the user being
100- * requested
101- *
102- * @return array the github info returned by github as an associative array
103- */
104- public function getUserInfo (string $ accessToken ): array
105- {
106- return $ this ->apiRequest ('user ' , [], ClientRequest::METHOD_GET , true , $ accessToken );
86+ return $ this ->getApi ()->apiRequest ($ path , $ data , $ method , $ returnStatus , $ access_token );
10787 }
10888
10989 /**
11090 * perform an http request using curl given a url, the post data to send, the
11191 * request method and whether or not to return a status.
11292 *
93+ * @see GithubApi::sendRequest()
94+ *
11395 * @param string $url the url to preform the request to
114- * @param array|string $data the post data to send in the request. This only works with POST requests. GET requests need the data appended in the url.
115- * with POST requests. GET requests need the data appended
116- * in the url.
96+ * @param array|string $data the post data to send in the request
11797 * @param string $method the method type of the request
11898 * @param bool $returnCode whether to return the status code with the
11999 * request
120100 * @param string $accessToken the github access token
121101 * @phpstan-param ClientRequest::METHOD_* $method
122102 *
123- * @return array the returned response decoded and optionally the status code,
124- * eg: array($decodedResponse, $statusCode) or just $decodedResponse
103+ * @return array the returned response decoded and optionally the status code
125104 */
126105 public function sendRequest (
127106 string $ url ,
@@ -130,45 +109,31 @@ public function sendRequest(
130109 bool $ returnCode = false ,
131110 string $ accessToken = '' ,
132111 ): array {
133- Log ::debug ('Request-url: ' . $ url );
134-
135- $ headers = [
136- 'Accept ' => 'application/json ' ,
137- 'User-Agent ' => 'phpMyAdmin - Error Reporting Server ' ,
138- ];
139- if ($ accessToken !== '' ) {
140- $ headers ['Authorization ' ] = 'token ' . $ accessToken ;
141- }
142-
143- $ request = new ClientRequest (
144- url: $ url ,
145- method: $ method ,
146- headers: $ headers ,
147- data: $ data ,
148- );
149- $ http = new Client ([
150- 'redirect ' => 5 ,// Max redirects to follow
151- ]);
152- $ response = $ http ->sendRequest ($ request );
153-
154- $ status = $ response ->getStatusCode ();
155- assert ($ response instanceof ClientResponse);
156-
157- Log ::debug ('Response-code: ' . $ status . ' for: ' . $ url );
158-
159- $ decodedResponse = $ response ->getJson ();
160- if ($ status === 401 ) {
161- Log ::error ('[GitHub] error ' . $ status . ' on ' . $ url . ' and data ' . json_encode ($ decodedResponse ) . ' maybe the token is expired ? ' );
162- }
112+ return $ this ->getApi ()->sendRequest ($ url , $ data , $ method , $ returnCode , $ accessToken );
113+ }
163114
164- if ($ returnCode ) {
165- return [
166- $ decodedResponse ,
167- $ status ,
168- ];
169- }
115+ /**
116+ * Exchange the code of the callback for an access token.
117+ *
118+ * @param string $code The code github sends back
119+ *
120+ * @return string|null the access token, null when it was refused
121+ */
122+ public function getAccessToken (string $ code ): string |null
123+ {
124+ return $ this ->getApi ()->getAccessToken ($ code );
125+ }
170126
171- return $ decodedResponse ;
127+ /**
128+ * Read the account of the token.
129+ *
130+ * @param string $accessToken the github access token
131+ *
132+ * @return array the github info returned by github as an associative array
133+ */
134+ public function getUserInfo (string $ accessToken ): array
135+ {
136+ return $ this ->getApi ()->getUserInfo ($ accessToken );
172137 }
173138
174139 /**
@@ -217,63 +182,9 @@ public function getRedirectUrl(string|null $scope = null, string|null $state = n
217182 */
218183 public function canCommitTo (string $ username , string $ repoPath , string $ access_token ): bool
219184 {
220- [$ role , $ status ] = $ this ->getCollaboratorPermission ($ username , $ repoPath , $ access_token );
221-
222- if ($ status !== 200 ) {
223- // The token of a login only carries the scopes the user agreed to
224- // share, and the endpoint refuses a read-only account as well:
225- // ask a second time with the token of the server, which is not
226- // tied to what a user agreed to give.
227- $ serverToken = (string ) Configure ::read ('GithubAccessToken ' );
228- if ($ serverToken !== '' && $ serverToken !== $ access_token ) {
229- [$ role , $ status ] = $ this ->getCollaboratorPermission ($ username , $ repoPath , $ serverToken );
230- }
231- }
232-
233- if ($ status !== 200 ) {
234- Log ::error (
235- 'Collaborator permission call for ' . $ username . ' on ' . $ repoPath
236- . ' ended in a status code: ' . $ status ,
237- );
238-
239- return false ;
240- }
241-
242- if ($ role === null ) {
243- Log ::error ('Collaborator permission call for ' . $ username . ' on ' . $ repoPath . ' returned no data ' );
244-
245- return false ;
246- }
247-
248- return in_array ($ role , ['admin ' , 'maintain ' , 'write ' ], true );
249- }
250-
251- /**
252- * Read the permission of a user on a repository.
253- *
254- * @param string $username GitHub login of the user
255- * @param string $repoPath The repo slug
256- * @param string $access_token Token used to ask the API
257- *
258- * @return array{string|null, int} The role of the user, and the status code
259- */
260- private function getCollaboratorPermission (string $ username , string $ repoPath , string $ access_token ): array
261- {
262- [$ permission , $ status ] = $ this ->apiRequest (
263- 'repos/ ' . $ repoPath . '/collaborators/ ' . $ username . '/permission ' ,
264- [],
265- ClientRequest::METHOD_GET ,
266- true ,
267- $ access_token ,
268- );
269-
270- if (! is_array ($ permission )) {
271- return [null , $ status ];
272- }
273-
274- $ role = $ permission ['permission ' ] ?? $ permission ['role_name ' ] ?? null ;
275-
276- return [is_string ($ role ) ? $ role : null , $ status ];
185+ // A controller cannot keep an older answer: an unanswered check is a
186+ // refusal.
187+ return $ this ->getApi ()->canCommitTo ($ username , $ repoPath , $ access_token ) ?? false ;
277188 }
278189
279190 /**
0 commit comments