diff --git a/jira_howto.ipynb b/jira_howto.ipynb index 676d137..1bf23e7 100644 --- a/jira_howto.ipynb +++ b/jira_howto.ipynb @@ -199,40 +199,13 @@ "Otherwise, they do not have the visibility key." ] }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "metadata": {} - }, - "outputs": [], - "source": [ - "from jiraissues import Issue\n", - "import os\n", - "\n", - "\n", - "i = Issue(jira, \"OCTOET-85\")\n", - "print(i.project_key)\n", - "\n", - "print(i.project_key in os.environ.get(\"ALLOWED_PROJECTS\", \"\").split(\",\"))" - ] - }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ - "from requests import HTTPError\n", - "\n", - "\n", - "issue = Issue(jira, \"OCTOET-377\")\n", - "pprint(issue.changelog)\n", - "try:\n", - " pprint(jira.get_issue_changelog(\"OCTOET-377\"))\n", - "except HTTPError as e:\n", - " pprint(e.response.json())\n", - " pprint(e.request.params)" + "pprint(jira.get_issue(\"OCTO-2\")[\"fields\"][\"customfield_12315950\"])" ] }, { @@ -241,45 +214,19 @@ "metadata": {}, "outputs": [], "source": [ - "from datetime import UTC, datetime\n", + "import jiraissues\n", "\n", - "print(issue.updated.tzname())\n", - "print(datetime.now(UTC).tzname())\n", - "print(datetime.now(UTC))\n", - "print(datetime.now())\n", - "print(datetime.now().isoformat())" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "import time\n", - "from zoneinfo import ZoneInfo\n", + "# re-import jiraissues\n", + "import importlib\n", "\n", + "jiraissues = importlib.reload(jiraissues)\n", "\n", - "us_east = ZoneInfo(\"America/New_York\")\n", - "issue = Issue(jira, \"OCTOET-377\")\n", - "print(issue.updated.astimezone(us_east))\n", + "i = jiraissues.Issue(jira, \"OCTO-2\")\n", + "for user in i.contributors:\n", + " print(user)\n", "\n", - "time.time()" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "issue = Issue(jira, \"OCTOET-85\")\n", - "us_east = ZoneInfo(\"America/New_York\")\n", "\n", - "print(issue.updated.astimezone(us_east))\n", - "comments = issue.comments\n", - "for comment in comments:\n", - " print(comment.created.astimezone(us_east))" + "print(f\"\\n\\nI am: {jiraissues.get_self(jira)}\")" ] } ], diff --git a/jiraissues.py b/jiraissues.py index 5a717d5..a4328ff 100644 --- a/jiraissues.py +++ b/jiraissues.py @@ -16,6 +16,7 @@ # Custom field IDs CF_BLOCKED = "customfield_12316543" # option CF_BLOCKED_REASON = "customfield_12316544" # string +CF_CONTRIBUTORS = "customfield_12315950" # list CF_EPIC_LINK = "customfield_12311140" # any CF_FEATURE_LINK = "customfield_12318341" # issuelinks CF_PARENT_LINK = "customfield_12313140" # any @@ -112,6 +113,20 @@ def is_child(self) -> bool: return self.how in [_HOW_SUBTASK, _HOW_INEPIC, _HOW_INPARENT] +class User: # pylint: disable=too-few-public-methods + """A Jira user.""" + + def __init__(self, data: dict[str, Any]) -> None: + self.display_name = str(data.get("displayName", "")) + self.key = str(data.get("key", "")) + self.name = str(data.get("name", "")) + self.timezone = str(data.get("timeZone", "")) + self.tzinfo = ZoneInfo(self.timezone) + + def __str__(self) -> str: + return f"{self.display_name} ({self.key})" + + class Issue: # pylint: disable=too-many-instance-attributes """ Represents a Jira issue as a proper object. @@ -135,6 +150,7 @@ def __init__(self, client: Jira, issue_key: str) -> None: CF_STATUS_SUMMARY, CF_BLOCKED, CF_BLOCKED_REASON, + CF_CONTRIBUTORS, "comment", ] data = check_response( @@ -168,6 +184,9 @@ def __init__(self, client: Jira, issue_key: str) -> None: blocked_dict = data["fields"].get(CF_BLOCKED) or {} self.blocked = str(blocked_dict.get("value", "False")).lower() in ["true"] self.blocked_reason = str(data["fields"].get(CF_BLOCKED_REASON) or "") + self.contributors = { + User(user) for user in data["fields"].get(CF_CONTRIBUTORS, []) + } _logger.info("Retrieved issue: %s", self) def __str__(self) -> str: @@ -469,31 +488,17 @@ def check_response(response: Any) -> dict: raise ValueError(f"Unexpected response: {response}") -class Myself: # pylint: disable=too-few-public-methods - """ - Represents the current user in Jira. - """ - - def __init__(self, client: Jira) -> None: - self.client = client - self._data = check_response(with_retry(client.myself)) - # Break out the fields we care about - self.display_name: str = self._data["displayName"] - self.key: str = self._data["key"] - self.timezone: str = self._data["timeZone"] - self.tzinfo = ZoneInfo(self.timezone) - - -_self: Optional[Myself] = None +_self: Optional[User] = None -def get_self(client: Jira) -> Myself: +def get_self(client: Jira) -> User: """ Caching function for the Myself object. """ global _self # pylint: disable=global-statement if _self is None: - _self = Myself(client) + data = check_response(with_retry(client.myself)) + _self = User(data) return _self