From 9727594d899b4aef977731f87731c5914634a71e Mon Sep 17 00:00:00 2001 From: Jon Date: Sat, 12 Jul 2025 19:12:57 +0930 Subject: [PATCH 01/11] feat(annotations): when no problem found, remove comment and set to approve PR ref: #6 #5 --- includes/usr/bin/annotations.py | 11 ++++++++++- playbooks/problem_matcher.yaml | 4 ++-- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/includes/usr/bin/annotations.py b/includes/usr/bin/annotations.py index 733983b..4ffef7b 100755 --- a/includes/usr/bin/annotations.py +++ b/includes/usr/bin/annotations.py @@ -6,7 +6,7 @@ import json # import requests import os - +# API Docs: https://docs.github.com/en/rest/pulls/reviews?apiVersion=2022-11-28#create-a-review-for-a-pull-request def default_matcher( entry, tool_name = '' ) -> dict: @@ -277,6 +277,15 @@ for msg_type, value in review_body.items(): ) +if len(api_body['comments']) == 0: + + api_body.update({ + 'event': 'APPROVE' + }) + del api_body['body'] + + + data = { "pull_request": pull_request, "api_body": api_body diff --git a/playbooks/problem_matcher.yaml b/playbooks/problem_matcher.yaml index 443d9d8..656ff71 100644 --- a/playbooks/problem_matcher.yaml +++ b/playbooks/problem_matcher.yaml @@ -18,14 +18,14 @@ ansible.builtin.set_fact: git_api_url: "{{ payload.repository.url }}" when: > - git_api_url == "" + git_api_url == "" - name: Set var gitea_replace_url if empty ansible.builtin.set_fact: gitea_replace_url: "{{ payload.repository.url }}" when: > - gitea_replace_url == "" + gitea_replace_url == "" - name: Set required Facts From d60383e6f6b3028c7ca9afd124334d9846add159 Mon Sep 17 00:00:00 2001 From: Jon Date: Sat, 12 Jul 2025 19:15:17 +0930 Subject: [PATCH 02/11] ci: Add PR Linting ref: #6 --- .gitea/workflows/pull_request.yaml | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 .gitea/workflows/pull_request.yaml diff --git a/.gitea/workflows/pull_request.yaml b/.gitea/workflows/pull_request.yaml new file mode 100644 index 0000000..bf2819e --- /dev/null +++ b/.gitea/workflows/pull_request.yaml @@ -0,0 +1,28 @@ +--- + +name: Lint (Pull Request) + + +on: + pull_request: {} + + +jobs: + + lint: + runs-on: ubuntu-latest + steps: + + # - name: Enable Matcher Service + # run: | + # echo "NFC_PROBLEM_MATCHER=${GITHUB_REF_NAME}"; + + - uses: actions/checkout@v3 + + - name: Install YAMLLint + run: pip install yamllint + + - name: Run YAMLLint + run: | + echo "NFC_PROBLEM_MATCHER_TYPE=YAML-Lint" + yamllint -f github . || true From dc57a65a1c92711b11c4a2ba0678ae382e0e529a Mon Sep 17 00:00:00 2001 From: Jon Date: Sat, 12 Jul 2025 19:22:45 +0930 Subject: [PATCH 03/11] feat(problem_matcher): when not enabled, dont process further ref: #6 --- includes/usr/bin/annotations.py | 6 +++++- playbooks/problem_matcher.yaml | 8 +++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/includes/usr/bin/annotations.py b/includes/usr/bin/annotations.py index 4ffef7b..ce05480 100755 --- a/includes/usr/bin/annotations.py +++ b/includes/usr/bin/annotations.py @@ -205,7 +205,11 @@ for line in sys.stdin: if not NFC_PROBLEM_MATCHER: - sys.exit(2) + print(json.dumps({ + 'pull_request': '' + }, indent=4)) + + sys.exit(0) if not results: diff --git a/playbooks/problem_matcher.yaml b/playbooks/problem_matcher.yaml index 656ff71..94a3fcb 100644 --- a/playbooks/problem_matcher.yaml +++ b/playbooks/problem_matcher.yaml @@ -94,11 +94,15 @@ validate_certs: false no_log: "{{ disable_logging }}" register: http_get_pull_request + when: > + annotations.pull_request | string - name: Trace - Display Pull Request State ansible.builtin.debug: msg: "{{ http_get_pull_request.json.state | default('No PR found') }}" + when: > + not http_get_pull_request.skipped | bool | default(false) - name: Post review @@ -116,4 +120,6 @@ when: > http_get_pull_request.json.state | default('-') != 'closed' and - http_get_pull_request.status == 200 + http_get_pull_request.status | default(0) == 200 + and + not http_get_pull_request.skipped | bool | default(false) From 4b9133a626d57ad68d34b395ac215a0c543aa79f Mon Sep 17 00:00:00 2001 From: Jon Date: Sat, 12 Jul 2025 21:15:45 +0930 Subject: [PATCH 04/11] ci: enable problem matcher parsing ref: #6 #5 --- .gitea/workflows/pull_request.yaml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.gitea/workflows/pull_request.yaml b/.gitea/workflows/pull_request.yaml index bf2819e..2c79b68 100644 --- a/.gitea/workflows/pull_request.yaml +++ b/.gitea/workflows/pull_request.yaml @@ -13,9 +13,9 @@ jobs: runs-on: ubuntu-latest steps: - # - name: Enable Matcher Service - # run: | - # echo "NFC_PROBLEM_MATCHER=${GITHUB_REF_NAME}"; + - name: Enable Matcher Service + run: | + echo "NFC_PROBLEM_MATCHER=${GITHUB_REF_NAME}"; - uses: actions/checkout@v3 From a2b935c5cb792d10c5c72ff7137022ea563698b7 Mon Sep 17 00:00:00 2001 From: Jon Date: Sat, 12 Jul 2025 21:31:41 +0930 Subject: [PATCH 05/11] fix(problem_matcher): Ensure commit var set for annotation parsing ref: #6 --- playbooks/problem_matcher.yaml | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/playbooks/problem_matcher.yaml b/playbooks/problem_matcher.yaml index 94a3fcb..4d54f1b 100644 --- a/playbooks/problem_matcher.yaml +++ b/playbooks/problem_matcher.yaml @@ -67,7 +67,9 @@ cmd: | set -o pipefail; - export GITHUB_ACTOR={{ payload.sender.username }} + export GITHUB_ACTOR={{ payload.sender.username }}; + + export GITHUB_SHA={{ payload.workflow_job.head_sha }}; cat /tmp/job.log | annotations > /tmp/annotations.json; @@ -102,7 +104,7 @@ ansible.builtin.debug: msg: "{{ http_get_pull_request.json.state | default('No PR found') }}" when: > - not http_get_pull_request.skipped | bool | default(false) + not http_get_pull_request.skipped | default(false) | bool - name: Post review @@ -122,4 +124,4 @@ and http_get_pull_request.status | default(0) == 200 and - not http_get_pull_request.skipped | bool | default(false) + not http_get_pull_request.skipped | default(false) | bool From 4de933151a647bee6a90c71100e95925b449e313 Mon Sep 17 00:00:00 2001 From: Jon Date: Sat, 12 Jul 2025 21:31:59 +0930 Subject: [PATCH 06/11] ci(lint): add yaml lint file ref: #6 --- .yamllint | 77 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 .yamllint diff --git a/.yamllint b/.yamllint new file mode 100644 index 0000000..31b58ec --- /dev/null +++ b/.yamllint @@ -0,0 +1,77 @@ +--- + +# extends: default + + +ignore: + - '.github/' + - '**/crd/**' + - mkdocs.yml + - '*PrometheusRule*' + - '**/source/**' + +rules: + braces: + level: error + max-spaces-inside: 1 + min-spaces-inside: 1 + min-spaces-inside-empty: 0 + max-spaces-inside-empty: 0 + + brackets: + level: error + max-spaces-inside: 1 + min-spaces-inside: 1 + min-spaces-inside-empty: 0 + max-spaces-inside-empty: 0 + + colons: + level: warning + max-spaces-after: 1 + + commas: + level: warning + + comments: + level: error + require-starting-space: true + ignore-shebangs: true + min-spaces-from-content: 4 + + comments-indentation: + level: error + + document-end: + level: error + present: false + + document-start: + level: error + present: true + + empty-lines: + level: error + max: 3 + max-start: 0 + max-end: 0 + + hyphens: + level: error + max-spaces-after: 1 + + indentation: + level: error + spaces: 2 + indent-sequences: true + check-multi-line-strings: true + + line-length: + level: warning + max: 100 + allow-non-breakable-inline-mappings: true + + new-lines: + level: error + type: unix + + truthy: disable \ No newline at end of file From 5e69fb2807b86dcbd5d28089a2f8482beca3a93b Mon Sep 17 00:00:00 2001 From: Jon Date: Sat, 12 Jul 2025 21:32:15 +0930 Subject: [PATCH 07/11] chore: linting fixes ref: #6 --- .cz.yaml | 2 + extensions/eda/rulebooks/problem_matcher.yml | 2 + galaxy.yml | 39 ++------------------ playbooks/problem_matcher.yaml | 10 ++++- 4 files changed, 15 insertions(+), 38 deletions(-) diff --git a/.cz.yaml b/.cz.yaml index 3b76791..7ed25d0 100644 --- a/.cz.yaml +++ b/.cz.yaml @@ -12,7 +12,9 @@ commitizen: - fix - test - refactor + # yamllint disable rule:line-length commit_parser: ^(?Pfeat|fix|test|refactor|perf|BREAKING CHANGE)(?:\((?P[^()\r\n]*)\)|\()?(?P!)?:\s(?P.*)? + # yamllint enable rule:line-length name: cz_customize prerelease_offset: 1 tag_format: $version diff --git a/extensions/eda/rulebooks/problem_matcher.yml b/extensions/eda/rulebooks/problem_matcher.yml index f16a7fb..2f12778 100644 --- a/extensions/eda/rulebooks/problem_matcher.yml +++ b/extensions/eda/rulebooks/problem_matcher.yml @@ -27,10 +27,12 @@ - name: Process Completed workflow_job + # yamllint disable rule:indentation condition: > event.meta.headers['X-GitHub-Event'] == 'workflow_job' and event.payload.action == 'completed' + # yamllint enable rule:indentation actions: - run_playbook: diff --git a/galaxy.yml b/galaxy.yml index 4755466..37c96d7 100644 --- a/galaxy.yml +++ b/galaxy.yml @@ -1,79 +1,46 @@ --- -### 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: nofusscomputing -# The name of the collection. Has the same character restrictions as 'namespace' name: git_events -# The version of the collection. Must be compatible with semantic versioning version: 0.0.1 -# 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: - No Fuss Computing -### OPTIONAL but strongly recommended -# A short summary description of the collection description: Git[ea/hub] Problem matcher parser with PR Code Review - -# 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: - MIT -# 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: LICENCE -# 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: - ci - event - rulebook - tools -# 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://nofusscomputing.com/git/ansible-collections/git-events -# The URL to any online docs documentation: https://nofusscomputing.com/git/ansible-collections/git-events -# The URL to the homepage of the collection/project homepage: https://nofusscomputing.com/git/ansible-collections/git-events -# The URL to the collection issue tracker issues: https://nofusscomputing.com/git/ansible-collections/git-events/issues -# 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. Mutually exclusive with 'manifest' build_ignore: [ '.ansible', artifacts/, + '.cz.yaml', + '.dockerignore', + 'dockerfile', '.git', 'galaxy.yml', '*.tmp.*' ] -# A dict controlling use of manifest directives used in building the collection artifact. The key 'directives' is a -# list of MANIFEST.in style -# L(directives,https://packaging.python.org/en/latest/guides/using-manifest-in/#manifest-in-commands). The key -# 'omit_default_directives' is a boolean that controls whether the default directives are used. Mutually exclusive -# with 'build_ignore' -# manifest: null diff --git a/playbooks/problem_matcher.yaml b/playbooks/problem_matcher.yaml index 4d54f1b..b8aff3f 100644 --- a/playbooks/problem_matcher.yaml +++ b/playbooks/problem_matcher.yaml @@ -30,7 +30,9 @@ - name: Set required Facts ansible.builtin.set_fact: - git_url_api: "{{ payload.repository.url | replace((gitea_replace_url | split('/api/'))[0], git_api_url) }}" + git_url_api: >- + {{ payload.repository.url | + replace((gitea_replace_url | split('/api/'))[0], git_api_url) }} git_url_path_jobs: 'actions/jobs' head_sha: "{{ payload.workflow_job.head_sha }}" @@ -51,7 +53,9 @@ - name: Fetch job log ansible.builtin.uri: - url: "{{ git_url_api + '/' + git_url_path_jobs + '/' + payload.workflow_job.id | string + '/logs' }}" + url: >- + {{ git_url_api + '/' + git_url_path_jobs + + '/' + payload.workflow_job.id | string + '/logs' }} dest: /tmp/job.log headers: Authorization: token {{ lookup('env', 'GIT_API_TOKEN') }} @@ -119,9 +123,11 @@ timeout: 10 validate_certs: false no_log: "{{ disable_logging }}" + # yamllint disable rule:indentation when: > http_get_pull_request.json.state | default('-') != 'closed' and http_get_pull_request.status | default(0) == 200 and not http_get_pull_request.skipped | default(false) | bool + # yamllint enable rule:indentation From eb11625b66639936c5ce70f75e5a3f18468665f7 Mon Sep 17 00:00:00 2001 From: Jon Date: Sat, 12 Jul 2025 21:49:10 +0930 Subject: [PATCH 08/11] fix(problem_matcher): On approval body is required contrary to docs ref: #6 #5 --- includes/usr/bin/annotations.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/includes/usr/bin/annotations.py b/includes/usr/bin/annotations.py index ce05480..2b3383a 100755 --- a/includes/usr/bin/annotations.py +++ b/includes/usr/bin/annotations.py @@ -284,10 +284,9 @@ for msg_type, value in review_body.items(): if len(api_body['comments']) == 0: api_body.update({ + 'body': "G'day, I didn't find any problems to report on", 'event': 'APPROVE' }) - del api_body['body'] - data = { From f10663a2d5c9e89d2f930a2108c6114d045d4ce6 Mon Sep 17 00:00:00 2001 From: Jon Date: Sat, 12 Jul 2025 22:40:20 +0930 Subject: [PATCH 09/11] ci: add ansible-lint ref: #6 --- .gitea/workflows/pull_request.yaml | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/.gitea/workflows/pull_request.yaml b/.gitea/workflows/pull_request.yaml index 2c79b68..629cd9f 100644 --- a/.gitea/workflows/pull_request.yaml +++ b/.gitea/workflows/pull_request.yaml @@ -13,16 +13,30 @@ jobs: runs-on: ubuntu-latest steps: + - name: Enable Matcher Service run: | echo "NFC_PROBLEM_MATCHER=${GITHUB_REF_NAME}"; + - uses: actions/checkout@v3 + - name: Install YAMLLint run: pip install yamllint + - name: Run YAMLLint run: | echo "NFC_PROBLEM_MATCHER_TYPE=YAML-Lint" yamllint -f github . || true + + + - name: Install Ansible-Lint + run: pip install ansible-lint + + + - name: Run Ansible-Lint + run: | + echo "NFC_PROBLEM_MATCHER_TYPE=pylint-json"; + ansible-lint -f json . || true From ec9304fdc50e38dcc3cb16aad2c4a9edaca031d2 Mon Sep 17 00:00:00 2001 From: Jon Date: Sat, 12 Jul 2025 23:04:01 +0930 Subject: [PATCH 10/11] fix(problem_matcher): correct regex for when pylint content is not available ref: #6 --- includes/usr/bin/annotations.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/includes/usr/bin/annotations.py b/includes/usr/bin/annotations.py index 2b3383a..431e218 100755 --- a/includes/usr/bin/annotations.py +++ b/includes/usr/bin/annotations.py @@ -144,8 +144,8 @@ regex = { r'"description":\s*"(?P[^"]+)",\s*' r'"fingerprint":\s*"(?P[^"]+)",\s*' r'"location":\s*\{\s*"path":\s*"(?P[^"]+)".+?' - r'"line[s]?":.+?(?P\d+).*?\}},' - r'(?:\s"content":\s\{"body":\s"(?P.+?)")?' + r'"line[s]?":.+?(?P\d+).*?\}}' + r'(?:,\s"content":\s\{"body":\s"(?P.+?)")?' ) } From e03a201c30686524baafd9c68fa3e0c9aac52451 Mon Sep 17 00:00:00 2001 From: Jon Date: Sat, 12 Jul 2025 23:38:11 +0930 Subject: [PATCH 11/11] chore: linting fix ref: #6 --- .yamllint | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.yamllint b/.yamllint index 31b58ec..67b7637 100644 --- a/.yamllint +++ b/.yamllint @@ -74,4 +74,4 @@ rules: level: error type: unix - truthy: disable \ No newline at end of file + truthy: disable