Skip to content

Commit

Permalink
Enable using system keyring for accesstoken/PAT with azuredevops (#1000)
Browse files Browse the repository at this point in the history
* Added support for @oracle with AzureDeVops, and fetching password using system keyring

* Fixed typo

* Fixed flake8 formatting complaints
  • Loading branch information
bratne authored Aug 30, 2023
1 parent 740afbd commit 7cd4612
Showing 1 changed file with 12 additions and 16 deletions.
28 changes: 12 additions & 16 deletions bugwarrior/services/azuredevops.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,6 @@
log = logging.getLogger(__name__)


class PersonalAccessToken(str):

@classmethod
def __get_validators__(cls):
yield cls.validate

@classmethod
def validate(cls, pat):
if pat[0] != ":":
pat = f":{pat}"
return base64.b64encode(pat.encode("ascii")).decode("ascii")


class EscapedStr(str):

@classmethod
Expand All @@ -39,7 +26,7 @@ def validate(cls, value):

class AzureDevopsConfig(config.ServiceConfig):
service: typing_extensions.Literal['azuredevops']
PAT: PersonalAccessToken
PAT: str
project: EscapedStr
organization: EscapedStr

Expand All @@ -66,7 +53,9 @@ def format_item(item):

class AzureDevopsClient(ServiceClient):
def __init__(self, pat, org, project, host):
self.pat = pat
if pat[0] != ":":
pat = f":{pat}"
self.pat = base64.b64encode(pat.encode("ascii")).decode("ascii")
self.organization = org
self.project = project
self.host = host
Expand All @@ -88,6 +77,9 @@ def get_work_item(self, workitemid):
def get_work_items_from_query(self, query):
data = str({"query": query})
resp = self.session.post(f"{self.base_url}/wiql", data=data, params=self.params)
if resp.status_code == 401:
log.critical("HTTP 401 - Error authenticating! Please check 'PAT' in the configuration")
sys.exit(1)
if resp.status_code == 400 and resp.json(
)['typeKey'] == "WorkItemTrackingQueryResultSizeLimitExceededException":
log.critical("Too many azure devops results in query, please "
Expand Down Expand Up @@ -194,7 +186,7 @@ class AzureDevopsService(IssueService):
def __init__(self, *args, **kw):
super().__init__(*args, **kw)
self.client = AzureDevopsClient(
pat=self.config.PAT,
pat=self.get_password('PAT'),
project=self.config.project,
org=self.config.organization,
host=self.config.host
Expand Down Expand Up @@ -260,3 +252,7 @@ def issues(self):
def get_owner(self, issue):
# Issue filtering is implemented as part of issue aggregation.
pass

@staticmethod
def get_keyring_service(config):
return f"azuredevops://{config.organization}@{config.host}"

0 comments on commit 7cd4612

Please sign in to comment.