Skip to content

Commit 3ce10c6

Browse files
mlbonhommemarco-c
andauthored
Add an expiry to not create try pushes for phabricator revisions that… (#106)
Co-authored-by: Marco Castelluccio <[email protected]>
1 parent 02df27d commit 3ce10c6

File tree

2 files changed

+86
-2
lines changed

2 files changed

+86
-2
lines changed

libmozevent/mercurial.py

+25-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import os
1212
import tempfile
1313
import time
14-
from datetime import datetime
14+
from datetime import datetime, timedelta
1515

1616
import hglib
1717
import requests
@@ -335,12 +335,20 @@ class MercurialWorker(object):
335335
]
336336
]
337337

338-
def __init__(self, queue_name, queue_phabricator, repositories, skippable_files=[]):
338+
def __init__(
339+
self,
340+
queue_name,
341+
queue_phabricator,
342+
repositories,
343+
diff_expiry=timedelta(hours=24),
344+
skippable_files=[],
345+
):
339346
assert all(map(lambda r: isinstance(r, Repository), repositories.values()))
340347
self.queue_name = queue_name
341348
self.queue_phabricator = queue_phabricator
342349
self.repositories = repositories
343350
self.skippable_files = skippable_files
351+
self.diff_expiry = diff_expiry
344352

345353
def register(self, bus):
346354
self.bus = bus
@@ -448,6 +456,21 @@ async def handle_build(self, repository, build):
448456
build,
449457
{"message": error_log, "duration": time.time() - start},
450458
)
459+
if (
460+
build.diff.get("fields")
461+
and build.diff["fields"].get("dateCreated")
462+
and (
463+
datetime.now()
464+
- datetime.fromtimestamp(build.diff["fields"]["dateCreated"])
465+
> self.diff_expiry
466+
)
467+
):
468+
error_log = "This build is too old to push to try repository"
469+
return (
470+
"fail:mercurial",
471+
build,
472+
{"message": error_log, "duration": time.time() - start},
473+
)
451474
elif build.retries:
452475
logger.warning(
453476
"Trying to apply build's diff after a remote push error "

tests/test_mercurial.py

+61
Original file line numberDiff line numberDiff line change
@@ -917,3 +917,64 @@ def test_get_base_identifier(mock_mc):
917917
assert (
918918
mock_mc.get_base_identifier(stack) == "tip"
919919
), "`tip` commit should be used when `use_latest_revision` is `True`."
920+
921+
922+
@responses.activate
923+
@pytest.mark.asyncio
924+
async def test_push_failure_diff_expiry(PhabricatorMock, mock_mc):
925+
diff = {
926+
"revisionPHID": "PHID-DREV-badutf8",
927+
"baseRevision": "missing",
928+
"phid": "PHID-DIFF-badutf8",
929+
"id": 555,
930+
# a date in 2017
931+
"fields": {"dateCreated": 1510251135},
932+
}
933+
build = MockBuild(4444, "PHID-REPO-mc", 5555, "PHID-build-badutf8", diff)
934+
with PhabricatorMock as phab:
935+
phab.load_patches_stack(build)
936+
937+
bus = MessageBus()
938+
bus.add_queue("phabricator")
939+
940+
from libmozevent import mercurial
941+
942+
mercurial.TRY_STATUS_URL = "http://test.status/try"
943+
944+
sleep_history = []
945+
946+
class AsyncioMock(object):
947+
async def sleep(self, value):
948+
nonlocal sleep_history
949+
sleep_history.append(value)
950+
951+
mercurial.asyncio = AsyncioMock()
952+
953+
responses.get(
954+
"http://test.status/try", status=200, json={"result": {"status": "open"}}
955+
)
956+
957+
repository_mock = MagicMock(spec=Repository)
958+
959+
worker = MercurialWorker(
960+
"mercurial", "phabricator", repositories={"PHID-REPO-mc": repository_mock}
961+
)
962+
worker.register(bus)
963+
964+
await bus.send("mercurial", build)
965+
assert bus.queues["mercurial"].qsize() == 1
966+
task = asyncio.create_task(worker.run())
967+
968+
# Check the treeherder link was queued
969+
mode, out_build, details = await bus.receive("phabricator")
970+
task.cancel()
971+
972+
assert build.retries == 0
973+
974+
assert mode == "fail:mercurial"
975+
assert out_build == build
976+
assert details["duration"] > 0
977+
assert details["message"] == "This build is too old to push to try repository"
978+
979+
# no call sent to TRY_STATUS_URL
980+
assert len(responses.calls) == 0

0 commit comments

Comments
 (0)