PageRenderTime 34ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/api/App.php

https://github.com/rmiddle/wgm.issues
PHP | 467 lines | 334 code | 105 blank | 28 comment | 35 complexity | c7453f4a7e4b5d8b5757d96d5cba07aa MD5 | raw file
  1. <?php
  2. if (class_exists('Extension_ActivityTab')):
  3. class IssuesActivityTab extends Extension_ActivityTab {
  4. const VIEW_ACTIVITY_ISSUES = 'activity_issue';
  5. function showTab() {
  6. $translate = DevblocksPlatform::getTranslationService();
  7. $tpl = DevblocksPlatform::getTemplateService();
  8. $defaults = new C4_AbstractViewModel();
  9. $defaults->class_name = 'View_Issue';
  10. $defaults->id = self::VIEW_ACTIVITY_ISSUES;
  11. $defaults->name = $translate->_('issues.activity.tab');
  12. $defaults->view_columns = array(
  13. SearchFields_Issue::CREATED_DATE,
  14. SearchFields_Issue::UPDATED_DATE
  15. );
  16. $defaults->renderSortBy = SearchFields_Issue::CREATED_DATE;
  17. $defaults->renderSortAsc = 0;
  18. $view = C4_AbstractViewLoader::getView(self::VIEW_ACTIVITY_ISSUES, $defaults);
  19. $tpl->assign('view', $view);
  20. $tpl->display('devblocks:wgm.issues::activity_tab/index.tpl');
  21. }
  22. }
  23. endif;
  24. class Wgm_Controller extends DevblocksControllerExtension {
  25. function isVisible() {
  26. // The current session must be a logged-in worker to use this page.
  27. if(null == ($worker = CerberusApplication::getActiveWorker()))
  28. return false;
  29. return true;
  30. }
  31. /*
  32. * Request Overload
  33. */
  34. function handleRequest(DevblocksHttpRequest $request) {
  35. $worker = CerberusApplication::getActiveWorker();
  36. if(empty($worker)) return;
  37. $stack = $request->path;
  38. array_shift($stack); // internal
  39. @$action = array_shift($stack) . 'Action';
  40. switch($action) {
  41. case NULL:
  42. // [TODO] Index/page render
  43. break;
  44. default:
  45. // Default action, call arg as a method suffixed with Action
  46. if(method_exists($this,$action)) {
  47. call_user_func(array(&$this, $action));
  48. }
  49. break;
  50. }
  51. }
  52. function writeResponse(DevblocksHttpResponse $response) {
  53. return;
  54. }
  55. public function showIssuePeekAction() {
  56. }
  57. public function displayIssueAction() {
  58. @$id = DevblocksPlatform::importGPC($_REQUEST['id'], 'int', 0);
  59. }
  60. }
  61. if(class_exists('Extension_PageMenuItem')):
  62. class Wgm_SetupPluginsMenuItem extends Extension_PageMenuItem {
  63. const POINT = 'wgmissue.setup.menu.plugins.issue';
  64. function render() {
  65. $tpl = DevblocksPlatform::getTemplateService();
  66. $tpl->assign('extension', $this);
  67. $tpl->display('devblocks:wgm.issues::setup/menu_item.tpl');
  68. }
  69. };
  70. endif;
  71. if(class_exists('Extension_PageSection')):
  72. class Wgm_SetupSection extends Extension_PageSection {
  73. const ID = 'wgmissue.setup.issue';
  74. function render() {
  75. // check whether extensions are loaded or not
  76. $extensions = array(
  77. 'oauth' => extension_loaded('oauth')
  78. );
  79. $tpl = DevblocksPlatform::getTemplateService();
  80. $visit = CerberusApplication::getVisit();
  81. $visit->set(ChConfigurationPage::ID, 'issue');
  82. $params = array(
  83. 'client_id' => DevblocksPlatform::getPluginSetting('wgm.issues', 'client_id', ''),
  84. 'client_secret' => DevblocksPlatform::getPluginSetting('wgm.issues', 'client_secret', ''),
  85. 'repos' => DAO_Repository::getAll(),
  86. 'users' => DAO_User::getWhere(),
  87. );
  88. $tpl->assign('params', $params);
  89. $tpl->assign('extensions', $extensions);
  90. $tpl->display('devblocks:wgm.issues::setup/index.tpl');
  91. }
  92. function saveJsonAction() {
  93. try {
  94. @$client_id = DevblocksPlatform::importGPC($_REQUEST['client_id'], 'string', '');
  95. @$client_secret = DevblocksPlatform::importGPC($_REQUEST['client_secret'], 'string', '');
  96. if(empty($client_id) || empty($client_secret))
  97. throw new Exception("Both the API Auth Token and URL are required.");
  98. DevblocksPlatform::setPluginSetting('wgm.issues', 'client_id', $client_id);
  99. DevblocksPlatform::setPluginSetting('wgm.issues', 'client_secret', $client_secret);
  100. echo json_encode(array('status' => true, 'message' => 'Saved!'));
  101. return;
  102. } catch (Exception $e) {
  103. echo json_encode(array('status' => false, 'error' => $e->getMessage()));
  104. return;
  105. }
  106. }
  107. function toggleRepoAction() {
  108. @$repo_id = DevblocksPlatform::importGPC($_REQUEST['repo_id'], 'int', 0);
  109. @$repo_action = DevblocksPlatform::importGPC($_REQUEST['repo_action'], 'string', '');
  110. $repos = DAO_Repository::getAll();
  111. try {
  112. if(!array_key_exists($repo_id, $repos))
  113. throw new Exception("This is not the repo you are looking for. Does it exist?");
  114. $user = DAO_User::get($repos[$repo_id]->user_id);
  115. $repository = sprintf("%s/%s",$user->login, $repos[$repo_id]->name);
  116. // enable/disable repo
  117. if($repo_action == 'disable') {
  118. $fields = array(
  119. DAO_Repository::ENABLED => false
  120. );
  121. echo json_encode(array('status'=>true,'message'=>$repository.' was disabled!'));
  122. } elseif ($repo_action == 'enable') {
  123. $fields = array(
  124. DAO_Repository::ENABLED => false
  125. );
  126. echo json_encode(array('status'=>true,'message'=>$repository.' was enabled!'));
  127. }
  128. DAO_Repository::update($repo_id, $fields);
  129. } catch (Exception $e) {
  130. echo json_encode(array('status'=>false,'error'=>$e->getMessage()));
  131. return;
  132. }
  133. }
  134. };
  135. endif;
  136. abstract class Extension_IssueSource extends DevblocksExtension {
  137. const POINT = 'source.issues.wgm';
  138. public function sync($max_issues, &$synced) {}
  139. }
  140. abstract class Extension_ContainerSource extends DevblocksExtension {
  141. const POINT = 'source.containers.wgm';
  142. public function sync($max_containers, &$synced) {
  143. }
  144. }
  145. abstract class Extension_MilestoneSource extends DevblocksExtension {
  146. const POINT = 'source.milestones.wgm';
  147. public function sync($max_milestones, &$synced) {
  148. }
  149. }
  150. class IssueCron extends CerberusCronPageExtension {
  151. function run() {
  152. $logger = DevblocksPlatform::getConsoleLog();
  153. $logger->info("[Issues] Syncing Issues");
  154. $timeout = ini_get('max_execution_time');
  155. $runtime = microtime(true);
  156. // Max issues to sync
  157. $max_issues = $this->getParam('max_issues', 100);
  158. // Load source extensions
  159. $source_exts = DevblocksPlatform::getExtensions(Extension_IssueSource::POINT, true);
  160. $synced = 0;
  161. foreach($source_exts as $source) {
  162. $logger->info(sprintf('[Issues] Syncing issues from %s', $source->manifest->name));
  163. $source->sync($max_issues, $synced);
  164. // check amount of issues synced
  165. if($synced == $max_issues) {
  166. break;
  167. }
  168. }
  169. $logger->info("[Issues] Total Runtime: ".number_format((microtime(true)-$runtime)*1000,2)." ms");
  170. }
  171. function configure($instance) {
  172. $tpl = DevblocksPlatform::getTemplateService();
  173. $tpl->assign('max_issues', $this->getParam('max_issues', 100));
  174. $tpl->display('devblocks:wgm.issues::setup/cron/issues.tpl');
  175. }
  176. function saveConfigurationAction() {
  177. @$max_issues = DevblocksPlatform::importGPC($_POST['max_issues'],'integer');
  178. $this->setParam('max_issues', $max_issues);
  179. }
  180. }
  181. class ContainerSyncCron extends CerberusCronPageExtension {
  182. function run() {
  183. $logger = DevblocksPlatform::getConsoleLog();
  184. $logger->info("[Issues] Syncing Containers");
  185. $timeout = ini_get('max_execution_time');
  186. $runtime = microtime(true);
  187. // Max containers to sync
  188. $max_containers = $this->getParam('max_containers', 100);
  189. // Load container sources
  190. $source_exts = DevblocksPlatform::getExtensions(Extension_ContainerSource::POINT, true);
  191. $synced = 0;
  192. foreach($source_exts as $source) {
  193. $logger->info(sprintf('[Issues] Syncing containers from %s', $source->manifest->name));
  194. $source->sync($max_containers, $synced);
  195. // check amount of issues synced
  196. if($synced == $max_containers) {
  197. break;
  198. }
  199. }
  200. $logger->info("[Issues] Total Runtime: ".number_format((microtime(true)-$runtime)*1000,2)." ms");
  201. }
  202. function configure($instance) {
  203. $tpl = DevblocksPlatform::getTemplateService();
  204. $tpl->assign('max_containers', $this->getParam('max_containers', 100));
  205. $tpl->display('devblocks:wgm.issues::setup/cron/containers.tpl');
  206. }
  207. function saveConfigurationAction() {
  208. @$max_containers = DevblocksPlatform::importGPC($_POST['max_containers'],'integer');
  209. $this->setParam('max_containers', $max_containers);
  210. }
  211. }
  212. class ContainerRecacheCron extends CerberusCronPageExtension {
  213. function run() {
  214. $logger = DevblocksPlatform::getConsoleLog();
  215. $logger->info("[Issues] Recaching Containers");
  216. $timeout = ini_get('max_execution_time');
  217. $runtime = microtime(true);
  218. // Max containers to sync
  219. $max_containers = $this->getParam('max_containers', 100);
  220. // Load container sources
  221. $source_exts = DevblocksPlatform::getExtensions(Extension_ContainerSource::POINT, true);
  222. $synced = 0;
  223. foreach($source_exts as $source) {
  224. $logger->info(sprintf('[Issues] Recaching containers from %s', $source->manifest->name));
  225. $source->recache($max_containers, $synced);
  226. // check amount of issues synced
  227. if($synced == $max_containers) {
  228. break;
  229. }
  230. }
  231. $logger->info("[Issues] Total Runtime: ".number_format((microtime(true)-$runtime)*1000,2)." ms");
  232. }
  233. function configure($instance) {
  234. $tpl = DevblocksPlatform::getTemplateService();
  235. $tpl->assign('max_containers', $this->getParam('max_containers', 100));
  236. $tpl->display('devblocks:wgm.issues::setup/cron/containers.tpl');
  237. }
  238. function saveConfigurationAction() {
  239. @$max_containers = DevblocksPlatform::importGPC($_POST['max_containers'],'integer');
  240. $this->setParam('max_containers', $max_containers);
  241. }
  242. }
  243. class MilestoneCron extends CerberusCronPageExtension {
  244. function run() {
  245. $logger = DevblocksPlatform::getConsoleLog();
  246. $logger->info("[Issues] Syncing Milestones");
  247. $timeout = ini_get('max_execution_time');
  248. $runtime = microtime(true);
  249. // Max milestones to sync
  250. $max_milestones = $this->getParam('max_milestones', 100);
  251. // Load milestone sources
  252. $source_exts = DevblocksPlatform::getExtensions(Extension_MilestoneSource::POINT, true);
  253. $synced = 0;
  254. foreach($source_exts as $source) {
  255. $logger->info(sprintf('[Issues] Syncing milestones from %s', $source->manifest->name));
  256. $source->sync($max_milestones, $synced);
  257. // check amount of issues synced
  258. if($synced == $max_milestones) {
  259. break;
  260. }
  261. }
  262. $logger->info("[Issues] Total Runtime: ".number_format((microtime(true)-$runtime)*1000,2)." ms");
  263. }
  264. function configure($instance) {
  265. $tpl = DevblocksPlatform::getTemplateService();
  266. $tpl->assign('max_milestones', $this->getParam('max_milestones', 100));
  267. $tpl->display('devblocks:wgm.issues::setup/cron/milestones.tpl');
  268. }
  269. function saveConfigurationAction() {
  270. @$max_milestones = DevblocksPlatform::importGPC($_POST['max_milestones'],'integer');
  271. $this->setParam('max_milestones', $max_milestones);
  272. }
  273. }
  274. class Wgm_SyncMilestones extends CerberusCronPageExtension {
  275. function run() {
  276. $logger = DevblocksPlatform::getConsoleLog();
  277. $logger->info("[] Syncing Repositories");
  278. if (!extension_loaded("oauth")) {
  279. $logger->err("[] The 'oauth' extension is not loaded. Aborting!");
  280. return false;
  281. }
  282. $timeout = ini_get('max_execution_time');
  283. $runtime = microtime(true);
  284. $issue = Wgm_API::getInstance();
  285. // get config
  286. $token = DevblocksPlatform::getPluginSetting('wgm.issues', 'access_token', '');
  287. $issue->setCredentials($token);
  288. // get last sync repo
  289. $last_sync_repo = $this->getParam('repos.last_repo', '');
  290. // max repos to sync
  291. $max_repos = $this->getParam('max_repos', 100);
  292. // get repos
  293. $repos = $issue->get('user/repos');
  294. $synced = 0;
  295. if($last_sync_repo !== '' && array_key_exists($last_sync_repo, $repos))
  296. $logger->info(sprintf("[] Starting sync from %s/%s", $repos[$last_sync_repo]['user'], $repos[$last_sync_repo]['name']));
  297. foreach($repos as $repo) {
  298. if($last_sync_repo !== '' && $repo['id'] != $last_sync_repo) {
  299. $logger->info(sprintf("[] Skipping repository %s!", $repository));
  300. continue;
  301. }
  302. // does the owner of the repository exist in the DB?
  303. if(null === $user = DAO_User::getByLogin($repo['owner']['login'])) {
  304. $user = $issue->get(sprintf('users/%s', $repo['owner']['login']));
  305. $fields = array(
  306. DAO_User::NUMBER => $user['id'],
  307. DAO_User::LOGIN => $user['login'],
  308. DAO_User::NAME => $user['name'],
  309. DAO_User::EMAIL => $user['email']
  310. );
  311. $user = DAO_User::create($fields);
  312. }
  313. $fields = array(
  314. DAO_Repository::NUMBER => $repo['id'],
  315. DAO_Repository::NAME => $repo['name'],
  316. DAO_Repository::DESCRIPTION => $repo['description'],
  317. DAO_Repository::USER_ID => $user->id,
  318. DAO_Repository::ENABLED => true
  319. );
  320. // does the repo exist in the DB?
  321. if(null === $repository = DAO_Repository::getByNumber($repo['id'])) {
  322. DAO_Repository::create($fields);
  323. } else {
  324. DAO_Repository::update($repository->id, $fields);
  325. }
  326. $synced++;
  327. // check amount of repos synced
  328. if($synced == $max_repos) {
  329. $this->setParam('repos.last_repo', $repo_id);
  330. break 2;
  331. }
  332. }
  333. foreach($repos as $repo) {
  334. // is the repo enabled?
  335. $user = DAO_User::get($repo->user_id);
  336. $repository = sprintf("%s/%s", $user->login, $repo->name);
  337. if($last_sync_repo !== '' && $repo_id != $last_sync_repo) {
  338. $logger->info(sprintf("[] Skipping repository %s!", $repository));
  339. continue;
  340. } elseif(!$repo->enabled) {
  341. $logger->info(sprintf("[] Skipping repository %s since it isn't enabled!", $repository));
  342. continue;
  343. }
  344. }
  345. $logger->info("[] Total Runtime: ".number_format((microtime(true)-$runtime)*1000,2)." ms");
  346. }
  347. function configure($instance) {
  348. $tpl = DevblocksPlatform::getTemplateService();
  349. $tpl->assign('max_milestones', $this->getParam('max_milestones', 100));
  350. $tpl->display('devblocks:wgm.issues::setup/cron/milestones.tpl');
  351. }
  352. function saveConfigurationAction() {
  353. @$max_milestones = DevblocksPlatform::importGPC($_POST['max_milestones'],'integer');
  354. $this->setParam('max_milestones', $max_milestones);
  355. }
  356. }