Skip to content
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 86 additions & 0 deletions jira/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
Filter,
Group,
Issue,
IssueField,
IssueLink,
IssueLinkType,
IssueProperty,
Expand Down Expand Up @@ -1722,6 +1723,82 @@ def create_customer_request(
else:
return Issue(self._options, self._session, raw=raw_issue_json)

def _get_project_createmeta_issuetype(
self,
project: Union[str, int],
issueTypeName: str = None,
issueTypeId: int = None,
) -> Dict[str, Any]:
"""Returns createmeta fields for project+issuetype. if issuetypeId specifyies it skips list of issuetypes"""

issuetype_id = issueTypeId
if issueTypeId is None:
try:
f: ResultList[IssueType] = self._fetch_pages(
IssueType,
"values",
f"issue/createmeta/{project}/issuetypes",
0,
0,
None,
)
_meta = [v.id for v in f if v.name == issueTypeName]
if len(_meta) == 1:
issuetype_id = _meta[0]
except JIRAError:
raise JIRAError(
status_code=500,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
status_code=500,

We shouldn't try and set our own status code. I would just let this exception bubble up

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could instead use the existing methods on the client to check if a project exists, for example like so:

def _project_exists(self, project_key: str) -> bool:

Might be making this its own method, and use it in the test harness as well.

text=f"JIRA: There is no such project {project}",
)
if issuetype_id is None:
raise JIRAError(
status_code=500,
text=f"JIRA: There is no such issueType {issueTypeName} in {project} project",
)
meta_source: ResultList[IssueField] = self._fetch_pages(
IssueField,
"values",
f"issue/createmeta/{project}/issuetypes/{issuetype_id}",
0,
0,
None,
)
return {v.fieldId: v for v in meta_source}

def is_84_and_later(self):
return self._version >= (8, 4, 0)

def createmeta_v2(
self,
projectKey: Optional[str] = None,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@studioj did we decide what we wanted to do with the naming convention of new functions?

projectId: Optional[int] = None,
issueTypeName: Optional[str] = None,
issueTypeId: Optional[int] = None,
) -> Dict[str, IssueField]:
"""Get the metadata required to create issues for particular project and issue type

Args:
projectKey (Optional[str]): key of project to find issue type.
It is single value string. Has precedence over `projectId`
projectId (Optional[int]): id of project to find issue type.
It is single value int
issueTypeName (Optional[str]): issueType to find fields metadata
It is single value string. Has precedence over `issueTypeId`
issueTypeId (Optional[int]): issueType to find fields metadata
It is single value int.
Returns:
Dict[str, IssueField]: dict fieldId:IssueField
"""

if self.is_84_and_later():
project: Union[str, int] = projectKey or projectId
meta = self._get_project_createmeta_issuetype(
project=project, issueTypeName=issueTypeName, issueTypeId=issueTypeId
)
return meta
else:
raise JIRAError("Not supported REST API in pre 8.4 versions")

def createmeta(
self,
projectKeys: Optional[Union[Tuple[str, str], str]] = None,
Expand Down Expand Up @@ -1750,6 +1827,15 @@ def createmeta(
Dict[str, Any]

"""

if self._version >= (9, 0, 0):
raise NotImplementedError(
"Support for createmeta search in server" ">= 9.0.0 has been removed."
)

if self.is_84_and_later():
DeprecationWarning("The `createmeta` is deprecated, use createmeta_v2")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We definitely cannot use a function name called _v2.

Let's instead deprecate at the argument level.

e.g.
User passes projectKeys as a List[str]
if version > 9 raise ValueError
else if version is >8.4 - raise deprecation warning
The deprecation warning can be more detailed, explaining exactly what is wrong with the arguments and must include a link to the Atlassian docs where they describe this deprecation, ie: https://confluence.atlassian.com/jiracore/createmeta-rest-endpoint-to-be-removed-975040986.html

I might structure it like this:

def createmeta(...): 
    is_deprecation_msg_raised = false
    def deprecate_or_raise(msg: str):
        if self._version >= (9, 0, 0):
            raise ValueError(msg)
        elif self._version >= (9, 0, 0):
            DeprecationWarning(f"From Jira version 9.0.0: {msg}") # might need to change the stacklevel for this to work as intended

    if not isinstance(projectKeys, str) or ("," in projectKeys):
        deprecate_or_raise("projectKeys must be a str of a single project key")

    # and so on for other
    ...

    if is_deprecation_msg_raised :
        DeprecationWarning(
            "Atlassian wil have deprecated the `/createmeta` endpoint in Jira version 9.0.0."
            + "Full details can be found on:"
            + "https://confluence.atlassian.com/jiracore/createmeta-rest-endpoint-to-be-removed-975040986.html"
        )

In the above proposal, people will be warned about ways they can change the calls in preparation. Feel free to use another method if more appropriate

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry, i have to skip this activity till war in Ukraine is finished

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dshvedchenko sorry to hear you're affected 😔 hope all is well for you! If you give write access to your fork, I'm more than happy to finish the job :)


params: Dict[str, Any] = {}
if projectKeys is not None:
params["projectKeys"] = projectKeys
Expand Down
16 changes: 16 additions & 0 deletions jira/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class AnyLike:
"PermissionScheme",
"Watchers",
"Worklog",
"IssueField",
"IssueLink",
"IssueLinkType",
"IssueProperty",
Expand Down Expand Up @@ -1081,6 +1082,21 @@ def __init__(
self.raw: Dict[str, Any] = cast(Dict[str, Any], self.raw)


class IssueField(Resource):
"""Type of an issue."""

def __init__(
self,
options: Dict[str, str],
session: ResilientSession,
raw: Dict[str, Any] = None,
):
Resource.__init__(self, "issuefield/{0}", options, session)
if raw:
self._parse_raw(raw)
self.raw: Dict[str, Any] = cast(Dict[str, Any], self.raw)


class Priority(Resource):
"""Priority that can be set on an issue."""

Expand Down