Skip to content

Commit

Permalink
Raise a 'PushNotFound' when specified revision/branch does not exist
Browse files Browse the repository at this point in the history
  • Loading branch information
ahal committed Mar 24, 2020
1 parent 1f870e0 commit 6f5300f
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
16 changes: 16 additions & 0 deletions mozci/errors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# -*- coding: utf-8 -*-


class BasePushException(Exception):
def __init__(self, rev, branch, msg):
self.rev = rev
self.branch = branch
self.msg = f"Error processing push '{rev}' on {branch}: {msg}"


class PushNotFound(BasePushException):
"""Raised when the requested push does not exist."""

def __init__(self, *args, **kwargs):
kwargs["msg"] = "does not exist!"
super(PushNotFound, self).__init__(*args, **kwargs)
5 changes: 5 additions & 0 deletions mozci/util/hgmo.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-
from adr.util.memoize import memoize

from mozci.errors import PushNotFound
from mozci.util.req import get_session


Expand Down Expand Up @@ -35,6 +36,10 @@ def create(rev, branch="autoland"):
@memoize
def _get_resource(self, url):
r = get_session("hgmo").get(url)

if r.status_code == 404:
raise PushNotFound(**self.context)

r.raise_for_status()
return r.json()

Expand Down
27 changes: 27 additions & 0 deletions tests/test_push.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import pytest

from mozci.errors import PushNotFound
from mozci.push import MAX_DEPTH, Push
from mozci.task import Task
from mozci.util.hgmo import HGMO
Expand Down Expand Up @@ -624,3 +625,29 @@ def test_create_push(responses):
assert p2.rev == "123456"
assert p2.id == 123
assert p2.date == 1213174092


def test_push_does_not_exist(responses):
# We hit hgmo when 'rev' is less than 40 characters.
rev = "foobar"
responses.add(
responses.GET,
HGMO.JSON_TEMPLATE.format(branch="integration/autoland", rev=rev),
json={f"error": "unknown revision '{rev}'"},
status=404,
)

with pytest.raises(PushNotFound):
Push(rev)

# Otherwise we need to hit hgmo some other way.
rev = "a" * 40
responses.add(
responses.GET,
HGMO.JSON_TEMPLATE.format(branch="integration/autoland", rev=rev),
json={f"error": "unknown revision '{rev}'"},
status=404,
)
p = Push(rev)
with pytest.raises(PushNotFound):
p.id

0 comments on commit 6f5300f

Please sign in to comment.