From 913ac73ae24b25430101f51ec2b719b4816cff7e Mon Sep 17 00:00:00 2001 From: Markus Bergholz Date: Tue, 4 Jan 2022 09:11:04 +0100 Subject: [PATCH] init --- .gitignore | 140 ++++++++++++++++++ README.md | 24 +++ galaxy.yml | 62 ++++++++ .../modules/gitlab_merge_request_comment.py | 95 ++++++++++++ 4 files changed, 321 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100644 galaxy.yml create mode 100644 plugins/modules/gitlab_merge_request_comment.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f8b73e7 --- /dev/null +++ b/.gitignore @@ -0,0 +1,140 @@ +# ---> Python +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +share/python-wheels/ +*.egg-info/ +.installed.cfg +*.egg +MANIFEST + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.nox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +*.py,cover +.hypothesis/ +.pytest_cache/ +cover/ + +# Translations +*.mo +*.pot + +# Django stuff: +*.log +local_settings.py +db.sqlite3 +db.sqlite3-journal + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +# PyBuilder +.pybuilder/ +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# IPython +profile_default/ +ipython_config.py + +# pyenv +# For a library or package, you might want to ignore these files since the code is +# intended to run in multiple environments; otherwise, check them in: +# .python-version + +# pipenv +# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. +# However, in case of collaboration, if having platform-specific dependencies or dependencies +# having no cross-platform support, pipenv may install dependencies that don't work, or not +# install all needed dependencies. +#Pipfile.lock + +# PEP 582; used by e.g. github.com/David-OConnor/pyflow +__pypackages__/ + +# Celery stuff +celerybeat-schedule +celerybeat.pid + +# SageMath parsed files +*.sage.py + +# Environments +.env +.venv +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ + +# Spyder project settings +.spyderproject +.spyproject + +# Rope project settings +.ropeproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ +.dmypy.json +dmypy.json + +# Pyre type checker +.pyre/ + +# pytype static type analyzer +.pytype/ + +# Cython debug symbols +cython_debug/ + diff --git a/README.md b/README.md new file mode 100644 index 0000000..ed103b5 --- /dev/null +++ b/README.md @@ -0,0 +1,24 @@ +# Ansible Collection - markuman.scm + +Documentation for the collection. + + +## markuman.scm.gitlab_merge_request_comment + +* Task works only if `CI_MERGE_REQUEST_IID` is defined +* `api_token` parameter can also be read from ENV `ANSIBLE_GITLAB_API_TOKEN` +* It's designed to act like a notification bot for merge requests +* Designed to run in a GitLab CI/CD Pipeline + +```yml + + - name: post message + markuman.scm.gitlab_merge_request_comment: + api_url: gitlab.com + comment: | + Summary + + | some | table | + | --- | --- | + | yes | 🐧 | +``` \ No newline at end of file diff --git a/galaxy.yml b/galaxy.yml new file mode 100644 index 0000000..3375f6f --- /dev/null +++ b/galaxy.yml @@ -0,0 +1,62 @@ +### REQUIRED +# The namespace of the collection. This can be a company/brand/organization or product namespace under which all +# content lives. May only contain alphanumeric lowercase characters and underscores. Namespaces cannot start with +# underscores or numbers and cannot contain consecutive underscores +namespace: markuman + +# The name of the collection. Has the same character restrictions as 'namespace' +name: scm + +# The version of the collection. Must be compatible with semantic versioning +version: 1.0.0-dev + +# The path to the Markdown (.md) readme file. This path is relative to the root of the collection +readme: README.md + +# A list of the collection's content authors. Can be just the name or in the format 'Full Name (url) +# @nicks:irc/im.site#channel' +authors: +- Markus Bergholz + + +### OPTIONAL but strongly recommended +# A short summary description of the collection +description: gitea modules and some costum gitlab modules + +# Either a single license or a list of licenses for content inside of a collection. Ansible Galaxy currently only +# accepts L(SPDX,https://spdx.org/licenses/) licenses. This key is mutually exclusive with 'license_file' +license: +- GPL-2.0-or-later + +# The path to the license file for the collection. This path is relative to the root of the collection. This key is +# mutually exclusive with 'license' +license_file: '' + +# A list of tags you want to associate with the collection for indexing/searching. A tag name has the same character +# requirements as 'namespace' and 'name' +tags: [] + +# Collections that this collection requires to be installed for it to be usable. The key of the dict is the +# collection label 'namespace.name'. The value is a version range +# L(specifiers,https://python-semanticversion.readthedocs.io/en/latest/#requirement-specification). Multiple version +# range specifiers can be set and are separated by ',' +dependencies: {} + +# The URL of the originating SCM repository +repository: https://git.osuv.de/m/markuman.scm + +# The URL to any online docs +documentation: https://git.osuv.de/m/markuman.scm + +# The URL to the homepage of the collection/project +homepage: https://git.osuv.de/m/markuman.scm + +# The URL to the collection issue tracker +issues: https://github.com/markuman/markuman.scm/issue + +# A list of file glob-like patterns used to filter any files or directories that should not be included in the build +# artifact. A pattern is matched from the relative path of the file or directory of the collection directory. This +# uses 'fnmatch' to match the files or directories. Some directories and files like 'galaxy.yml', '*.pyc', '*.retry', +# and '.git' are always filtered +build_ignore: [] + diff --git a/plugins/modules/gitlab_merge_request_comment.py b/plugins/modules/gitlab_merge_request_comment.py new file mode 100644 index 0000000..a12c85c --- /dev/null +++ b/plugins/modules/gitlab_merge_request_comment.py @@ -0,0 +1,95 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- +# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) + +from __future__ import absolute_import, division, print_function +__metaclass__ = type + +DOCUMENTATION = ''' +--- +module: gitlab_merge_request_comment +short_description: write messages to gitlab merge requests +description: + - write messages to gitlab merge requests. + - works only if `CI_MERGE_REQUEST_IID` is defined + - it's designed to run inside gitlab ci/cd pipeline +version_added: "1.0.0" +author: + - "Markus Bergholz (@markuman)" +options: + comment: + description: + - Comment message. + required: true + type: str + api_url: + description: + - Your gitlab url + required: true + api_token: + description: + - API Token. + - If not provided, it's read from ENV ANSIBLE_GITLAB_API_TOKEN + required: false + type: str +''' + +EXAMPLES = ''' + - name: post message + markuman.scm.gitlab_merge_request_comment: + api_url: gitlab.com + comment: | + Summary + + | some | table | + | --- | --- | + | yes | 🐧 | +''' + +from ansible.module_utils.basic import AnsibleModule +import requests +import os + + +def main(): + module = AnsibleModule( + argument_spec=dict( + comment=dict(required=True, type='str'), + api_token=dict(required=False, type='str', no_log=True), + api_url=dict(required=True, type='str') + ) + ) + + comment = module.params.get("comment") + api_url = module.params.get("api_url") + api_token = module.params.get("api_token") or os.environ.get('ANSIBLE_GITLAB_API_TOKEN') + + mr_id = os.environ.get('CI_MERGE_REQUEST_IID') + + if mr_id and api_token: + pr_id = os.environ.get('CI_MERGE_REQUEST_PROJECT_ID') + gitlab_mr_url = f'https://{api_url}/api/v4/projects/{pr_id}/merge_requests/{mr_id}/notes' + + headers = { + 'Accept': 'application/json', + 'Private-Token': api_token + } + + data = { + 'body': comment + } + + x = requests.post(gitlab_mr_url, data = data, headers = headers) + + change = False + if x.status_code == 201: + change = True + + module.exit_json(changed=change, status=x.status_code) + + else: + module.exit_json(changed=False, status=None) + + +if __name__ == '__main__': + main()