*/ if (!defined('DOKU_INC')) { die(); } require_once __DIR__.'/gitlab/gitlabapi.php'; class syntax_plugin_gitlabapi extends DokuWiki_Syntax_Plugin { /** * Syntax Plugins * * The class needs to implement at least the following functions: * -) getType() * -) getSort() * -) connectTo($mode) * -) handle($match, $state, $pos, Doku_Handler $handler) * -) render($mode, Doku_Renderer $renderer, $data) * * More information: https://www.dokuwiki.org/devel:syntax_plugins#syntax_types */ public function getType() { return 'substition'; } public function getPType() { return 'normal'; } public function getSort() { return 196; } public function connectTo($mode) { $this->Lexer->addSpecialPattern(']*/>', $mode, 'plugin_gitlabapi'); } function getServerFromJson($server) { $json_file = file_get_contents(__DIR__.'/server.json'); $json_data = json_decode($json_file, true); if (isset($json_data[$server])) { return $json_data[$server]; } else { return null; } } function handle($match, $state, $pos, Doku_Handler $handler) { switch ($state) { case DOKU_LEXER_SPECIAL: $data = array( 'state' => $state ); // match @server and @token preg_match("/server *= *(['\"])(.*?)\\1/", $match, $server); if (!empty($server)) { $server_data = $this->getServerFromJson($server[2]); if (!is_null($server_data)) { $data['server'] = $server_data['url']; $data['token'] = $server_data['api_token']; } } if (!isset($data['server'])) { $data['server'] = $this->getConf('server.default'); } if (!isset($data['token'])) { $data['token'] = $this->getConf('token.default'); } // match @project-path preg_match("/project-path *= *(['\"])(.*?)\\1/", $match, $project_path); if (!empty($project_path)) { $data['project-path'] = $project_path[2]; } // match @milestones preg_match("/milestones *= *(['\"])(.*?)\\1/", $match, $milestones); if(!empty($milestones)) { $data['milestones'] = $milestones[2]; } // match @commits preg_match("/commits *= *(['\"])(.*?)\\1/", $match, $commits); if(!empty($commits)) { $data['commits'] = $commits[2]; } // match @issues preg_match("/issues *= *(['\"])(.*?)\\1/", $match, $issues); if(!empty($issues)) { $data['issues'] = $issues[2]; } // match @pipelines preg_match("/pipelines *= *(['\"])(.*?)\\1/", $match, $pipelines); if(!empty($pipelines)) { $data['pipelines'] = $pipelines[2]; } return $data; case DOKU_LEXER_UNMATCHED: return array('state' => $state, 'text' => $match); default: return array('state' => $state, 'bytepos_end' => $pos + strlen($match)); } } function render($mode, Doku_Renderer $renderer, $data) { if ($mode !== 'xhtml') { return false; } if ($mode != 'xhtml') { return false; } if (isset($data['error'])) { $renderer->doc .= $data['text']; return true; } $renderer->info['cache'] = false; switch ($data['state']) { case DOKU_LEXER_SPECIAL: $this->renderGitLab($renderer, $data); break; case DOKU_LEXER_ENTER: case DOKU_LEXER_EXIT: case DOKU_LEXER_UNMATCHED: $renderer->doc .= $renderer->_xmlEntities($data['text']); break; } return true; } function renderGitLab($renderer, $data) { // create GitLabApi object $gitlab = new GitLabApi($data); // get project $project = $gitlab->getProject(); //dbg($project); if (empty($project)) { $this->renderProjectError($renderer, $data); return array('state' => $state, 'bytepos_end' => $pos + strlen($match)); } $project_id = $project['id']; $project_url = $project['web_url']; $project_name = $project['name']; $date_time = $this->getDateTime($project['last_activity_at']); $namespace = $project['namespace']['full_path']; $img_url = DOKU_URL . 'lib/plugins/gitlabapi/images/gitlab.png'; $renderer->doc .= '
'; $renderer->doc .= ''; $renderer->doc .= ''.$this->getLang('gitlab.project').'
'; $renderer->doc .= '
'; $renderer->doc .= ''.$project_name.''; $renderer->doc .= ' - Namespace: '.$namespace.''; $renderer->doc .= '

'.$this->getLang('gitlab.activity').': '.$date_time['date'].' - '.$date_time['time'].'

'; if (!empty($data['milestones'])) { $this->renderProjectMilestones($renderer, $gitlab, $project_id, $data['milestones']); } if (!empty($data['commits'])) { $this->renderProjectCommits($renderer, $gitlab, $project_id, $data['commits']); } if (!empty($data['issues'])) { $this->renderProjectIssues($renderer, $gitlab, $project_id, $data['issues']); } if (!empty($data['pipelines'])) { $this->renderProjectPipelines($renderer, $gitlab, $project_id, $data['pipelines']); } $renderer->doc .= '

'; $renderer->doc .= '
'; $gitlab->closeClient(); } function renderProjectCommits($renderer, $gitlab, $project_id, $number) { $commits = $gitlab->getCommits($project_id); $renderer->doc .= ''.$this->getLang('gitlab.commits').'
'; $renderer->doc .= ' '; $total = count($commits) < $number ? count($commits) : $number; for ($i = 0; $i < $total; $i++) { $renderer->doc .= ''; $renderer->doc .= ''; $renderer->doc .= ''; $renderer->doc .= ''; $renderer->doc .= ''; $renderer->doc .= ''; $renderer->doc .= ''; } $renderer->doc .= '
Committer Name Title Message Date URL
'.$commits[$i]['committer_name'].''.$commits[$i]['title'].''.$commits[$i]['message'].''.$commits[$i]['committed_date'].'Commit link
'; } function renderProjectIssues($renderer, $gitlab, $project_id, $number) { $issues = $gitlab->getIssues($project_id); $renderer->doc .= ''.$this->getLang('gitlab.issues').'
'; $renderer->doc .= ' '; $total = count($issues) < $number ? count($issues) : $number; for ($i = 0; $i < $total; $i++) { if ($issues[$i]['state'] == 'closed') { continue; } $renderer->doc .= ''; $renderer->doc .= ''; $renderer->doc .= ''; $labels = $issues[$i]['labels']; $l_list = ''; foreach ($labels as $l) { $l_list .= $l.', '; } $renderer->doc .= ''; $assignees = $issues[$i]['assignees']; $a_list = ''; foreach ($assignees as $a) { $a_list .= $a['name'].', '; } $renderer->doc .= ''; $renderer->doc .= ''; $renderer->doc .= ''; $renderer->doc .= ''; } $renderer->doc .= '
Author Title Labels Assignees State URL
'.$issues[$i]['author']['name'].''.$issues[$i]['title'].''.rtrim($l_list,', ').''.rtrim($a_list, ', ').''.$issues[$i]['state'].'Issue link
'; } function renderProjectMilestones($renderer, $gitlab, $project_id, $number) { $milestones = $gitlab->getMilestones($project_id); $renderer->doc .= ''.$this->getLang('gitlab.milestones').'
'; $renderer->doc .= ' '; $total = count($milestones) < $number ? count($milestones) : $number; for ($i = 0; $i < $total; $i++) { $renderer->doc .= ''; $renderer->doc .= ''; $renderer->doc .= ''; $renderer->doc .= ''; $renderer->doc .= ''; $renderer->doc .= ''; $renderer->doc .= ''; } $renderer->doc .= '
Title Description State Created URL
'.$milestones[$i]['title'].''.$milestones[$i]['description'].''.$milestones[$i]['state'].''.$milestones[$i]['created_at'].'Milestone link
'; } function renderProjectPipelines($renderer, $gitlab, $project_id, $number) { $pipelines = $gitlab->getPipelines($project_id); $renderer->doc .= ''.$this->getLang('gitlab.pipelines').'
'; $renderer->doc .= ' '; $total = count($pipelines) < $number ? count($pipelines) : $number; for ($i = 0; $i < $total; $i++) { $renderer->doc .= ''; $renderer->doc .= ''; $renderer->doc .= ''; $renderer->doc .= ''; $renderer->doc .= ''; $renderer->doc .= ''; } $renderer->doc .= '
Ref Status Source URL
'.$pipelines[$i]['ref'].''.$pipelines[$i]['status'].''.$pipelines[$i]['source'].'Pipeline link
'; } function renderProjectError($renderer, $data) { $img_url = DOKU_URL . 'lib/plugins/gitlabapi/images/gitlab.png'; $renderer->doc .= '
'; $renderer->doc .= ''; $renderer->doc .= ''.$this->getLang('gitlab.project').'
'; $renderer->doc .= '
'; $renderer->doc .= '

'.$this->getLang('gitlab.error').'

'; $renderer->doc .= '
'; } function getDateTime($activity_time) { $date_exploded = explode('T', $activity_time); $time_exploded = explode('Z', $date_exploded[1]); return array('date' => $date_exploded[0], 'time' => substr($time_exploded[0], 0, -4)); } }