Undeploy builds on PR close

This commit is contained in:
Stéphane Bidoul 2022-01-29 15:51:57 +01:00
parent 37dae56d35
commit e5392bb33f
No known key found for this signature in database
GPG key ID: BCAB2555446B5B92
4 changed files with 56 additions and 15 deletions

View file

@ -101,10 +101,7 @@ async def undeploy_builds(
branch: str | None = None,
pr: int | None = None,
) -> None:
for build in controller.db.search(
repo=repo, target_branch=target_branch, branch=branch, pr=pr
):
await build.undeploy()
await controller.undeploy_builds(repo, target_branch, branch, pr)
@router.post(

View file

@ -106,6 +106,18 @@ class Controller:
if build is None:
await Build.deploy(commit_info)
async def undeploy_builds(
self,
repo: str | None = None,
target_branch: str | None = None,
branch: str | None = None,
pr: int | None = None,
) -> None:
for build in self.db.search(
repo=repo, target_branch=target_branch, branch=branch, pr=pr
):
await build.undeploy()
async def get_build(self, build_name: str, db_only: bool = True) -> Build | None:
build = self.db.get(build_name)
if build is not None:

View file

@ -20,17 +20,17 @@ async def receive_payload(
# TODO check x-hub-signature
payload = await request.json()
if x_github_event == "pull_request":
repo = payload["repository"]["full_name"]
target_branch = payload["pull_request"]["base"]["ref"]
if not settings.is_repo_and_branch_supported(repo, target_branch):
_logger.debug(
"Ignoring %s payload for unsupported repo %s or target branch %s",
x_github_event,
repo,
target_branch,
)
return
if payload["action"] in ("opened", "synchronize"):
repo = payload["repository"]["full_name"]
target_branch = payload["pull_request"]["base"]["ref"]
if not settings.is_repo_and_branch_supported(repo, target_branch):
_logger.debug(
"Ignoring %s payload for unsupported repo %s or target branch %s",
x_github_event,
repo,
target_branch,
)
return
background_tasks.add_task(
controller.deploy_commit,
CommitInfo(
@ -40,6 +40,12 @@ async def receive_payload(
git_commit=payload["pull_request"]["head"]["sha"],
),
)
elif payload["action"] in ("closed",):
background_tasks.add_task(
controller.undeploy_builds,
repo=repo,
pr=payload["pull_request"]["number"],
)
elif x_github_event == "push":
repo = payload["repository"]["full_name"]
target_branch = payload["ref"].split("/")[-1]

View file

@ -85,7 +85,7 @@ def test_webhook_github_pr(action: str, mocker: MockerFixture) -> None:
)
@pytest.mark.parametrize("action", ["opened", "synchronize"])
@pytest.mark.parametrize("action", ["opened", "synchronize", "closed"])
def test_webhook_github_pr_unsupported_branch(
action: str, mocker: MockerFixture
) -> None:
@ -111,3 +111,29 @@ def test_webhook_github_pr_unsupported_branch(
)
response.raise_for_status()
mock.assert_not_called()
def test_webhook_github_pr_close(mocker: MockerFixture) -> None:
mock = mocker.patch("fastapi.BackgroundTasks.add_task")
response = client.post(
"/webhooks/github",
headers={
"X-GitHub-Event": "pull_request",
},
json={
"action": "closed",
"repository": {"full_name": "oca/mis-builder"},
"pull_request": {
"base": {
"ref": "15.0",
},
"number": 381,
},
},
)
response.raise_for_status()
mock.assert_called_with(
controller.undeploy_builds,
repo="oca/mis-builder",
pr=381,
)