Block creation of issue if existing open issue exists about the same PIS code.

This commit is contained in:
Fred Boniface 2024-11-02 20:20:31 +00:00
parent fb7eb20c98
commit e2ded85833
3 changed files with 54 additions and 9 deletions

View File

@ -1,6 +1,6 @@
import requests import requests
import base64 import base64
from urllib.parse import urljoin from urllib.parse import urljoin, urlencode
class GiteaConnector: class GiteaConnector:
def __init__(self, repo_url, user, key): def __init__(self, repo_url, user, key):
@ -14,22 +14,54 @@ class GiteaConnector:
"Content-Type": "application/json", "Content-Type": "application/json",
} }
## Get open issues in repo
def get_open_issue_titles(self):
url_suffix = "issues"
url = urljoin(self.repo_url, url_suffix)
queries = {
"state": "open",
"type": "issues",
}
full_url = f"{url}?{urlencode(queries)}"
response = requests.get(full_url, headers=self.header)
if response.status_code == 200:
try:
data = response.json() # Parse JSON response
print(data)
if isinstance(data, list): # Confirm it's a list
# Extract 'title' from each item in the list
existing_titles = [item['title'] for item in data if 'title' in item]
print(existing_titles)
return existing_titles
else:
print("Unexpected response format: Expected a list.")
except ValueError:
print("Error: Response is not valid JSON.")
else:
print("Error fetching existing issues")
return []
## Create an issue in the repo ## Create an issue in the repo
def create_issue(self,title,content): def create_issue(self,title,content):
url_suffix = "issues" url_suffix = "issues"
url = urljoin(self.repo_url, url_suffix) url = urljoin(self.repo_url, url_suffix)
existing_issue_titles = self.get_open_issue_titles()
if title in existing_issue_titles:
print("Issue regarding this code exists already")
return True
issue_data = { issue_data = {
"title": title, "title": title,
"body": content, "body": content,
"assignees": ["fred.boniface"], "assignees": ["fred.boniface"],
"labels": [261], "labels": [261,269],
} }
print(f"URL: {url}") print(f"URL: {url}")
response = requests.post(url, headers=self.header, json=issue_data) response = requests.post(url, headers=self.header, json=issue_data)
if response.status_code == 201: if response.status_code == 201:
print("Succesfully creates issue: ", response.json()) print("Succesfully created issue")
return True return True
else: else:
print("Failed to create issue: ", response.status_code, response.text) print("Failed to create issue: ", response.status_code, response.text)

3
src/pis_file.py Normal file
View File

@ -0,0 +1,3 @@
## Produces a PIS YAML file in the OwlBoard/data format
## and pushes it to a new branch for review

View File

@ -58,14 +58,24 @@ def check_and_validate_against_owlboard(train_entries):
elif len(train_entry['timetable_entries']) == 1: elif len(train_entry['timetable_entries']) == 1:
issue_title = f"PIS Error | Code: {train_entry['diagram_pis_code']}" issue_title = f"PIS Error | Code: {train_entry['diagram_pis_code']}"
issue_content = f""" issue_content = f"""
PIS Code {train_entry['diagram_pis_code']}. PIS Code {train_entry['diagram_pis_code']}.
Diagram dated {train_entry['diagram_date']} has been parsed and produced a mismatch with the OwlBoard database. Diagram dated {train_entry['diagram_date']} has been parsed and produced a mismatch with the OwlBoard database.
Diagram Stops: {",".join(train_entry['timetable_entries'][0]["stops"])} Diagram Stops: {",".join(train_entry['timetable_entries'][0]["stops"])}
Database Stops: {",".join(database_stops)} Database Stops: {",".join(database_stops)}
This requires a manual check of the PIS file to identify mistakes or changes. The train which was identified in the database is:
stpIndicator: {train_entry['timetable_entries'][0]['stpIndicator']}
operator: {train_entry['timetable_entries'][0]['operator']}
trainUID: {train_entry['timetable_entries'][0]['trainUid']}
scheduleStart: {train_entry['timetable_entries'][0]['scheduleStart']}
scheduleEnd: {train_entry['timetable_entries'][0]['scheduleEnd']}
daysRun: {",".join(train_entry['timetable_entries'][0]['daysRun'])}
This requires a manual check of the PIS file to identify mistakes or changes.
Make sure that you validate that the service runs on the correct day.
If the stpIndicator is 'O' or 'N', it is possible that the train schedule is newer than the parsed diagram.
""" """
print(issue_content) print(issue_content)
gitea_client.create_issue(issue_title, issue_content) gitea_client.create_issue(issue_title, issue_content)