1+ import os
2+ import subprocess
3+ from unittest import mock
4+
5+ from cherry_picker import cherry_picker
6+
7+ os .environ .setdefault ("HEROKU_REDIS_MAROON_URL" , "someurl" )
8+
9+ from miss_islington import tasks
10+
11+
12+ async def test_invalid_repo_exception_posts_comment_and_clears_state ():
13+ """A stuck cherry-picker.state in git config used to wedge every
14+ subsequent backport: CherryPicker() init raised InvalidRepoException
15+ and the task crashed with no comment posted. The task now wipes
16+ stale state pre-flight and posts an error comment if init still fails.
17+ """
18+ with (
19+ mock .patch ("miss_islington.tasks.aiohttp.ClientSession" ),
20+ mock .patch (
21+ "miss_islington.tasks.apps.get_installation_access_token" ,
22+ new = mock .AsyncMock (return_value = {"token" : "test-token" }),
23+ ),
24+ mock .patch ("miss_islington.tasks.util.is_cpython_repo" , return_value = True ),
25+ mock .patch ("miss_islington.tasks.subprocess.check_output" ),
26+ mock .patch ("miss_islington.tasks.subprocess.run" ) as run_mock ,
27+ mock .patch (
28+ "miss_islington.tasks.cherry_picker.CherryPicker" ,
29+ side_effect = cherry_picker .InvalidRepoException ("stuck state" ),
30+ ),
31+ mock .patch (
32+ "miss_islington.tasks.util.comment_on_pr" , new = mock .AsyncMock ()
33+ ) as comment_mock ,
34+ mock .patch (
35+ "miss_islington.tasks.util.assign_pr_to_core_dev" , new = mock .AsyncMock ()
36+ ) as assign_mock ,
37+ ):
38+ await tasks .backport_task_asyncio (
39+ "7a4c6dfb8839eb05fb87baf70364680e45001dd4" ,
40+ "3.15" ,
41+ issue_number = 130749 ,
42+ created_by = "medmunds" ,
43+ merged_by = "bitdancer" ,
44+ installation_id = 42958231 ,
45+ )
46+
47+ run_mock .assert_any_call (
48+ ["git" , "config" , "--local" , "--remove-section" , "cherry-picker" ],
49+ stderr = subprocess .DEVNULL ,
50+ check = False ,
51+ )
52+
53+ comment_mock .assert_awaited_once ()
54+ posted_message = comment_mock .await_args .args [2 ]
55+ assert "3.15" in posted_message
56+ assert "@bitdancer" in posted_message
57+
58+ assign_mock .assert_awaited_once ()
59+ assert assign_mock .await_args .args [1 ] == 130749
60+ assert assign_mock .await_args .args [2 ] == "bitdancer"
0 commit comments