Skip to content

Commit

Permalink
Store transition settings in IssueTransitions
Browse files Browse the repository at this point in the history
  • Loading branch information
kkaarreell committed Nov 7, 2024
1 parent 9b3e5c4 commit c05b6f6
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 9 deletions.
4 changes: 4 additions & 0 deletions component-config.yaml.sample
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ transitions:
- Completed
dropped:
- Dropped
results:
- In Progress
passed:
- Done

issues:

Expand Down
29 changes: 21 additions & 8 deletions newa/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -645,6 +645,8 @@ class Issue(Cloneable, Serializable):
summary: Optional[str] = None
closed: Optional[bool] = None
url: Optional[str] = None
transition_pass: Optional[str] = None
transition_results: Optional[str] = None

def __str__(self) -> str:
return self.id
Expand Down Expand Up @@ -1108,6 +1110,7 @@ class IssueAction: # type: ignore[no-untyped-def]
type: IssueType = field(converter=IssueType)
on_respin: OnRespinAction = field( # type: ignore[var-annotated]
converter=lambda value: OnRespinAction(value), default=OnRespinAction.CLOSE)
transition: bool = False
assignee: Optional[str] = None
parent_id: Optional[str] = None
job_recipe: Optional[str] = None
Expand All @@ -1116,11 +1119,20 @@ class IssueAction: # type: ignore[no-untyped-def]
fields: Optional[dict[str, str | float | list[str]]] = None


@define
class IssueTransitions(Serializable):
closed: list[str] = field()
dropped: list[str] = field()
passed: Optional[list[str]] = field()
results: Optional[list[str]] = field()


@define
class IssueConfig(Serializable): # type: ignore[no-untyped-def]

project: str = field()
transitions: dict[str, list[str]] = field()
transitions: IssueTransitions = field( # type: ignore[var-annotated]
converter=lambda x: x if isinstance(x, IssueTransitions) else IssueTransitions(**x))
issues: list[IssueAction] = field( # type: ignore[var-annotated]
factory=list, converter=lambda issues: [
IssueAction(**issue) for issue in issues])
Expand All @@ -1136,7 +1148,7 @@ class JiraField:


@frozen
class IssueHandler:
class IssueHandler: # type: ignore[no-untyped-def]
""" An interface to Jira instance handling a specific ArtifactJob """

artifact_job: ArtifactJob = field()
Expand All @@ -1145,7 +1157,8 @@ class IssueHandler:
project: str = field()

# Each project can have different semantics of issue status.
transitions: dict[str, list[str]] = field()
transitions: IssueTransitions = field( # type: ignore[var-annotated]
converter=lambda x: x if isinstance(x, IssueTransitions) else IssueTransitions(**x))

# field name=>JiraField mapping will be obtained from Jira later
# see https://JIRASERVER/rest/api/2/field
Expand Down Expand Up @@ -1273,7 +1286,7 @@ def get_related_issues(self,
f"project = '{self.project}' AND " + \
f"labels in ({IssueHandler.newa_label}) AND " + \
f"description ~ '{newa_description}' AND " + \
f"status not in ({','.join(self.transitions['closed'])})"
f"status not in ({','.join(self.transitions.closed)})"
search_result = self.connection.search_issues(query, fields=fields, json_result=True)
if not isinstance(search_result, dict):
raise Exception(f"Unexpected search result type {type(search_result)}!")
Expand All @@ -1286,7 +1299,7 @@ def get_related_issues(self,
for jira_issue in search_result["issues"]:
if newa_description in jira_issue["fields"]["description"]:
result[jira_issue["key"]] = {"description": jira_issue["fields"]["description"]}
if jira_issue["fields"]["status"]["name"] in self.transitions['closed']:
if jira_issue["fields"]["status"]["name"] in self.transitions.closed:
result[jira_issue["key"]] |= {"status": "closed"}
else:
result[jira_issue["key"]] |= {"status": "opened"}
Expand Down Expand Up @@ -1433,15 +1446,15 @@ def drop_obsoleted_issue(self, issue: Issue, obsoleted_by: Issue) -> None:
'value': self.group} if self.group else None,
})
# if the transition has a format status.resolution close with resolution
if '.' in self.transitions["dropped"][0]:
status, resolution = self.transitions["dropped"][0].split('.', 1)
if '.' in self.transitions.dropped[0]:
status, resolution = self.transitions.dropped[0].split('.', 1)
self.connection.transition_issue(issue.id,
transition=status,
resolution={'name': resolution})
# otherwise close just using the status
else:
self.connection.transition_issue(issue.id,
transition=self.transitions["dropped"][0])
transition=self.transitions.dropped[0])
except jira.JIRAError as e:
raise Exception(f"Cannot close issue {issue}!") from e

Expand Down
2 changes: 1 addition & 1 deletion newa/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -568,7 +568,7 @@ def _jira_fake_id_generator() -> Generator[str, int, None]:
group=config.group)
jira_issue = jira_handler.get_details(mapped_issue)
mapped_issue.closed = jira_issue.get_field(
"status").name in jira_handler.transitions['closed']
"status").name in jira_handler.transitions.closed
new_issues.append(mapped_issue)

# otherwise we need to search for the issue in Jira
Expand Down

0 comments on commit c05b6f6

Please sign in to comment.