diff --git a/README.md b/README.md index 6edc4b5..6614b18 100644 --- a/README.md +++ b/README.md @@ -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: ``` -[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 -* `url="STRING"`: Mandatory (example: `https://tk-sls.de/gitlab`). -* `project_id=NUM`: Mandatory; `NUM`: Positive Integer, valid project ID in Gitlab specified by `url` (example: `42`). +The following attributes are mandatory: + +* `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`). * `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`). * `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 diff --git a/lang/linuxfoo-gitlab-de_DE.po b/lang/linuxfoo-gitlab-de_DE.po index 524b928..200b3a2 100644 --- a/lang/linuxfoo-gitlab-de_DE.po +++ b/lang/linuxfoo-gitlab-de_DE.po @@ -50,6 +50,10 @@ msgstr "Jahren" msgid "since %d %s ago" 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 msgid "Required parameter missing." msgstr "Notwendiger Parameter fehlt" diff --git a/lang/linuxfoo-gitlab.pot b/lang/linuxfoo-gitlab.pot index 13f8001..810ea64 100644 --- a/lang/linuxfoo-gitlab.pot +++ b/lang/linuxfoo-gitlab.pot @@ -9,7 +9,7 @@ msgstr "" "MIME-Version: 1.0\n" "Content-Type: text/plain; charset=UTF-8\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" "X-Generator: WP-CLI 2.5.0\n" "X-Domain: linuxfoo-gitlab\n" @@ -23,7 +23,7 @@ msgid "http://tk-sls.de/ref/gitlab-list-commits" 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." +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 "" #. Author of the plugin @@ -34,90 +34,94 @@ msgstr "" msgid "https://tk-sls.de" msgstr "" -#: linuxfoo-gitlab.php:39 +#: linuxfoo-gitlab.php:37 msgid "days" msgstr "" -#: linuxfoo-gitlab.php:42 +#: linuxfoo-gitlab.php:40 msgid "months" msgstr "" -#: linuxfoo-gitlab.php:45 +#: linuxfoo-gitlab.php:43 msgid "years" msgstr "" #. translators: %d: count, %s unit of time interval -#: linuxfoo-gitlab.php:49 +#: linuxfoo-gitlab.php:47 msgid "since %d %s ago" msgstr "" -#: linuxfoo-gitlab.php:63 -msgid "Required parameter missing." +#: linuxfoo-gitlab.php:57 +msgid "Invalid value for parameter \"commits\"." msgstr "" -#: linuxfoo-gitlab.php:66 -#: 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 +#: linuxfoo-gitlab.php:61 msgid "Invalid value for parameter \"since\"." msgstr "" -#: linuxfoo-gitlab.php:119 +#: linuxfoo-gitlab.php:91 msgid "Commits URL not reachable." msgstr "" -#: linuxfoo-gitlab.php:125 +#: linuxfoo-gitlab.php:97 msgid "Commits information not readable." msgstr "" -#: linuxfoo-gitlab.php:136 +#: linuxfoo-gitlab.php:108 msgid "No commits" msgstr "" -#: linuxfoo-gitlab.php:137 -#: linuxfoo-gitlab.php:151 +#: linuxfoo-gitlab.php:109 +#: linuxfoo-gitlab.php:118 msgid "in branch" msgstr "" #. translators: %d: maximum number of commits displayed -#: linuxfoo-gitlab.php:150 +#: linuxfoo-gitlab.php:117 msgid "Last %d commits" msgstr "" -#: linuxfoo-gitlab.php:172 +#: linuxfoo-gitlab.php:150 msgid "Invalid value for parameter \"releases\"." msgstr "" -#: linuxfoo-gitlab.php:179 +#: linuxfoo-gitlab.php:157 msgid "Releases URL not reachable." msgstr "" -#: linuxfoo-gitlab.php:185 +#: linuxfoo-gitlab.php:163 msgid "Releases information not readable." msgstr "" -#: linuxfoo-gitlab.php:189 +#: linuxfoo-gitlab.php:169 msgid "This project has currently no releases." msgstr "" -#: linuxfoo-gitlab.php:194 +#: linuxfoo-gitlab.php:175 msgid "Latest release" msgstr "" -#: linuxfoo-gitlab.php:201 +#: linuxfoo-gitlab.php:182 msgid "Releases" 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 "" diff --git a/linuxfoo-gitlab.php b/linuxfoo-gitlab.php index 0e6f1b9..db9beee 100644 --- a/linuxfoo-gitlab.php +++ b/linuxfoo-gitlab.php @@ -3,9 +3,9 @@ /* Plugin Name: Gitlab REST API Shortcodes 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 -Version: 1.2 +Version: 1.4 Author URI: https://tk-sls.de */ @@ -18,16 +18,14 @@ class LinuxfooGitlab { } 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 ) { return - '
'. - ''. - $msg. - ''. - '
'; + ''. + $msg. + ''; } static function format_since($since) { @@ -49,55 +47,29 @@ class LinuxfooGitlab { return sprintf(__('since %d %s ago', 'linuxfoo-gitlab'), $num, $unit); } - static function list_commits($atts, $content, $tag) { - return self::show_project($atts, $content, $tag); - } + static function commits_list($atts, $project_url) { + $since = null; - static function show_project($atts, $content, $tag) { - global $post; - - if( - is_null($atts['url']) || - is_null($atts['project_id']) - ) { - return self::error(__('Required parameter missing.', 'linuxfoo-gitlab')); + if($atts['commits']=='none') { + return ''; } - elseif(!preg_match('/^[1-9][0-9]*$/', $atts['project_id'])) { - return self::error(__('Invalid value for parameter "project_id".', 'linuxfoo-gitlab')); + elseif(!is_null($atts['commits']) && $atts['commits']!='all') { + return self::error(__('Invalid value for parameter "commits".', '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 = - ''. - __('Gitlab project', 'linuxfoo-gitlab').' '.$project->name.''. - ''; - - $commits_url = $project_url.'/repository/commits?'; - - if(!is_null($atts['since'])) { + elseif(!is_null($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')); } + 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; } @@ -116,100 +88,164 @@ class LinuxfooGitlab { $commits_json = file_get_contents($commits_url); 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)) { - 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. - ''. - /* translators: %d: maximum number of commits displayed */ - sprintf(__('Last %d commits', 'linuxfoo-gitlab'), $commits_count). - (is_null($branch) ? '' : ' '.__('in branch', 'linuxfoo-gitlab').' "'.$branch.'"'). - (is_null($atts['since']) ? '' : ' '.self::format_since($atts['since'])).':'. - ''. - ''; - - if(!is_null($atts['releases'])) { - if(!preg_match('/^(all|latest)$/', $atts['releases'])) { - return self::error(__('Invalid value for parameter "releases".', 'linuxfoo-gitlab')); + if(is_null($commits)) { + $out .= self::error(__('Commits information not readable.', 'linuxfoo-gitlab')); } + else { + $commits_count = count($commits); + $commits_count = is_null($atts['max']) ? $commits_count : min($commits_count, $atts['max']); + $commits = array_slice($commits, 0, $commits_count); + + if($commits_count==0) { + $out .= + ''. + __('No commits', 'linuxfoo-gitlab'). + (is_null($branch) ? '' : ' '.__('in branch', 'linuxfoo-gitlab').' "'.$branch.'"'). + (is_null($atts['since']) ? '' : ' '.('since '.$atts['since'].' ago')).'.'. + ''; + } + else { + $out .= + ''. + /* translators: %d: maximum number of commits displayed */ + sprintf(__('Last %d commits', 'linuxfoo-gitlab'), $commits_count). + (is_null($branch) ? '' : ' '.__('in branch', 'linuxfoo-gitlab').' "'.$branch.'"'). + (is_null($atts['since']) ? '' : ' '.self::format_since($atts['since'])).':'. + ''. + ''; + } + } + } + + 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_json = file_get_contents($releases_url); 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)) { - return self::error(__('Releases information not readable.', 'linuxfoo-gitlab')); - } - - if(count($releases)==0) { - $out .= '

'.__('This project has currently no releases.', 'linuxfoo-gitlab').'

'; - } - elseif($atts['releases']=='latest') { - $out .= - '

'. - __('Latest release', 'linuxfoo-gitlab').': '. - ''.$releases[0]->name.''. - '

'; - } - elseif($atts['releases']=='all') { - $out .= - '

'. - __('Releases', 'linuxfoo-gitlab').': '. - '

'. - ''; + } + } + } + } + + 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 = '
'; + + 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 .= + ''. + __('Gitlab project', 'linuxfoo-gitlab').' '. + ''.$project->name.''. + ''; + + $out .= self::commits_list($atts, $project_url); + $out .= self::releases_list($atts, $project_url); + } } } @@ -223,4 +259,3 @@ add_action('plugins_loaded', 'LinuxfooGitlab::load_textdomain'); add_action('wp_enqueue_scripts', 'LinuxfooGitlab::css' ); add_shortcode('gitlab-list-commits', 'LinuxfooGitlab::list_commits'); add_shortcode('gitlab-show-project', 'LinuxfooGitlab::show_project'); -