Skip to content

Commit

Permalink
Finish implementing issue opening
Browse files Browse the repository at this point in the history
  • Loading branch information
jbruechert committed Mar 25, 2024
1 parent 1dc5c0d commit be741cd
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 19 deletions.
3 changes: 3 additions & 0 deletions .woodpecker/data-import.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,6 @@ steps:
- chmod 600 ./deploy_key
- "rsync -avz --progress --delete -e 'ssh -i ./deploy_key -p 22 -o StrictHostKeyChecking=no' out/ [email protected]:"
- rm deploy_key
environment:
GITHUB_TOKEN:
from_secret: github_issues_token
73 changes: 54 additions & 19 deletions ci/fetch-feeds.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import multiprocessing
import concurrent.futures
import requests
import json
from datetime import datetime, timezone

if len(sys.argv) < 2:
print("Argument run-reason is missing.")
Expand All @@ -19,6 +21,7 @@
run_reason = sys.argv[1]
feed_dir = Path("feeds/")
repo = "public-transport/transitous"
bot_account = "transitous-bot"


def create_feed_error_issue(feed: str, details: str, github_token: str) -> None:
Expand All @@ -28,34 +31,65 @@ def create_feed_error_issue(feed: str, details: str, github_token: str) -> None:
response = requests.get(
"https://api.github.com/search/issues",
headers={"Authorization": f"Bearer {github_token}"},
params={"q": f"repo:{repo} is:issue {issue_title}"},
params={"q": f"repo:{repo} user:{bot_account} is:issue is:open {issue_title}"},
)

if response.status_code < 200 or response.status_code >= 300:
print(f"Error searching for existing issue for {feed}")
print(response.json())
return

assignees = []
with open(feed) as f:
maintainers = json.load(f)["maintainers"]
assignees = [maintainer["github"] for maintainer in maintainers]

print("!!! Would assign:", assignees)

time_string = datetime.now(tz=timezone.utc).strftime("%Y-%m-%d %H:%M:%S %Z")

if response.json()["total_count"] > 0:
if issue["title"] == issue_title:
# Update the issue with the new error
issue_number = response.json()["items"][0]["number"]
requests.patch(
f"https://api.github.com/repos/{repo}/issues/{issue_number}",
headers={"Authorization": f"Bearer {github_token}"},
json={"body": f"{issue['body']}\n\n```{details}```"},
)
print(f"Updated error issue for {feed}")
return
issues = response.json()["items"]
for issue in issues:
if issue["title"] == issue_title:
# Update the issue with the new error
issue_number = issue["number"]

issue_text = \
f"""
{issue['body']}
On **{time_string}**
```{details}```
"""
requests.patch(
f"https://api.github.com/repos/{repo}/issues/{issue_number}",
headers={"Authorization": f"Bearer {github_token}"},
json={"body": issue_text},
)
print(f"Updated error issue for {feed}")
return

issue_text = \
f"""
An error occured while fetching a feed from `{feed}`.
If the error is not temporary, please consider replacing or removing the feed.
Thanks!
Here are the logs of the error(s):
On **{time_string}**:
```{details}```
"""

# Create an issue on the repository
response = requests.post(
f"https://api.github.com/repos/{repo}/issues",
headers={"Authorization": f"Bearer {github_token}"},
json={
"title": issue_title,
"body": f"Error fetching {feed}\n\n```{details}```",
"body": issue_text,
"labels": ["import-issue"],
"assignees": ["jbruechert"]
},
)

Expand All @@ -69,14 +103,15 @@ def create_feed_error_issue(feed: str, details: str, github_token: str) -> None:

def do_fetch(feed: str):
try:
subprocess.check_call(["./src/fetch.py", feed])
except:
print(f"Error fetching {feed}")
details = subprocess.check_output(
["./src/fetch.py", feed], stderr=subprocess.STDOUT
).decode()
print(details)
except subprocess.CalledProcessError as error:
print(f"Error fetching {feed}:")
print(error.output.decode())
if "GITHUB_TOKEN" in os.environ:
details = subprocess.check_output(
["./src/fetch.py", feed], stderr=subprocess.STDOUT
).decode()
create_feed_error_issue(feed, details, os.environ["GITHUB_TOKEN"])
create_feed_error_issue(feed, error.output.decode(), os.environ["GITHUB_TOKEN"])


match run_reason:
Expand Down

0 comments on commit be741cd

Please sign in to comment.