11<?php
22
3- namespace JiraSmartCommits ;
3+ namespace JiraPerforceSmartCommits ;
44
5- use P4 \Spec \Change ;
6- use Zend \Http \Client as HttpClient ;
7- use Zend \Json \Json ;
8- use Zend \Mvc \MvcEvent ;
9- use Zend \ServiceManager \ServiceLocatorInterface as ServiceLocator ;
5+ use Application \Config \ConfigManager ;
6+ use Laminas \Http \Client as HttpClient ;
7+ use Laminas \Json \Json ;
8+ use Laminas \ServiceManager \ServiceLocatorInterface as ServiceLocator ;
109
1110class Module
1211{
13- public function onBootstrap ( MvcEvent $ event )
12+ public static function handleSmartCommitMessage ( $ item , ServiceLocator $ services )
1413 {
15- $ services = $ event ->getApplication ()->getServiceManager ();
16- $ events = $ services ->get ('queue ' )->getEventManager ();
17- $ config = $ this ->getJiraConfig ($ services );
18- $ projects = $ this ->getProjects ();
19- $ module = $ this ;
20-
21- // bail out if we lack a host, we won't be able to do anything
22- if (!$ config ['host ' ]) {
23- return ;
24- }
25-
26- // connect to worker 1 startup to refresh our cache of jira project ids
27- $ events ->attach (
28- 'worker.startup ' ,
29- function ($ event ) use ($ services , $ module ) {
30- // only run for the first worker.
31- if ($ event ->getParam ('slot ' ) !== 1 ) {
32- return ;
33- }
34-
35- // attempt to request the list of projects, if the request fails keep
36- // whatever list we have though as something is better than nothing.
37- $ cacheDir = $ module ->getCacheDir ();
38- $ result = $ module ->doRequest ('get ' , 'project ' , null , $ services );
39- if ($ result !== false ) {
40- $ projects = array ();
41- foreach ((array ) $ result as $ project ) {
42- if (isset ($ project ['key ' ])) {
43- $ projects [] = $ project ['key ' ];
44- }
45- }
46-
47- file_put_contents ($ cacheDir . '/projects ' , Json::encode ($ projects ));
48- }
49- },
50- -300
51- );
52-
53- $ events ->attach (
54- 'task.commit ' ,
55- function ($ event ) use ($ services , $ module ) {
56- $ change = $ event ->getParam ('change ' );
57-
58- if (!$ change instanceof Change || !$ change ->isSubmitted ()) {
59- return ;
60- }
61-
62- try {
63- $ module ->handleSmartCommitMessage ($ change , $ services );
64- } catch (\Exception $ e ) {
65- $ services ->get ('logger ' )->err ($ e );
66- }
67- },
68- -300
69- );
70- }
71-
72- public function handleSmartCommitMessage ($ item , ServiceLocator $ services )
73- {
74- $ callouts = $ this ->getJiraCallouts ($ item ->getDescription ());
75- $ config = $ this ->getJiraConfig ($ services );
14+ $ callouts = self ::getJiraCallouts ($ item ->getDescription ());
15+ $ config = $ services ->get ('config ' );
7616 $ logger = $ services ->get ('logger ' );
7717
7818 foreach ($ callouts as $ callout ) {
@@ -81,21 +21,21 @@ public function handleSmartCommitMessage($item, ServiceLocator $services)
8121 }
8222
8323 foreach ($ callout ['issues ' ] as $ issue ) {
84- $ commentCommand = $ this -> getCommand ('comment ' , $ callout ['commands ' ]);
85- $ timeCommand = $ this -> getCommand ('time ' , $ callout ['commands ' ]);
86- $ transitionCommands = $ this -> getTransitionCommands ($ callout ['commands ' ]);
24+ $ commentCommand = self :: getCommand ('comment ' , $ callout ['commands ' ]);
25+ $ timeCommand = self :: getCommand ('time ' , $ callout ['commands ' ]);
26+ $ transitionCommands = self :: getTransitionCommands ($ callout ['commands ' ]);
8727
8828 if ($ commentCommand && array_key_exists ('args ' , $ commentCommand )) {
8929 $ logger ->info ('JiraSmartCommits: building comment ' );
9030
91- if ($ config[ ' cite_submitter_username '] ) {
31+ if (ConfigManager:: getValue ( $ config, ' jirasmartcommits. cite_submitter_username ', true ) ) {
9232 $ prefix = "[~ " . $ item ->getUser () . "] says in c " ;
9333 } else {
9434 $ prefix = "C " ;
9535 }
9636
97- if ($ config[ ' link_changelist_comment_reference '] ) {
98- $ qualifiedUrl = $ services ->get ('viewhelpermanager ' )->get ('qualifiedUrl ' );
37+ if (ConfigManager:: getValue ( $ config, ' jirasmartcommits. link_changelist_comment_reference ', true ) ) {
38+ $ qualifiedUrl = $ services ->get ('ViewHelperManager ' )->get ('qualifiedUrl ' );
9939 $ changelist = "[ " . $ item ->getId () . "| " . $ qualifiedUrl ('change ' , array ('change ' => $ item ->getId ())) . "] " ;
10040 } else {
10141 $ changelist = $ item ->getId ();
@@ -122,7 +62,7 @@ public function handleSmartCommitMessage($item, ServiceLocator $services)
12262 $ timeCommented = true ;
12363 }
12464
125- if ($ this -> doRequest (
65+ if (self :: doRequest (
12666 'post ' ,
12767 "issue/ $ issue/worklog " ,
12868 $ msg ,
@@ -137,7 +77,7 @@ public function handleSmartCommitMessage($item, ServiceLocator $services)
13777 if (count ($ transitionCommands ) > 0 ) {
13878 $ logger ->info ('JiraSmartCommits: asked for transition(s) ' );
13979
140- $ availableTransitions = $ this -> doRequest (
80+ $ availableTransitions = self :: doRequest (
14181 'get ' ,
14282 "issue/ $ issue/transitions " ,
14383 null ,
@@ -190,7 +130,7 @@ public function handleSmartCommitMessage($item, ServiceLocator $services)
190130 $ transitionCommented = true ;
191131 }
192132
193- if ($ this -> doRequest (
133+ if (self :: doRequest (
194134 'post ' ,
195135 "issue/ $ issue/transitions " ,
196136 $ msg ,
@@ -208,7 +148,7 @@ public function handleSmartCommitMessage($item, ServiceLocator $services)
208148 if (isset ($ commentObj ) && !isset ($ commented )) {
209149 $ logger ->info ('JiraSmartCommits: comment wanted, not already handled. handling. ' );
210150
211- $ this -> doRequest (
151+ self :: doRequest (
212152 'post ' ,
213153 "issue/ $ issue/comment " ,
214154 $ commentObj ,
@@ -220,7 +160,7 @@ public function handleSmartCommitMessage($item, ServiceLocator $services)
220160 }
221161 }
222162
223- private function getCommand ($ key , $ commandArray )
163+ private static function getCommand ($ key , $ commandArray )
224164 {
225165 foreach ($ commandArray as $ command ) {
226166 if (is_array ($ command ) && array_key_exists ('command ' , $ command ) && $ command ['command ' ] == $ key ) {
@@ -231,7 +171,7 @@ private function getCommand($key, $commandArray)
231171 return null ;
232172 }
233173
234- private function getTransitionCommands ($ commandArray )
174+ private static function getTransitionCommands ($ commandArray )
235175 {
236176 $ ret = array ();
237177
@@ -244,16 +184,16 @@ private function getTransitionCommands($commandArray)
244184 return $ ret ;
245185 }
246186
247- public function getJiraCallouts ($ value )
187+ public static function getJiraCallouts ($ value )
248188 {
249- $ projects = array_map ('preg_quote ' , $ this -> getProjects ());
189+ $ projects = array_map ('preg_quote ' , self :: getProjects ());
250190 $ callouts = array ();
251191
252192 foreach (explode ("\n" , $ value ) as $ line ) {
253193 $ mode = 0 ;
254194 foreach (preg_split ('/(\s+)/ ' , $ line ) as $ word ) {
255- $ issues = $ this -> getJiraIssue ($ word , $ projects );
256- $ command = $ this -> getSmartCommitCommand ($ word );
195+ $ issues = self :: getJiraIssue ($ word , $ projects );
196+ $ command = self :: getSmartCommitCommand ($ word );
257197
258198 if (!isset ($ last )) {
259199 $ last = array ();
@@ -295,7 +235,7 @@ public function getJiraCallouts($value)
295235 return $ callouts ;
296236 }
297237
298- public function getJiraIssue ($ value , $ projects )
238+ public static function getJiraIssue ($ value , $ projects )
299239 {
300240 if (preg_match_all ("/((?: " . implode ('| ' , $ projects ) . ")-[0-9]+)/ " , $ value , $ match )) {
301241 return $ match [1 ];
@@ -304,7 +244,7 @@ public function getJiraIssue($value, $projects)
304244 return null ;
305245 }
306246
307- public function getSmartCommitCommand ($ value )
247+ public static function getSmartCommitCommand ($ value )
308248 {
309249 if (strpos ($ value , '# ' ) === 0 && strlen ($ value ) > 1 ) {
310250 return strtolower (substr ($ value , 1 ));
@@ -313,44 +253,44 @@ public function getSmartCommitCommand($value)
313253 return null ;
314254 }
315255
316- public function doRequest ($ method , $ resource , $ data , ServiceLocator $ services )
256+ public static function doRequest ($ method , $ resource , $ data , ServiceLocator $ services )
317257 {
318258 // we commonly do a number of requests and don't want one failure to bork them all,
319259 // if anything goes wrong just log it
320260 try {
321261 // setup the client and request details
322- $ config = $ this -> getJiraConfig ( $ services );
323- $ url = $ config[ ' host '] . '/rest/api/latest/ ' . $ resource ;
262+ $ config = $ services -> get ( ' config ' );
263+ $ url = ConfigManager:: getValue ( $ config, ' jirasmartcommits. host ') . '/rest/api/latest/ ' . $ resource ;
324264 $ client = new HttpClient ;
325265 $ client ->setUri ($ url )
326266 ->setHeaders (array ('Content-Type ' => 'application/json ' ))
327267 ->setMethod ($ method );
328268
329269 // set the http client options; including any special overrides for our host
330- $ commandtions = $ services ->get ('config ' ) + array ('http_client_options ' => array ());
331- $ commandtions = (array ) $ commandtions ['http_client_options ' ];
332- if (isset ($ commandtions ['hosts ' ][$ client ->getUri ()->getHost ()])) {
333- $ commandtions = (array ) $ commandtions ['hosts ' ][$ client ->getUri ()->getHost ()] + $ commandtions ;
270+ $ options = $ services ->get ('config ' ) + array ('http_client_options ' => array ());
271+ $ options = (array ) $ options ['http_client_options ' ];
272+ if (isset ($ options ['hosts ' ][$ client ->getUri ()->getHost ()])) {
273+ $ options = (array ) $ options ['hosts ' ][$ client ->getUri ()->getHost ()] + $ options ;
334274 }
335- unset($ commandtions ['hosts ' ]);
336- $ client ->setOptions ($ commandtions );
275+ unset($ options ['hosts ' ]);
276+ $ client ->setOptions ($ options );
337277
338278 if ($ method == 'post ' ) {
339279 $ client ->setRawBody (Json::encode ($ data ));
340280 } else {
341281 $ client ->setParameterGet ((array ) $ data );
342282 }
343283
344- if ($ config[ ' user '] ) {
345- $ client ->setAuth ($ config[ ' user '], $ config[ ' password '] );
284+ if (ConfigManager:: getValue ( $ config, ' jirasmartcommits. user ') ) {
285+ $ client ->setAuth (ConfigManager:: getValue ( $ config, ' jirasmartcommits. user '), ConfigManager:: getValue ( $ config, ' jirasmartcommits. password ') );
346286 }
347287
348288 // attempt the request and log any errors
349- $ services ->get ('logger ' )->info ('JIRA making ' . $ method . ' request to resource: ' . $ url , (array ) $ data );
289+ $ services ->get ('logger ' )->info ('JiraSmartCommits making ' . $ method . ' request to resource: ' . $ url , (array ) $ data );
350290 $ response = $ client ->dispatch ($ client ->getRequest ());
351291 if (!$ response ->isSuccess ()) {
352292 $ services ->get ('logger ' )->err (
353- 'JIRA failed to ' . $ method . ' resource: ' . $ url . ' ( ' .
293+ 'JiraSmartCommits failed to ' . $ method . ' resource: ' . $ url . ' ( ' .
354294 $ response ->getStatusCode () . " - " . $ response ->getReasonPhrase () . '). ' ,
355295 array (
356296 'request ' => $ client ->getLastRawRequest (),
@@ -371,7 +311,7 @@ public function doRequest($method, $resource, $data, ServiceLocator $services)
371311 return false ;
372312 }
373313
374- public function getProjects ()
314+ public static function getProjects ()
375315 {
376316 $ file = DATA_PATH . '/cache/jirasmartcommits/projects ' ;
377317 if (!file_exists ($ file )) {
@@ -381,7 +321,7 @@ public function getProjects()
381321 return (array ) json_decode (file_get_contents ($ file ), true );
382322 }
383323
384- public function getCacheDir ()
324+ public static function getCacheDir ()
385325 {
386326 $ dir = DATA_PATH . '/cache/jirasmartcommits ' ;
387327 if (!is_dir ($ dir )) {
@@ -399,32 +339,8 @@ public function getCacheDir()
399339 return $ dir ;
400340 }
401341
402- public function getJiraConfig (ServiceLocator $ services )
403- {
404- $ config = $ services ->get ('config ' );
405- $ config = isset ($ config ['jirasmartcommits ' ]) ? $ config ['jirasmartcommits ' ] : array ();
406- $ config += array ('host ' => null , 'user ' => null , 'password ' => null , 'job_field ' => null );
407-
408- $ config ['host ' ] = rtrim ($ config ['host ' ], '/ ' );
409- if ($ config ['host ' ] && strpos (strtolower ($ config ['host ' ]), 'http ' ) !== 0 ) {
410- $ config ['host ' ] = 'http:// ' . $ config ['host ' ];
411- }
412- return $ config ;
413- }
414-
415342 public function getConfig ()
416343 {
417344 return include __DIR__ . '/config/module.config.php ' ;
418345 }
419-
420- public function getAutoloaderConfig ()
421- {
422- return array (
423- 'Zend\Loader\StandardAutoloader ' => array (
424- 'namespaces ' => array (
425- __NAMESPACE__ => __DIR__ . '/src/ ' . __NAMESPACE__ ,
426- ),
427- ),
428- );
429- }
430346}
0 commit comments