-
-
Notifications
You must be signed in to change notification settings - Fork 889
Remove internal user lookup #1874
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
base: main
Are you sure you want to change the base?
Changes from 7 commits
48cd70b
c12a221
b4e4aee
59d2376
77d3480
d6e15ce
b0b552e
974296e
790732d
6397dce
e732ab5
46265fc
f1c46cf
35abeb9
045ca15
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -2209,7 +2209,7 @@ def createmeta( | |||
params["expand"] = expand | ||||
return self._get_json("issue/createmeta", params) | ||||
|
||||
def _get_user_identifier(self, user: User) -> str: | ||||
def get_user_identifier(self, user: User) -> str: | ||||
"""Get the unique identifier depending on the deployment type. | ||||
|
||||
- Cloud: 'accountId' | ||||
|
@@ -2223,59 +2223,24 @@ def _get_user_identifier(self, user: User) -> str: | |||
""" | ||||
return user.accountId if self._is_cloud else user.name | ||||
|
||||
def _get_user_id(self, user: str | None) -> str | None: | ||||
"""Internal method for translating a user search (str) to an id. | ||||
|
||||
Return None and -1 unchanged. | ||||
|
||||
This function uses :py:meth:`JIRA.search_users` to find the user and then using :py:meth:`JIRA._get_user_identifier` extracts | ||||
the relevant identifier property depending on whether the instance is a Cloud or self-hosted Instance. | ||||
|
||||
Args: | ||||
user (Optional[str]): The search term used for finding a user. None, '-1' and -1 are equivalent to 'Unassigned'. | ||||
|
||||
Raises: | ||||
JIRAError: If any error occurs. | ||||
|
||||
Returns: | ||||
Optional[str]: The Jira user's identifier. Or "-1" and None unchanged. | ||||
""" | ||||
if user in (None, -1, "-1"): | ||||
return user | ||||
try: | ||||
user_obj: User | ||||
if self._is_cloud: | ||||
users = self.search_users(query=user, maxResults=20) | ||||
else: | ||||
users = self.search_users(user=user, maxResults=20) | ||||
|
||||
if len(users) < 1: | ||||
raise JIRAError(f"No matching user found for: '{user}'") | ||||
|
||||
matches = [] | ||||
if len(users) > 1: | ||||
matches = [u for u in users if self._get_user_identifier(u) == user] | ||||
user_obj = matches[0] if matches else users[0] | ||||
|
||||
except Exception as e: | ||||
raise JIRAError(str(e)) | ||||
return self._get_user_identifier(user_obj) | ||||
|
||||
# non-resource | ||||
@translate_resource_args | ||||
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.
Suggested change
It will be critical for this approach to work to remove this decorator which would have automagically converted the object to a string 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. I don't think the decorator touches on the |
||||
def assign_issue(self, issue: int | str, assignee: str | None) -> bool: | ||||
def assign_issue(self, issue: int | str, assignee: str | None | User) -> bool: | ||||
"""Assign an issue to a user. | ||||
|
||||
Args: | ||||
issue (Union[int, str]): the issue ID or key to assign | ||||
assignee (str): the user to assign the issue to. None will set it to unassigned. -1 will set it to Automatic. | ||||
assignee (Union[str, User]): username (for hosted) or account ID (for cloud) of the user to add to the | ||||
watchers list. Alternatively, you can provide the User object itself. | ||||
adehad marked this conversation as resolved.
Show resolved
Hide resolved
|
||||
None will set it to unassigned. -1 will set it to Automatic. | ||||
|
||||
Returns: | ||||
bool | ||||
""" | ||||
url = self._get_latest_url(f"issue/{issue}/assignee") | ||||
user_id = self._get_user_id(assignee) | ||||
payload = {"accountId": user_id} if self._is_cloud else {"name": user_id} | ||||
if isinstance(assignee, User): | ||||
assignee = self.get_user_identifier(assignee) | ||||
payload = {"accountId": assignee} if self._is_cloud else {"name": assignee} | ||||
adehad marked this conversation as resolved.
Show resolved
Hide resolved
|
||||
self._session.put(url, data=json.dumps(payload)) | ||||
return True | ||||
|
||||
|
@@ -2719,36 +2684,38 @@ def watchers(self, issue: str | int) -> Watchers: | |||
return self._find_for_resource(Watchers, issue) | ||||
|
||||
@translate_resource_args | ||||
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.
Suggested change
|
||||
def add_watcher(self, issue: str | int, watcher: str) -> Response: | ||||
def add_watcher(self, issue: str | int, watcher: str | User) -> Response: | ||||
"""Add a user to an issue's watchers list. | ||||
|
||||
Args: | ||||
issue (Union[str, int]): ID or key of the issue affected | ||||
watcher (str): name of the user to add to the watchers list | ||||
watcher (str | User): username (for hosted) or account ID (for cloud) of the user to add to the watchers | ||||
list | ||||
|
||||
Returns: | ||||
Response | ||||
""" | ||||
url = self._get_url("issue/" + str(issue) + "/watchers") | ||||
# Use user_id when adding watcher | ||||
watcher_id = self._get_user_id(watcher) | ||||
return self._session.post(url, data=json.dumps(watcher_id)) | ||||
if isinstance(watcher, User): | ||||
watcher = self.get_user_identifier(watcher) | ||||
return self._session.post(url, data=json.dumps(watcher)) | ||||
|
||||
@translate_resource_args | ||||
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.
Suggested change
|
||||
def remove_watcher(self, issue: str | int, watcher: str) -> Response: | ||||
def remove_watcher(self, issue: str | int, watcher: str | User) -> Response: | ||||
"""Remove a user from an issue's watch list. | ||||
|
||||
Args: | ||||
issue (Union[str, int]): ID or key of the issue affected | ||||
watcher (str): name of the user to remove from the watchers list | ||||
watcher (str): username (for hosted) or account ID (for cloud) of the user to add to the watchers list | ||||
|
||||
Returns: | ||||
Response | ||||
""" | ||||
url = self._get_url("issue/" + str(issue) + "/watchers") | ||||
# https://docs.atlassian.com/software/jira/docs/api/REST/8.13.6/#api/2/issue-removeWatcher | ||||
user_id = self._get_user_id(watcher) | ||||
payload = {"accountId": user_id} if self._is_cloud else {"username": user_id} | ||||
if isinstance(watcher, User): | ||||
watcher = self.get_user_identifier(watcher) | ||||
payload = {"accountId": watcher} if self._is_cloud else {"username": watcher} | ||||
result = self._session.delete(url, params=payload) | ||||
return result | ||||
|
||||
|
Uh oh!
There was an error while loading. Please reload this page.