Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Zammad plugin by filtering tickets after search #328

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 28 additions & 5 deletions did/plugins/zammad.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import urllib.parse
import urllib.request

from datetime import datetime

from did.base import Config, ReportError, get_token
from did.stats import Stats, StatsGroup
from did.utils import listed, log, pretty
Expand Down Expand Up @@ -61,7 +63,25 @@ def search(self, query):
try:
result = result["Ticket"]
except KeyError:
result = dict()
result = {}
log.debug("Result: {0} fetched".format(listed(len(result), "item")))
log.data(pretty(result))
return result

def get_articles(self, ticket_id):
""" Perform Zammad query """
url = self.url + "/ticket_articles/by_ticket/" + str(ticket_id)
log.debug("Zammad query: {0}".format(url))
try:
request = urllib.request.Request(url, headers=self.headers)
response = urllib.request.urlopen(request)
log.debug("Response headers:\n{0}".format(
str(response.info()).strip()))
except urllib.error.URLError as error:
log.debug(error)
raise ReportError(
"Zammad search on {0} failed.".format(self.url))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This part seems to be copy & pasted from the search() method. What about separating the common code to prevent duplication? There could be a common search() method used by both get_articles() and get_tickets(). What do you think, @come-nc?

result = json.loads(response.read())
log.debug("Result: {0} fetched".format(listed(len(result), "item")))
log.data(pretty(result))
return result
Expand Down Expand Up @@ -97,10 +117,13 @@ def fetch(self):
search = "article.from:\"{0}\" and article.created_at:[{1} TO {2}]".format(
self.user.name, self.options.since, self.options.until)
query = "tickets/search?query={0}".format(urllib.parse.quote(search))
self.stats = [
Ticket(ticket) for id,
ticket in self.parent.zammad.search(query).items()]

self.stats = []
for _,ticket in self.parent.zammad.search(query).items():
for article in self.parent.zammad.get_articles(ticket["id"]):
updated_at = datetime.fromisoformat(article["updated_at"].replace('Z', '+00:00')).date()
if (article["created_by"] == self.user.email) and (updated_at >= self.options.since.date) and (updated_at <= self.options.until.date):
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, wrap the long line to make pre-commit happy. Thanks.

self.stats.append(Ticket(ticket))
break

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Stats Group
Expand Down
Loading