Skip to content
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
35 changes: 31 additions & 4 deletions events/code_review_events/bugbug_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@

class BugbugUtils:
def __init__(self, phabricator_api):
self.phabricator_deployment = taskcluster_config.secrets.get(
"bugbug_phabricator_deployment", "prod"
)
assert self.phabricator_deployment in (
"prod",
"dev",
), f"{self.phabricator_deployment} should be either 'prod' or 'dev'"

self.test_selection_enabled = taskcluster_config.secrets.get(
"test_selection_enabled", False
)
Expand Down Expand Up @@ -148,7 +156,12 @@ async def start_risk_analysis(self, build: PhabricatorBuild):
return

task = self.community_tc["hooks"].triggerHook(
"project-relman", "bugbug-classify-patch", {"DIFF_ID": build.diff_id}
"project-relman",
"bugbug-classify-patch",
{
"PHABRICATOR_DEPLOYMENT": self.phabricator_deployment,
"DIFF_ID": build.diff_id,
},
)
task_id = task["status"]["taskId"]
logger.info("Triggered a new risk analysis task", id=task_id)
Expand Down Expand Up @@ -181,7 +194,12 @@ async def start_test_selection(self, build: PhabricatorBuild):
return

task = self.community_tc["hooks"].triggerHook(
"project-relman", "bugbug-test-select", {"DIFF_ID": build.diff_id}
"project-relman",
"bugbug-test-select",
{
"PHABRICATOR_DEPLOYMENT": self.phabricator_deployment,
"DIFF_ID": build.diff_id,
},
)
task_id = task["status"]["taskId"]
logger.info("Triggered a new test selection task", id=task_id)
Expand All @@ -197,22 +215,26 @@ async def start_test_selection(self, build: PhabricatorBuild):
async def get_test_selection_results(self, task_id):
# Get the Phabricator diff ID from bugbug task definition.
bugbug_task = self.community_tc["queue"].task(task_id)
phabricator_deployment = str(bugbug_task["extra"]["phabricator-deployment"])
diff_id = str(bugbug_task["extra"]["phabricator-diff-id"])

if self.phabricator_deployment != phabricator_deployment:
return (phabricator_deployment, diff_id, False, [])

# Retrieve artifacts from bugbug test selection task.
failure_risk = self.community_tc["queue"].getLatestArtifact(
task_id, "public/failure_risk"
)
assert isinstance(failure_risk, int)

if failure_risk == 0:
return (diff_id, False, [])
return (phabricator_deployment, diff_id, False, [])

selected_tasks = self.community_tc["queue"].getLatestArtifact(
task_id, "public/selected_tasks"
)

return (diff_id, True, selected_tasks)
return (phabricator_deployment, diff_id, True, selected_tasks)

def add_new_jobs(self, revision, selected_tasks):
selected_tasks = {"tasks": selected_tasks["response"].text.splitlines()}
Expand Down Expand Up @@ -265,6 +287,7 @@ async def got_bugbug_test_select_end(self, payload):

try:
(
phabricator_deployment,
diff_id,
failure_risk,
selected_tasks,
Expand All @@ -277,6 +300,10 @@ async def got_bugbug_test_select_end(self, payload):
)
return

# If this diff belongs to a Phabricator deployment we are not attached to, return.
if self.phabricator_deployment != phabricator_deployment:
return

# If this diff does not belong to a revision we pushed to try, return.
try:
push = await self.diff_to_push.get(diff_id)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
},
"tags": {},
"extra": {
"phabricator-deployment": "prod",
"phabricator-diff-id": 125397
}
}
81 changes: 40 additions & 41 deletions events/tests/test_bugbug_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ def __init__(self, **kwargs):
self.rel_url = MockURL(**kwargs)


taskcluster_config.secrets = {
"bugbug_phabricator_deployment": "prod",
"taskcluster_community": {"client_id": "xxx", "access_token": "yyy"},
}


@pytest.mark.asyncio
async def test_risk_analysis_should_trigger(PhabricatorMock, mock_taskcluster):
bus = MessageBus()
Expand All @@ -41,21 +47,15 @@ async def test_risk_analysis_should_trigger(PhabricatorMock, mock_taskcluster):
phab.update_state(build)

# Reviewer of the patch is in the list of risk analysis users.
taskcluster_config.secrets = {
"risk_analysis_users": ["ehsan", "heycam"],
"taskcluster_community": {"client_id": "xxx", "access_token": "yyy"},
}
taskcluster_config.secrets["risk_analysis_users"] = ["ehsan", "heycam"]

bugbug_utils = BugbugUtils(phab.api)
bugbug_utils.register(bus)

assert bugbug_utils.should_run_risk_analysis(build)

# Author of the patch is in the list of risk analysis users.
taskcluster_config.secrets = {
"risk_analysis_users": ["ehsan", "tnguyen"],
"taskcluster_community": {"client_id": "xxx", "access_token": "yyy"},
}
taskcluster_config.secrets["risk_analysis_users"] = ["ehsan", "tnguyen"]

bugbug_utils = BugbugUtils(phab.api)

Expand All @@ -74,10 +74,7 @@ async def test_risk_analysis_shouldnt_trigger(PhabricatorMock, mock_taskcluster)
)
)

taskcluster_config.secrets = {
"risk_analysis_users": ["ehsan"],
"taskcluster_community": {"client_id": "xxx", "access_token": "yyy"},
}
taskcluster_config.secrets["risk_analysis_users"] = ["ehsan"]

with PhabricatorMock as phab:
phab.load_patches_stack(build)
Expand Down Expand Up @@ -112,37 +109,26 @@ def calc_perc():

bugbug_utils = BugbugUtils(phab.api)

taskcluster_config.secrets = {
"test_selection_enabled": False,
"test_selection_share": 0.0,
}
taskcluster_config.secrets["test_selection_enabled"] = False
taskcluster_config.secrets["test_selection_share"] = 0.0

bugbug_utils = BugbugUtils(phab.api)
assert calc_perc() == 0.0

taskcluster_config.secrets = {
"test_selection_enabled": False,
"test_selection_share": 0.0,
"taskcluster_community": {"client_id": "xxx", "access_token": "yyy"},
}
taskcluster_config.secrets["test_selection_enabled"] = False
taskcluster_config.secrets["test_selection_share"] = 0.1

bugbug_utils = BugbugUtils(phab.api)
assert calc_perc() == 0.0

taskcluster_config.secrets = {
"test_selection_enabled": True,
"test_selection_share": 0.0,
"taskcluster_community": {"client_id": "xxx", "access_token": "yyy"},
}
taskcluster_config.secrets["test_selection_enabled"] = True
taskcluster_config.secrets["test_selection_share"] = 0.0

bugbug_utils = BugbugUtils(phab.api)
assert calc_perc() == 0.0

taskcluster_config.secrets = {
"test_selection_enabled": True,
"test_selection_share": 0.1,
"taskcluster_community": {"client_id": "xxx", "access_token": "yyy"},
}
taskcluster_config.secrets["test_selection_enabled"] = True
taskcluster_config.secrets["test_selection_share"] = 0.1

bugbug_utils = BugbugUtils(phab.api)
assert 0.03 < calc_perc() < 0.18
Expand Down Expand Up @@ -266,11 +252,8 @@ async def test_got_bugbug_test_select_end(PhabricatorMock, mock_taskcluster):

taskGroupId = payload["body"]["status"]["taskGroupId"]

taskcluster_config.secrets = {
"test_selection_enabled": True,
"test_selection_share": 0.1,
"taskcluster_community": {"client_id": "xxx", "access_token": "yyy"},
}
taskcluster_config.secrets["test_selection_enabled"] = True
taskcluster_config.secrets["test_selection_share"] = 0.1

with PhabricatorMock as phab:
phab.load_patches_stack(build)
Expand Down Expand Up @@ -316,6 +299,25 @@ async def test_got_bugbug_test_select_end(PhabricatorMock, mock_taskcluster):
with pytest.raises(KeyError):
await bugbug_utils.task_group_to_push.get(taskGroupId)

# Nothing happens when we are on the wrong Phabricator deployment.
with responses.RequestsMock(assert_all_requests_are_fired=True) as rsps:
rsps.add(
responses.GET,
"http://community_taskcluster.test/api/queue/v1/task/bugbug-test-select",
body=mock_taskcluster("task-bugbug-test-select.json"),
content_type="application/json",
)

bugbug_utils.phabricator_deployment = "dev"

await bugbug_utils.diff_to_push.set(diffId, {"revision": "123", "build": build})
await bugbug_utils.got_bugbug_test_select_end(payload)
with pytest.raises(KeyError):
await bugbug_utils.task_group_to_push.get(taskGroupId)
await bugbug_utils.diff_to_push.rem(diffId)

bugbug_utils.phabricator_deployment = "prod"

# Nothing happens when the failure risk is low.
with responses.RequestsMock(assert_all_requests_are_fired=True) as rsps:
rsps.add(
Expand Down Expand Up @@ -503,11 +505,8 @@ async def test_got_try_task_end(PhabricatorMock, mock_taskcluster, mock_treeherd

taskGroupId = payload["body"]["status"]["taskGroupId"]

taskcluster_config.secrets = {
"test_selection_enabled": True,
"test_selection_share": 0.1,
"taskcluster_community": {"client_id": "xxx", "access_token": "yyy"},
}
taskcluster_config.secrets["test_selection_enabled"] = True
taskcluster_config.secrets["test_selection_share"] = 0.1

with PhabricatorMock as phab:
bus.add_queue(QUEUE_PHABRICATOR_RESULTS)
Expand Down