Skip to content

Commit 808f31e

Browse files
authored
Merge pull request #3 from en-ver/dev
Validation and other improvements
2 parents e45383b + 67238a6 commit 808f31e

15 files changed

+533
-158
lines changed

Diff for: .vscode/settings.json

-3
This file was deleted.

Diff for: docs/index.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# jira2py
2+
3+
The documentation WIP

Diff for: examples/fields_examples.py

+16-7
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,25 @@
11
from jira2py import Jira
2-
import os
32
from dotenv import load_dotenv
3+
import os
44

5-
"""Load .env variables"""
65
load_dotenv()
76

8-
"""Create a Jira instance"""
97
jira = Jira(
10-
url=os.getenv("JIRA_URL", ""),
11-
user=os.getenv("JIRA_USER", ""),
12-
api_token=os.getenv("JIRA_API_TOKEN", ""),
8+
jira_url=os.environ.get("JIRA_URL"),
9+
jira_user=os.environ.get("JIRA_USER"),
10+
jira_api_token=os.environ.get("JIRA_API_TOKEN"),
1311
)
1412

13+
"""Create fields instance"""
14+
fields = jira.fields()
15+
1516
"""Get the list of Jira fields with its metadata"""
16-
fields = jira.fields().get()
17+
jira_fields = fields.get()
18+
19+
"""Get the field id by its name"""
20+
field_ids = fields.get_field_id(["Summary", "Reporter", "Parent"])
21+
22+
"""Get the field id by its id"""
23+
field_names = fields.get_field_name(["summary", "reporter", "parent"])
24+
25+
"""Follow https://en-ver.github.io/jira2py/ for more details"""

Diff for: examples/issue_examples.py

+28-21
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,41 @@
11
from jira2py import Jira
2-
import os
32
from dotenv import load_dotenv
3+
import os
44

5-
"""Load .env variables"""
65
load_dotenv()
76

8-
"""Create a Jira instance"""
97
jira = Jira(
10-
url=os.getenv("JIRA_URL", ""),
11-
user=os.getenv("JIRA_USER", ""),
12-
api_token=os.getenv("JIRA_API_TOKEN", ""),
8+
jira_url=os.environ.get("JIRA_URL"),
9+
jira_user=os.environ.get("JIRA_USER"),
10+
jira_api_token=os.environ.get("JIRA_API_TOKEN"),
1311
)
12+
issue_key = os.environ.get("ISSUE_KEY")
1413

1514
"""Create an Issue instance"""
16-
issue = jira.issue()
15+
issue = jira.issue(issue_key)
1716

1817
"""Get Issue details"""
19-
response = issue.get("PRJ-1111") # issue object including the issue details
20-
names = response["names"] # field ID-name mapping
21-
fields = response["fields"] # field ID-name mapping
22-
status = fields["status"]["name"] # status field value
23-
summary = fields["summary"] # summary field value
18+
issue_json = issue.get(fields=["status", "summary"]) # Raw JSON reposnse form jira API
19+
names = issue_json["names"] # Field ID-name mapping
20+
fields = issue_json["fields"] # Fields of the issues
21+
status = issue_json["fields"]["status"]["name"] # Status field value
22+
summary = issue_json.get("fields", {}).get("summary", None) # Summary field value
2423

2524
"""Update "summary" field"""
26-
response = issue.edit(key="PRJ-1111", fields={"summary": f"Test summary"})
27-
28-
"""Get changelog of the issue"""
29-
search_first_page = issue.get_changelogs(
30-
"PRJ-1111"
31-
) # Long changelogs returned paginated
32-
search_second_page = issue.get_changelogs(
33-
"PRJ-1111", start_at=50, max_results=50
34-
) # set the # of item to start from load and items to load to get the next page of results
25+
enable_edit = False # put here True to let teh script proceed with edit
26+
if enable_edit:
27+
edit_response_json = issue.edit(fields={"summary": f"Test summary"})
28+
29+
"""Get the the first page of the changelog"""
30+
changelog_page_json = issue.changelog_page()
31+
32+
"""Get the changelog first page filtered by field ids"""
33+
changelog_page_json_filtered = issue.changelog_page(fields=["issuetype", "labels"])
34+
35+
"""Get full changelog of the issue"""
36+
full_changelog_list = issue.changelog_all_pages()
37+
38+
"""Get full changelog of the issue filtered by field ids"""
39+
full_changelog_list_filtered = issue.changelog_all_pages(fields=["issuetype", "labels"])
40+
41+
"""Follow https://en-ver.github.io/jira2py/ for more details"""

Diff for: examples/jql_examples.py

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from jira2py import Jira
2+
from dotenv import load_dotenv
3+
import os
4+
5+
load_dotenv()
6+
7+
jira = Jira(
8+
jira_url=os.environ.get("JIRA_URL"),
9+
jira_user=os.environ.get("JIRA_USER"),
10+
jira_api_token=os.environ.get("JIRA_API_TOKEN"),
11+
)
12+
jql = os.getenv("JQL", None)
13+
14+
"""Create search instance"""
15+
search = jira.jql(jql=jql)
16+
17+
"""Search and return all fields except priority, status category changedate, and status"""
18+
search_results = search.get_page(
19+
fields=["*all", "-priority", "-statuscategorychangedate", "-status"],
20+
expand="names,changelog",
21+
)
22+
23+
"""Get all pages of search results using paginated method"""
24+
all_issues = search.get_all_pages(fields=["*all"], expand="names,changelog")
25+
26+
"""Follow https://en-ver.github.io/jira2py/ for more details"""

Diff for: examples/search_examples.py

-44
This file was deleted.

Diff for: jira2py/fields.py

+85-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,93 @@
11
from .jirabase import JiraBase
2+
from pydantic import validate_call
3+
4+
"""
5+
This module provides the `Fields` class, which allows interaction with Jira field metadata.
6+
7+
The `Fields` class enables users to retrieve and manage information about fields available in a Jira instance.
8+
It includes methods for listing all fields, retrieving field IDs based on field names, and retrieving field
9+
names based on field IDs.
10+
11+
This functionality simplifies working with Jira's field data for custom integrations and automation.
12+
"""
213

314

415
class Fields(JiraBase):
516

6-
def get(self) -> list[dict]:
17+
def __init__(self, auth_kwargs: tuple):
718

8-
kwargs = {"method": "GET", "context_path": "field"}
19+
self._set_jira_auth(auth_kwargs)
20+
21+
def _load_fields(self):
922

23+
kwargs = {"method": "GET", "context_path": "field"}
1024
return self._request_jira(**kwargs)
25+
26+
def _get_field_attr(
27+
self,
28+
in_attr_name: str,
29+
in_attr_values: str | list[str],
30+
out_attr_name: str | list[str],
31+
):
32+
33+
fields = self._load_fields()
34+
35+
return [
36+
field.get(out_attr_name, None)
37+
for attr_value in in_attr_values
38+
for field in fields
39+
if field.get(in_attr_name, None) == attr_value
40+
]
41+
42+
def get(self) -> list[dict]:
43+
"""
44+
Retrieve all available fields in the Jira instance.
45+
46+
This method fetches a list of all fields currently available in the Jira instance,
47+
including standard and custom fields. It provides comprehensive metadata for each
48+
field, which can be used for various integrations and automations.
49+
50+
Returns:
51+
list[dict]: A list of dictionaries where each dictionary contains metadata
52+
about a field (e.g., field ID, name, and other attributes).
53+
"""
54+
55+
return self._load_fields()
56+
57+
@validate_call
58+
def get_field_id(self, field_names: list[str]) -> list[str]:
59+
"""
60+
Retrieve the IDs of fields based on their names.
61+
62+
This method takes a list of field names and returns a list of corresponding field IDs
63+
available in the Jira instance.
64+
65+
Args:
66+
field_names (list[str]): A list of field names to search for.
67+
68+
Returns:
69+
list[str]: A list of field IDs that correspond to the provided field names.
70+
"""
71+
72+
return self._get_field_attr(
73+
in_attr_name="name", in_attr_values=field_names, out_attr_name="id"
74+
)
75+
76+
@validate_call
77+
def get_field_name(self, field_ids: list[str]) -> list[str]:
78+
"""
79+
Retrieve the names of fields based on their IDs.
80+
81+
This method takes a list of field IDs and returns a list of corresponding field names
82+
available in the Jira instance.
83+
84+
Args:
85+
field_ids (list[str]): A list of field IDs to search for.
86+
87+
Returns:
88+
list[str]: A list of field names that correspond to the provided field IDs.
89+
"""
90+
91+
return self._get_field_attr(
92+
in_attr_name="id", in_attr_values=field_ids, out_attr_name="name"
93+
)

0 commit comments

Comments
 (0)