forked from github/plane
fix: update jira summary endpoints (#3333)
* dev: update jira summary endpoints * dev: update jira project key validations * dev: updated key length
This commit is contained in:
parent
4b0ccea146
commit
d887b780ae
@ -1,53 +1,89 @@
|
|||||||
import requests
|
import requests
|
||||||
|
import re
|
||||||
from requests.auth import HTTPBasicAuth
|
from requests.auth import HTTPBasicAuth
|
||||||
from sentry_sdk import capture_exception
|
from sentry_sdk import capture_exception
|
||||||
|
from urllib.parse import urlparse, urljoin
|
||||||
|
|
||||||
from urllib.parse import urlparse
|
|
||||||
|
|
||||||
def is_allowed_hostname(hostname):
|
def is_allowed_hostname(hostname):
|
||||||
allowed_lists = ["atl-paas.net", "atlassian.com", "atlassian.net", "jira.com"]
|
allowed_domains = ["atl-paas.net", "atlassian.com", "atlassian.net", "jira.com"]
|
||||||
# Extract the base domain from the hostname
|
parsed_uri = urlparse(f"https://{hostname}")
|
||||||
parsed_uri = urlparse(f"https://{hostname}") # Add scheme for urlparse to work properly
|
domain = parsed_uri.netloc.split(":")[0] # Ensures no port is included
|
||||||
domain = parsed_uri.netloc.split(":")[0] # Removes port number if included
|
base_domain = ".".join(domain.split(".")[-2:])
|
||||||
base_domain = ".".join(domain.split(".")[-2:]) # Extract base domain
|
return base_domain in allowed_domains
|
||||||
|
|
||||||
# Check if the base domain is in the allowed list
|
|
||||||
return base_domain in allowed_lists
|
def is_valid_project_key(project_key):
|
||||||
|
if project_key:
|
||||||
|
project_key = project_key.strip().upper()
|
||||||
|
# Adjust the regular expression as needed based on your specific requirements.
|
||||||
|
if len(project_key) > 30:
|
||||||
|
return False
|
||||||
|
# Check the validity of the key as well
|
||||||
|
pattern = re.compile(r'^[A-Z0-9]{1,10}$')
|
||||||
|
return pattern.match(project_key) is not None
|
||||||
|
else:
|
||||||
|
False
|
||||||
|
|
||||||
|
def generate_valid_project_key(project_key):
|
||||||
|
return project_key.strip().upper()
|
||||||
|
|
||||||
|
def generate_url(hostname, path):
|
||||||
|
if not is_allowed_hostname(hostname):
|
||||||
|
raise ValueError("Invalid or unauthorized hostname")
|
||||||
|
return urljoin(f"https://{hostname}", path)
|
||||||
|
|
||||||
|
|
||||||
def jira_project_issue_summary(email, api_token, project_key, hostname):
|
def jira_project_issue_summary(email, api_token, project_key, hostname):
|
||||||
try:
|
try:
|
||||||
|
|
||||||
|
|
||||||
if not is_allowed_hostname(hostname):
|
if not is_allowed_hostname(hostname):
|
||||||
print("Errored Hostname")
|
|
||||||
return {"error": "Invalid or unauthorized hostname"}
|
return {"error": "Invalid or unauthorized hostname"}
|
||||||
|
|
||||||
|
if not is_valid_project_key(project_key):
|
||||||
|
return {"error": "Invalid project key"}
|
||||||
|
|
||||||
auth = HTTPBasicAuth(email, api_token)
|
auth = HTTPBasicAuth(email, api_token)
|
||||||
headers = {"Accept": "application/json"}
|
headers = {"Accept": "application/json"}
|
||||||
|
|
||||||
issue_url = f"https://{hostname}/rest/api/3/search?jql=project={project_key} AND issuetype!=Epic"
|
# make the project key upper case
|
||||||
|
project_key = generate_valid_project_key(project_key)
|
||||||
|
|
||||||
|
# issues
|
||||||
|
issue_url = generate_url(
|
||||||
|
hostname,
|
||||||
|
f"/rest/api/3/search?jql=project={project_key} AND issuetype!=Epic",
|
||||||
|
)
|
||||||
issue_response = requests.request(
|
issue_response = requests.request(
|
||||||
"GET", issue_url, headers=headers, auth=auth
|
"GET", issue_url, headers=headers, auth=auth
|
||||||
).json()["total"]
|
).json()["total"]
|
||||||
|
|
||||||
module_url = f"https://{hostname}/rest/api/3/search?jql=project={project_key} AND issuetype=Epic"
|
# modules
|
||||||
|
module_url = generate_url(
|
||||||
|
hostname, f"/rest/api/3/search?jql=project={project_key} AND issuetype=Epic"
|
||||||
|
)
|
||||||
module_response = requests.request(
|
module_response = requests.request(
|
||||||
"GET", module_url, headers=headers, auth=auth
|
"GET", module_url, headers=headers, auth=auth
|
||||||
).json()["total"]
|
).json()["total"]
|
||||||
|
|
||||||
status_url = f"https://{hostname}/rest/api/3/project/${project_key}/statuses"
|
# status
|
||||||
|
status_url = generate_url(
|
||||||
|
hostname, f"/rest/api/3/project/${project_key}/statuses"
|
||||||
|
)
|
||||||
status_response = requests.request(
|
status_response = requests.request(
|
||||||
"GET", status_url, headers=headers, auth=auth
|
"GET", status_url, headers=headers, auth=auth
|
||||||
).json()
|
).json()
|
||||||
|
|
||||||
labels_url = f"https://{hostname}/rest/api/3/label/?jql=project={project_key}"
|
# labels
|
||||||
|
labels_url = generate_url(
|
||||||
|
hostname, f"/rest/api/3/label/?jql=project={project_key}"
|
||||||
|
)
|
||||||
labels_response = requests.request(
|
labels_response = requests.request(
|
||||||
"GET", labels_url, headers=headers, auth=auth
|
"GET", labels_url, headers=headers, auth=auth
|
||||||
).json()["total"]
|
).json()["total"]
|
||||||
|
|
||||||
users_url = (
|
# users
|
||||||
f"https://{hostname}/rest/api/3/users/search?jql=project={project_key}"
|
users_url = generate_url(
|
||||||
|
hostname, f"/rest/api/3/users/search?jql=project={project_key}"
|
||||||
)
|
)
|
||||||
users_response = requests.request(
|
users_response = requests.request(
|
||||||
"GET", users_url, headers=headers, auth=auth
|
"GET", users_url, headers=headers, auth=auth
|
||||||
|
Loading…
Reference in New Issue
Block a user