Undeploy builds on PR close
This commit is contained in:
parent
37dae56d35
commit
e5392bb33f
4 changed files with 56 additions and 15 deletions
|
|
@ -101,10 +101,7 @@ async def undeploy_builds(
|
||||||
branch: str | None = None,
|
branch: str | None = None,
|
||||||
pr: int | None = None,
|
pr: int | None = None,
|
||||||
) -> None:
|
) -> None:
|
||||||
for build in controller.db.search(
|
await controller.undeploy_builds(repo, target_branch, branch, pr)
|
||||||
repo=repo, target_branch=target_branch, branch=branch, pr=pr
|
|
||||||
):
|
|
||||||
await build.undeploy()
|
|
||||||
|
|
||||||
|
|
||||||
@router.post(
|
@router.post(
|
||||||
|
|
|
||||||
|
|
@ -106,6 +106,18 @@ class Controller:
|
||||||
if build is None:
|
if build is None:
|
||||||
await Build.deploy(commit_info)
|
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:
|
async def get_build(self, build_name: str, db_only: bool = True) -> Build | None:
|
||||||
build = self.db.get(build_name)
|
build = self.db.get(build_name)
|
||||||
if build is not None:
|
if build is not None:
|
||||||
|
|
|
||||||
|
|
@ -20,17 +20,17 @@ async def receive_payload(
|
||||||
# TODO check x-hub-signature
|
# TODO check x-hub-signature
|
||||||
payload = await request.json()
|
payload = await request.json()
|
||||||
if x_github_event == "pull_request":
|
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"):
|
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(
|
background_tasks.add_task(
|
||||||
controller.deploy_commit,
|
controller.deploy_commit,
|
||||||
CommitInfo(
|
CommitInfo(
|
||||||
|
|
@ -40,6 +40,12 @@ async def receive_payload(
|
||||||
git_commit=payload["pull_request"]["head"]["sha"],
|
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":
|
elif x_github_event == "push":
|
||||||
repo = payload["repository"]["full_name"]
|
repo = payload["repository"]["full_name"]
|
||||||
target_branch = payload["ref"].split("/")[-1]
|
target_branch = payload["ref"].split("/")[-1]
|
||||||
|
|
|
||||||
|
|
@ -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(
|
def test_webhook_github_pr_unsupported_branch(
|
||||||
action: str, mocker: MockerFixture
|
action: str, mocker: MockerFixture
|
||||||
) -> None:
|
) -> None:
|
||||||
|
|
@ -111,3 +111,29 @@ def test_webhook_github_pr_unsupported_branch(
|
||||||
)
|
)
|
||||||
response.raise_for_status()
|
response.raise_for_status()
|
||||||
mock.assert_not_called()
|
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,
|
||||||
|
)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue