Skip to content

Commit 1a2d320

Browse files
committed
review suggestions
1 parent d7e3855 commit 1a2d320

7 files changed

Lines changed: 166 additions & 158 deletions

File tree

Packs/Mattermost/Integrations/MattermostV2/MattermostV2.py

Lines changed: 34 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1247,15 +1247,18 @@ def list_private_channels_for_user_command(client: HTTPClient, args: dict[str, A
12471247
""" Lists private channels for user """
12481248
team_name = args.get('team_name', client.team_name)
12491249
user_id = args.get('user_id', '')
1250-
channels = []
1250+
list_private_channels = []
12511251

12521252
team_details = client.get_team_request(team_name)
12531253

1254-
user_channels = client.list_channels_for_user_request(team_details.get('id', ''), user_id)
1254+
all_user_channels = client.list_channels_for_user_request(team_details.get('id', ''), user_id)
12551255

12561256
params: dict[Any, Any] = {}
1257-
channels = client.list_channel_request(team_details.get('id', ''), params, get_private=True)
1258-
channels = [channel for channel in channels if channel['id'] in [c['channel_id'] for c in user_channels]]
1257+
list_private_channels = client.list_channel_request(team_details.get('id', ''), params, get_private=True)
1258+
channels = [
1259+
private_channel for private_channel in list_private_channels
1260+
if private_channel['id'] in [channel['channel_id'] for channel in all_user_channels]
1261+
]
12591262

12601263
user_details = client.get_user_request(user_id)
12611264

@@ -1268,6 +1271,7 @@ def list_private_channels_for_user_command(client: HTTPClient, args: dict[str, A
12681271
'id': user_id,
12691272
'channels': channels
12701273
},
1274+
raw_response=all_user_channels,
12711275
readable_output=hr,
12721276
)
12731277

@@ -1689,7 +1693,8 @@ def send_notification(client: HTTPClient, **args):
16891693
demisto.debug(f'MM: Found entitlement, saving message to context: {entitlement}')
16901694
save_entitlement(entitlement, message_id, reply, expiry, default_response, to if to else channel_id)
16911695
return CommandResults(
1692-
readable_output=f'Message sent to MatterMost successfully. Message ID is: {message_id}'
1696+
readable_output=f'Message sent to MatterMost successfully. Message ID is: {message_id}',
1697+
raw_response=raw_data
16931698
)
16941699

16951700

@@ -1707,65 +1712,47 @@ def list_groups_command(client: HTTPClient, args: dict[str, Any]) -> CommandResu
17071712
params = {'page': page, 'per_page': page_size, 'q': q}
17081713
group_details = client.list_groups_request(params)
17091714

1710-
hr = tableToMarkdown('User groups:', group_details, headers=['name', 'display_name', 'description', 'id'])
1715+
hr = tableToMarkdown('Groups:', group_details, headers=['name', 'display_name', 'description', 'id'])
17111716
return CommandResults(
17121717
outputs_prefix='Mattermost.Groups',
1713-
outputs_key_field='name',
1718+
outputs_key_field='id',
17141719
outputs=group_details,
1720+
raw_response=group_details,
17151721
readable_output=hr,
17161722
)
17171723

17181724

17191725
def list_group_members_command(client: HTTPClient, args: dict[str, Any]) -> CommandResults:
17201726
""" List the members of a user group """
1721-
group_name = args.get('group', '')
1727+
group_id = args.get('group_id', '')
17221728
member_details = {}
17231729

1724-
params = {'q': group_name}
1725-
group_details = client.list_groups_request(params)
1726-
1727-
if len(group_details) == 1:
1728-
group_detail = group_details[0]
1729-
elif len(group_details) == 0:
1730-
raise DemistoException('No matching user group found')
1731-
else:
1732-
raise DemistoException('User group pattern is not unique:\n' + '\n'.join([x['name'] for x in group_details]))
1733-
1734-
member_details = client.list_group_members_request(group_detail.get('id', ''))
1735-
member_details['id'] = group_detail.get('id', '')
1736-
member_details['name'] = group_detail.get('name', group_name)
1730+
member_details = client.list_group_members_request(group_id)
1731+
member_details['id'] = group_id
17371732

1738-
hr = tableToMarkdown('User group members:', member_details.get("members"), headers=['username', 'email', 'id'])
1733+
hr = tableToMarkdown('Group members:', member_details.get("members"), headers=['username', 'email', 'id'])
17391734
return CommandResults(
17401735
outputs_prefix='Mattermost.Groups',
1741-
outputs_key_field='name',
1736+
outputs_key_field='id',
17421737
outputs=member_details,
17431738
readable_output=hr,
1739+
raw_response=member_details
17441740
)
17451741

17461742

17471743
def add_group_member_command(client: HTTPClient, args: dict[str, Any]) -> CommandResults:
17481744
""" Adds a member to a user group """
1749-
group_name = args.get('group', '')
1745+
group_id = args.get('group_id', '')
17501746
user_ids = argToList(args.get('user_ids', ''))
17511747

1752-
params = {'q': group_name}
1753-
group_details = client.list_groups_request(params)
1754-
1755-
if len(group_details) == 1:
1756-
group_detail = group_details[0]
1757-
elif len(group_details) == 0:
1758-
raise DemistoException('No matching user group found')
1759-
else:
1760-
raise DemistoException('User group pattern is not unique:\n' + '\n'.join([x['name'] for x in group_details]))
1761-
17621748
data = {'user_ids': user_ids}
1763-
response = client.add_group_member_request(group_detail.get('id', ''), data)
1749+
response = client.add_group_member_request(group_id, data)
17641750

17651751
hr = []
17661752
for user in user_ids:
17671753
user_details = client.get_user_request(user)
1768-
hr.append(f'The member {user_details.get("username", user)} was added to the user group successfully, with group ID: {group_detail.get("id")}') # noqa: E501
1754+
hr.append(f'The member {user_details.get("username", user)} was added to the group successfully, with group ID: '
1755+
f'{group_id}')
17691756

17701757
return CommandResults(
17711758
readable_output="\n".join(hr),
@@ -1775,26 +1762,17 @@ def add_group_member_command(client: HTTPClient, args: dict[str, Any]) -> Comman
17751762

17761763
def remove_group_member_command(client: HTTPClient, args: dict[str, Any]) -> CommandResults:
17771764
""" Removes a member form a user group """
1778-
group_name = args.get('group', '')
1765+
group_id = args.get('group_id', '')
17791766
user_ids = argToList(args.get('user_ids', ''))
17801767

1781-
params = {'q': group_name}
1782-
group_details = client.list_groups_request(params)
1783-
1784-
if len(group_details) == 1:
1785-
group_detail = group_details[0]
1786-
elif len(group_details) == 0:
1787-
raise DemistoException('No matching user group found')
1788-
else:
1789-
raise DemistoException('User group pattern is not unique:\n' + '\n'.join([x['name'] for x in group_details]))
1790-
17911768
data = {'user_ids': user_ids}
1792-
response = client.remove_group_member_request(group_detail.get('id', ''), data)
1769+
response = client.remove_group_member_request(group_id, data)
17931770

17941771
hr = []
17951772
for user in user_ids:
17961773
user_details = client.get_user_request(user)
1797-
hr.append(f'The member {user_details.get("username", user)} was removed from the user group successfully, with group ID: {group_detail.get("id")}') # noqa: E501
1774+
hr.append(f'The member {user_details.get("username", user)} was removed from the group successfully, with group ID:'
1775+
f' {group_id}')
17981776

17991777
return CommandResults(
18001778
readable_output="\n".join(hr),
@@ -1806,12 +1784,13 @@ def set_channel_role_command(client: HTTPClient, args: dict[str, Any]) -> Comman
18061784
""" Set channel role for a channel member """
18071785
channel_id = args.get('channel_id', '')
18081786
user_id = args.get('user_id', '')
1809-
channel_role = "channel_user" + (" channel_admin" if args.get("role", "admin").lower() == "admin" else "")
1787+
channel_role = "channel_user" + (" channel_admin" if args.get("role", "member").lower() == "admin" else "")
18101788

18111789
client.set_channel_role_request(channel_id, user_id, channel_role)
18121790

18131791
user_details = client.get_user_request(user_id)
1814-
hr = f'Set channel role for {user_details.get("username", user_id)} successfully to {"Admin" if args.get("role", "admin").lower() == "admin" else "Member"}.' # noqa: E501
1792+
hr = f'Set channel role for {user_details.get("username", user_id)} successfully to ' \
1793+
f'{"Admin" if args.get("role", "admin").lower() == "admin" else "Member"}.'
18151794

18161795
return CommandResults(
18171796
readable_output=hr
@@ -1934,13 +1913,13 @@ def main(): # pragma: no cover
19341913
return_results(close_channel_command(client, args))
19351914
elif command == 'mattermost-send-file':
19361915
return_results(send_file_command(client, args))
1937-
elif command == 'mattermost-list-usergroups':
1916+
elif command == 'mattermost-list-groups':
19381917
return_results(list_groups_command(client, args))
1939-
elif command == 'mattermost-list-usergroup-members':
1918+
elif command == 'mattermost-list-group-members':
19401919
return_results(list_group_members_command(client, args))
1941-
elif command == 'mattermost-add-usergroup-member':
1920+
elif command == 'mattermost-add-group-member':
19421921
return_results(add_group_member_command(client, args))
1943-
elif command == 'mattermost-remove-usergroup-member':
1922+
elif command == 'mattermost-remove-group-member':
19441923
return_results(remove_group_member_command(client, args))
19451924
elif command == 'mattermost-set-channel-role':
19461925
return_results(set_channel_role_command(client, args))

Packs/Mattermost/Integrations/MattermostV2/MattermostV2.yml

Lines changed: 35 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,7 @@ script:
411411
name: entry
412412
required: false
413413
- description: The username or email of the user to send the file to.
414+
isArray: true
414415
name: to
415416
required: false
416417
- description: Adds the War Room link to the message.
@@ -546,84 +547,87 @@ script:
546547
description: How many results to retrieve. Will override the page and page_size
547548
arguments if given.
548549
- name: group
549-
description: Search for a specific user group by this pattern.
550-
name: mattermost-list-usergroups
550+
description: Search for a specific group by this pattern.
551+
name: mattermost-list-groups
551552
outputs:
552553
- contextPath: Mattermost.Groups.id
553-
description: The ID of the user group.
554+
description: The ID of the group.
554555
type: String
555556
- contextPath: Mattermost.Groups.create_at
556-
description: When was the user group created.
557+
description: When was the group created.
557558
type: Unknown
558559
- contextPath: Mattermost.Groups.update_at
559-
description: When was the user group updated.
560+
description: When was the group updated.
560561
type: Unknown
561562
- contextPath: Mattermost.Groups.delete_at
562-
description: When was the user group deleted.
563+
description: When was the group deleted.
563564
type: Unknown
564565
- contextPath: Mattermost.Groups.display_name
565-
description: The display name of the user group.
566+
description: The display name of the group.
566567
type: String
567568
- contextPath: Mattermost.Groups.name
568-
description: The name of the user group.
569+
description: The name of the group.
569570
type: String
570571
- contextPath: Mattermost.Groups.description
571-
description: The description of the user group.
572+
description: The description of the group.
572573
type: String
573574
- contextPath: Mattermost.Groups.source
574-
description: The source of the user group.
575+
description: The source of the group.
575576
type: String
576577
- contextPath: Mattermost.Groups.remote_id
577-
description: The remote id of the user group.
578+
description: The remote id of the group.
578579
type: String
579580
- contextPath: Mattermost.Groups.has_syncables
580-
description: If the user group has any syncables.
581+
description: If the group has any syncables.
581582
type: boolean
582-
description: Lists user groups.
583-
- name: mattermost-list-usergroup-members
583+
description: Lists groups.
584+
- name: mattermost-list-group-members
584585
arguments:
585-
- name: group
586+
- name: group_id
586587
required: true
587-
description: The group name of the user group to add the user to.
588+
description: The group id of the group to list members for. Use the command
589+
'mattermost-list-groups' to fetch the group ID.
588590
outputs:
589-
- contextPath: Mattermost.Groups.name
590-
description: The name of the user group.
591-
type: String
592591
- contextPath: Mattermost.Groups.id
593-
description: The ID of the user group.
592+
description: The ID of the group.
594593
type: String
595594
- contextPath: Mattermost.Groups.total_member_count
596-
description: The total count of members in the user group.
595+
description: The total count of members in the group.
597596
type: Unknown
598597
- contextPath: Mattermost.Groups.members
599-
description: Detailed list of the user group members.
598+
description: Detailed list of the group members.
600599
type: Unknown
601-
description: Lists user group members.
602-
- name: mattermost-add-usergroup-member
600+
description: Lists group members.
601+
- name: mattermost-add-group-member
603602
arguments:
604-
- name: group
603+
- name: group_id
605604
required: true
606-
description: The group name of the user group to add the user to.
605+
description: The group id of the group to add the user to. Use the command
606+
'mattermost-list-groups' to fetch the group ID.
607607
- name: user_ids
608608
required: true
609+
isArray: true
609610
description: The ID(s) of the user(s) to add. Use the command 'mattermost-list-users'
610611
to fetch the user ID(s).
611-
description: Add user group member(s).
612-
- name: mattermost-remove-usergroup-member
612+
description: Add group member(s).
613+
- name: mattermost-remove-group-member
613614
arguments:
614615
- name: group
615616
required: true
616-
description: The group name of the user group to remove the user from.
617+
description: The group id of the group to remove the user from. Use the command
618+
'mattermost-list-groups' to fetch the group ID.
617619
- name: user_ids
618620
required: true
621+
isArray: true
619622
description: The ID(s) of the user(s) to remove. Use the command 'mattermost-list-users'
620623
to fetch the user ID(s).
621-
description: Remove user group member(s).
624+
description: Remove group member(s).
622625
- name: mattermost-set-channel-role
623626
arguments:
624627
- name: channel_id
625628
required: true
626-
description: The channel id to set the role in.
629+
description: The channel ID to set the role in. Use the command 'mattermost-list-channels'
630+
to fetch the channel ID.
627631
- name: user_id
628632
required: true
629633
description: The ID of the user to set role for. Use the command 'mattermost-list-users'

0 commit comments

Comments
 (0)