Skip to content

Commit

Permalink
Upgrade githubkit to latest version
Browse files Browse the repository at this point in the history
  • Loading branch information
tcarmet committed Mar 18, 2024
1 parent 13d7daf commit ff65b77
Show file tree
Hide file tree
Showing 21 changed files with 159 additions and 97 deletions.
73 changes: 56 additions & 17 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 1 addition & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ google-cloud-compute = "^1.17.0"
boto3 = "^1.28.85"
botocore = "^1.31.85"
boto3-stubs = { extras = ["ec2"], version = "^1.34.54" }
githubkit = { extras = [
"auth-app",
], git = "https://github.com/yanyongyu/githubkit", rev = "a4275ac3d3babd64061f3693353db740e6a8e892" }
githubkit = { extras = ["auth-app"], version = "^0.11.2" }
rq-scheduler = "^0.13.1"


Expand Down
2 changes: 1 addition & 1 deletion runner_manager/backend/aws.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from boto3 import client
from botocore.exceptions import ClientError
from githubkit.webhooks.types import WorkflowJobEvent
from githubkit.versions.latest.webhooks import WorkflowJobEvent
from mypy_boto3_ec2 import EC2Client
from pydantic import Field
from redis_om import NotFoundError
Expand Down
2 changes: 1 addition & 1 deletion runner_manager/backend/base.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import List, Literal, Optional

from githubkit.webhooks.types import WorkflowJobEvent
from githubkit.versions.latest.webhooks import WorkflowJobEvent
from pydantic import BaseModel, Field
from redis_om import NotFoundError

Expand Down
2 changes: 1 addition & 1 deletion runner_manager/backend/docker.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from docker import DockerClient
from docker.errors import APIError, NotFound
from docker.models.containers import Container
from githubkit.webhooks.types import WorkflowJobEvent
from githubkit.versions.latest.webhooks import WorkflowJobEvent
from pydantic import Field
from redis_om import NotFoundError

Expand Down
2 changes: 1 addition & 1 deletion runner_manager/backend/gcloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import time
from typing import List, Literal, MutableMapping, Optional

from githubkit.webhooks.types import WorkflowJobEvent
from githubkit.versions.latest.webhooks import WorkflowJobEvent
from google.api_core.exceptions import BadRequest, NotFound
from google.api_core.extended_operation import ExtendedOperation
from google.cloud.compute import (
Expand Down
29 changes: 8 additions & 21 deletions runner_manager/clients/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,18 @@
"""


from datetime import datetime
from functools import cached_property
from typing import Dict, List, Literal, Optional
from typing import Dict, List, Optional

from githubkit import GitHub as GitHubKit
from githubkit.compat import GitHubModel as GitHubRestModel
from githubkit.response import Response
from githubkit.rest import RestNamespace as RestNamespaceKit
from githubkit.rest.actions import ActionsClient as ActionsClientKit
from githubkit.rest.models import GitHubRestModel
from githubkit.utils import UNSET, Missing, exclude_unset
from githubkit.webhooks.models import GitHubWebhookModel
from pydantic import Field
from githubkit.typing import Missing
from githubkit.utils import UNSET, exclude_unset
from githubkit.versions.v2022_11_28.rest import RestNamespace as RestNamespaceKit
from githubkit.versions.v2022_11_28.rest.actions import (
ActionsClient as ActionsClientKit,
)


class RunnerGroup(GitHubRestModel):
Expand All @@ -43,18 +42,6 @@ class OrgsOrgActionsRunnerGroupsGetResponse200(GitHubRestModel):
runner_groups: List[RunnerGroup]


# Missing methods from githubkit.webhooks.models
class WorkflowStepQueued(GitHubWebhookModel):
"""Workflow Step (Queued)"""

name: str = Field(default=...)
status: Literal["queued"] = Field(default=...)
conclusion: None = Field(default=...)
number: int = Field(default=...)
started_at: datetime = Field(default=...)
completed_at: None = Field(default=...)


class ActionsClient(ActionsClientKit):
def get_self_hosted_runner_group_for_org(
self,
Expand Down
7 changes: 7 additions & 0 deletions runner_manager/dependencies.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from datetime import timedelta

Check failure on line 1 in runner_manager/dependencies.py

View workflow job for this annotation

GitHub Actions / Trunk Check

ruff(F401)

[new] `datetime.timedelta` imported but unused

Check failure on line 1 in runner_manager/dependencies.py

View workflow job for this annotation

GitHub Actions / Trunk Check

black

Incorrect formatting, autoformat by running 'trunk fmt'
from functools import lru_cache

import httpx
from githubkit.config import Config
from githubkit.retry import RetryRateLimit
from redis import Redis
from redis_om import get_redis_connection
from rq import Queue
Expand Down Expand Up @@ -37,11 +39,16 @@ def get_scheduler() -> Scheduler:
@lru_cache()
def get_github() -> GitHub:
settings: Settings = get_settings()
auto_retry = RetryRateLimit(
max_retry=3
) if settings.github_auto_retry else None
config: Config = Config(
base_url=httpx.URL(str(settings.github_base_url)),
follow_redirects=True,
accept="*/*",
user_agent="runner-manager",
timeout=httpx.Timeout(30.0),
http_cache=True,
auto_retry=auto_retry,
)
return GitHub(settings.github_auth_strategy(), config=config)
27 changes: 19 additions & 8 deletions runner_manager/jobs/workflow_job.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
from __future__ import annotations

Check failure on line 1 in runner_manager/jobs/workflow_job.py

View workflow job for this annotation

GitHub Actions / Trunk Check

isort

Incorrect formatting, autoformat by running 'trunk fmt'

Check failure on line 1 in runner_manager/jobs/workflow_job.py

View workflow job for this annotation

GitHub Actions / Trunk Check

black

Incorrect formatting, autoformat by running 'trunk fmt'

import logging
from datetime import timedelta

from githubkit.webhooks.models import (
WorkflowJobCompleted,
WorkflowJobInProgress,
WorkflowJobQueued,
from datetime import timedelta, datetime

from githubkit.versions.latest.models import (
WebhookWorkflowJobCompleted as WorkflowJobCompleted,
)
from githubkit.versions.latest.models import (
WebhookWorkflowJobInProgress as WorkflowJobInProgress,
)
from githubkit.versions.latest.models import (
WebhookWorkflowJobQueued as WorkflowJobQueued,
)
from githubkit.webhooks.types import WorkflowJobEvent
from githubkit.versions.latest.webhooks import WorkflowJobEvent

from runner_manager import Settings
from runner_manager.clients.github import GitHub
Expand All @@ -27,9 +32,15 @@ def log_workflow_job(webhook: WorkflowJobEvent) -> None:
)


def time_to_start(webhook: WorkflowJobInProgress | WorkflowJobCompleted) -> timedelta:
def time_to_start(webhook: WorkflowJobEvent) -> timedelta:
"""From a given webhook, calculate the time it took to start the job"""
return webhook.workflow_job.started_at - webhook.workflow_job.created_at

assert isinstance(webhook.workflow_job.started_at, datetime)
assert isinstance(webhook.workflow_job.created_at, datetime)
return (
webhook.workflow_job.started_at - webhook.workflow_job.created_at
)



def completed(webhook: WorkflowJobCompleted) -> int:
Expand Down
19 changes: 9 additions & 10 deletions runner_manager/models/runner.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import logging
from datetime import datetime, timedelta, timezone
from enum import Enum
from typing import Dict, List, Literal, Optional
from typing import Dict, List, Literal, Optional, TypedDict

Check failure on line 4 in runner_manager/models/runner.py

View workflow job for this annotation

GitHub Actions / Trunk Check

ruff(F401)

[new] `typing.TypedDict` imported but unused

import redis
from githubkit.exception import RequestFailed
from githubkit.rest.models import Runner as GitHubRunner
from githubkit.rest.types import OrgsOrgActionsRunnersGenerateJitconfigPostBodyType
from githubkit.webhooks.types import WorkflowJobEvent
from githubkit.versions.latest.models import Runner as GitHubRunner
from githubkit.versions.latest.webhooks import WorkflowJobEvent
from pydantic import BaseModel as PydanticBaseModel
from redis_om import Field, NotFoundError

Expand Down Expand Up @@ -70,7 +69,7 @@ class Runner(BaseModel):
labels: List[RunnerLabel] = []
organization: str = Field(default=None, index=True, description="Organization name")
created_at: Optional[datetime]
started_at: Optional[datetime]
started_at: Optional[datetime | str]

def __str__(self):
return f"{self.name} (status: {self.status}, busy: {self.busy})"
Expand Down Expand Up @@ -182,11 +181,11 @@ def generate_jit_config(self, github: GitHub) -> "Runner":
assert self.runner_group_id is not None, "Runner group id is required"
jitconfig = github.rest.actions.generate_runner_jitconfig_for_org(
org=self.organization,
data=OrgsOrgActionsRunnersGenerateJitconfigPostBodyType(
name=self.name,
runner_group_id=self.runner_group_id,
labels=[label.name for label in self.labels],
),
data={
"name": self.name,
"runner_group_id": self.runner_group_id,
"labels": [label.name for label in self.labels],
},
).parsed_data
self.id = jitconfig.runner.id
self.encoded_jit_config = jitconfig.encoded_jit_config
Expand Down
6 changes: 4 additions & 2 deletions runner_manager/models/runner_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
import redis
from githubkit import Response
from githubkit.exception import RequestFailed
from githubkit.webhooks.models import WorkflowJobInProgress
from githubkit.webhooks.types import WorkflowJobEvent
from githubkit.versions.latest.models import (
WebhookWorkflowJobInProgress as WorkflowJobInProgress,
)
from githubkit.versions.latest.webhooks import WorkflowJobEvent
from pydantic import BaseModel as PydanticBaseModel
from pydantic import Field as PydanticField
from pydantic import validator
Expand Down
1 change: 1 addition & 0 deletions runner_manager/models/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class Settings(BaseSettings):
github_installation_id: int = 0
github_client_id: Optional[str] = None
github_client_secret: SecretStr = SecretStr("")
github_auto_retry: bool = True

@property
def app_install(self) -> bool:
Expand Down
5 changes: 3 additions & 2 deletions runner_manager/models/webhook.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from typing import Union

from githubkit.webhooks.types import PingEvent, WorkflowJobEvent
from githubkit.versions.latest.models import WebhookPing
from githubkit.versions.latest.webhooks import WorkflowJobEvent
from pydantic import BaseModel


Expand All @@ -10,4 +11,4 @@ class WebhookResponse(BaseModel):
job_id: str | None = None


AcceptedWebhookEvents = Union[WorkflowJobEvent, PingEvent]
AcceptedWebhookEvents = Union[WorkflowJobEvent, WebhookPing]
4 changes: 2 additions & 2 deletions runner_manager/routers/webhook.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from typing import Annotated, Set

from fastapi import APIRouter, Depends, Header, HTTPException, Security
from githubkit.versions.latest.models import WebhookPing
from githubkit.webhooks import verify
from githubkit.webhooks.types import PingEvent
from rq import Queue

from runner_manager.dependencies import get_queue, get_settings
Expand Down Expand Up @@ -43,7 +43,7 @@ def post(
valid: bool = Security(validate_webhook),
queue: Queue = Depends(get_queue),
) -> WebhookResponse:
if not isinstance(webhook, PingEvent):
if not isinstance(webhook, WebhookPing):
action = webhook.action
else:
action = None
Expand Down
2 changes: 1 addition & 1 deletion tests/api/test_webhook_router.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from functools import lru_cache

Check failure on line 1 in tests/api/test_webhook_router.py

View workflow job for this annotation

GitHub Actions / Trunk Check

black

Incorrect formatting, autoformat by running 'trunk fmt'

Check failure on line 1 in tests/api/test_webhook_router.py

View workflow job for this annotation

GitHub Actions / Trunk Check

isort

Incorrect formatting, autoformat by running 'trunk fmt'

from githubkit.webhooks import sign
from githubkit.webhooks.models import WorkflowJobCompleted
from githubkit.versions.latest.models import WebhookWorkflowJobCompleted as WorkflowJobCompleted
from hypothesis import given
from pytest import fixture

Expand Down
2 changes: 2 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ def github(settings, monkeypatch) -> GitHub:
user_agent="runner-manager",
follow_redirects=True,
timeout=httpx.Timeout(5.0),
auto_retry=None,
http_cache=False
)

monkeypatch.setattr(Paginator, "_get_next_page", get_next_monkeypatch)
Expand Down
Loading

0 comments on commit ff65b77

Please sign in to comment.