From bdb1121857cc5f5942c36d5c8639cc0322412290 Mon Sep 17 00:00:00 2001 From: Tilman Kranz Date: Sat, 27 Nov 2021 03:20:48 +0100 Subject: [PATCH] initial commit of v1.1 --- .gitignore | 1 + lang/linuxfoo-gitlab-de_DE.po | 52 +++++++++++++ lang/linuxfoo-gitlab.pot | 51 ++++++++++++ linuxfoo-gitlab.php | 142 ++++++++++++++++++++++++++++++++++ styles.css | 14 ++++ 5 files changed, 260 insertions(+) create mode 100644 .gitignore create mode 100644 lang/linuxfoo-gitlab-de_DE.po create mode 100644 lang/linuxfoo-gitlab.pot create mode 100644 linuxfoo-gitlab.php create mode 100644 styles.css diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a23b892 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +lang/*.mo diff --git a/lang/linuxfoo-gitlab-de_DE.po b/lang/linuxfoo-gitlab-de_DE.po new file mode 100644 index 0000000..ba1dc7b --- /dev/null +++ b/lang/linuxfoo-gitlab-de_DE.po @@ -0,0 +1,52 @@ +# Copyright (C) 2021 Tilman Kranz +# This file is distributed under the same license as the Gitlab REST API Shortcodes plugin. +msgid "" +msgstr "" +"Project-Id-Version: Gitlab REST API Shortcodes 1.1\n" +"Report-Msgid-Bugs-To: https://tk-sls.de/ref/linuxfoo-gitlab\n" +"Last-Translator: Tilman Kranz \n" +"Language: de\n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: 8bit\n" +"POT-Creation-Date: 2021-11-27T03:05:12+01:00\n" +"PO-Revision-Date: 2021-11-27 00:00+0100\n" +"X-Generator: WP-CLI 2.5.0\n" +"X-Domain: linuxfoo-gitlab\n" + +#. Plugin Name of the plugin +msgid "Gitlab REST API Shortcodes" +msgstr "Gitlab REST API Shortcodes" + +#. Description of the plugin +msgid "

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:

" +msgstr "

Zusammenfassung: Lies Liste neuester Commits eines Projekts aus einem Projekt in einer Gitlab-Instanz via v4 REST API.

Beispiel: [gitlab-list-commits url=https://tk-sls.de/gitlab project_id=42 since=\"3 month\" max=5] Liste bis zu 5 Commits (Neueste zuerst) aus den letzten 3 Monaten.

Attribute \"url\" und \"project_id\" sind Pflichtangaben.

Attribute \"since\" und \"max\" sind optional (Achtung, Standardverhalten ist unlimitiertes Auflisten).

Weitere optionale Attribute:

" + +#: linuxfoo-gitlab.php:36 +msgid "Required parameter missing." +msgstr "Benötigter Parameter fehlt." + +#: linuxfoo-gitlab.php:43 +msgid "Project URL not reachable." +msgstr "Projekt-URL nicht erreichbar." + +#: linuxfoo-gitlab.php:79 +msgid "Commits URL not reachable." +msgstr "Commits-URL nicht erreichbar." + +#: linuxfoo-gitlab.php:85 +msgid "Commits information not readable." +msgstr "Commit-Information nicht verfügbar." + +#: linuxfoo-gitlab.php:96 +msgid "No commits" +msgstr "Keine Commits" + +#: linuxfoo-gitlab.php:97 +#: linuxfoo-gitlab.php:110 +msgid "in branch" +msgstr "in Branch" + +#: linuxfoo-gitlab.php:109 +msgid "Last %d commits" +msgstr "Letzte %d Commits" diff --git a/lang/linuxfoo-gitlab.pot b/lang/linuxfoo-gitlab.pot new file mode 100644 index 0000000..fb32698 --- /dev/null +++ b/lang/linuxfoo-gitlab.pot @@ -0,0 +1,51 @@ +# Copyright (C) 2021 Tilman Kranz +# This file is distributed under the same license as the Gitlab REST API Shortcodes plugin. +msgid "" +msgstr "" +"Project-Id-Version: Gitlab REST API Shortcodes 1.1\n" +"Report-Msgid-Bugs-To: https://wordpress.org/support/plugin/linuxfoo-gitlab\n" +"Last-Translator: Tilman Kranz \n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: 8bit\n" +"POT-Creation-Date: 2021-11-27T03:05:12+01:00\n" +"PO-Revision-Date: 2021-11-27 00:00+0100\n" +"X-Generator: WP-CLI 2.5.0\n" +"X-Domain: linuxfoo-gitlab\n" + +#. Plugin Name of the plugin +msgid "Gitlab REST API Shortcodes" +msgstr "" + +#. Description of the plugin +msgid "

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:

  • ref_name=STRING: List only commit from branch or reference name STRING.
  • default_branch: If set, list only commits from the project's default branch.
" +msgstr "" + +#: linuxfoo-gitlab.php:36 +msgid "Required parameter missing." +msgstr "" + +#: linuxfoo-gitlab.php:43 +msgid "Project URL not reachable." +msgstr "" + +#: linuxfoo-gitlab.php:79 +msgid "Commits URL not reachable." +msgstr "" + +#: linuxfoo-gitlab.php:85 +msgid "Commits information not readable." +msgstr "" + +#: linuxfoo-gitlab.php:96 +msgid "No commits" +msgstr "" + +#: linuxfoo-gitlab.php:97 +#: linuxfoo-gitlab.php:110 +msgid "in branch" +msgstr "" + +#: linuxfoo-gitlab.php:109 +msgid "Last %d commits" +msgstr "" diff --git a/linuxfoo-gitlab.php b/linuxfoo-gitlab.php new file mode 100644 index 0000000..449134c --- /dev/null +++ b/linuxfoo-gitlab.php @@ -0,0 +1,142 @@ +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:

  • ref_name=STRING: List only commit from branch or reference name STRING.
  • default_branch: If set, list only commits from the project's default branch.
+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').':'. + ''. + '
    '; + + 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 .= + '
  • '. + $date.': '.$commit->short_id.' '.$title. + '
  • '; + } + + $out .= + '
'. + '
'; + + 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'); + diff --git a/styles.css b/styles.css new file mode 100644 index 0000000..cf8bdcd --- /dev/null +++ b/styles.css @@ -0,0 +1,14 @@ +.linuxfoo_gitlab { + border: 1px dashed #8a8a8a; + border-radius: .5em; + padding: .25em .5em; +} +.linuxfoo_gitlab .commits { + margin: .25em 0 .25em 0; +} +.linuxfoo_gitlab .project_header { + display: block; +} +.linuxfoo_gitlab .stats { + font-size: smaller; +}