From 79b5fae2ccb999dadd77c8519f2719d59f0ba2ed Mon Sep 17 00:00:00 2001 From: Jonathan Sick Date: Tue, 18 Apr 2023 12:10:49 -0400 Subject: [PATCH] Switch models to safir.github Use safir.github.webhooks for webhook payloads and safir.github.models for the GitHub REST API resources. The githubcheckrun, githubcheckout, and githubtree models persist because they reflect internal models (but they're related to github concepts) --- src/timessquare/domain/githubapi.py | 343 ------------------ src/timessquare/domain/githubcheckout.py | 2 +- src/timessquare/domain/githubcheckrun.py | 8 +- src/timessquare/domain/githubwebhook.py | 166 --------- src/timessquare/handlers/v1/models.py | 6 +- src/timessquare/services/github/repo.py | 20 +- src/timessquare/services/github/webhooks.py | 8 +- .../worker/functions/compute_check_run.py | 2 +- .../worker/functions/create_check_run.py | 2 +- .../functions/create_rerequested_check_run.py | 2 +- .../worker/functions/pull_request_sync.py | 2 +- .../worker/functions/repo_added.py | 8 +- src/timessquare/worker/functions/repo_push.py | 2 +- .../worker/functions/repo_removed.py | 8 +- tests/domain/githubcheckout_test.py | 2 +- tests/domain/githubwebhook_test.py | 101 ------ 16 files changed, 36 insertions(+), 646 deletions(-) delete mode 100644 src/timessquare/domain/githubapi.py delete mode 100644 src/timessquare/domain/githubwebhook.py delete mode 100644 tests/domain/githubwebhook_test.py diff --git a/src/timessquare/domain/githubapi.py b/src/timessquare/domain/githubapi.py deleted file mode 100644 index fd03fac3..00000000 --- a/src/timessquare/domain/githubapi.py +++ /dev/null @@ -1,343 +0,0 @@ -"""Pydantic models describing resources from the GitHub REST v3 API.""" - -from __future__ import annotations - -from base64 import b64decode -from enum import Enum -from typing import List, Optional - -from pydantic import BaseModel, Field, HttpUrl - -__all__ = [ - "GitHubRepoOwnerModel", - "GitHubUserModel", - "GitHubRepositoryModel", - "GitHubPullState", - "GitHubPullRequestModel", - "GitHubBranchCommitModel", - "GitHubBranchModel", - "GitHubBlobModel", - "GitHubCheckSuiteStatus", - "GitHubCheckSuiteConclusion", - "GitHubCheckSuiteModel", - "GitHubCheckRunStatus", - "GitHubCheckRunConclusion", - "GitHubCheckRunAnnotationLevel", - "GitHubCheckSuiteId", - "GitHubCheckRunOutput", - "GitHubCheckRunPrInfoModel", - "GitHubCheckRunModel", -] - - -class GitHubRepoOwnerModel(BaseModel): - """A Pydantic model for the "owner" field found in repository objects. - - https://docs.github.com/en/rest/repos/repos#get-a-repository - """ - - login: str = Field( - title="Login name of the owner (either a user or an organization)" - ) - - -class GitHubUserModel(BaseModel): - """A Pydantic model for the "user" field found in GitHub API resources. - - This contains brief (public) info about a user. - """ - - login: str = Field(title="Login name", description="GitHub username") - - html_url: HttpUrl = Field(description="Homepage for the user on GitHub") - - url: HttpUrl = Field( - description="URL for the user's resource in the GitHub API" - ) - - avatar_url: HttpUrl = Field(description="URL to the user's avatar") - - -class GitHubRepositoryModel(BaseModel): - """A Pydantic model for the "repository" field, often found in webhook - payloads. - - https://docs.github.com/en/rest/repos/repos#get-a-repository - """ - - name: str = Field( - title="Repository name", - description="Excludes owner prefix.", - example="times-square-demo", - ) - - full_name: str = Field( - title="Full name", - description="Includes owner prefix", - example="lsst-sqre/times-square-demo", - ) - - owner: GitHubRepoOwnerModel = Field(title="The repository's owner") - - default_branch: str = Field(title="The default branch", example="main") - - html_url: HttpUrl = Field( - title="URL of the repository for browsers", - example="https://github.com/lsst-sqre/times-square-demo", - ) - - branches_url: str = Field( - title="URI template for the repo's branches endpoint", - example=( - "https://github.com/lsst-sqre/times-square-demo/branches{/branch}" - ), - ) - - contents_url: str = Field( - title="URI template for the contents endpoint", - example=( - "https://github.com/lsst-sqre/times-square-demo/contents/{+path}" - ), - ) - - trees_url: str = Field( - title="URI template for the Git tree API", - example=( - "https://github.com/lsst-sqre/times-square-demo/git/trees{/sha}" - ), - ) - - blobs_url: str = Field( - title="URI template for the Git blobs API", - example=( - "https://github.com/lsst-sqre/times-square-demo/git/blobs{/sha}" - ), - ) - - -class GitHubPullState(str, Enum): - """The state of a GitHub PR. - - https://docs.github.com/en/rest/pulls/pulls#get-a-pull-request - """ - - open = "open" - closed = "closed" - - -class GitHubPullRequestModel(BaseModel): - """A Pydantic model for a GitHub Pull Request. - - This is also the ``pull_request`` field inside the - GitHubPullRequestEventModel. - - https://docs.github.com/en/rest/pulls/pulls#get-a-pull-request - """ - - html_url: HttpUrl = Field(title="Web URL of the PR") - - number: int = Field(title="Pull request number") - - title: str = Field(title="Title") - - state: GitHubPullState = Field( - description="Whether the PR is opened or closed" - ) - - draft: bool = Field(description="True if the PR is a draft") - - merged: bool = Field(description="True if the PR is merged") - - user: GitHubUserModel = Field(description="The user that opened the PR") - - -class GitHubBranchCommitModel(BaseModel): - """A Pydantic model for the commit field found in GitHubBranchModel.""" - - sha: str = Field(title="Git commit SHA") - - url: HttpUrl = Field(title="URL for commit resource") - - -class GitHubBranchModel(BaseModel): - """A Pydantic model for a GitHub branch. - - https://docs.github.com/en/rest/branches/branches#get-a-branch - """ - - name: str = Field(title="Branch name", example="main") - - commit: GitHubBranchCommitModel = Field(title="HEAD commit info") - - -class GitHubBlobModel(BaseModel): - """A Pydantic model for a blob, returned the GitHub blob endpoint - - See https://docs.github.com/en/rest/git/blobs#get-a-blob - """ - - content: str = Field(title="Encoded content") - - encoding: str = Field( - title="Content encoding", description="Typically is base64" - ) - - url: HttpUrl = Field(title="API url of this resource") - - sha: str = Field(title="Git sha of tree object") - - size: int = Field(title="Size of the content in bytes") - - def decode(self) -> str: - """Decode content. - - Currently supports these encodings: - - - base64 - """ - if self.encoding == "base64": - return b64decode(self.content).decode() - else: - raise NotImplementedError( - f"GitHub blob content encoding {self.encoding} " - f"is unknown by GitHubBlobModel for url {self.url}" - ) - - -class GitHubCheckSuiteStatus(str, Enum): - queued = "queued" - in_progress = "in_progress" - completed = "completed" - - -class GitHubCheckSuiteConclusion(str, Enum): - success = "success" - failure = "failure" - neutral = "neutral" - cancelled = "cancelled" - timed_out = "timed_out" - action_required = "action_required" - stale = "stale" - - -class GitHubCheckSuiteModel(BaseModel): - """A Pydantic model for the "check_suite" field in a check_suite webhook - (`GitHubCheckSuiteRequestModel`). - """ - - id: str = Field(description="Identifier for this check run") - - head_branch: str = Field( - title="Head branch", - description="Name of the branch the changes are on.", - ) - - head_sha: str = Field( - title="Head sha", - description="The SHA of the most recent commit for this check suite.", - ) - - url: HttpUrl = Field( - description="GitHub API URL for the check suite resource." - ) - - status: GitHubCheckSuiteStatus - - conclusion: Optional[GitHubCheckSuiteConclusion] - - -class GitHubCheckRunStatus(str, Enum): - """The check run status.""" - - queued = "queued" - in_progress = "in_progress" - completed = "completed" - - -class GitHubCheckRunConclusion(str, Enum): - """The check run conclusion state.""" - - success = "success" - failure = "failure" - neutral = "neutral" - cancelled = "cancelled" - timed_out = "timed_out" - action_required = "action_required" - stale = "stale" - - -class GitHubCheckRunAnnotationLevel(str, Enum): - """The level of a check run output annotation.""" - - notice = "notice" - warning = "warning" - failure = "failure" - - -class GitHubCheckSuiteId(BaseModel): - """Brief information about a check suite in the `GitHubCheckRunModel`.""" - - id: str = Field(description="Check suite ID") - - -class GitHubCheckRunOutput(BaseModel): - """Check run output report.""" - - title: Optional[str] = Field(None, description="Title of the report") - - summary: Optional[str] = Field( - None, description="Summary information (markdown formatted" - ) - - text: Optional[str] = Field(None, description="Extended report (markdown)") - - -class GitHubCheckRunPrInfoModel(BaseModel): - """A Pydantic model of the "pull_requsts[]" items in a check run - GitHub API model. - - https://docs.github.com/en/rest/checks/runs#get-a-check-run - """ - - url: HttpUrl = Field(description="GitHub API URL for this pull request") - - -class GitHubCheckRunModel(BaseModel): - """A Pydantic model for the "check_run" field in a check_run webhook - payload (`GitHubCheckRunPayloadModel`). - """ - - id: str = Field(description="Identifier for this check run") - - external_id: Optional[str] = Field( - description="Identifier set by the check runner." - ) - - head_sha: str = Field( - title="Head sha", - description="The SHA of the most recent commit for this check suite.", - ) - - status: GitHubCheckRunStatus = Field( - description="Status of the check run." - ) - - conclusion: Optional[GitHubCheckRunConclusion] = Field( - description="Conclusion status, if completed." - ) - - name: str = Field(description="Name of the check run.") - - url: HttpUrl = Field(description="URL of the check run API resource.") - - html_url: HttpUrl = Field(description="URL of the check run webpage.") - - check_suite: GitHubCheckSuiteId - - output: Optional[GitHubCheckRunOutput] = Field( - None, title="Output", description="Check run output, if available." - ) - - pull_requests: List[GitHubCheckRunPrInfoModel] = Field( - default_factory=list - ) diff --git a/src/timessquare/domain/githubcheckout.py b/src/timessquare/domain/githubcheckout.py index 04fc4fc1..9d4ef206 100644 --- a/src/timessquare/domain/githubcheckout.py +++ b/src/timessquare/domain/githubcheckout.py @@ -12,8 +12,8 @@ import yaml from gidgethub.httpx import GitHubAPI from pydantic import BaseModel, EmailStr, Field, HttpUrl, root_validator +from safir.github.models import GitHubBlobModel, GitHubRepositoryModel -from .githubapi import GitHubBlobModel, GitHubRepositoryModel from .page import PageParameterSchema, PersonModel diff --git a/src/timessquare/domain/githubcheckrun.py b/src/timessquare/domain/githubcheckrun.py index 82327eb1..270a6a95 100644 --- a/src/timessquare/domain/githubcheckrun.py +++ b/src/timessquare/domain/githubcheckrun.py @@ -9,10 +9,7 @@ from gidgethub.httpx import GitHubAPI from pydantic import ValidationError - -from timessquare.config import config - -from .githubapi import ( +from safir.github.models import ( GitHubBlobModel, GitHubCheckRunAnnotationLevel, GitHubCheckRunConclusion, @@ -20,6 +17,9 @@ GitHubCheckRunStatus, GitHubRepositoryModel, ) + +from timessquare.config import config + from .githubcheckout import ( GitHubRepositoryCheckout, NotebookSidecarFile, diff --git a/src/timessquare/domain/githubwebhook.py b/src/timessquare/domain/githubwebhook.py deleted file mode 100644 index 79e38138..00000000 --- a/src/timessquare/domain/githubwebhook.py +++ /dev/null @@ -1,166 +0,0 @@ -"""Domain models related to payloads from GitHub webhook events. - -These Pydantic models are designed to capture relevant subsets of data from -GitHub webhook payloads. -""" - -from __future__ import annotations - -from typing import List - -from pydantic import BaseModel, Field - -from .githubapi import ( - GitHubCheckRunModel, - GitHubCheckSuiteModel, - GitHubPullRequestModel, - GitHubRepositoryModel, -) - - -class GitHubAppInstallationModel(BaseModel): - """A Pydantic model for the "installation" field found in webhook payloads - for GitHub Apps. - """ - - id: str = Field(title="Installation ID") - - -class GitHubPushEventModel(BaseModel): - """A Pydantic model for the push event webhook.""" - - repository: GitHubRepositoryModel - - installation: GitHubAppInstallationModel - - ref: str = Field( - title="The full git_ref that was pushed.", - description=( - "The full git ref that was pushed. Example: refs/heads/main or " - "refs/tags/v3.14.1." - ), - example="refs/heads/main", - ) - - before: str = Field( - title="The SHA of the most recent commit on ref before the push." - ) - - after: str = Field( - title="The SHA of the most recent commit on ref after the push." - ) - - -class AppInstallationRepoModel(BaseModel): - """A pydantic model for repository objects used by - `GitHubAppInstallationRepositoriesEventModel`. - """ - - name: str - - full_name: str - - @property - def owner_name(self) -> str: - return self.full_name.split("/")[0] - - -class GitHubAppInstallationEventModel(BaseModel): - """A Pydantic model for an "installation" webhook. - - https://docs.github.com/en/developers/webhooks-and-events/webhooks/webhook-events-and-payloads#installation - """ - - action: str = Field( - title="Action performed", description="Either 'added' or 'removed'." - ) - - repositories: List[AppInstallationRepoModel] = Field( - title="Repositories accessible to this installation" - ) - - installation: GitHubAppInstallationModel - - -class GitHubAppInstallationRepositoriesEventModel(BaseModel): - """A Pydantic model for a "installation_repositories" webhook. - - https://docs.github.com/en/developers/webhooks-and-events/webhooks/webhook-events-and-payloads#installation_repositories - """ - - action: str = Field( - title="Action performed", description="Either 'added' or 'removed'." - ) - - repositories_added: List[AppInstallationRepoModel] = Field( - title="Repositories added" - ) - - repositories_removed: List[AppInstallationRepoModel] = Field( - title="Repositories removed" - ) - - installation: GitHubAppInstallationModel - - -class GitHubPullRequestEventModel(BaseModel): - """A Pydantic model for a "pull_request" webhook. - - https://docs.github.com/en/developers/webhooks-and-events/webhooks/webhook-events-and-payloads#pull_request - """ - - repository: GitHubRepositoryModel - - installation: GitHubAppInstallationModel - - action: str = Field( - title="The action that was performed.", - description=( - "Many event types are possible. The most relevant to Times Square " - "are ``opened`` and ``synchronize`` (when the head branch is " - "updated)." - ), - ) - - number: int = Field(title="Pull request number") - - pull_request: GitHubPullRequestModel - - -class GitHubCheckSuiteEventModel(BaseModel): - """A Pydantic model for the "check_suite" webhook payload. - - https://docs.github.com/en/developers/webhooks-and-events/webhooks/webhook-events-and-payloads#check_suite - """ - - action: str = Field( - title="Action performed", - description="Either requested or rerequested.", - ) - - check_suite: GitHubCheckSuiteModel - - repository: GitHubRepositoryModel - - installation: GitHubAppInstallationModel - - -class GitHubCheckRunEventModel(BaseModel): - """A Pydantic model for the "check_run" webhook payload. - - https://docs.github.com/en/developers/webhooks-and-events/webhooks/webhook-events-and-payloads#check_run - """ - - action: str = Field( - title="The action that was performed.", - description=( - "Many event types are possible: created, completed, rerequested, " - "rerequested_action" - ), - ) - - repository: GitHubRepositoryModel - - installation: GitHubAppInstallationModel - - check_run: GitHubCheckRunModel diff --git a/src/timessquare/handlers/v1/models.py b/src/timessquare/handlers/v1/models.py index 678366cc..d28dfd57 100644 --- a/src/timessquare/handlers/v1/models.py +++ b/src/timessquare/handlers/v1/models.py @@ -10,15 +10,15 @@ from fastapi import Request from markdown_it import MarkdownIt from pydantic import AnyHttpUrl, BaseModel, EmailStr, Field, HttpUrl -from safir.metadata import Metadata as SafirMetadata - -from timessquare.domain.githubapi import ( +from safir.github.models import ( GitHubCheckRunConclusion, GitHubCheckRunModel, GitHubCheckRunStatus, GitHubPullRequestModel, GitHubPullState, ) +from safir.metadata import Metadata as SafirMetadata + from timessquare.domain.githubtree import GitHubNode, GitHubNodeType from timessquare.domain.nbhtml import NbHtmlModel from timessquare.domain.page import PageModel, PageSummaryModel, PersonModel diff --git a/src/timessquare/services/github/repo.py b/src/timessquare/services/github/repo.py index d062a156..376a1e13 100644 --- a/src/timessquare/services/github/repo.py +++ b/src/timessquare/services/github/repo.py @@ -14,11 +14,7 @@ from gidgethub.httpx import GitHubAPI from httpx import AsyncClient from safir.github import GitHubAppClientFactory -from safir.slack.blockkit import SlackException -from structlog.stdlib import BoundLogger - -from timessquare.config import config -from timessquare.domain.githubapi import ( +from safir.github.models import ( GitHubBlobModel, GitHubBranchModel, GitHubCheckRunConclusion, @@ -26,6 +22,15 @@ GitHubPullRequestModel, GitHubRepositoryModel, ) +from safir.github.webhooks import ( + GitHubCheckRunEventModel, + GitHubCheckSuiteEventModel, + GitHubPushEventModel, +) +from safir.slack.blockkit import SlackException +from structlog.stdlib import BoundLogger + +from timessquare.config import config from timessquare.domain.githubcheckout import ( GitHubRepositoryCheckout, RepositoryNotebookModel, @@ -35,11 +40,6 @@ GitHubConfigsCheck, NotebookExecutionsCheck, ) -from timessquare.domain.githubwebhook import ( - GitHubCheckRunEventModel, - GitHubCheckSuiteEventModel, - GitHubPushEventModel, -) from timessquare.domain.noteburst import NoteburstJobStatus from timessquare.domain.page import PageExecutionInfo, PageModel diff --git a/src/timessquare/services/github/webhooks.py b/src/timessquare/services/github/webhooks.py index 3a670f23..47042eec 100644 --- a/src/timessquare/services/github/webhooks.py +++ b/src/timessquare/services/github/webhooks.py @@ -7,10 +7,7 @@ from gidgethub.routing import Router from gidgethub.sansio import Event from safir.arq import ArqQueue -from structlog.stdlib import BoundLogger - -from timessquare.config import config -from timessquare.domain.githubwebhook import ( +from safir.github.webhooks import ( GitHubAppInstallationEventModel, GitHubAppInstallationRepositoriesEventModel, GitHubCheckRunEventModel, @@ -18,6 +15,9 @@ GitHubPullRequestEventModel, GitHubPushEventModel, ) +from structlog.stdlib import BoundLogger + +from timessquare.config import config __all__ = ["router"] diff --git a/src/timessquare/worker/functions/compute_check_run.py b/src/timessquare/worker/functions/compute_check_run.py index 18af1455..18169af8 100644 --- a/src/timessquare/worker/functions/compute_check_run.py +++ b/src/timessquare/worker/functions/compute_check_run.py @@ -2,7 +2,7 @@ from typing import Any, Dict -from timessquare.domain.githubwebhook import GitHubCheckRunEventModel +from safir.github.webhooks import GitHubCheckRunEventModel async def compute_check_run( diff --git a/src/timessquare/worker/functions/create_check_run.py b/src/timessquare/worker/functions/create_check_run.py index 96f0816f..fc9cbab4 100644 --- a/src/timessquare/worker/functions/create_check_run.py +++ b/src/timessquare/worker/functions/create_check_run.py @@ -3,8 +3,8 @@ from typing import Any, Dict from safir.dependencies.db_session import db_session_dependency +from safir.github.webhooks import GitHubCheckSuiteEventModel -from timessquare.domain.githubwebhook import GitHubCheckSuiteEventModel from timessquare.worker.servicefactory import create_github_repo_service diff --git a/src/timessquare/worker/functions/create_rerequested_check_run.py b/src/timessquare/worker/functions/create_rerequested_check_run.py index 93e2f473..d65dde18 100644 --- a/src/timessquare/worker/functions/create_rerequested_check_run.py +++ b/src/timessquare/worker/functions/create_rerequested_check_run.py @@ -3,8 +3,8 @@ from typing import Any, Dict from safir.dependencies.db_session import db_session_dependency +from safir.github.webhooks import GitHubCheckRunEventModel -from timessquare.domain.githubwebhook import GitHubCheckRunEventModel from timessquare.worker.servicefactory import create_github_repo_service diff --git a/src/timessquare/worker/functions/pull_request_sync.py b/src/timessquare/worker/functions/pull_request_sync.py index aa25836c..9dfae3ef 100644 --- a/src/timessquare/worker/functions/pull_request_sync.py +++ b/src/timessquare/worker/functions/pull_request_sync.py @@ -3,8 +3,8 @@ from typing import Any, Dict from safir.dependencies.db_session import db_session_dependency +from safir.github.webhooks import GitHubPullRequestEventModel -from timessquare.domain.githubwebhook import GitHubPullRequestEventModel from timessquare.worker.servicefactory import create_github_repo_service diff --git a/src/timessquare/worker/functions/repo_added.py b/src/timessquare/worker/functions/repo_added.py index 8cf0ba15..3ba9bea9 100644 --- a/src/timessquare/worker/functions/repo_added.py +++ b/src/timessquare/worker/functions/repo_added.py @@ -3,12 +3,12 @@ from typing import Any, Dict, Union from safir.dependencies.db_session import db_session_dependency - -from timessquare.domain.githubwebhook import ( - AppInstallationRepoModel, +from safir.github.webhooks import ( GitHubAppInstallationEventModel, + GitHubAppInstallationEventRepoModel, GitHubAppInstallationRepositoriesEventModel, ) + from timessquare.worker.servicefactory import create_github_repo_service @@ -19,7 +19,7 @@ async def repo_added( GitHubAppInstallationRepositoriesEventModel, GitHubAppInstallationEventModel, ], - repo: AppInstallationRepoModel, + repo: GitHubAppInstallationEventRepoModel, ) -> str: """Process repo_added queue tasks, triggered by Times Square app installation events on GitHub. diff --git a/src/timessquare/worker/functions/repo_push.py b/src/timessquare/worker/functions/repo_push.py index 2e94b402..3dbe74f5 100644 --- a/src/timessquare/worker/functions/repo_push.py +++ b/src/timessquare/worker/functions/repo_push.py @@ -3,8 +3,8 @@ from typing import Any, Dict from safir.dependencies.db_session import db_session_dependency +from safir.github.webhooks import GitHubPushEventModel -from timessquare.domain.githubwebhook import GitHubPushEventModel from timessquare.worker.servicefactory import create_github_repo_service diff --git a/src/timessquare/worker/functions/repo_removed.py b/src/timessquare/worker/functions/repo_removed.py index 15ebf122..486c08f4 100644 --- a/src/timessquare/worker/functions/repo_removed.py +++ b/src/timessquare/worker/functions/repo_removed.py @@ -3,12 +3,12 @@ from typing import Any, Dict, Union from safir.dependencies.db_session import db_session_dependency - -from timessquare.domain.githubwebhook import ( - AppInstallationRepoModel, +from safir.github.webhooks import ( GitHubAppInstallationEventModel, + GitHubAppInstallationEventRepoModel, GitHubAppInstallationRepositoriesEventModel, ) + from timessquare.worker.servicefactory import create_page_service @@ -19,7 +19,7 @@ async def repo_removed( GitHubAppInstallationRepositoriesEventModel, GitHubAppInstallationEventModel, ], - repo: AppInstallationRepoModel, + repo: GitHubAppInstallationEventRepoModel, ) -> str: """Process repo_removed queue tasks, triggered by Times Square app installation events on GitHub. diff --git a/tests/domain/githubcheckout_test.py b/tests/domain/githubcheckout_test.py index daaa22a5..fd580ef7 100644 --- a/tests/domain/githubcheckout_test.py +++ b/tests/domain/githubcheckout_test.py @@ -10,8 +10,8 @@ import respx from gidgethub.httpx import GitHubAPI from httpx import Response +from safir.github.models import GitHubBlobModel -from timessquare.domain.githubapi import GitHubBlobModel from timessquare.domain.githubcheckout import ( GitHubRepositoryCheckout, GitTreeMode, diff --git a/tests/domain/githubwebhook_test.py b/tests/domain/githubwebhook_test.py deleted file mode 100644 index 182cb7a2..00000000 --- a/tests/domain/githubwebhook_test.py +++ /dev/null @@ -1,101 +0,0 @@ -"""Tests for Github webhook domain models.""" - -from __future__ import annotations - -from pathlib import Path - -from timessquare.domain.githubapi import ( - GitHubCheckRunStatus, - GitHubCheckSuiteConclusion, - GitHubCheckSuiteStatus, -) -from timessquare.domain.githubwebhook import ( - GitHubAppInstallationEventModel, - GitHubAppInstallationRepositoriesEventModel, - GitHubCheckRunEventModel, - GitHubCheckSuiteEventModel, - GitHubPullRequestEventModel, - GitHubPushEventModel, -) - - -def test_push_event() -> None: - data_path = Path(__file__).parent.joinpath( - "../data/github_webhooks/push_event.json" - ) - data = GitHubPushEventModel.parse_raw(data_path.read_text()) - - assert data.ref == "refs/tags/simple-tag" - assert data.repository.name == "Hello-World" - - -def test_installation_event() -> None: - data_path = Path(__file__).parent.joinpath( - "../data/github_webhooks/installation.json" - ) - data = GitHubAppInstallationEventModel.parse_raw(data_path.read_text()) - - assert data.action == "deleted" - assert data.repositories[0].name == "Hello-World" - assert data.repositories[0].owner_name == "octocat" - - -def test_installation_repositories_event() -> None: - data_path = Path(__file__).parent.joinpath( - "../data/github_webhooks/installation_repositories.json" - ) - data = GitHubAppInstallationRepositoriesEventModel.parse_raw( - data_path.read_text() - ) - - assert data.action == "added" - assert data.repositories_added[0].name == "Space" - assert data.repositories_added[0].owner_name == "Codertocat" - - -def test_pull_request_event() -> None: - data_path = Path(__file__).parent.joinpath( - "../data/github_webhooks/pull_request_event.json" - ) - data = GitHubPullRequestEventModel.parse_raw(data_path.read_text()) - - assert data.number == 2 - assert data.action == "opened" - assert data.pull_request.number == 2 - assert data.pull_request.title == "Update the README with new information." - - -def test_check_suite_completed_event() -> None: - data_path = Path(__file__).parent.joinpath( - "../data/github_webhooks/check_suite_completed.json" - ) - data = GitHubCheckSuiteEventModel.parse_raw(data_path.read_text()) - - assert data.action == "completed" - assert data.check_suite.id == "118578147" - assert data.check_suite.head_branch == "changes" - assert data.check_suite.head_sha == ( - "ec26c3e57ca3a959ca5aad62de7213c562f8c821" - ) - assert data.check_suite.status == GitHubCheckSuiteStatus.completed - assert data.check_suite.conclusion == GitHubCheckSuiteConclusion.success - - -def test_check_run_created_event() -> None: - data_path = Path(__file__).parent.joinpath( - "../data/github_webhooks/check_run_created.json" - ) - data = GitHubCheckRunEventModel.parse_raw(data_path.read_text()) - - assert data.action == "created" - assert data.check_run.id == "128620228" - assert data.check_run.external_id == "" - assert data.check_run.url == ( - "https://api.github.com/repos/Codertocat/Hello-World" - "/check-runs/128620228" - ) - assert data.check_run.html_url == ( - "https://github.com/Codertocat/Hello-World/runs/128620228" - ) - assert data.check_run.status == GitHubCheckRunStatus.queued - assert data.check_run.check_suite.id == "118578147"