recurse teams

This commit is contained in:
Nils Büchner 2024-04-05 05:49:37 +02:00
parent 6d8d6ff1f2
commit 8b60597340
2 changed files with 34 additions and 6 deletions

View file

@ -5,6 +5,29 @@ from bugtracker.launchpad_singleton import get_launchpad
# Connect to Redis
cache = redis.Redis(host='localhost', port=6379, db=0)
def fetch_individual_or_team_members(person_or_team, launchpad):
members = []
if person_or_team.is_team:
# Recursively fetch members for a team
group = launchpad.people[person_or_team.name]
for person in group.members:
if person.is_team:
ext = fetch_individual_or_team_members(person, launchpad)
if ext and not person.is_team:
members.append({
'name': person_or_team.name,
})
else:
members.append({
'name': person.name,
})
else:
# Append individual member details
members.append({
'name': person_or_team.name,
})
return members
def fetch_group_members(group_name):
try:
#Try to fetch from cache first
@ -12,11 +35,17 @@ def fetch_group_members(group_name):
if cached_result:
return json.loads(cached_result)
# If not cached, fetch from Launchpad
launchpad = get_launchpad()
group = launchpad.people[group_name]
group_members = [person.name for person in group.members]
mxids = ['@' + person.name + ':ubuntu.com' for person in group.members]
group_members = []
for person in group.members:
ext = fetch_individual_or_team_members(person, launchpad)
if ext:
group_members.extend(ext)
# MXIDs should be generated for individuals only
mxids = ['@' + member['name'] + ':ubuntu.com' for member in group_members]
result = {'group_members': group_members, 'group_name': group_name, 'mxids': mxids}
# Cache the result with expiration time of 30 minutes (1800 seconds)
@ -28,5 +57,4 @@ def fetch_group_members(group_name):
return False
except Exception as e:
print(f"An error occurred: {e}")
print(f"Error processing request for launchpad group {group_name}: {str(e)}")
return False

View file

@ -14,7 +14,7 @@ import json
import requests
@api_view(['GET'])
@cache_page(60 * 15) # Cache for 15 minutes
@cache_page(60 * 30) # Cache for 30 minutes
def group_members(self, group_name):
try:
result = fetch_group_members(group_name)