|
| 1 | +# This file is part of Ansible collection geoffreyvanwyk.moodle. |
| 2 | +# |
| 3 | +# Ansible collection geoffreyvanwyk.moodle is free software: you can redistribute it |
| 4 | +# and/or modify it under the terms of the GNU General Public License as |
| 5 | +# published by the Free Software Foundation, either version 3 of the License, or |
| 6 | +# (at your option) any later version. |
| 7 | +# |
| 8 | +# Ansible collection geoffreyvanwyk.moodle is distributed in the hope that it will be |
| 9 | +# useful, but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General |
| 11 | +# Public License for more details. |
| 12 | +# |
| 13 | +# You should have received a copy of the GNU General Public License along with |
| 14 | +# Ansible collection geoffreyvanwyk.moodle. If not, see |
| 15 | +# <https://www.gnu.org/licenses/>. |
| 16 | + |
| 17 | +## |
| 18 | +# Ansible lookup plugin that retrieves the name of the stable Git branch that |
| 19 | +# corresponds to the given Moodle major version. |
| 20 | +# |
| 21 | +# @copyright 2024 Geoffrey Bernardo van Wyk {@link https://geoffreyvanwyk.dev} |
| 22 | +# @link https://github.com/moodle/moodle |
| 23 | +## |
| 24 | + |
| 25 | +DOCUMENTATION = """ |
| 26 | +name: stable_branch |
| 27 | +author: Geoffrey Bernardo van Wyk (https://geoffreyvanwyk.dev) |
| 28 | +version_added: 1.1.0 |
| 29 | +short_description: Stable branch for Moodle version |
| 30 | +description: |
| 31 | + - This lookup plugin retrieves the name of the stable Git branch that |
| 32 | + corresponds to the given Moodle version. |
| 33 | + - The Git branch is in Moodle's repository on GitHub (https://github.com/moodle/moodle). |
| 34 | +options: |
| 35 | + moodle_version: |
| 36 | + description: |
| 37 | + - The human-readable version of Moodle in the format x.y, where x is a |
| 38 | + single-digit number and y is a single or double-digit number. |
| 39 | + type: str |
| 40 | + required: true |
| 41 | +""" |
| 42 | + |
| 43 | +EXAMPLE = """ |
| 44 | +- name: Find stable branch for major Moodle version |
| 45 | + ansible.builtin.debug: |
| 46 | + msg: lookup('geoffreyvanwyk.moodle.stable_branch', moodle_version="3.11") |
| 47 | +""" |
| 48 | + |
| 49 | +RETURN = """ |
| 50 | + _raw: |
| 51 | + description: |
| 52 | + - A single-element list containing the name of a stable Git branch, e.g. V(['MOODLE_311_STABLE']). |
| 53 | + type: list |
| 54 | + elements: str |
| 55 | +""" |
| 56 | + |
| 57 | +from ansible.errors import AnsibleLookupError |
| 58 | +from ansible.plugins.lookup import LookupBase |
| 59 | + |
| 60 | + |
| 61 | +class LookupModule(LookupBase): |
| 62 | + def run(self, terms, variables=None, **kwargs): |
| 63 | + self.set_options(var_options=variables, direct=kwargs) |
| 64 | + moodle_version = str(self.get_option("moodle_version")) |
| 65 | + major_version, minor_version = moodle_version.split(".", maxsplit=1) |
| 66 | + |
| 67 | + if len(major_version) != 1: |
| 68 | + raise AnsibleLookupError("Invalid moodle_version") |
| 69 | + |
| 70 | + if len(minor_version) not in [1, 2]: |
| 71 | + raise AnsibleLookupError("Invalid moodle_version") |
| 72 | + |
| 73 | + branch_number = major_version + ( |
| 74 | + minor_version if len(minor_version) == 2 else "0" + minor_version |
| 75 | + ) |
| 76 | + |
| 77 | + return ["MOODLE_" + branch_number + "_STABLE"] |
0 commit comments