Skip to content

Commit

Permalink
Jira’s API Docs are incorrect
Browse files Browse the repository at this point in the history
This shoudl address #257, #258, and #259.  The issue seems to be related to the API docs referring to “issue id OR key” when in reality it only seems to update on the issue key.  Refactored the code to use the issue keys instead of the numerical IDs, and updating seems to be occuring as expected.

https://developer.atlassian.com/cloud/jira/platform/rest/v3/api-group-issues/#api-rest-api-3-issue-issueidorkey-put
  • Loading branch information
SteveMcGrath committed May 21, 2024
1 parent e3cc6aa commit 3c4286f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 20 deletions.
8 changes: 4 additions & 4 deletions tenb2jira/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class TaskMap(Base):
plugin_id: Mapped[int] = mapped_column(primary_key=True,
sqlite_on_conflict_unique='IGNORE'
)
jira_id: Mapped[int]
jira_id: Mapped[str]
updated: Mapped[datetime]
subtasks: Mapped[List["SubTaskMap"]] = relationship(
back_populates="task", cascade="all, delete-orphan"
Expand All @@ -28,7 +28,7 @@ class SubTaskMap(Base):
sqlite_on_conflict_unique='IGNORE'
)
asset_id: Mapped[UUID]
jira_id: Mapped[int]
jira_key: Mapped[str]
plugin_id: Mapped[int] = mapped_column(ForeignKey('task.plugin_id'))
is_open: Mapped[bool]
updated: Mapped[datetime]
Expand All @@ -37,14 +37,14 @@ class SubTaskMap(Base):
def __init__(self,
finding_id: str,
asset_id: str,
jira_id: int,
jira_id: str,
plugin_id: int,
is_open: bool = True,
**kwargs,
):
self.finding_id = UUID(finding_id)
self.asset_id = UUID(asset_id)
self.jira_id = int(jira_id)
self.jira_id = str(jira_id)
self.plugin_id = int(plugin_id)
self.is_open = bool(is_open)
super().__init__(**kwargs)
Expand Down
26 changes: 10 additions & 16 deletions tenb2jira/processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def get_closed_transition(self, jira_id: int) -> int:
self.config['jira']['closed_id'] = transition.id
return transition.id

def close_task(self, jira_id: int):
def close_task(self, jira_id: str):
"""
Closes the Jira issue and appends the configured comment within Jira.
"""
Expand Down Expand Up @@ -114,7 +114,7 @@ def build_mapping_db_model(self,
item[fields[key]] = value
# item = {fields[k]: v for k, v in issue.fields.items()}
item['updated'] = self.start_time
item['jira_id'] = int(issue.id)
item['jira_id'] = issue.key
if not skip:
issues.append(model(**item).asdict())
if issues:
Expand Down Expand Up @@ -172,7 +172,6 @@ def upsert_task(self, s: Session, finding: dict) -> (int | None):
if sql.updated <= self.start_time:
self.jira.api.issues.update(sql.jira_id,
fields=task.fields,
priority=task.priority,
)
sql.updated = datetime.now()
s.commit()
Expand All @@ -197,15 +196,14 @@ def upsert_task(self, s: Session, finding: dict) -> (int | None):
# back to the caller.
if len(page.issues) == 1:
sql = TaskMap(plugin_id=task.fields[self.plugin_id],
jira_id=page.issues[0].id,
jira_id=page.issues[0].key,
updated=datetime.now(),
)
s.add(sql)
s.commit()
if finding.get('integration_pid_updated') > self.last_run:
self.jira.api.issues.update(sql.jira_id,
fields=task.fields,
priority=task.priority,
)
log.info(f'Found Task "{sql.jira_id}", '
'added to SQL Cache and updated.')
Expand All @@ -215,16 +213,14 @@ def upsert_task(self, s: Session, finding: dict) -> (int | None):
# then create a new task and map it back into the sql cache. Just like
# above, we will then return the jira issue id to the caller.
if len(page.issues) == 0:
resp = self.jira.api.issues.create(fields=task.fields,
priority=task.priority,
)
resp = self.jira.api.issues.create(fields=task.fields)
sql = TaskMap(plugin_id=task.fields[self.plugin_id],
jira_id=resp.id,
jira_id=resp.key,
updated=datetime.now()
)
s.add(sql)
s.commit()
log.info(f'Created Task "{resp.id}" and added to SQL Cache.')
log.info(f'Created Task "{resp.key}" and added to SQL Cache.')
return resp.id

# In the event that multiple tasks are returned from the search,
Expand All @@ -240,7 +236,7 @@ def upsert_task(self, s: Session, finding: dict) -> (int | None):

def upsert_subtask(self,
s: Session,
task_id: (int | None),
task_id: (str | None),
finding: dict
) -> (int | None):
"""
Expand All @@ -266,7 +262,6 @@ def upsert_subtask(self,
else:
self.jira.api.issues.update(sql.jira_id,
fields=task.fields,
priority=task.priority,
)
action = 'updated subtask'
log.info(f'Matched SubTask "{sql.jira_id}" to '
Expand Down Expand Up @@ -300,7 +295,7 @@ def upsert_subtask(self,
sql = SubTaskMap(plugin_id=task.fields[self.plugin_id],
asset_id=task.fields[self.asset_id],
finding_id=task.fields[self.finding_id],
jira_id=page.issues[0].id,
jira_id=page.issues[0].key,
is_open=task.is_open,
updated=datetime.now(),
)
Expand All @@ -309,7 +304,6 @@ def upsert_subtask(self,
if task.is_open:
self.jira.api.issues.update(sql.jira_id,
fields=task.fields,
priority=task.priority,
)
action = 'updated subtask'
else:
Expand All @@ -329,13 +323,13 @@ def upsert_subtask(self,
sql = SubTaskMap(plugin_id=task.fields[self.plugin_id],
asset_id=task.fields[self.asset_id][0],
finding_id=task.fields[self.finding_id],
jira_id=resp.id,
jira_id=resp.key,
is_open=task.is_open,
updated=datetime.now(),
)
s.add(sql)
s.commit()
log.info(f'Created Subtask "{resp.id}" and '
log.info(f'Created Subtask "{resp.key}" and '
'added to SQL Cache.'
)
return resp.id
Expand Down

0 comments on commit 3c4286f

Please sign in to comment.