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

Ticket 829: Fix Error processing due to: 'str' object has no attribute 'id' bug #22

Merged
merged 1 commit into from
May 2, 2024
Merged
Show file tree
Hide file tree
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
22 changes: 16 additions & 6 deletions cog_tickets.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,21 @@ def resolve_query_term(self, term):
ticket = self.redmine.get_ticket(int_id)
return [ticket]
except ValueError:
# not a numeric id, check team
if self.redmine.user_mgr.is_user_or_group(term):
return self.redmine.tickets_for_team(term)
else:
# assume a search term
return self.redmine.search_tickets(term)
# ignore
pass

# not a numeric id, check for known user or group
user_team = self.redmine.user_mgr.find(term)
if user_team:
log.debug(f"{term} -> {user_team}")
result = self.redmine.ticket_mgr.tickets_for_team(user_team)
if result:
return result
# note: fall thru for empty result from team query.

# assume a search term
log.debug(f"QUERY {term}")
return self.redmine.search_tickets(term)


@ticket.command(description="Query tickets")
Expand All @@ -71,6 +80,7 @@ async def query(self, ctx: discord.ApplicationContext, term: str):
results = self.resolve_query_term(query)
await self.bot.formatter.print_tickets(f"Search for '{query}'", results, ctx)


@ticket.command(description="Get ticket details")
@option("term", description="ticket ID")
async def details(self, ctx: discord.ApplicationContext, ticket_id:int):
Expand Down
5 changes: 3 additions & 2 deletions model.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,15 +190,16 @@ def get_custom_field(self, name: str) -> str:

return None

def full_name(self) -> str:
@property
def name(self) -> str:
if self.firstname is None or len(self.firstname) < 2:
return self.lastname
if self.lastname is None or len(self.lastname) < 2:
return self.firstname
return self.firstname + " " + self.lastname

def __str__(self):
return f"#{self.id} {self.full_name()} login={self.login} discord={self.discord_id}"
return f"#{self.id} {self.name} login={self.login} discord={self.discord_id}"

def set_field(self, fieldname:str, value):
setattr(self, fieldname, value)
Expand Down
4 changes: 0 additions & 4 deletions redmine.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,6 @@ def find_tickets(self) -> list[Ticket]:
def my_tickets(self, user=None) -> list[Ticket]:
return self.ticket_mgr.my_tickets(user)

def tickets_for_team(self, team_str:str) -> list[Ticket]:
return self.ticket_mgr.tickets_for_team(team_str)

def search_tickets(self, term) -> list[Ticket]:
return self.ticket_mgr.search(term)

Expand Down Expand Up @@ -171,7 +168,6 @@ def resolve_ticket(self, ticket_id, user_id=None):
return self.ticket_mgr.resolve_ticket(ticket_id, user_id)

def get_team(self, teamname:str) -> Team:
#return self.user_mgr.get_team_by_name(teamname) # FIXME consistent naming
return self.user_mgr.cache.get_team_by_name(teamname)

def update_sync_record(self, record:synctime.SyncRecord):
Expand Down
4 changes: 2 additions & 2 deletions test_cog_scn.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ async def test_team_join_leave(self):
# confirm in team via cog teams response
ctx = self.build_context()
await self.cog.teams(ctx, test_team_name)
self.assertIn(self.user.full_name(), str(ctx.respond.call_args))
self.assertIn(self.user.name, str(ctx.respond.call_args))

# leave team users
ctx = self.build_context()
Expand All @@ -97,7 +97,7 @@ async def test_team_join_leave(self):
# confirm not in team via cog teams response
ctx = self.build_context()
await self.cog.teams(ctx, test_team_name)
self.assertNotIn(self.user.full_name(), str(ctx.respond.call_args))
self.assertNotIn(self.user.name, str(ctx.respond.call_args))


async def test_thread_sync(self):
Expand Down
24 changes: 24 additions & 0 deletions test_cog_tickets.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,30 @@ async def test_thread_sync(self):
self.redmine.remove_ticket(ticket.id)


async def test_resolve_query_term(self):
ticket = self.create_test_ticket()

# expected results:
# 1. ticket ID
result_1 = self.cog.resolve_query_term(ticket.id)
self.assertEqual(ticket.id, result_1[0].id)

# 2. ticket team
result_2 = self.cog.resolve_query_term("ticket-intake")
self.assertEqual(ticket.id, result_2[0].id)

# 3. ticket user
result_3 = self.cog.resolve_query_term(self.user.login)
self.assertEqual(0, len(result_3)) # NOTHING ASSIGNED TO NEW TEST USER

# 4. ticket query term
result_4 = self.cog.resolve_query_term(self.tag)
self.assertEqual(ticket.id, result_4[0].id)

# delete the ticket
self.redmine.remove_ticket(ticket.id)


if __name__ == '__main__':
logging.basicConfig(level=logging.DEBUG, format="{asctime} {levelname:<8s} {name:<16} {message}", style='{')
logging.getLogger("urllib3.connectionpool").setLevel(logging.INFO)
Expand Down
2 changes: 1 addition & 1 deletion test_netbot.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ async def test_synchronize_ticket(self):

# assert method send called on mock thread, with the correct values
self.assertIn(self.tag, thread.send.call_args.args[0])
self.assertIn(self.user.full_name(), thread.send.call_args.args[0])
self.assertIn(self.user.name, thread.send.call_args.args[0])
self.assertIn(body, thread.send.call_args.args[0])

# get notes from redmine, assert tags in most recent
Expand Down
5 changes: 2 additions & 3 deletions tickets.py
Original file line number Diff line number Diff line change
Expand Up @@ -360,8 +360,6 @@ def my_tickets(self, user=None) -> list[Ticket]:


def tickets_for_team(self, team:Team) -> list[Ticket]:
# validate team?
#team = self.user_mgr.get_by_name(team_str) # find_user is dsigned to be broad
response = self.session.get(f"/issues.json?assigned_to_id={team.id}&status_id=open&sort={DEFAULT_SORT}&limit=100")

if not response:
Expand All @@ -371,7 +369,7 @@ def tickets_for_team(self, team:Team) -> list[Ticket]:
if result.total_count > 0:
return result.issues
else:
log.info("No open ticket for me.")
log.info(f"No open ticket for {team}: {result}")
return None


Expand All @@ -383,6 +381,7 @@ def search(self, term) -> list[Ticket]:

response = self.session.get(query)
if not response:
log.debug(f"SEARCH FAILED for {query}, zero results")
return None

# the response has only IDs....
Expand Down
Loading