Ignore webhooks for unsupported repos/branches

This commit is contained in:
Stéphane Bidoul 2021-11-21 19:19:22 +01:00
parent 6912176be1
commit 65a0805956
No known key found for this signature in database
GPG key ID: BCAB2555446B5B92
2 changed files with 75 additions and 5 deletions

View file

@ -1,7 +1,12 @@
import logging
from fastapi import APIRouter, BackgroundTasks, Header, Request from fastapi import APIRouter, BackgroundTasks, Header, Request
from .controller import controller from .controller import controller
from .github import CommitInfo from .github import CommitInfo
from .settings import settings
_logger = logging.getLogger(__name__)
router = APIRouter() router = APIRouter()
@ -16,21 +21,41 @@ async def receive_payload(
payload = await request.json() payload = await request.json()
if x_github_event == "pull_request": if x_github_event == "pull_request":
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_or_start, controller.deploy_or_start,
CommitInfo( CommitInfo(
repo=payload["repository"]["full_name"], repo=repo,
target_branch=payload["pull_request"]["base"]["ref"], target_branch=target_branch,
pr=payload["pull_request"]["number"], pr=payload["pull_request"]["number"],
git_commit=payload["pull_request"]["head"]["sha"], git_commit=payload["pull_request"]["head"]["sha"],
), ),
) )
elif x_github_event == "push": elif x_github_event == "push":
repo = payload["repository"]["full_name"]
target_branch = payload["ref"].split("/")[-1]
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_or_start, controller.deploy_or_start,
CommitInfo( CommitInfo(
repo=payload["repository"]["full_name"], repo=repo,
target_branch=payload["ref"].split("/")[-1], target_branch=target_branch,
pr=None, pr=None,
git_commit=payload["after"], git_commit=payload["after"],
), ),

View file

@ -34,8 +34,25 @@ def test_webhook_github_push(mocker: MockerFixture) -> None:
) )
def test_webhook_github_push_unsupported_repo(mocker: MockerFixture) -> None:
mock = mocker.patch("fastapi.BackgroundTasks.add_task")
response = client.post(
"/webhooks/github",
headers={
"X-GitHub-Event": "push",
},
json={
"repository": {"full_name": "org/not-a-repo"}, # repo not in .env.test
"ref": "refs/heads/15.0",
"after": "abcde",
},
)
response.raise_for_status()
mock.assert_not_called()
@pytest.mark.parametrize("action", ["opened", "synchronize"]) @pytest.mark.parametrize("action", ["opened", "synchronize"])
def test_webhook_github_pr_open_synchronize(action: str, mocker: MockerFixture) -> None: def test_webhook_github_pr(action: str, mocker: MockerFixture) -> None:
mock = mocker.patch("fastapi.BackgroundTasks.add_task") mock = mocker.patch("fastapi.BackgroundTasks.add_task")
response = client.post( response = client.post(
"/webhooks/github", "/webhooks/github",
@ -66,3 +83,31 @@ def test_webhook_github_pr_open_synchronize(action: str, mocker: MockerFixture)
git_commit="abcde", git_commit="abcde",
), ),
) )
@pytest.mark.parametrize("action", ["opened", "synchronize"])
def test_webhook_github_pr_unsupported_branch(
action: str, mocker: MockerFixture
) -> None:
mock = mocker.patch("fastapi.BackgroundTasks.add_task")
response = client.post(
"/webhooks/github",
headers={
"X-GitHub-Event": "pull_request",
},
json={
"action": action,
"repository": {"full_name": "oca/mis-builder"},
"pull_request": {
"base": {
"ref": "14.0", # branch 14.0 not declared in .env.test
},
"number": 381,
"head": {
"sha": "abcde",
},
},
},
)
response.raise_for_status()
mock.assert_not_called()