finer control over release/commit lists

This commit is contained in:
Tilman Kranz 2021-11-27 20:04:17 +01:00
parent db6308e791
commit 1c00192836
4 changed files with 233 additions and 175 deletions

View File

@ -17,18 +17,33 @@ Add the following shortcode for a list of at most 5 commits to that project that
Add the following shortcode for a list of at most 3 commits followed by a link to the latest release: Add the following shortcode for a list of at most 3 commits followed by a link to the latest release:
``` ```
[gitlab-show-project url="https://tk-sls.de/gitlab" project_id=42 max=3 release="latest"] [gitlab-show-project url="https://tk-sls.de/gitlab" project_id=42 max=3 releases="latest"]
```
Add the following shortcode to just link to the Gitlab project, without a list of commits or releases:
```
[gitlab-show-project url="https://tk-sls.de/gitlab" project_id=42 commit="none" releases="none"]
``` ```
### Attributes ### Attributes
* `url="STRING"`: Mandatory (example: `https://tk-sls.de/gitlab`). The following attributes are mandatory:
* `project_id=NUM`: Mandatory; `NUM`: Positive Integer, valid project ID in Gitlab specified by `url` (example: `42`).
* `url="STRING"`: Base URL of Gitlab instance (example: `https://tk-sls.de/gitlab`).
* `project_id=NUM`: ; `NUM`: Positive Integer, valid project ID in Gitlab specified by `url` (example: `42`).
The following optional attributes enable or disable additional project information:
* `commits="STRING"`: Optional; if set, `STRING` can be either `all` (show a list of recent commits, the default) or `none` (do not show a list of recent commits).
* `releases=STRING`: Optional; if set, `STRING` can be either `all` (show a list of all releases of this project, if any) or `latest` (show a link to the latest release of this project, if any) or `none` (do not show releases of this project, the default).
The following optional attributes modify the behavior of the list of commits:
* `since="NUM UNIT"`: Optional; `NUM`: Positive integer; `UNIT`: `days`, `months`, `years`; exclude commits older than specified time interval (example: `3 months`). * `since="NUM UNIT"`: Optional; `NUM`: Positive integer; `UNIT`: `days`, `months`, `years`; exclude commits older than specified time interval (example: `3 months`).
* `max=NUM`: Optional; `NUM`: Positive integer; list not more than `NUM` commits (example: `5`). * `max=NUM`: Optional; `NUM`: Positive integer; list not more than `NUM` commits (example: `5`).
* `ref_name=STRING`: Optional: list only commit from branch or reference name `STRING` (examples: `main`, `v1.1`). * `ref_name=STRING`: Optional: list only commit from branch or reference name `STRING` (examples: `main`, `v1.1`).
* `default_branch`: Optional; if set, list only commits from the project's default branch. * `default_branch`: Optional; if set, list only commits from the project's default branch.
* `release=STRING`: Optional; if set, `STRING` can be either `all` (show a list of all releases of this project) or `latest` (show a link to the latest release of this project, if any).
## Author and License ## Author and License

View File

@ -50,6 +50,10 @@ msgstr "Jahren"
msgid "since %d %s ago" msgid "since %d %s ago"
msgstr "seit %d %s" msgstr "seit %d %s"
#: linuxfoo-gitlab.php:57
msgid "Invalid value for parameter \"commits\"."
msgstr "Unzulässiger Wert für Parameter \"commits\"."
#: linuxfoo-gitlab.php:59 #: linuxfoo-gitlab.php:59
msgid "Required parameter missing." msgid "Required parameter missing."
msgstr "Notwendiger Parameter fehlt" msgstr "Notwendiger Parameter fehlt"

View File

@ -9,7 +9,7 @@ msgstr ""
"MIME-Version: 1.0\n" "MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n" "Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n" "Content-Transfer-Encoding: 8bit\n"
"POT-Creation-Date: 2021-11-27T16:45:29+01:00\n" "POT-Creation-Date: 2021-11-27T19:40:19+01:00\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"X-Generator: WP-CLI 2.5.0\n" "X-Generator: WP-CLI 2.5.0\n"
"X-Domain: linuxfoo-gitlab\n" "X-Domain: linuxfoo-gitlab\n"
@ -23,7 +23,7 @@ msgid "http://tk-sls.de/ref/gitlab-list-commits"
msgstr "" msgstr ""
#. Description of the plugin #. 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." 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 commits=\"all\" since=\"3 month\" max=5 releases=\"latest\"] for a list of at most 5 commits to that project that were made since at most 3 months ago, followed by alink to the latest release of the project (if any). To disable the list of commits, set commits=\"none\". To generate a list of all releases, set releases=\"all\". To disable the list of releases, omit the \"releases\" attribute or set releases=\"none\"."
msgstr "" msgstr ""
#. Author of the plugin #. Author of the plugin
@ -34,90 +34,94 @@ msgstr ""
msgid "https://tk-sls.de" msgid "https://tk-sls.de"
msgstr "" msgstr ""
#: linuxfoo-gitlab.php:39 #: linuxfoo-gitlab.php:37
msgid "days" msgid "days"
msgstr "" msgstr ""
#: linuxfoo-gitlab.php:42 #: linuxfoo-gitlab.php:40
msgid "months" msgid "months"
msgstr "" msgstr ""
#: linuxfoo-gitlab.php:45 #: linuxfoo-gitlab.php:43
msgid "years" msgid "years"
msgstr "" msgstr ""
#. translators: %d: count, %s unit of time interval #. translators: %d: count, %s unit of time interval
#: linuxfoo-gitlab.php:49 #: linuxfoo-gitlab.php:47
msgid "since %d %s ago" msgid "since %d %s ago"
msgstr "" msgstr ""
#: linuxfoo-gitlab.php:63 #: linuxfoo-gitlab.php:57
msgid "Required parameter missing." msgid "Invalid value for parameter \"commits\"."
msgstr "" msgstr ""
#: linuxfoo-gitlab.php:66 #: linuxfoo-gitlab.php:61
#: linuxfoo-gitlab.php:72
msgid "Invalid value for parameter \"project_id\"."
msgstr ""
#: linuxfoo-gitlab.php:69
msgid "Invalid value for parameter \"url\"."
msgstr ""
#: linuxfoo-gitlab.php:79
msgid "Project URL not reachable."
msgstr ""
#: linuxfoo-gitlab.php:90
msgid "Gitlab project"
msgstr ""
#: linuxfoo-gitlab.php:97
msgid "Invalid value for parameter \"since\"." msgid "Invalid value for parameter \"since\"."
msgstr "" msgstr ""
#: linuxfoo-gitlab.php:119 #: linuxfoo-gitlab.php:91
msgid "Commits URL not reachable." msgid "Commits URL not reachable."
msgstr "" msgstr ""
#: linuxfoo-gitlab.php:125 #: linuxfoo-gitlab.php:97
msgid "Commits information not readable." msgid "Commits information not readable."
msgstr "" msgstr ""
#: linuxfoo-gitlab.php:136 #: linuxfoo-gitlab.php:108
msgid "No commits" msgid "No commits"
msgstr "" msgstr ""
#: linuxfoo-gitlab.php:137 #: linuxfoo-gitlab.php:109
#: linuxfoo-gitlab.php:151 #: linuxfoo-gitlab.php:118
msgid "in branch" msgid "in branch"
msgstr "" msgstr ""
#. translators: %d: maximum number of commits displayed #. translators: %d: maximum number of commits displayed
#: linuxfoo-gitlab.php:150 #: linuxfoo-gitlab.php:117
msgid "Last %d commits" msgid "Last %d commits"
msgstr "" msgstr ""
#: linuxfoo-gitlab.php:172 #: linuxfoo-gitlab.php:150
msgid "Invalid value for parameter \"releases\"." msgid "Invalid value for parameter \"releases\"."
msgstr "" msgstr ""
#: linuxfoo-gitlab.php:179 #: linuxfoo-gitlab.php:157
msgid "Releases URL not reachable." msgid "Releases URL not reachable."
msgstr "" msgstr ""
#: linuxfoo-gitlab.php:185 #: linuxfoo-gitlab.php:163
msgid "Releases information not readable." msgid "Releases information not readable."
msgstr "" msgstr ""
#: linuxfoo-gitlab.php:189 #: linuxfoo-gitlab.php:169
msgid "This project has currently no releases." msgid "This project has currently no releases."
msgstr "" msgstr ""
#: linuxfoo-gitlab.php:194 #: linuxfoo-gitlab.php:175
msgid "Latest release" msgid "Latest release"
msgstr "" msgstr ""
#: linuxfoo-gitlab.php:201 #: linuxfoo-gitlab.php:182
msgid "Releases" msgid "Releases"
msgstr "" msgstr ""
#: linuxfoo-gitlab.php:215
msgid "Required parameter missing."
msgstr ""
#: linuxfoo-gitlab.php:218
#: linuxfoo-gitlab.php:224
msgid "Invalid value for parameter \"project_id\"."
msgstr ""
#: linuxfoo-gitlab.php:221
msgid "Invalid value for parameter \"url\"."
msgstr ""
#: linuxfoo-gitlab.php:231
msgid "Project URL not reachable."
msgstr ""
#: linuxfoo-gitlab.php:242
msgid "Gitlab project"
msgstr ""

View File

@ -3,9 +3,9 @@
/* /*
Plugin Name: Gitlab REST API Shortcodes Plugin Name: Gitlab REST API Shortcodes
Plugin URI: http://tk-sls.de/ref/gitlab-list-commits Plugin URI: http://tk-sls.de/ref/gitlab-list-commits
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. 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 commits="all" since="3 month" max=5 releases="latest"] for a list of at most 5 commits to that project that were made since at most 3 months ago, followed by alink to the latest release of the project (if any). To disable the list of commits, set commits="none". To generate a list of all releases, set releases="all". To disable the list of releases, omit the "releases" attribute or set releases="none".
Author: Tilman Kranz Author: Tilman Kranz
Version: 1.2 Version: 1.4
Author URI: https://tk-sls.de Author URI: https://tk-sls.de
*/ */
@ -18,16 +18,14 @@ class LinuxfooGitlab {
} }
static function css() { static function css() {
wp_enqueue_style('linuxfoo_gitlab_css', plugins_url('styles.css', __FILE__ ), '', '1.2' ); wp_enqueue_style('linuxfoo_gitlab_css', plugins_url('styles.css', __FILE__ ), '', '1.4' );
} }
static function error ( $msg ) { static function error ( $msg ) {
return return
'<div class="linuxfoo_gitlab">'.
'<span class="error">'. '<span class="error">'.
$msg. $msg.
'</span>'. '</span>';
'</div>';
} }
static function format_since($since) { static function format_since($since) {
@ -49,55 +47,29 @@ class LinuxfooGitlab {
return sprintf(__('since %d %s ago', 'linuxfoo-gitlab'), $num, $unit); return sprintf(__('since %d %s ago', 'linuxfoo-gitlab'), $num, $unit);
} }
static function list_commits($atts, $content, $tag) { static function commits_list($atts, $project_url) {
return self::show_project($atts, $content, $tag); $since = null;
if($atts['commits']=='none') {
return '';
} }
elseif(!is_null($atts['commits']) && $atts['commits']!='all') {
static function show_project($atts, $content, $tag) { return self::error(__('Invalid value for parameter "commits".', 'linuxfoo-gitlab'));
global $post;
if(
is_null($atts['url']) ||
is_null($atts['project_id'])
) {
return self::error(__('Required parameter missing.', 'linuxfoo-gitlab'));
} }
elseif(!preg_match('/^[1-9][0-9]*$/', $atts['project_id'])) { elseif(!is_null($atts['since'])) {
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'));
}
$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">'.
__('Gitlab project', 'linuxfoo-gitlab').' <a href="'.$project->web_url.'">'.$project->name.'</a>'.
'</span>';
$commits_url = $project_url.'/repository/commits?';
if(!is_null($atts['since'])) {
if(!preg_match('/^[1-9][0-9]*\s+(days|months|years)$/', $atts['since'])) { if(!preg_match('/^[1-9][0-9]*\s+(days|months|years)$/', $atts['since'])) {
return self::error(__('Invalid value for parameter "since".', 'linuxfoo-gitlab')); return self::error(__('Invalid value for parameter "since".', 'linuxfoo-gitlab'));
} }
else {
$since = date(DATE_ISO8601, strtotime('-'.$atts['since'])); $since = date(DATE_ISO8601, strtotime('-'.$atts['since']));
}
}
$out = '';
$commits_url = $project_url.'/repository/commits?';
if(!is_null($since)) {
$commits_url .= '&since='.$since; $commits_url .= '&since='.$since;
} }
@ -116,35 +88,30 @@ class LinuxfooGitlab {
$commits_json = file_get_contents($commits_url); $commits_json = file_get_contents($commits_url);
if(is_null($commits_json)) { if(is_null($commits_json)) {
return self::error(__('Commits URL not reachable.', 'linuxfoo-gitlab')); $out .= self::error(__('Commits URL not reachable.', 'linuxfoo-gitlab'));
} }
else {
$commits = json_decode($commits_json); $commits = json_decode($commits_json);
if(is_null($commits)) { if(is_null($commits)) {
return self::error(__('Commits information not readable.', 'linuxfoo-gitlab')); $out .= self::error(__('Commits information not readable.', 'linuxfoo-gitlab'));
} }
else {
$commits_count = count($commits); $commits_count = count($commits);
$commits_count = is_null($atts['max']) ? $commits_count : min($commits_count, $atts['max']); $commits_count = is_null($atts['max']) ? $commits_count : min($commits_count, $atts['max']);
$commits = array_slice($commits, 0, $commits_count);
if($commits_count==0) { if($commits_count==0) {
return $out .=
'<div class="linuxfoo_gitlab">'.
$project_header.
'<span class="stats">'. '<span class="stats">'.
__('No commits', 'linuxfoo-gitlab'). __('No commits', 'linuxfoo-gitlab').
(is_null($branch) ? '' : ' '.__('in branch', 'linuxfoo-gitlab').' "'.$branch.'"'). (is_null($branch) ? '' : ' '.__('in branch', 'linuxfoo-gitlab').' "'.$branch.'"').
(is_null($atts['since']) ? '' : ' '.('since '.$atts['since'].' ago')).'.'. (is_null($atts['since']) ? '' : ' '.('since '.$atts['since'].' ago')).'.'.
'</span>'. '</span>';
'</div>';
} }
else {
$commits = array_slice($commits, 0, $commits_count); $out .=
$out =
'<div class="linuxfoo_gitlab">'.
$project_header.
'<span class="stats">'. '<span class="stats">'.
/* translators: %d: maximum number of commits displayed */ /* translators: %d: maximum number of commits displayed */
sprintf(__('Last %d commits', 'linuxfoo-gitlab'), $commits_count). sprintf(__('Last %d commits', 'linuxfoo-gitlab'), $commits_count).
@ -166,27 +133,41 @@ class LinuxfooGitlab {
} }
$out .= '</ul>'; $out .= '</ul>';
}
if(!is_null($atts['releases'])) { }
if(!preg_match('/^(all|latest)$/', $atts['releases'])) {
return self::error(__('Invalid value for parameter "releases".', 'linuxfoo-gitlab'));
} }
return $out;
}
static function releases_list($atts, $project_url) {
$out = '';
if(is_null($atts['releases']) || $atts['releases']=='none') {
$out .= '';
}
elseif(!preg_match('/^(all|latest)$/', $atts['releases'])) {
$out .= self::error(__('Invalid value for parameter "releases".', 'linuxfoo-gitlab'));
}
else {
$releases_url = $project_url.'/releases?'; $releases_url = $project_url.'/releases?';
$releases_json = file_get_contents($releases_url); $releases_json = file_get_contents($releases_url);
if(is_null($releases_json)) { if(is_null($releases_json)) {
return self::error(__('Releases URL not reachable.', 'linuxfoo-gitlab')); $out .= self::error(__('Releases URL not reachable.', 'linuxfoo-gitlab'));
} }
else {
$releases = json_decode($releases_json); $releases = json_decode($releases_json);
if(is_null($releases)) { if(is_null($releases)) {
return self::error(__('Releases information not readable.', 'linuxfoo-gitlab')); $out .= self::error(__('Releases information not readable.', 'linuxfoo-gitlab'));
} }
else {
if(count($releases)==0) { if(count($releases)==0) {
$out .= '<p class="releases">'.__('This project has currently no releases.', 'linuxfoo-gitlab').'</p>'; $out .=
'<p class="releases">'.
__('This project has currently no releases.', 'linuxfoo-gitlab').
'</p>';
} }
elseif($atts['releases']=='latest') { elseif($atts['releases']=='latest') {
$out .= $out .=
@ -212,6 +193,61 @@ class LinuxfooGitlab {
$out .= '</ul>'; $out .= '</ul>';
} }
} }
}
}
return $out;
}
static function list_commits($atts, $content, $tag) {
return self::show_project($atts, $content, $tag);
}
static function show_project($atts, $content, $tag) {
global $post;
$out = '<div class="linuxfoo_gitlab">';
if(
is_null($atts['url']) ||
is_null($atts['project_id'])
) {
$out .= self::error(__('Required parameter missing.', 'linuxfoo-gitlab'));
}
elseif(!preg_match('/^[1-9][0-9]*$/', $atts['project_id'])) {
$out .= self::error(__('Invalid value for parameter "project_id".', 'linuxfoo-gitlab'));
}
elseif(!filter_var($atts['url'], FILTER_VALIDATE_URL)) {
$out .= self::error(__('Invalid value for parameter "url".', 'linuxfoo-gitlab'));
}
elseif(!preg_match('/^[1-9][0-9]*$/', $atts['project_id'])) {
$out .= self::error(__('Invalid value for parameter "project_id".', 'linuxfoo-gitlab'));
}
else {
$project_url = $atts['url'].'/api/v4/projects/'.$atts['project_id'];
$project_json = file_get_contents($project_url);
if(is_null($project_json)) {
$out .= self::error(__('Project URL not reachable.', 'linuxfoo-gitlab'));
}
else {
$project = json_decode($project_json);
if(is_null($project)) {
$out .= self::error('Project information not readable.');
}
else {
$out .=
'<span class="project_header">'.
__('Gitlab project', 'linuxfoo-gitlab').' '.
'<a href="'.$project->web_url.'">'.$project->name.'</a>'.
'</span>';
$out .= self::commits_list($atts, $project_url);
$out .= self::releases_list($atts, $project_url);
}
}
}
$out .= '</div>'; $out .= '</div>';
@ -223,4 +259,3 @@ add_action('plugins_loaded', 'LinuxfooGitlab::load_textdomain');
add_action('wp_enqueue_scripts', 'LinuxfooGitlab::css' ); add_action('wp_enqueue_scripts', 'LinuxfooGitlab::css' );
add_shortcode('gitlab-list-commits', 'LinuxfooGitlab::list_commits'); add_shortcode('gitlab-list-commits', 'LinuxfooGitlab::list_commits');
add_shortcode('gitlab-show-project', 'LinuxfooGitlab::show_project'); add_shortcode('gitlab-show-project', 'LinuxfooGitlab::show_project');