Skip to content

Commit a100d4f

Browse files
authored
[jira] new feature added scrap_regex_from_issue + docs + example (#1325)
* [jira] new feature added scrap_regex_from_issue + docs + example
1 parent ac0e1fe commit a100d4f

File tree

6 files changed

+51
-1
lines changed

6 files changed

+51
-1
lines changed

atlassian/jira.py

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1409,7 +1409,7 @@ def issue_get_watchers(self, issue_key):
14091409

14101410
def assign_issue(self, issue, account_id=None):
14111411
"""Assign an issue to a user. None will set it to unassigned. -1 will set it to Automatic.
1412-
:param issue: the issue ID or key to assign
1412+
:param issue : the issue ID or key to assign
14131413
:type issue: int or str
14141414
:param account_id: the account ID of the user to assign the issue to;
14151415
for jira server the value for account_id should be a valid jira username
@@ -1531,6 +1531,44 @@ def issue_edit_comment(self, issue_key, comment_id, comment, visibility=None, no
15311531
params = {"notifyUsers": "true" if notify_users else "false"}
15321532
return self.put(url, data=data, params=params)
15331533

1534+
def scrap_regex_from_issue(self, issue, regex):
1535+
"""
1536+
This function scrapes the output of the given regex matches from the issue's description and comments.
1537+
1538+
Parameters:
1539+
issue (str): jira issue ide.
1540+
regex (str): The regex to match.
1541+
1542+
Returns:
1543+
list: A list of matches.
1544+
"""
1545+
regex_output = []
1546+
issue_output = self.get_issue(issue)
1547+
description = issue_output["fields"]["description"]
1548+
comments = issue_output["fields"]["comment"]["comments"]
1549+
1550+
try:
1551+
description_matches = [x.group(0) for x in re.finditer(regex, description)]
1552+
if description_matches:
1553+
regex_output.extend(description_matches)
1554+
1555+
for comment in comments:
1556+
comment_html = comment["body"]
1557+
comment_matches = [x.group(0) for x in re.finditer(regex, comment_html)]
1558+
if comment_matches:
1559+
regex_output.extend(comment_matches)
1560+
1561+
return regex_output
1562+
except HTTPError as e:
1563+
if e.response.status_code == 404:
1564+
# Raise ApiError as the documented reason is ambiguous
1565+
log.error("couldn't find issue: ", issue["key"])
1566+
raise ApiNotFoundError(
1567+
"There is no content with the given issue ud,"
1568+
"or the calling user does not have permission to view the issue",
1569+
reason=e,
1570+
)
1571+
15341572
def get_issue_remotelinks(self, issue_key, global_id=None, internal_id=None):
15351573
"""
15361574
Compatibility naming method with get_issue_remote_links()

docs/jira.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,8 @@ Manage issues
363363
# started is a date string in the format %Y-%m-%dT%H:%M:%S.000+0000%z
364364
jira.issue_worklog(issue_key, started, time_in_sec)
365365
366+
# Scrap regex matches from issue description and comments:
367+
jira.scrap_regex_from_issue(issue_key, regex)
366368
367369
368370
Epic Issues

examples/jira/jira_get_comment.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
""" Example: Get Comments on a Jira Issue
22
"""
3+
34
# coding=utf-8
45
from atlassian import Jira
56

examples/jira/jira_oauth2.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
token and Using access token, Jira cloud ID is identified and
66
the available projects are returned.
77
"""
8+
89
from requests_oauthlib import OAuth2Session
910
from atlassian.jira import Jira
1011
from flask import Flask, request, redirect, session
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from atlassian import Jira
2+
3+
# This feature can be useful if you need to scrap some data from issue description or comments.
4+
jira = Jira(url="http://localhost:8080", username="admin", password="admin")
5+
regex = r"((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\[?\.\]?){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)" # regex for ipv4 address + ipv4 with [.] instead of dot
6+
issue = "TEST-1" # id of the jira issue
7+
result = jira.scrap_regex_from_issue(issue, regex)
8+
# scrap_regex_from_issue will return results of positive regexes matches from issue description and issue comments.

0 commit comments

Comments
 (0)