-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsyntax.php
63 lines (52 loc) · 2.43 KB
/
syntax.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
<?php
/**
* Plugin GitLab: Gets commit message by project name and commit id.
*
* @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
* @author Karsten Kosmala <[email protected]>
*/
if(!defined('DOKU_INC')) die();
class syntax_plugin_gitlab extends DokuWiki_Syntax_Plugin {
public function getType() { return 'substition'; }
public function getSort() { return 32; }
public function connectTo($mode) {
$this->Lexer->addSpecialPattern('\[\[gitlabapi>[a-zA-Z0-9.-]+>[a-z0-9]+\]\]', $mode, 'plugin_gitlab');
}
public function handle($match, $state, $pos, Doku_Handler &$handler) {
list($name, $repository_name, $commit_id) = explode('>', $match);
$commit_id_short = substr($commit_id, 0, -2);
list($repository_id, $web_url)= $this->getInfoByName($repository_name);
list($commit_msg, $commit_id_long) = $this->getInfoByHash($repository_id, $commit_id_short);
return array($web_url, $commit_id_long, $commit_msg);
}
public function render($mode, Doku_Renderer &$renderer, $data) {
// $data is what the function handle return'ed.
if($mode == 'xhtml'){
/** @var Doku_Renderer_xhtml $renderer */
$renderer->doc .= '<a target="_blank" href="' . htmlspecialchars($data[0]) . '/commit/' . htmlspecialchars($data[1]) . '">' . htmlspecialchars($data[2]) . '</a>';
return true;
}
return false;
}
public function getInfoByName($name) {
$gitlabServer = $this->getConf('server');
$apiToken = $this->getConf('api_token');
$http = new DokuHTTPClient();
$reqUrl = $gitlabServer . '/api/v3/projects/search/' . $name . '/?private_token=' . $apiToken;
$repositories = json_decode($http->get($reqUrl), true);
foreach ($repositories as &$repository) {
if ($repository['name'] == $name) {
$data = $repository;
}
}
return array($data['id'], $data['web_url']);
}
public function getInfoByHash($repository_id, $commit_id) {
$gitlabServer = $this->getConf('server');
$apiToken = $this->getConf('api_token');
$http = new DokuHTTPClient();
$reqUrl = $gitlabServer . '/api/v3/projects/' . $repository_id . '/repository/commits/' . $commit_id . '/?private_token=' .$apiToken;
$data = json_decode($http->get($reqUrl), true);
return array($data['message'], $data['id']);
}
}