Summary: Embed list of most recent commits to a Gitlab project from a Gitlab instance's public REST API.

Example: Add [gitlab-list-commits url=https://tk-sls.de/gitlab project_id=42 since="3 month" max=5] for a list of at most 5 commits to that project that were made since at most 3 months ago.

The attributes "url" and "project_id" are mandatory.

The attributes "since" and "max" are optional (beware, the default is an unlimited list of commits).

Other optional attributes:

Author: Tilman Kranz Version: 1.1 Author URI: https://tk-sls.de */ class LinuxfooGitlab { public function __construct() { } static function css() { wp_enqueue_style('linuxfoo_gitlab_css', plugins_url('styles.css', __FILE__ ), '', '1.1' ); } static function error ( $msg ) { return '
'. ''. $msg. ''. '
'; } static function list_commits($atts, $content, $tag) { global $post; if( is_null($atts['url']) || is_null($atts['project_id']) ) { return self::error(__('Required parameter missing.', 'linuxfoo-gitlab')); } $project_url = $atts['url'].'/api/v4/projects/'.$atts['project_id']; $project_json = file_get_contents($project_url); if(is_null($project_json)) { return self::error(__('Project URL not reachable.', 'linuxfoo-gitlab')); } $project = json_decode($project_json); if(is_null($project)) { return self::error('Project information not readable.'); } $project_header = ''. 'Gitlab project'.' '.$project->name.''. ''; $commits_url = $project_url.'/repository/commits?'; if(!is_null($atts['since'])) { $since = date(DATE_ISO8601, strtotime('-'.$atts['since'])); $commits_url .= '&since='.$since; } if(!is_null($atts['ref_name'])) { $commits_url .= '&ref_name='.$atts['ref_name']; $branch = $atts['ref_name']; } elseif(!is_null($atts['default_branch'])) { $commits_url .= '&ref_name='.$project->default_branch; $branch = $project->default_branch; } else { $branch = null; } $commits_json = file_get_contents($commits_url); if(is_null($project_json)) { return self::error(__('Commits URL not reachable.', 'linuxfoo-gitlab')); } $commits = json_decode($commits_json); if(is_null($project)) { return self::error(__('Commits information not readable.', 'linuxfoo-gitlab')); } $commits_count = count($commits); $commits_count = is_null($atts['max']) ? $commits_count : min($commits_count, $atts['max']); if($commits_count==0) { return '
'. $project_header. ''. __('No commits', 'linuxfoo-gitlab'). (is_null($branch) ? '' : ' '.__('in branch', 'linuxfoo-gitlab').' "'.$branch.'"'). (is_null($atts['since']) ? '' : ' since '.$atts['since'].' ago').'.'. ''. '
'; } $commits = array_slice($commits, 0, $commits_count); $out = '
'. $project_header. ''. sprintf(__('Last %d commits', 'linuxfoo-gitlab'), $commits_count). (is_null($branch) ? '' : ' '.__('in branch', 'linuxfoo-gitlab').' "'.$branch.'"'). (is_null($atts['since']) ? '' : ' since '.$atts['since'].' ago').':'. ''. ''. '
'; return $out; } static function load_textdomain() { load_plugin_textdomain('linuxfoo-gitlab', false, dirname(plugin_basename(__FILE__)).'/lang'); } } add_action('plugins_loaded', 'LinuxfooGitlab::load_textdomain'); add_action('wp_enqueue_scripts', 'LinuxfooGitlab::css' ); add_shortcode('gitlab-list-commits', 'LinuxfooGitlab::list_commits');