From 1a4d1553b326ee87d090e4a403abb04ef4e3bbce Mon Sep 17 00:00:00 2001 From: Meni Yakove <441263+myakove@users.noreply.github.com> Date: Thu, 2 Dec 2021 14:17:47 +0200 Subject: [PATCH] Support Jira user token (#119) * Support Jira user token * Support use user token to connect to Jira --- README.rst | 1 + pytest_jira.py | 27 +++++++++++++++++++++++---- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/README.rst b/README.rst index 03c7b33..45ddadb 100644 --- a/README.rst +++ b/README.rst @@ -185,6 +185,7 @@ Usage url = https://jira.atlassian.com username = USERNAME (or blank for no authentication password = PASSWORD (or blank for no authentication) + token = TOKEN (either use token or username and password) # ssl_verification = True/False # version = foo-1.0 # components = com1,second component,com3 diff --git a/pytest_jira.py b/pytest_jira.py index f24a187..df1a241 100644 --- a/pytest_jira.py +++ b/pytest_jira.py @@ -177,28 +177,40 @@ def __init__( self, url, username=None, password=None, - verify=True + verify=True, + token=None, ): self.url = url self.username = username self.password = password self.verify = verify + self.token = token self.is_connected = False + if self.token: + token_bearer = f"Bearer {self.token}" + self.headers = {'Authorization': token_bearer} + # Setup basic_auth - if self.username and self.password: + elif self.username and self.password: self.basic_auth = (self.username, self.password) + else: self.basic_auth = None def _jira_request(self, url, method='get', **kwargs): if 'verify' not in kwargs: kwargs['verify'] = self.verify - if self.basic_auth: + + if self.token: + rsp = requests.request(method, url, headers=self.headers, **kwargs) + + elif self.basic_auth: rsp = requests.request( method, url, auth=self.basic_auth, **kwargs ) + else: rsp = requests.request(method, url, **kwargs) rsp.raise_for_status() @@ -376,6 +388,12 @@ def pytest_addoption(parser): default=_get_value(config, 'DEFAULT', 'password'), metavar='password', help='JIRA password.') + group.addoption('--jira-token', + action='store', + dest='jira_token', + default=_get_value(config, 'DEFAULT', 'token'), + metavar='token', + help='JIRA token.') group.addoption('--jira-no-ssl-verify', action='store_false', dest='jira_verify', @@ -497,7 +515,8 @@ def pytest_configure(config): config.getvalue('jira_url'), config.getvalue('jira_username'), os.getenv(PASSWORD_ENV_VAR) or config.getvalue('jira_password'), - config.getvalue('jira_verify') + config.getvalue('jira_verify'), + config.getvalue('jira_token'), ) jira_marker = JiraMarkerReporter( config.getvalue('jira_marker_strategy'),