Skip to content

tests: improve github integration test coverage #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Jan 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions .github/workflows/build_test_and_lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
name: Build Test and Lint

on:
push:
branches:
- main
pull_request:

jobs:
build-test-and-lint:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2

- name: Cache Docker layers
uses: actions/cache@v3
with:
path: /tmp/.buildx-cache
key: ${{ runner.os }}-buildx-${{ github.sha }}
restore-keys: |
${{ runner.os }}-buildx-

- name: Build Docker image
run: |
make docker/build

- name: Run Docker container for tests
run: |
docker run --rm \
-e DJANGO_SETTINGS_MODULE=intbot.settings \
-e DATABASE_URL=postgres://testuser:testpassword@localhost:5432/testdb \
--network host \
intbot \
make in-container/tests

- name: Run Docker container for lint
run: |
docker run --rm intbot make ci/lint
docker run --rm intbot make ci/type-check

services:
postgres:
image: postgres:16.4
env:
POSTGRES_USER: intbot_user
POSTGRES_PASSWORD: intbot_password
POSTGRES_DB: intbot_database_test
ports:
- 14672:5432
options: >-
--health-cmd="pg_isready -U intbot_user -d intbot_database_test"
--health-interval=10s
--health-timeout=5s
--health-retries=5

20 changes: 20 additions & 0 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: Deploy latest version of the app

on:
push:
branches:
- main

jobs:
deploy:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Install uv
uses: astral-sh/setup-uv@v5

- name: Run deployment
run: make deploy/app
22 changes: 21 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ UV_RUN_DEV=cd intbot && DJANGO_ENV="dev" uv run

# Docker
DOCKER_RUN_WITH_PORT=docker run -p 4672:4672 --add-host=host.internal:host-gateway -e DJANGO_ENV="local_container" -it intbot:$(V)
DOCKER_RUN=docker run --add-host=host.internal:host-gateway -e DJANGO_ENV="test" -it intbot:$(V)
MANAGE=cd intbot && ./manage.py
# In container we run with migrations
CONTAINER_TEST_CMD=DJANGO_SETTINGS_MODULE="intbot.settings" DJANGO_ENV="test" pytest --migrations
CI_RUN=cd intbot && DJANGO_SETTINGS_MODULE="intbot.settings" DJANGO_ENV="ci"

# Deployment
DEPLOY_CMD=cd deploy && uvx --from "ansible-core" ansible-playbook -i hosts.yml
Expand Down Expand Up @@ -108,16 +112,32 @@ in-container/migrate:
in-container/manage:
$(MANAGE) $(ARG)

in-container/tests:
$(CONTAINER_TEST_CMD) -vvv

ci/lint:
$(CI_RUN) ruff check .

ci/type-check:
$(CI_RUN) mypy intbot


# Docker management targets
# =========================

docker/build:
docker build . -t intbot:$(current_git_hash)
docker build . -t intbot:$(current_git_hash) -t intbot:latest

docker/run/gunicorn:
$(DOCKER_RUN_WITH_PORT) make in-container/gunicorn

docker/run/tests:
$(DOCKER_RUN) make in-container/tests

docker/run/lint:
$(DOCKER_RUN) make ci/lint
$(DOCKER_RUN) make ci/type-check


# Deploymenet targets
# ====================
Expand Down
21 changes: 12 additions & 9 deletions intbot/core/integrations/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ def fetch_github_item_details(item_id):


class GithubProjectV2Item:
# NOTE: This might be something for pydantic schemas in the future

def __init__(self, content: dict):
self.content = content

Expand All @@ -107,30 +109,30 @@ def content_type(self):
return self.content["projects_v2_item"]["content_type"]

def node_id(self):
# NOTE(artcz): This is more relevant, because of how the graphql query
# above is constructed.
# NOTE(artcz): This is relevant, because of how the graphql query above
# is constructed.
# Using node_id, which is an id of a ProjectV2Item we can get both
# DraftIssue and Issue from one query.
# If we use the content_node_id we need to adjust the query as that ID
# points us directly either an Issue or DraftIssue
# If we use the content_node_id we would probably need two separate
# ways of getting that data.
return self.content["projects_v2_item"]["node_id"]

def content_node_id(self):
return self.content["projects_v2_item"]["content_node_id"]

def changes(self) -> dict:
if "changes" in self.content:
fv = self.content["changes"]["field_value"]
field_name = fv["field_name"]
field_type = fv["field_type"]

if field_type == "date":
changed_from = (
fv["from"].split("T")[0] if fv["from"] is not None else "None"
)
changed_to = fv["to"].split("T")[0] if fv["to"] is not None else "None"

elif field_type == "single_select":
changed_from = fv["from"]["name"] if fv["from"] is not None else "None"
changed_to = fv["to"]["name"] if fv["to"] is not None else "None"

else:
changed_from = "None"
changed_to = "None"
Expand All @@ -152,8 +154,9 @@ def as_discord_message(self, github_object: GithubDraftIssue | GithubIssue) -> s
changes = self.changes()

if changes:
details = "**{field}** of **{obj}** from **{from}** to **{to}**".format
details = details(**{"obj": github_object.as_discord_message(), **changes})
details = "**{field}** of **{obj}** from **{from}** to **{to}**".format(
**{"obj": github_object.as_discord_message(), **changes}
)

else:
details = github_object.as_discord_message()
Expand Down
7 changes: 7 additions & 0 deletions intbot/intbot/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import os
import warnings
from typing import Any
from pathlib import Path

# Build paths inside the project like this: BASE_DIR / 'subdir'.
Expand Down Expand Up @@ -112,6 +113,9 @@
DJANGO_ENV = os.environ["DJANGO_ENV"]
APP_VERSION = os.environ.get("APP_VERSION", "latest")[:8]

# Just to make mypy happy
TASKS: dict[str, Any]

if DJANGO_ENV == "dev":
DEBUG = True
ALLOWED_HOSTS = ["127.0.0.1", "localhost"]
Expand Down Expand Up @@ -306,5 +310,8 @@ def warn_if_missing(name, default=""):
# Currently used only for collecting staticfiles in docker
DEBUG = False

elif DJANGO_ENV == "ci":
DEBUG = False

else:
raise ValueError(f"Unsupported DJANGO_ENV `{DJANGO_ENV}`")
Loading
Loading