Skip to content

Commit

Permalink
Merge pull request #7651 from tdesveaux/typing/db/steps
Browse files Browse the repository at this point in the history
 typing: Add StepsModel dataclass
  • Loading branch information
p12tic committed May 21, 2024
2 parents 30cb113 + 212afec commit eb83ecd
Show file tree
Hide file tree
Showing 13 changed files with 388 additions and 312 deletions.
9 changes: 5 additions & 4 deletions master/buildbot/data/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
if TYPE_CHECKING:
from buildbot.db.builders import BuilderModel
from buildbot.db.builds import BuildModel
from buildbot.db.steps import StepModel
from buildbot.db.workers import WorkerModel


Expand Down Expand Up @@ -197,14 +198,14 @@ def __init__(self, master, args) -> None:
self.args = args
# False is used as special value as "not set". None is used as "not exists". This solves
# the problem of multiple database queries in case entity does not exist.
self.step_dict = False
self.step_dict: StepModel | None | False = False
self.build_dict: BuildModel | None | False = False
self.builder_dict: BuilderModel | None | False = False
self.log_dict = False
self.worker_dict: WorkerModel | None | False = False

@async_to_deferred
async def get_step_dict(self):
async def get_step_dict(self) -> StepModel | None:
if self.step_dict is not False:
return self.step_dict

Expand Down Expand Up @@ -259,7 +260,7 @@ async def get_build_dict(self) -> BuilderModel | None:
# fallback when there's only indirect information
step_dict = await self.get_step_dict()
if step_dict is not None:
self.build_dict = await self.master.db.builds.getBuild(step_dict['buildid'])
self.build_dict = await self.master.db.builds.getBuild(step_dict.buildid)
return self.build_dict

self.build_dict = None
Expand Down Expand Up @@ -327,7 +328,7 @@ async def get_log_dict(self):
self.log_dict = None
return None
self.log_dict = await self.master.db.logs.getLogBySlug(
step_dict['id'], self.args.get('log_slug')
step_dict.id, self.args.get('log_slug')
)
return self.log_dict

Expand Down
2 changes: 1 addition & 1 deletion master/buildbot/data/logchunks.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def get_info():
if build_dict is not None:
informative_parts += ['build', str(build_dict.number)]
if step_dict is not None:
informative_parts += ['step', step_dict['name']]
informative_parts += ['step', step_dict.name]
informative_parts += ['log', log_dict['slug']]
informative_slug = '_'.join(informative_parts)

Expand Down
4 changes: 2 additions & 2 deletions master/buildbot/data/logs.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def get(self, resultSpec, kwargs):
if step_dict is None:
return None

dbdict = yield self.master.db.logs.getLogBySlug(step_dict['id'], kwargs.get('log_slug'))
dbdict = yield self.master.db.logs.getLogBySlug(step_dict.id, kwargs.get('log_slug'))
return (yield self.db2data(dbdict)) if dbdict else None


Expand All @@ -81,7 +81,7 @@ def get(self, resultSpec, kwargs):
step_dict = yield retriever.get_step_dict()
if step_dict is None:
return []
logs = yield self.master.db.logs.getLogs(stepid=step_dict['id'])
logs = yield self.master.db.logs.getLogs(stepid=step_dict.id)
results = []
for dbdict in logs:
results.append((yield self.db2data(dbdict)))
Expand Down
44 changes: 23 additions & 21 deletions master/buildbot/data/steps.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,30 +13,35 @@
#
# Copyright Buildbot Team Members

from __future__ import annotations

from typing import TYPE_CHECKING

from twisted.internet import defer

from buildbot.data import base
from buildbot.data import types

if TYPE_CHECKING:
from buildbot.db.steps import StepModel


class Db2DataMixin:
def db2data(self, dbdict):
data = {
'stepid': dbdict['id'],
'number': dbdict['number'],
'name': dbdict['name'],
'buildid': dbdict['buildid'],
'started_at': dbdict['started_at'],
"locks_acquired_at": dbdict["locks_acquired_at"],
'complete': dbdict['complete_at'] is not None,
'complete_at': dbdict['complete_at'],
'state_string': dbdict['state_string'],
'results': dbdict['results'],
'urls': dbdict['urls'],
'hidden': dbdict['hidden'],
def db2data(self, model: StepModel):
return {
'stepid': model.id,
'number': model.number,
'name': model.name,
'buildid': model.buildid,
'started_at': model.started_at,
"locks_acquired_at": model.locks_acquired_at,
'complete': model.complete_at is not None,
'complete_at': model.complete_at,
'state_string': model.state_string,
'results': model.results,
'urls': [{'name': item.name, 'url': item.url} for item in model.urls],
'hidden': model.hidden,
}
return defer.succeed(data)


class StepEndpoint(Db2DataMixin, base.BuildNestingMixin, base.Endpoint):
Expand All @@ -55,14 +60,14 @@ class StepEndpoint(Db2DataMixin, base.BuildNestingMixin, base.Endpoint):
def get(self, resultSpec, kwargs):
if 'stepid' in kwargs:
dbdict = yield self.master.db.steps.getStep(kwargs['stepid'])
return (yield self.db2data(dbdict)) if dbdict else None
return self.db2data(dbdict) if dbdict else None
buildid = yield self.getBuildid(kwargs)
if buildid is None:
return None
dbdict = yield self.master.db.steps.getStep(
buildid=buildid, number=kwargs.get('step_number'), name=kwargs.get('step_name')
)
return (yield self.db2data(dbdict)) if dbdict else None
return self.db2data(dbdict) if dbdict else None


class StepsEndpoint(Db2DataMixin, base.BuildNestingMixin, base.Endpoint):
Expand All @@ -82,10 +87,7 @@ def get(self, resultSpec, kwargs):
if buildid is None:
return None
steps = yield self.master.db.steps.getSteps(buildid=buildid)
results = []
for dbdict in steps:
results.append((yield self.db2data(dbdict)))
return results
return [self.db2data(model) for model in steps]


class UrlEntityType(types.Entity):
Expand Down
4 changes: 2 additions & 2 deletions master/buildbot/data/test_result_sets.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ def get(self, resultSpec, kwargs):
complete = resultSpec.popBooleanFilter('complete')
if 'stepid' in kwargs:
step_dbdict = yield self.master.db.steps.getStep(kwargs['stepid'])
build_dbdict = yield self.master.db.builds.getBuild(step_dbdict['buildid'])
build_dbdict = yield self.master.db.builds.getBuild(step_dbdict.buildid)

sets = yield self.master.db.test_result_sets.getTestResultSets(
build_dbdict.builderid,
buildid=step_dbdict['buildid'],
buildid=step_dbdict.buildid,
stepid=kwargs['stepid'],
complete=complete,
result_spec=resultSpec,
Expand Down
Loading

0 comments on commit eb83ecd

Please sign in to comment.