From c93fc9f959ff0853b3829931bb8ea6b5f59343b4 Mon Sep 17 00:00:00 2001 From: Marc Michalsky Date: Thu, 4 Nov 2021 17:14:00 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20add=20template=20variables=20for=20?= =?UTF-8?q?'days=5Fsince=5Flast=5Fupdate'=20and=20'assigned=5Fto'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- example_config.yaml | 2 +- main.py | 10 ++++++++-- requirements.txt | 1 + templates/example_template | 10 ++++++++-- 4 files changed, 18 insertions(+), 5 deletions(-) diff --git a/example_config.yaml b/example_config.yaml index e28135a..1d0d73c 100644 --- a/example_config.yaml +++ b/example_config.yaml @@ -4,7 +4,7 @@ redmine: url: ${REDMINE_URL} version: ${REDMINE_VERSION} api_key: ${REDMINE_API_KEY} - issue_closed_id: 5 # Id of the "closed" status + time_zone: "Europe/Berlin" no_bot_tag: "#nobot" # a tag that signals bot not to handle an issue actions: diff --git a/main.py b/main.py index 12884bb..d962b44 100644 --- a/main.py +++ b/main.py @@ -1,7 +1,8 @@ #!/usr/bin/env python3 import sys import os -from datetime import date, timedelta +from datetime import date, datetime, timedelta +import pytz import logging from redminelib import Redmine from envyaml import EnvYAML @@ -24,6 +25,7 @@ def treat_issues(): url = config['redmine']['url'] version = config['redmine']['version'] api_key = config['redmine']['api_key'] + time_zone = pytz.timezone(config['redmine']['time_zone']) no_bot_tag = config['redmine']['no_bot_tag'] actions = config['actions'] logging.getLogger().setLevel(config['logging']['level'].upper()) @@ -62,7 +64,11 @@ def treat_issues(): with open(f"{dir_path}templates/{action['template']}", newline='\r\n') as f: content = f.read() template = Template(content) - notes = template.render(author=issue.author.name, time_range=action['time_range'], url=issue.url) + notes = template.render( + issue=issue, + time_range=action['time_range'], + days_since_last_update=(datetime.now(time_zone) - issue.updated_on.replace(tzinfo=time_zone)).days + ) # Update issue if action.get('close_ticket') is True: diff --git a/requirements.txt b/requirements.txt index 1fdc5f6..ff66481 100644 --- a/requirements.txt +++ b/requirements.txt @@ -8,3 +8,4 @@ python-redmine==2.3.0 PyYAML==5.4.1 requests==2.25.1 urllib3==1.26.5 +pytz~=2021.3 \ No newline at end of file diff --git a/templates/example_template b/templates/example_template index 94a5b1f..c17b252 100644 --- a/templates/example_template +++ b/templates/example_template @@ -1,6 +1,12 @@ -Hello {{ author }}, +{% if issue.assigned_to is defined %} +Hello {{ issue.assigned_to }}, -this ticket wasn't updated for at least {{ time_range }}. We therefore assume that the problem has been solved in the meantime. If the issue persists, please reopen the ticket and give us a brief update on the situation. +this ticket is assigned to you but wasn't updated for at least {{ days_since_last_update }}. We therefore assume that the problem has been solved in the meantime. If the issue persists, please reopen the ticket and give us a brief update on the situation. +{% else %} +Hello {{ issue.author }}, + +this ticket wasn't updated for at least {{ days_since_last_update }}. We therefore assume that the problem has been solved in the meantime. If the issue persists, please reopen the ticket and give us a brief update on the situation. +{% endif %} Here is the ticket: {{ url }} Yours sincerely