Skip to content
Permalink
Browse files
[FLINK-22570] combine jira client calls and reduce number of notifica…
…tions to one per ticket per rule
  • Loading branch information
knaufk committed May 19, 2021
1 parent f185d86 commit 7cccb045fb9a53d1e8d5b72a644099b0a90bc08d
Showing 4 changed files with 69 additions and 63 deletions.
@@ -60,58 +60,14 @@ def has_recently_updated_subtask(self, parent, updated_within_days):
issues = self.get_issues(find_subtasks_updated_within)
return len(issues) > 0

def add_label(self, issue, label):
labels = issue["fields"]["labels"] + [label]
fields = {"labels": labels}
key = issue["key"]

if not self.is_dry_run:
self.jira_client.update_issue_field(key, fields)
else:
logging.info(f'DRY RUN ({key}): Adding label "{label}".')

def replace_label(self, issue, old_label, new_label):
labels = issue["fields"]["labels"] + [new_label]
labels.remove(old_label)
fields = {"labels": labels}
key = issue["key"]

if not self.is_dry_run:
self.jira_client.update_issue_field(key, fields)
else:
logging.info(
f'DRY RUN ({key}): Replace label "{old_label}" for "{new_label}".'
)

def add_comment(self, key, comment):
def add_label_with_comment(self, key, label, comment):
if not self.is_dry_run:
self.jira_client.issue_add_comment(key, comment)
else:
logging.info(f'DRY_RUN ({key}): Adding comment "{comment}".')

def close_issue(self, key):
if not self.is_dry_run:
self.jira_client.set_issue_status(
key, "Closed", fields={"resolution": {"name": "Auto Closed"}}
self.jira_client.edit_issue(
key,
{"labels": [{"add": label}], "comment": [{"add": {"body": comment}}]},
)
else:
logging.info(f"DRY_RUN (({key})): Closing.")

def unassign(self, key):
if not self.is_dry_run:
if self.jira_client.get_issue_status(key) == "In Progress":
self.jira_client.assign_issue(key, self.jira_client.username)
self.jira_client.set_issue_status(key, "Open")
self.jira_client.assign_issue(key, None)
else:
logging.info(f"DRY_RUN (({key})): Unassigning.")

def set_priority(self, key, priority):
if not self.is_dry_run:
fields = {"priority": {"name": priority}}
self.jira_client.update_issue_field(key, fields)
else:
logging.info(f"DRY_RUN (({key})): Setting to {priority}")
logging.info(f'DRY RUN ({key}): Adding label "{label}".')

@abc.abstractmethod
def run(self):
@@ -140,12 +96,11 @@ def mark_stale_tickets_stale(self, jql_query):
warning_label=self.warning_label,
)

self.add_label(issue, self.warning_label)
self.add_comment(key, formatted_comment)
self.add_label_with_comment(key, self.warning_label, formatted_comment)

else:
logging.info(
f"Found https://issues.apache.org/jira/browse/{key}, but is has recently updated Subtasks. "
f"Found https://issues.apache.org/jira/browse/{key}, but is has recently updated Subtasks."
f"Ignoring for now."
)

@@ -165,6 +120,6 @@ def handle_tickets_marked_stale(self, jql_query):
done_label=self.done_label,
)

self.add_comment(key, formatted_comment)
self.replace_label(issue, self.warning_label, self.done_label)
self.handle_stale_ticket(key)
self.handle_stale_ticket(
key, self.warning_label, self.done_label, formatted_comment
)
@@ -41,5 +41,26 @@ def run(self):
f"AND updated < startOfDay(-{self.stale_days}d)"
)

def handle_stale_ticket(self, key):
self.unassign(key)
def handle_stale_ticket(self, key, warning_label, done_label, comment):
self.unassign(key, warning_label, done_label, comment)

def unassign(self, key, warning_label, done_label, comment):
if not self.is_dry_run:
if self.jira_client.get_issue_status(key) == "In Progress":
self.jira_client.edit_issue(
key,
{"assignee": [{"set": {"name": self.jira_client.username}}]},
notify_users=False,
)
self.jira_client.set_issue_status(key, "Open")
self.jira_client.edit_issue(
key,
{
"labels": [{"add": done_label}, {"remove": warning_label}],
"comment": [{"add": {"body": comment}}],
"assignee": [{"set": {"name": None}}],
},
notify_users=False,
)
else:
logging.info(f"DRY_RUN (({key})): Unassigning.")
@@ -17,6 +17,7 @@
################################################################################

from flink_jira_rule import FlinkJiraRule
import logging


class StaleMajorOrAboveRule(FlinkJiraRule):
@@ -42,8 +43,21 @@ def run(self):
)
self.mark_stale_tickets_stale(
f'project=FLINK AND type != "Sub-Task" AND priority = {self.priority} AND resolution = Unresolved '
f'AND assignee is empty AND updated < startOfDay(-{self.stale_days}d)'
f"AND assignee is empty AND updated < startOfDay(-{self.stale_days}d)"
)

def handle_stale_ticket(self, key):
self.set_priority(key, self.lower_priority)
def handle_stale_ticket(self, key, warning_label, done_label, comment):
self.set_priority(key, warning_label, done_label, self.lower_priority, comment)

def set_priority(self, key, warning_label, done_label, priority, comment):
if not self.is_dry_run:
self.jira_client.edit_issue(
key,
{
"labels": [{"add": done_label}, {"remove": warning_label}],
"comment": [{"add": {"body": comment}}],
"priority": [{"set": {"name": priority}}],
},
)
else:
logging.info(f"DRY_RUN (({key})): Setting to {priority}")
@@ -39,8 +39,24 @@ def run(self):
)
self.mark_stale_tickets_stale(
f'project = FLINK AND type != "Sub-Task" AND Priority = Minor AND resolution = Unresolved '
f'AND updated < startOfDay(-{self.stale_days}d)'
f"AND updated < startOfDay(-{self.stale_days}d)"
)

def handle_stale_ticket(self, key):
self.close_issue(key)
def handle_stale_ticket(self, key, warning_label, done_label, comment):
self.close_issue(key, warning_label, done_label, comment)

def close_issue(self, key, warning_label, done_label, comment):
if not self.is_dry_run:
self.jira_client.edit_issue(
key,
{"labels": [{"add": done_label}, {"remove": warning_label}]},
notify_users=False,
)
self.jira_client.set_issue_status(
key,
"Closed",
fields={"resolution": {"name": "Auto Closed"}},
update={"comment": [{"add": {"body": comment}}]},
)
else:
logging.info(f"DRY_RUN (({key})): Closing.")

0 comments on commit 7cccb04

Please sign in to comment.