Skip to content

Commit

Permalink
Define IssueTracker.rpc_credentials property
Browse files Browse the repository at this point in the history
to make it easier to override credentials for Issue Tracker integrations
  • Loading branch information
atodorov committed Aug 25, 2023
1 parent 5ef9f1a commit 9590e20
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 35 deletions.
8 changes: 6 additions & 2 deletions tcms/issuetracker/azure_boards.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,14 @@ class AzureBoards(IssueTrackerType):
"""

def _rpc_connection(self):
return AzureBoardsAPI(self.bug_system.base_url, self.bug_system.api_password)
(_, api_password) = self.rpc_credentials

return AzureBoardsAPI(self.bug_system.base_url, api_password)

def is_adding_testcase_to_issue_disabled(self):
return not (self.bug_system.base_url and self.bug_system.api_password)
(_, api_password) = self.rpc_credentials

return not (self.bug_system.base_url and api_password)

def _report_issue(self, execution, user):
"""
Expand Down
21 changes: 16 additions & 5 deletions tcms/issuetracker/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,11 +188,9 @@ def is_adding_testcase_to_issue_disabled(self): # pylint: disable=invalid-name
:return: True if bug system api url, username and password are provided
:rtype: bool
"""
return not (
self.bug_system.api_url
and self.bug_system.api_username
and self.bug_system.api_password
)
(api_username, api_password) = self.rpc_credentials

return not (self.bug_system.api_url and api_username and api_password)

def _rpc_connection(self):
"""
Expand All @@ -218,3 +216,16 @@ def rpc(self):
self.rpc_cache[self.bug_system.base_url] = self._rpc_connection()

return self.rpc_cache[self.bug_system.base_url]

@property
def rpc_credentials(self):
"""
Returns an tuple of (api_username, api_token_or_password) meant for connecting
to a 3rd party issue tracker system.
By default this is
(self.bug_system.api_username, self.bug_system.api_password)
however it can be overriden in order to provide more flexible integrations.
"""
return (self.bug_system.api_username, self.bug_system.api_password)
14 changes: 7 additions & 7 deletions tcms/issuetracker/bitbucket.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,18 +93,18 @@ class BitBucket(IssueTrackerType):
"""

def _rpc_connection(self):
(api_username, api_password) = self.rpc_credentials

return BitBucketAPI(
self.bug_system.base_url,
api_username=self.bug_system.api_username,
api_password=self.bug_system.api_password,
api_username=api_username,
api_password=api_password,
)

def is_adding_testcase_to_issue_disabled(self):
return not (
self.bug_system.base_url
and self.bug_system.api_username
and self.bug_system.api_password
)
(api_username, api_password) = self.rpc_credentials

return not (self.bug_system.base_url and api_username and api_password)

def _report_issue(self, execution, user):
"""
Expand Down
6 changes: 4 additions & 2 deletions tcms/issuetracker/bugzilla_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,12 @@ def _rpc_connection(self):
if not os.path.exists(self._bugzilla_cache_dir):
os.makedirs(self._bugzilla_cache_dir, 0o700)

(api_username, api_password) = self.rpc_credentials

return bugzilla.Bugzilla(
self.bug_system.api_url,
user=self.bug_system.api_username,
password=self.bug_system.api_password,
user=api_username,
password=api_password,
tokenfile=self._bugzilla_cache_dir + "token",
)

Expand Down
44 changes: 25 additions & 19 deletions tcms/issuetracker/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,18 @@ def _rpc_connection(self):
else:
options = None

(api_username, api_password) = self.rpc_credentials

return jira.JIRA(
self.bug_system.base_url,
basic_auth=(self.bug_system.api_username, self.bug_system.api_password),
basic_auth=(api_username, api_password),
options=options,
)

def is_adding_testcase_to_issue_disabled(self):
return not (
self.bug_system.base_url
and self.bug_system.api_username
and self.bug_system.api_password
)
(api_username, api_password) = self.rpc_credentials

return not (self.bug_system.base_url and api_username and api_password)

@classmethod
def bug_id_from_url(cls, url):
Expand Down Expand Up @@ -181,11 +181,15 @@ class GitHub(IssueTrackerType):
"""

def _rpc_connection(self):
(_, api_password) = self.rpc_credentials

# NOTE: we use an access token so only the password field is required
return github.Github(self.bug_system.api_password)
return github.Github(api_password)

def is_adding_testcase_to_issue_disabled(self):
return not (self.bug_system.base_url and self.bug_system.api_password)
(_, api_password) = self.rpc_credentials

return not (self.bug_system.base_url and api_password)

def _report_issue(self, execution, user):
"""
Expand Down Expand Up @@ -262,13 +266,15 @@ class Gitlab(IssueTrackerType):
"""

def _rpc_connection(self):
(_, api_password) = self.rpc_credentials

# we use an access token so only the password field is required
return gitlab.Gitlab(
self.bug_system.api_url, private_token=self.bug_system.api_password
)
return gitlab.Gitlab(self.bug_system.api_url, private_token=api_password)

def is_adding_testcase_to_issue_disabled(self):
return not (self.bug_system.api_url and self.bug_system.api_password)
(_, api_password) = self.rpc_credentials

return not (self.bug_system.api_url and api_password)

def _report_issue(self, execution, user):
project = self.rpc.projects.get(self.repo_id)
Expand Down Expand Up @@ -319,17 +325,17 @@ class Redmine(IssueTrackerType):
"""

def is_adding_testcase_to_issue_disabled(self):
return not (
self.bug_system.base_url
and self.bug_system.api_username
and self.bug_system.api_password
)
(api_username, api_password) = self.rpc_credentials

return not (self.bug_system.base_url and api_username and api_password)

def _rpc_connection(self):
(api_username, api_password) = self.rpc_credentials

return redminelib.Redmine(
self.bug_system.base_url,
username=self.bug_system.api_username,
password=self.bug_system.api_password,
username=api_username,
password=api_password,
)

def details(self, url):
Expand Down

0 comments on commit 9590e20

Please sign in to comment.