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

Improved exception handling #270

Merged
merged 3 commits into from
Oct 23, 2023
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
33 changes: 29 additions & 4 deletions openedx_webhooks/tasks/pr_tracking.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from __future__ import annotations

import contextlib
import copy
import dataclasses
import itertools
Expand Down Expand Up @@ -227,6 +228,10 @@
label_names = set(lbl["name"] for lbl in pr["labels"])
desired.jira_nicks = {name.partition(":")[-1] for name in label_names if name.startswith("jira:")}

if "crash!123" in label_names:
# Low-tech backdoor way to test error handling and reporting.
raise Exception(f"A crash label was applied by {user}")

desired.jira_title = pr["title"]
desired.jira_description = pr["body"] or ""

Expand Down Expand Up @@ -314,10 +319,23 @@

self.bot_data = copy.deepcopy(current.bot_data)
self.fix_result: FixResult = FixResult()
self.exceptions: List[Exception] = []

def result(self) -> FixResult:
return self.fix_result

@contextlib.contextmanager
def saved_exceptions(self):
"""
A context manager to wrap around isolatable steps.

An exception raised in the with-block will be added to `self.exceptions`.
"""
try:
yield
except Exception as exc:
self.exceptions.append(exc)

Check warning on line 337 in openedx_webhooks/tasks/pr_tracking.py

View check run for this annotation

Codecov / codecov/patch

openedx_webhooks/tasks/pr_tracking.py#L336-L337

Added lines #L336 - L337 were not covered by tests

def fix(self) -> None:
"""
The main routine for making needed changes.
Expand All @@ -331,19 +349,26 @@

if self.desired.cla_check != self.current.cla_check:
assert self.desired.cla_check is not None
self.actions.set_cla_status(status=self.desired.cla_check)
with self.saved_exceptions():
self.actions.set_cla_status(status=self.desired.cla_check)

if self.desired.is_ospr:
self._fix_ospr()
with self.saved_exceptions():
self._fix_ospr()

if self.desired.is_refused:
self._fix_comments()
with self.saved_exceptions():
self._fix_comments()

# Make needed Jira issues.
current_jira_nicks = {ji.nick for ji in self.current.bot_data.jira_issues}
for jira_nick in self.desired.jira_nicks:
if jira_nick not in current_jira_nicks:
self._make_jira_issue(jira_nick)
with self.saved_exceptions():
self._make_jira_issue(jira_nick)

if self.exceptions:
raise ExceptionGroup("Some actions failed", self.exceptions)

Check warning on line 371 in openedx_webhooks/tasks/pr_tracking.py

View check run for this annotation

Codecov / codecov/patch

openedx_webhooks/tasks/pr_tracking.py#L371

Added line #L371 was not covered by tests

def _fix_comments(self) -> None:
fix_comment = True
Expand Down
4 changes: 2 additions & 2 deletions openedx_webhooks/templates/main.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ <h2>GitHub</h2>

<h2>Other</h2>
<ul>
<li><a href="{{ url_for("github_views.generate_error") }}">Generate Immediate Error</a></li>
<li><a href="{{ url_for("github_views.generate_task_error") }}">Generate Task Error</a></li>
<li><a href="{{ url_for("github_views.generate_error") }}">Generate Immediate Flask Error</a></li>
<li><a href="{{ url_for("github_views.generate_task_error") }}">Generate Celery Task Error</a></li>
</ul>
</body>
</html>
7 changes: 7 additions & 0 deletions tests/test_pull_request_opened.py
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,13 @@ def test_add_to_multiple_projects(fake_github):
}


def test_crash_label(fake_github):
pr = fake_github.make_pull_request("openedx", user="nedbat")
pr.set_labels(["crash!123"])
with pytest.raises(Exception, match="A crash label was applied by nedbat"):
pull_request_changed(pr.as_json())


def test_jira_labelling(fake_github, fake_jira, fake_jira2):
# A PR with a "jira:" label makes a Jira issue.
pr = fake_github.make_pull_request("openedx", user="nedbat", title="Ned's PR")
Expand Down
Loading