@@ -15,8 +15,15 @@ class SourceGithubPlugin extends MantisSourceGitBasePlugin {
1515 const FRAMEWORK_VERSION_REQUIRED = '2.2.0 ' ;
1616 const MANTIS_VERSION = '2.3.0 ' ;
1717
18+ const URL_API = 'https://api.github.com/ ' ;
19+
1820 public $ linkPullRequest = '/pull/%s ' ;
1921
22+ /**
23+ * @var \GuzzleHttp\Client
24+ */
25+ private $ githubApi ;
26+
2027 public function register () {
2128 $ this ->name = plugin_lang_get ( 'title ' );
2229 $ this ->description = plugin_lang_get ( 'description ' );
@@ -133,21 +140,18 @@ public function route_webhook( $p_request, $p_response, $p_args ) {
133140 . '&api_key= ' . plugin_config_get ( 'api_key ' );
134141
135142 # Retrieve existing webhooks
143+ $ t_api_uri = "repos/ $ t_username/ $ t_reponame/hooks " ;
136144 try {
137- $ t_github_api = new \GuzzleHttp \Client ();
138- $ t_api_uri = SourceGithubPlugin::api_uri ( $ t_repo , "repos/ $ t_username/ $ t_reponame/hooks " );
139-
140- $ t_response = $ t_github_api ->get ( $ t_api_uri );
145+ $ t_hooks = $ this ->api_json_url ( $ t_repo , $ t_api_uri );
141146 }
142147 catch ( GuzzleHttp \Exception \ClientException $ e ) {
143148 return $ e ->getResponse ();
144149 }
145- $ t_hooks = json_decode ( (string ) $ t_response ->getBody () );
146150
147151 # Determine if there is already a webhook attached to the plugin's payload URL
148152 $ t_id = false ;
149153 foreach ( $ t_hooks as $ t_hook ) {
150- if ( $ t_hook ->name == 'web ' && $ t_hook ->config ->url == $ t_payload_url ) {
154+ if ( $ t_hook ->name == 'web ' && $ t_hook ->config ->url == $ t_payload_url ) {
151155 $ t_id = $ t_hook ->id ;
152156 break ;
153157 }
@@ -174,7 +178,8 @@ public function route_webhook( $p_request, $p_response, $p_args ) {
174178 )
175179 );
176180
177- $ t_github_response = $ t_github_api ->post ( $ t_api_uri ,
181+ $ t_github_response = $ this ->githubApi ->post (
182+ $ t_api_uri ,
178183 array ( GuzzleHttp \RequestOptions::JSON => $ t_payload )
179184 );
180185 }
@@ -408,17 +413,57 @@ public function update_repo( $p_repo ) {
408413 return $ p_repo ;
409414 }
410415
411- private function api_uri ( $ p_repo , $ p_path ) {
412- $ t_uri = 'https://api.github.com/ ' . $ p_path ;
416+ /**
417+ * Initialize GitHub API for the given Repository.
418+ *
419+ * @param SourceRepo $p_repo Repository
420+ */
421+ private function api_init ( $ p_repo ) {
422+ # Initialize Guzzle client if not done already
423+ if ( !$ this ->githubApi ) {
424+ $ t_options = array (
425+ 'base_uri ' => self ::URL_API
426+ );
413427
414- if ( isset ( $ p_repo ->info ['hub_app_access_token ' ] ) ) {
415- $ t_access_token = $ p_repo ->info ['hub_app_access_token ' ];
416- if ( !is_blank ( $ t_access_token ) ) {
417- $ t_uri .= '?access_token= ' . $ t_access_token ;
428+ # Set the Authorization header
429+ if ( isset ( $ p_repo ->info ['hub_app_access_token ' ] ) ) {
430+ $ t_access_token = $ p_repo ->info ['hub_app_access_token ' ];
431+ if ( !is_blank ( $ t_access_token ) ) {
432+ $ t_options [RequestOptions::HEADERS ] = array (
433+ 'Authorization ' => 'token ' . $ t_access_token ,
434+ );
435+ }
418436 }
437+
438+ $ this ->githubApi = new GuzzleHttp \Client ( $ t_options );
419439 }
440+ return $ this ->githubApi ;
441+ }
420442
421- return $ t_uri ;
443+ /**
444+ * Retrieves data from the GitHub API for the given repository.
445+ *
446+ * The JSON data is returned as an stdClass object,
447+ *
448+ * @param SourceRepo $p_repo Repository
449+ * @param string $p_path GitHub API path
450+ * @param string $p_member Optional top-level member to retrieve
451+ *
452+ * @return stdClass|false
453+ */
454+ private function api_get ( $ p_repo , $ p_path , $ p_member = '' ) {
455+ $ this ->api_init ( $ p_repo );
456+
457+ $ t_response = $ this ->githubApi ->get ( $ p_path );
458+ $ t_json = json_decode ( (string ) $ t_response ->getBody () );
459+
460+ if ( empty ( $ p_member ) ) {
461+ return $ t_json ;
462+ } elseif ( property_exists ( $ t_json , $ p_member ) ) {
463+ return $ t_json ->$ p_member ;
464+ } else {
465+ return false ;
466+ }
422467 }
423468
424469 private function api_json_url ( $ p_repo , $ p_url , $ p_member = null ) {
@@ -429,8 +474,7 @@ private function api_json_url( $p_repo, $p_url, $p_member = null ) {
429474 $ t_start_time = microtime ( true );
430475 }
431476
432- $ t_uri = $ this ->api_uri ( $ p_repo , 'rate_limit ' );
433- $ t_json = json_url ( $ t_uri , 'rate ' );
477+ $ t_json = $ this ->api_get ( $ p_repo , 'rate_limit ' , 'rate ' );
434478
435479 if ( false !== $ t_json && !is_null ( $ t_json ) ) {
436480 if ( $ t_json ->remaining <= 0 ) {
@@ -442,7 +486,7 @@ private function api_json_url( $p_repo, $p_url, $p_member = null ) {
442486 }
443487 }
444488
445- return json_url ( $ p_url , $ p_member );
489+ return $ this -> api_get ( $ p_repo , $ p_url , $ p_member );
446490 }
447491
448492 public function precommit () {
@@ -538,12 +582,10 @@ public function import_full( $p_repo ) {
538582 $ t_username = $ p_repo ->info ['hub_username ' ];
539583 $ t_reponame = $ p_repo ->info ['hub_reponame ' ];
540584
541- $ t_uri = $ this ->api_uri ( $ p_repo , "repos/ $ t_username/ $ t_reponame/branches " );
542- $ t_json = $ this ->api_json_url ( $ p_repo , $ t_uri );
585+ $ t_json = $ this ->api_json_url ( $ p_repo , "repos/ $ t_username/ $ t_reponame/branches " );
543586
544587 $ t_branches = array ();
545- foreach ($ t_json as $ t_branch )
546- {
588+ foreach ($ t_json as $ t_branch ) {
547589 $ t_branches [] = $ t_branch ->name ;
548590 }
549591 }
@@ -599,8 +641,7 @@ public function import_commits( $p_repo, $p_commit_ids, $p_branch='' ) {
599641 $ t_commit_id = array_shift ( $ s_parents );
600642
601643 echo "Retrieving $ t_commit_id ... " ;
602- $ t_uri = $ this ->api_uri ( $ p_repo , "repos/ $ t_username/ $ t_reponame/commits/ $ t_commit_id " );
603- $ t_json = $ this ->api_json_url ( $ p_repo , $ t_uri );
644+ $ t_json = $ this ->api_json_url ( $ p_repo , "repos/ $ t_username/ $ t_reponame/commits/ $ t_commit_id " );
604645
605646 if ( false === $ t_json || is_null ( $ t_json ) ) {
606647 # Some error occured retrieving the commit
0 commit comments