Igor Kromin |   Consultant. Coder. Blogger. Tinkerer. Gamer.

NOTE: This article is 3 years or older so its information may no longer be relevant. Read on at your own discretion! Comments for this article have automatically been locked, refer to the FAQ for more details.
I was surprised to find that there hasn't been a plugin created to show all of the commits from the GitLab Git commit page in DokuWiki, so I went about creating my own. The plugin is very rough and hardcodes a lot of things, but it gets the job done.

This is what the output looks like in DokuWiki:
dokugitcommit.png


The DokuWiki markup for it is:
[gitlabcommits]


GitLab provides a very handy API to fetching data. It requires a key to fetch the data, which is passed in the request URL and then it returns a JSON string with the response. Then it's a matter of iterating through the results and displaying them in a table.



So here's the plugin code. I've made use of some CSS that's defined by the pagelist plugin. That plugin does provide a way to use it programatically too, but I didn't bother with that since I wanted more control on the size of my output table.
<?php
/**
* Plugin: GitLab Commits
* @author Igor Kromin (http://www.igorkromin.net)
*/
if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/');
if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
require_once(DOKU_PLUGIN.'syntax.php');
class syntax_plugin_gitlabcommits extends DokuWiki_Syntax_Plugin {
function getInfo() {
return array('author' => 'Igor Kromin',
'email' => '[email protected]',
'date' => '2013-04-21',
'name' => 'GitLab Commits Plugin',
'desc' => 'Shows last commits from GitLab',
'url' => 'http://www.igorkromin.net/');
}
function getType(){ return 'substition'; }
function getPType(){ return 'block'; }
function getSort(){ return 100; }
function connectTo($mode) {
$this->Lexer->addSpecialPattern('[gitlabcommits]',$mode,'plugin_gitlabcommits');
}
function handle($match, $state, $pos, &$handler){
return array($match, $state, $pos);
}
function render($mode, &$renderer, $data) {
if($mode == 'xhtml'){
$pageurl = "*** GITLAB COMMITS URL ***";
$ch=curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $pageurl);
$html = curl_exec($ch);
curl_close($ch);
$json = new JSON();
$array = $json->decode($html);
$renderer->doc .= '<table class="pagelist" style="width:725px"><tr><th class="page">ID</th><th class="page">Author</th><th class="page">Title</th><th class="date">Date</th></tr>';
foreach($array as &$val) {
$renderer->doc .= '<tr><td class="desc">' . $val->short_id . '</td><td class="desc">' . $val->author_name . '</td><td class="desc">' . $val->title . '</td><td class="date">' . $val->created_at . '</td></tr>';
}
$renderer->doc .= '</table>';
return true;
}
return false;
}
}
?>


Without going into too much detail, this is a very basic syntax plugin. The majority of the code is really just setting up the plugin itself.

The bit that needs to change is the $pageurl, which is the URL to the GitLab commits page, which has to include the key assigned by GitLab.

There is definitely room for improvements here, however this plugin will get a list of commits and will display it in a wiki page. If I get more time, I will try to clean it up, but for now this plugin is 'as is'.

-i

Hope you found this post useful...

...so please read on! I love writing articles that provide beneficial information, tips and examples to my readers. All information on my blog is provided free of charge and I encourage you to share it as you wish. There is a small favour I ask in return however - engage in comments below, provide feedback, and if you see mistakes let me know.

If you want to show additional support and help me pay for web hosting and domain name registration, donations, no matter how small, are always welcome!

Use of any information contained in this blog post/article is subject to this disclaimer.
comments powered by Disqus
Other posts you may like...