Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Confluence] New method 'scrap_regex_from_page' + docs + example #1327

Merged
merged 3 commits into from
Feb 6, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
28 changes: 27 additions & 1 deletion atlassian/confluence.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import os
import time
import json

import re
from requests import HTTPError
import requests
from deprecated import deprecated
Expand Down Expand Up @@ -397,6 +397,32 @@ def get_tables_from_page(self, page_id):
except Exception as e:
log.error("Error occured", e)

def scrap_regex_from_page(self, page_id, regex):
"""
Method scraps regex patterns from a Confluence page_id.

:param page_id: The ID of the Confluence page.
:param regex: The regex pattern to scrape.
:return: A list of regex matches.
"""
regex_output = []
page_output = self.get_page_by_id(page_id, expand="body.storage")["body"]["storage"]["value"]
try:
if page_output is not None:
description_matches = [x.group(0) for x in re.finditer(regex, page_output)]
if description_matches:
regex_output.extend(description_matches)
return regex_output
except HTTPError as e:
if e.response.status_code == 404:
# Raise ApiError as the documented reason is ambiguous
log.error("couldn't find page_id : ", page_id)
raise ApiNotFoundError(
"There is no content with the given page id,"
"or the calling user does not have permission to view the page",
reason=e,
)

def get_page_labels(self, page_id, prefix=None, start=None, limit=None):
"""
Returns the list of labels on a piece of Content.
Expand Down
19 changes: 10 additions & 9 deletions atlassian/jira.py
Original file line number Diff line number Diff line change
Expand Up @@ -1553,15 +1553,16 @@ def scrap_regex_from_issue(self, issue, regex):
comments = issue_output["fields"]["comment"]["comments"]

try:
description_matches = [x.group(0) for x in re.finditer(regex, description)]
if description_matches:
regex_output.extend(description_matches)

for comment in comments:
comment_html = comment["body"]
comment_matches = [x.group(0) for x in re.finditer(regex, comment_html)]
if comment_matches:
regex_output.extend(comment_matches)
if description is not None:
description_matches = [x.group(0) for x in re.finditer(regex, description)]
if description_matches:
regex_output.extend(description_matches)

for comment in comments:
comment_html = comment["body"]
comment_matches = [x.group(0) for x in re.finditer(regex, comment_html)]
if comment_matches:
regex_output.extend(comment_matches)

return regex_output
except HTTPError as e:
Expand Down
5 changes: 4 additions & 1 deletion docs/confluence.rst
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,10 @@ Page actions
confluence.add_comment(page_id, text)

# Fetch tables from Confluence page
confluence.get_page_tables(page_id)
confluence.get_tables_from_page(page_id)

# Get regex matches from Confluence page
confluence.scrap_regex_from_page(page_id, regex)

Template actions
----------------
Expand Down
13 changes: 13 additions & 0 deletions examples/confluence/confluence_scrap_regex_from_page.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from atlassian import Confluence


confluence = Confluence(
url="<instance_url>",
username="<user_enamil>",
password="api_key",
)
page_id = 393464
ipv4_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]?)"
confluence.scrap_regex_from_page(
page_id, ipv4_regex
) # method returns list of matches of ipv4 addresses from page content.
Loading