-
Notifications
You must be signed in to change notification settings - Fork 2
/
summarizer_invalidate.py
87 lines (77 loc) · 2.64 KB
/
summarizer_invalidate.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
"""Watches the Jira API and invalidates summaries when the issues are updated."""
import argparse
import logging
import os
from datetime import UTC, datetime, timedelta
from time import sleep
from atlassian import Jira # type: ignore
from jiraissues import check_response, get_self, with_retry
from summary_dbi import mariadb_db, mark_stale
def main() -> None: # pylint: disable=too-many-locals
"""Main function"""
parser = argparse.ArgumentParser(
description="Watch the Jira API and invalidate summaries when issues are updated"
)
# pylint: disable=duplicate-code
parser.add_argument(
"--log-level",
default="WARNING",
choices=["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"],
help="Set the logging level",
)
parser.add_argument(
"--db-host",
default="localhost",
type=str,
help="MariaDB host",
)
parser.add_argument(
"--db-port",
default=3306,
type=int,
help="MariaDB port",
)
args = parser.parse_args()
logging.basicConfig(
level=getattr(logging, args.log_level),
format="%(asctime)s %(levelname)s:%(name)s - %(message)s",
# datefmt="%Y-%m-%d %H:%M:%S.%f",
)
db_host = str(args.db_host)
db_port = int(args.db_port)
db = mariadb_db(host=db_host, port=db_port)
jira = Jira(url=os.environ["JIRA_URL"], token=os.environ["JIRA_TOKEN"])
user_tz = get_self(jira).tzinfo
# The window must be at least 1 munute due to the granularity of the jql
# query syntax.
window = timedelta(minutes=1)
while True:
start_time = datetime.now(tz=UTC)
until = start_time - window
until_string = until.astimezone(user_tz).strftime("%Y-%m-%d %H:%M")
since = until - window
since_string = since.astimezone(user_tz).strftime("%Y-%m-%d %H:%M")
issues = check_response(
with_retry(
lambda: jira.jql(
f"updated >= '{since_string}' AND updated < '{
until_string}' ORDER BY updated DESC",
limit=1000,
fields="key",
)
)
)
logging.info(
"Found %d issues updated between %s and %s",
len(issues["issues"]),
since_string,
until_string,
)
for issue in issues["issues"]:
key = issue["key"]
marked = mark_stale(db, key, add_ok=False)
if marked:
logging.info("Marked %s as stale", key)
sleep((window - (datetime.now(tz=UTC) - start_time)).seconds)
if __name__ == "__main__":
main()