-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
replace deprecated confluence group api endpoint #3197
Merged
Merged
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
6d26d0b
replace deprecated confluence group api endpoint
hagen-danswer 95ab63b
reworked it
hagen-danswer 4fc196f
properly escaped the user query
hagen-danswer 31f4a68
less passing around is_cloud
hagen-danswer e2aaa60
done
hagen-danswer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -269,20 +269,3 @@ def datetime_from_string(datetime_string: str) -> datetime: | |
datetime_object = datetime_object.astimezone(timezone.utc) | ||
|
||
return datetime_object | ||
|
||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. moved this to the onyx_confluence.py file |
||
def build_confluence_client( | ||
credentials_json: dict[str, Any], is_cloud: bool, wiki_base: str | ||
) -> OnyxConfluence: | ||
return OnyxConfluence( | ||
api_version="cloud" if is_cloud else "latest", | ||
# Remove trailing slash from wiki_base if present | ||
url=wiki_base.rstrip("/"), | ||
# passing in username causes issues for Confluence data center | ||
username=credentials_json["confluence_username"] if is_cloud else None, | ||
password=credentials_json["confluence_access_token"] if is_cloud else None, | ||
token=credentials_json["confluence_access_token"] if not is_cloud else None, | ||
backoff_and_retry=True, | ||
max_backoff_retries=10, | ||
max_backoff_seconds=60, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
from atlassian import Confluence # type: ignore | ||
from typing import Any | ||
|
||
from danswer.connectors.confluence.onyx_confluence import build_confluence_client | ||
from danswer.connectors.confluence.onyx_confluence import OnyxConfluence | ||
from danswer.connectors.confluence.utils import build_confluence_client | ||
from danswer.connectors.confluence.utils import get_user_email_from_username__server | ||
from danswer.db.models import ConnectorCredentialPair | ||
from danswer.utils.logger import setup_logger | ||
|
@@ -11,22 +11,46 @@ | |
logger = setup_logger() | ||
|
||
|
||
def _get_group_members_email_paginated( | ||
def _get_group_ids_for_user( | ||
confluence_client: OnyxConfluence, | ||
group_name: str, | ||
is_cloud: bool, | ||
user: dict[str, Any], | ||
) -> set[str]: | ||
group_member_emails: set[str] = set() | ||
for member in confluence_client.paginated_group_members_retrieval(group_name): | ||
email = member.get("email") | ||
user_field = "accountId" if is_cloud else "key" | ||
user_value = user["accountId"] if is_cloud else user["userKey"] | ||
# Server uses userKey (but calls it key during the API call), Cloud uses accountId | ||
user_query = f"{user_field}={user_value}" | ||
|
||
group_ids_for_user: set[str] = set() | ||
for group in confluence_client.paginated_groups_by_user_retrieval(user_query): | ||
group_ids_for_user.add(group["name"]) | ||
|
||
return group_ids_for_user | ||
|
||
|
||
def _build_group_member_email_map( | ||
confluence_client: OnyxConfluence, | ||
is_cloud: bool, | ||
) -> dict[str, set[str]]: | ||
group_member_emails: dict[str, set[str]] = {} | ||
for user_result in confluence_client.paginated_cql_user_retrieval(is_cloud): | ||
user = user_result["user"] | ||
email = user.get("email") | ||
if not email: | ||
user_name = member.get("username") | ||
# This field is only present in Confluence Server | ||
user_name = user.get("username") | ||
# If it is present, try to get the email using a Server-specific method | ||
if user_name: | ||
email = get_user_email_from_username__server( | ||
confluence_client=confluence_client, | ||
user_name=user_name, | ||
) | ||
if email: | ||
group_member_emails.add(email) | ||
if not email: | ||
# If we still don't have an email, skip this user | ||
continue | ||
|
||
for group_id in _get_group_ids_for_user(confluence_client, is_cloud, user): | ||
group_member_emails.setdefault(group_id, set()).add(email) | ||
|
||
return group_member_emails | ||
|
||
|
@@ -38,41 +62,21 @@ def confluence_group_sync( | |
is_cloud = cc_pair.connector.connector_specific_config.get("is_cloud", False) | ||
wiki_base = cc_pair.connector.connector_specific_config["wiki_base"] | ||
|
||
# test connection with direct client, no retries | ||
confluence_client = Confluence( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. moved this to the onyx_confluence.py file |
||
api_version="cloud" if is_cloud else "latest", | ||
url=wiki_base.rstrip("/"), | ||
username=credentials["confluence_username"] if is_cloud else None, | ||
password=credentials["confluence_access_token"] if is_cloud else None, | ||
token=credentials["confluence_access_token"] if not is_cloud else None, | ||
) | ||
spaces = confluence_client.get_all_spaces(limit=1) | ||
if not spaces: | ||
raise RuntimeError(f"No spaces found at {wiki_base}!") | ||
|
||
confluence_client = build_confluence_client( | ||
credentials_json=credentials, | ||
is_cloud=is_cloud, | ||
wiki_base=wiki_base, | ||
) | ||
|
||
# Get all group names | ||
group_names: list[str] = [] | ||
for group in confluence_client.paginated_groups_retrieval(): | ||
if group_name := group.get("name"): | ||
group_names.append(group_name) | ||
|
||
# For each group name, get all members and create a danswer group | ||
group_member_email_map = _build_group_member_email_map( | ||
confluence_client=confluence_client, | ||
is_cloud=is_cloud, | ||
) | ||
danswer_groups: list[ExternalUserGroup] = [] | ||
for group_name in group_names: | ||
group_member_emails = _get_group_members_email_paginated( | ||
confluence_client, group_name | ||
) | ||
if not group_member_emails: | ||
continue | ||
for group_id, group_member_emails in group_member_email_map.items(): | ||
danswer_groups.append( | ||
ExternalUserGroup( | ||
id=group_name, | ||
id=group_id, | ||
user_emails=list(group_member_emails), | ||
) | ||
) | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
moved this to the onyx_confluence.py file