diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 1518d59..83dd56c 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -53,6 +53,7 @@ repos: # files: ^*.py additional_dependencies: - pydantic + - types-PyYAML args: - --check-untyped-defs - --disallow-any-generics diff --git a/cards/merge_request.json.j2 b/cards/merge_request.json.j2 deleted file mode 100644 index 3546ed3..0000000 --- a/cards/merge_request.json.j2 +++ /dev/null @@ -1,314 +0,0 @@ -{ - "type": "AdaptiveCard", - "speak": {{ fallback }}, - "fallbackText": {{ fallback }}, - "body": [ - { - "size": "Small", - "text": " {{ precalc.path_with_namespace }} [!{{ precalc.iid }}]", - "weight": "Bolder", - "wrap": true, - "type": "TextBlock" - }, - { - "columns": [ - { - "width": "auto", - "items": [ - { - "type": "Icon", - "name": "BranchRequest", - "color": "Accent" - } - ], - "type": "Column", - "verticalContentAlignment": "Center" - }, - { - "width": "stretch", - "items": [ - - { - "size": "Large", - "text": " {{ precalc.title }}", - "weight": "Bolder", - "wrap": true, - "type": "TextBlock" - }, - { - "size": "Small", - "text": "Opened by {{ precalc.openner.name }}", - "wrap": true, - "type": "TextBlock" - } - ], - "verticalContentAlignment": "Center", - "spacing": "Small", - "type": "Column" - } - ], - "spacing": "Small", - "type": "ColumnSet" - }, - { - "type": "Table", - "columns": [ - { - "width": 1 - }, - { - "width": 2 - } - ], - "rows": [ - { - "type": "TableRow", - "cells": [ - { - "type": "TableCell", - "items": [ - { - "type": "TextBlock", - "text": "Last MR status", - "wrap": true, - "weight": "Bolder" - } - ], - "verticalContentAlignment": "Center" - }, - { - "type": "TableCell", - "items": [ - { - "type": "TextBlock", - "text": "{{ precalc.detailed_merge_status }}", - "wrap": true - } - ], - "verticalContentAlignment": "Center" - } - ] - }, - { - "type": "TableRow", - "cells": [ - { - "type": "TableCell", - "items": [ - { - "type": "TextBlock", - "text": "Last MR action", - "wrap": true, - "weight": "Bolder" - } - ] - }, - { - "type": "TableCell", - "items": [ - { - "type": "TextBlock", - "text": "{{ precalc.latest_action }}", - "wrap": true - } - ] - } - ] - }, - { - "type": "TableRow", - "cells": [ - { - "type": "TableCell", - "items": [ - { - "type": "TextBlock", - "text": "Source branch", - "wrap": true, - "weight": "Bolder" - } - ] - }, - { - "type": "TableCell", - "items": [ - { - "type": "TextBlock", - "text": "{{ precalc.source_branch }}", - "wrap": true - } - ] - } - ] - }, - { - "type": "TableRow", - "cells": [ - { - "type": "TableCell", - "items": [ - { - "type": "TextBlock", - "text": "Target branch", - "wrap": true, - "weight": "Bolder" - } - ] - }, - { - "type": "TableCell", - "items": [ - { - "type": "TextBlock", - "text": "{{ precalc.target_branch }}", - "wrap": true - } - ] - } - ] - }, - - { - "type": "TableRow", - "cells": [ - { - "type": "TableCell", - "items": [ - { - "type": "TextBlock", - "text": "Approvals", - "wrap": true, - "weight": "Bolder" - } - ] - }, - { - "type": "TableCell", - "items": [ - { - "type": "TextBlock", - "text": "{{ precalc.approvers | join(",") }}", - "wrap": true - } - ] - } - ] - }, - - -{% if mri.head_pipeline_id %} - { - "type": "TableRow", - "cells": [ - { - "type": "TableCell", - "items": [ - { - "type": "TextBlock", - "text": "Head pipeline", - "wrap": true, - "weight": "Bolder" - } - ], - "verticalContentAlignment": "Center" - }, - { - "type": "TableCell", - "items": [ - { - "type": "TextBlock", - "text": "[{{ mri.head_pipeline_id }}]({{ precalc.project_url }}/-/pipelines/{{ mri.head_pipeline_id }})", - "wrap": true - } - ], - "verticalContentAlignment": "Center" - } - ] - }, -{%- if precalc.latest_pipeline %} - { - "type": "TableRow", - "cells": [ - { - "type": "TableCell", - "items": [ - { - "type": "TextBlock", - "text": "Pipeline status", - "wrap": true, - "weight": "Bolder" - } - ], - "verticalContentAlignment": "Center" - }, - { - "type": "TableCell", - "items": [ - { - "type": "TextBlock", - "text": "{{ precalc.latest_pipeline.status }}", - "color": "{{ precalc.latest_pipeline.color }}", - "wrap": true, - "weight": "{{ precalc.latest_pipeline.weight }}" - } - ], - "verticalContentAlignment": "Center" - } - ] - }, -{%- endif %} -{%- endif %} - - - { - "type": "TableRow", - "cells": [ - { - "type": "TableCell", - "items": [ - { - "type": "TextBlock", - "text": "Last update", - "wrap": true, - "weight": "Bolder" - } - ], - "verticalContentAlignment": "Center" - }, - { - "type": "TableCell", - "items": [ - { - "type": "TextBlock", - "text": "{{now}}", - "wrap": true - } - ], - "verticalContentAlignment": "Center" - } - ] - } - ], - "firstRowAsHeaders": false, - "showGridLines": false - }, - { - "actions": [ - { - "title": "Overview", - "type": "Action.OpenUrl", - "url": "{{mri.merge_request_payload.object_attributes.url}}" - }, - { - "title": "Diff", - "type": "Action.OpenUrl", - "url": "{{mri.merge_request_payload.object_attributes.url}}/diffs" - } - ], - "type": "ActionSet", - "spacing": "ExtraLarge" - } - ], - "version": "1.5", - "$schema": "https://adaptivecards.io/schemas/adaptive-card.json" -} diff --git a/cards/merge_request.yaml.j2 b/cards/merge_request.yaml.j2 new file mode 100644 index 0000000..410483d --- /dev/null +++ b/cards/merge_request.yaml.j2 @@ -0,0 +1,169 @@ +$schema: https://adaptivecards.io/schemas/adaptive-card.json +type: AdaptiveCard +version: '1.5' +fallbackText: {{ fallback }} +speak: {{ fallback }} +body: +- size: Small + text: '{{ precalc.path_with_namespace }} [!{{ precalc.iid }}]' + type: TextBlock + weight: Bolder + wrap: true +- columns: + - items: + - color: Accent + name: BranchRequest + type: Icon + type: Column + verticalContentAlignment: Center + width: auto + - items: + - size: Large + text: '{{ precalc.title | e }}' + type: TextBlock + weight: Bolder + wrap: true + - size: Small + text: Opened by {{ precalc.openner.name }} + type: TextBlock + wrap: true + spacing: Small + type: Column + verticalContentAlignment: Center + width: stretch + spacing: Small + type: ColumnSet +- columns: + - width: 1 + - width: 2 + firstRowAsHeaders: false + rows: + - cells: + - items: + - text: Last MR status + type: TextBlock + weight: Bolder + wrap: true + type: TableCell + verticalContentAlignment: Center + - items: + - text: '{{ precalc.detailed_merge_status }}' + type: TextBlock + wrap: true + type: TableCell + verticalContentAlignment: Center + type: TableRow + - cells: + - items: + - text: Last MR action + type: TextBlock + weight: Bolder + wrap: true + type: TableCell + - items: + - text: '{{ precalc.latest_action }}' + type: TextBlock + wrap: true + type: TableCell + type: TableRow + - cells: + - items: + - text: Source branch + type: TextBlock + weight: Bolder + wrap: true + type: TableCell + - items: + - text: '{{ precalc.source_branch }}' + type: TextBlock + wrap: true + type: TableCell + type: TableRow + - cells: + - items: + - text: Target branch + type: TextBlock + weight: Bolder + wrap: true + type: TableCell + - items: + - text: '{{ precalc.target_branch }}' + type: TextBlock + wrap: true + type: TableCell + type: TableRow + - cells: + - items: + - text: Approvals + type: TextBlock + weight: Bolder + wrap: true + type: TableCell + - items: + - text: '{{ precalc.approvers | join(",") }}' + type: TextBlock + wrap: true + type: TableCell + type: TableRow +{% if mri.head_pipeline_id %} + - cells: + - items: + - text: Head pipeline + type: TextBlock + weight: Bolder + wrap: true + type: TableCell + verticalContentAlignment: Center + - items: + - text: '[{{ mri.head_pipeline_id }}]({{ precalc.project_url }}/-/pipelines/{{ mri.head_pipeline_id }})' + type: TextBlock + wrap: true + type: TableCell + verticalContentAlignment: Center + type: TableRow +{% if precalc.latest_pipeline %} + - cells: + - items: + - text: Pipeline status + type: TextBlock + weight: Bolder + wrap: true + type: TableCell + verticalContentAlignment: Center + - items: + - color: {{ precalc.latest_pipeline.color }} + text: {{ precalc.latest_pipeline.status }} + type: TextBlock + weight: {{ precalc.latest_pipeline.weight }} + wrap: true + type: TableCell + verticalContentAlignment: Center + type: TableRow +{% endif %} +{% endif %} + - cells: + - items: + - text: Last update + type: TextBlock + weight: Bolder + wrap: true + type: TableCell + verticalContentAlignment: Center + - items: + - text: '{{now}}' + type: TextBlock + wrap: true + type: TableCell + verticalContentAlignment: Center + type: TableRow + showGridLines: false + type: Table +- actions: + - title: Overview + type: Action.OpenUrl + url: '{{ mri.merge_request_payload.object_attributes.url }}' + - title: Diff + type: Action.OpenUrl + url: '{{ mri.merge_request_payload.object_attributes.url }}/diffs' + spacing: ExtraLarge + type: ActionSet diff --git a/cards/render.py b/cards/render.py index 9b54ae2..66b1558 100644 --- a/cards/render.py +++ b/cards/render.py @@ -3,6 +3,7 @@ import json from typing import Any +import yaml from jinja2 import Environment from jinja2 import FileSystemLoader from jinja2 import Template @@ -12,7 +13,7 @@ def render(mri: MergeRequestInfos) -> dict[str, Any]: env = Environment(loader=FileSystemLoader("./cards/")) - templ: Template = env.get_template("merge_request.json.j2") + templ: Template = env.get_template("merge_request.yaml.j2") fallback = ( f"MR {mri.merge_request_payload.object_attributes.state}:" @@ -57,7 +58,7 @@ def render(mri: MergeRequestInfos) -> dict[str, Any]: now=datetime.datetime.now(), ) # print(rendered) - # open("/tmp/notiteams-gitlab-mr-api-OUTPUT.json", "w").write(rendered) - result_as_json: dict[str, Any] = json.loads(rendered) + # open("/tmp/notiteams-gitlab-mr-api-OUTPUT.yaml", "w").write(rendered) + result_as_json: dict[str, Any] = yaml.safe_load(rendered) return result_as_json diff --git a/requirements.txt b/requirements.txt index 9c9e277..e091329 100644 --- a/requirements.txt +++ b/requirements.txt @@ -5,6 +5,7 @@ httpx blibs fastapi[standard] jinja2 +pyyaml asgi-logger opentelemetry-distro opentelemetry-exporter-otlp