linuxfoo-gitea/linuxfoo-gitlab.php

227 lines
8.0 KiB
PHP
Raw Normal View History

2021-11-27 03:20:48 +01:00
<?php
/*
Plugin Name: Gitlab REST API Shortcodes
Plugin URI: http://tk-sls.de/ref/gitlab-list-commits
2021-11-27 12:03:15 +01:00
Description: 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.
2021-11-27 03:20:48 +01:00
Author: Tilman Kranz
2021-11-27 12:03:15 +01:00
Version: 1.2
2021-11-27 03:20:48 +01:00
Author URI: https://tk-sls.de
*/
class LinuxfooGitlab {
public function __construct() {
}
2021-11-27 12:03:15 +01:00
static function load_textdomain() {
load_plugin_textdomain('linuxfoo-gitlab', false, dirname(plugin_basename(__FILE__)).'/lang');
}
2021-11-27 03:20:48 +01:00
static function css() {
2021-11-27 12:03:15 +01:00
wp_enqueue_style('linuxfoo_gitlab_css', plugins_url('styles.css', __FILE__ ), '', '1.2' );
2021-11-27 03:20:48 +01:00
}
static function error ( $msg ) {
return
'<div class="linuxfoo_gitlab">'.
'<span class="error">'.
$msg.
'</span>'.
'</div>';
}
2021-11-27 12:03:15 +01:00
static function format_since($since) {
preg_match('/^([0-9]+)\s+(.*)$/', $since, $m);
$num = $m[1];
$unit = $m[2];
if($unit=='days') {
$unit = __('days', 'linuxfoo-gitlab');
}
elseif($unit=='months') {
$unit = __('months', 'linuxfoo-gitlab');
}
elseif($unit=='years') {
$unit = __('years', 'linuxfoo-gitlab');
}
/* translators: %d: count, %s unit of time interval*/
return sprintf(__('since %d %s ago', 'linuxfoo-gitlab'), $num, $unit);
}
2021-11-27 03:20:48 +01:00
static function list_commits($atts, $content, $tag) {
2021-11-27 16:50:41 +01:00
return self::show_project($atts, $content, $tag);
}
static function show_project($atts, $content, $tag) {
2021-11-27 03:20:48 +01:00
global $post;
if(
is_null($atts['url']) ||
is_null($atts['project_id'])
) {
return self::error(__('Required parameter missing.', 'linuxfoo-gitlab'));
}
2021-11-27 12:03:15 +01:00
elseif(!preg_match('/^[1-9][0-9]*$/', $atts['project_id'])) {
return self::error(__('Invalid value for parameter "project_id".', 'linuxfoo-gitlab'));
}
elseif(!filter_var($atts['url'], FILTER_VALIDATE_URL)) {
return self::error(__('Invalid value for parameter "url".', 'linuxfoo-gitlab'));
}
elseif(!preg_match('/^[1-9][0-9]*$/', $atts['project_id'])) {
return self::error(__('Invalid value for parameter "project_id".', 'linuxfoo-gitlab'));
}
2021-11-27 03:20:48 +01:00
$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 =
'<span class="project_header">'.
2021-11-27 12:03:15 +01:00
__('Gitlab project', 'linuxfoo-gitlab').' <a href="'.$project->web_url.'">'.$project->name.'</a>'.
2021-11-27 03:20:48 +01:00
'</span>';
$commits_url = $project_url.'/repository/commits?';
if(!is_null($atts['since'])) {
2021-11-27 12:03:15 +01:00
if(!preg_match('/^[1-9][0-9]*\s+(days|months|years)$/', $atts['since'])) {
return self::error(__('Invalid value for parameter "since".', 'linuxfoo-gitlab'));
}
2021-11-27 03:20:48 +01:00
$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);
2021-11-27 16:22:29 +01:00
if(is_null($commits_json)) {
2021-11-27 03:20:48 +01:00
return self::error(__('Commits URL not reachable.', 'linuxfoo-gitlab'));
}
$commits = json_decode($commits_json);
2021-11-27 16:50:41 +01:00
if(is_null($commits)) {
2021-11-27 03:20:48 +01:00
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
'<div class="linuxfoo_gitlab">'.
$project_header.
'<span class="stats">'.
__('No commits', 'linuxfoo-gitlab').
2021-11-27 03:52:33 +01:00
(is_null($branch) ? '' : ' '.__('in branch', 'linuxfoo-gitlab').' "'.$branch.'"').
2021-11-27 12:03:15 +01:00
(is_null($atts['since']) ? '' : ' '.('since '.$atts['since'].' ago')).'.'.
2021-11-27 03:20:48 +01:00
'</span>'.
'</div>';
}
$commits = array_slice($commits, 0, $commits_count);
$out =
'<div class="linuxfoo_gitlab">'.
$project_header.
'<span class="stats">'.
2021-11-27 12:03:15 +01:00
/* translators: %d: maximum number of commits displayed */
2021-11-27 03:20:48 +01:00
sprintf(__('Last %d commits', 'linuxfoo-gitlab'), $commits_count).
2021-11-27 03:52:33 +01:00
(is_null($branch) ? '' : ' '.__('in branch', 'linuxfoo-gitlab').' "'.$branch.'"').
2021-11-27 12:03:15 +01:00
(is_null($atts['since']) ? '' : ' '.self::format_since($atts['since'])).':'.
2021-11-27 03:20:48 +01:00
'</span>'.
'<ul class="commits">';
foreach($commits as $commit) {
preg_match('/^....-..-../', $commit->committed_date, $m);
$date = $m[0];
$title = htmlspecialchars($commit->title, ENT_NOQUOTES|ENT_HTML5|ENT_SUBSTITUTE, 'UTF-8', FALSE);
$out .=
'<li>'.
$date.': <a href="'.$commit->web_url.'">'.$commit->short_id.'</a> '.$title.
'</li>';
}
2021-11-27 16:22:29 +01:00
$out .= '</ul>';
2021-11-27 16:50:41 +01:00
if(!is_null($atts['releases'])) {
if(!preg_match('/^(all|latest)$/', $atts['releases'])) {
return self::error(__('Invalid value for parameter "releases".', 'linuxfoo-gitlab'));
}
$releases_url = $project_url.'/releases?';
$releases_json = file_get_contents($releases_url);
if(is_null($releases_json)) {
return self::error(__('Releases URL not reachable.', 'linuxfoo-gitlab'));
}
$releases = json_decode($releases_json);
if(is_null($releases)) {
return self::error(__('Releases information not readable.', 'linuxfoo-gitlab'));
}
if(count($releases)==0) {
$out .= '<p class="releases">'.__('This project has currently no releases.', 'linuxfoo-gitlab').'</p>';
}
elseif($atts['releases']=='latest') {
$out .=
'<p class="releases">'.
__('Latest release', 'linuxfoo-gitlab').': '.
'<a href="'.$releases[0]->evidences->_links->self.'">'.$releases[0]->name.'</a>'.
'</p>';
}
elseif($atts['releases']=='all') {
$out .=
'<p class="releases">'.
__('Releases', 'linuxfoo-gitlab').': '.
'</p>'.
'<ul class="releases">';
foreach($releases as $release) {
$out .=
'<li>'.
'<a href="'.$release->evidences->_links->self.'">'.$release->name.'</a>'.
'</li>';
}
$out .= '</ul>';
}
}
2021-11-27 16:22:29 +01:00
$out .= '</div>';
2021-11-27 03:20:48 +01:00
return $out;
}
}
add_action('plugins_loaded', 'LinuxfooGitlab::load_textdomain');
add_action('wp_enqueue_scripts', 'LinuxfooGitlab::css' );
add_shortcode('gitlab-list-commits', 'LinuxfooGitlab::list_commits');
2021-11-27 16:50:41 +01:00
add_shortcode('gitlab-show-project', 'LinuxfooGitlab::show_project');
2021-11-27 03:20:48 +01:00