From e2ded8583323259c051a051ee21460d37df20a24 Mon Sep 17 00:00:00 2001 From: Fred Boniface Date: Sat, 2 Nov 2024 20:20:31 +0000 Subject: [PATCH] Block creation of issue if existing open issue exists about the same PIS code. --- src/gitea.py | 40 ++++++++++++++++++++++++++++++++++++---- src/pis_file.py | 3 +++ src/validate.py | 20 +++++++++++++++----- 3 files changed, 54 insertions(+), 9 deletions(-) create mode 100644 src/pis_file.py diff --git a/src/gitea.py b/src/gitea.py index 5298e80..a0d4fff 100644 --- a/src/gitea.py +++ b/src/gitea.py @@ -1,6 +1,6 @@ import requests import base64 -from urllib.parse import urljoin +from urllib.parse import urljoin, urlencode class GiteaConnector: def __init__(self, repo_url, user, key): @@ -14,22 +14,54 @@ class GiteaConnector: "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 def create_issue(self,title,content): url_suffix = "issues" 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 = { "title": title, "body": content, "assignees": ["fred.boniface"], - "labels": [261], + "labels": [261,269], } print(f"URL: {url}") response = requests.post(url, headers=self.header, json=issue_data) if response.status_code == 201: - print("Succesfully creates issue: ", response.json()) + print("Succesfully created issue") return True else: print("Failed to create issue: ", response.status_code, response.text) diff --git a/src/pis_file.py b/src/pis_file.py new file mode 100644 index 0000000..c7b00a5 --- /dev/null +++ b/src/pis_file.py @@ -0,0 +1,3 @@ +## Produces a PIS YAML file in the OwlBoard/data format +## and pushes it to a new branch for review + diff --git a/src/validate.py b/src/validate.py index 5f92e85..7c020e1 100644 --- a/src/validate.py +++ b/src/validate.py @@ -58,14 +58,24 @@ def check_and_validate_against_owlboard(train_entries): elif len(train_entry['timetable_entries']) == 1: issue_title = f"PIS Error | Code: {train_entry['diagram_pis_code']}" issue_content = f""" - PIS Code {train_entry['diagram_pis_code']}. - Diagram dated {train_entry['diagram_date']} has been parsed and produced a mismatch with the OwlBoard database. +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 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) gitea_client.create_issue(issue_title, issue_content)