Skip to content

Commit

Permalink
Support Jira user token (#119)
Browse files Browse the repository at this point in the history
* Support Jira user token

* Support use user token to connect to Jira
  • Loading branch information
myakove authored Dec 2, 2021
1 parent da1580a commit 1a4d155
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
27 changes: 23 additions & 4 deletions pytest_jira.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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'),
Expand Down

0 comments on commit 1a4d155

Please sign in to comment.