Skip to content

Commit

Permalink
Merge pull request buildbot#7610 from tdesveaux/typing/db/builds
Browse files Browse the repository at this point in the history
 typing: Add BuildModel dataclass
  • Loading branch information
p12tic authored May 15, 2024
2 parents 95214ed + eaefb2d commit 0af03b4
Show file tree
Hide file tree
Showing 19 changed files with 372 additions and 325 deletions.
13 changes: 7 additions & 6 deletions master/buildbot/data/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

if TYPE_CHECKING:
from buildbot.db.builders import BuilderModel
from buildbot.db.builds import BuildModel


class EndpointKind(enum.Enum):
Expand Down Expand Up @@ -196,7 +197,7 @@ def __init__(self, master, args) -> None:
# 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.build_dict = False
self.build_dict: BuildModel | None | False = False
self.builder_dict: BuilderModel | None | False = False
self.log_dict = False
self.worker_dict = False
Expand All @@ -217,7 +218,7 @@ async def get_step_dict(self):
return None

self.step_dict = await self.master.db.steps.getStep(
buildid=build_dict['id'],
buildid=build_dict.id,
number=self.args.get('step_number'),
name=self.args.get('step_name'),
)
Expand All @@ -234,7 +235,7 @@ async def get_step_dict(self):
return self.step_dict

@async_to_deferred
async def get_build_dict(self):
async def get_build_dict(self) -> BuilderModel | None:
if self.build_dict is not False:
return self.build_dict

Expand Down Expand Up @@ -271,7 +272,7 @@ async def get_build_id(self):
build_dict = await self.get_build_dict()
if build_dict is None:
return None
return build_dict['id']
return build_dict.id

@async_to_deferred
async def get_builder_dict(self) -> BuilderModel | None:
Expand All @@ -295,7 +296,7 @@ async def get_builder_dict(self) -> BuilderModel | None:
# fallback when there's only indirect information
build_dict = await self.get_build_dict()
if build_dict is not None:
self.builder_dict = await self.master.db.builders.getBuilder(build_dict['builderid'])
self.builder_dict = await self.master.db.builders.getBuilder(build_dict.builderid)
return self.builder_dict

self.builder_dict = None
Expand Down Expand Up @@ -346,7 +347,7 @@ async def get_worker_dict(self):

build_dict = await self.get_build_dict()
if build_dict is not None:
workerid = build_dict.get('workerid', None)
workerid = build_dict.workerid
if workerid is not None:
self.worker_dict = await self.master.db.workers.getWorker(workerid=workerid)
return self.worker_dict
Expand Down
49 changes: 28 additions & 21 deletions master/buildbot/data/builds.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,37 @@
#
# 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
from buildbot.data.resultspec import ResultSpec

if TYPE_CHECKING:
from buildbot.db.builds import BuildModel


def _db2data(model: BuildModel):
return {
'buildid': model.id,
'number': model.number,
'builderid': model.builderid,
'buildrequestid': model.buildrequestid,
'workerid': model.workerid,
'masterid': model.masterid,
'started_at': model.started_at,
'complete_at': model.complete_at,
"locks_duration_s": model.locks_duration_s,
'complete': model.complete_at is not None,
'state_string': model.state_string,
'results': model.results,
'properties': {},
}


class Db2DataMixin:
def _generate_filtered_properties(self, props, filters):
Expand All @@ -42,24 +67,6 @@ def _generate_filtered_properties(self, props, filters):
)
return None

def db2data(self, dbdict):
data = {
'buildid': dbdict['id'],
'number': dbdict['number'],
'builderid': dbdict['builderid'],
'buildrequestid': dbdict['buildrequestid'],
'workerid': dbdict['workerid'],
'masterid': dbdict['masterid'],
'started_at': dbdict['started_at'],
'complete_at': dbdict['complete_at'],
"locks_duration_s": dbdict["locks_duration_s"],
'complete': dbdict['complete_at'] is not None,
'state_string': dbdict['state_string'],
'results': dbdict['results'],
'properties': {},
}
return defer.succeed(data)

fieldMapping = {
'buildid': 'builds.id',
'number': 'builds.number',
Expand Down Expand Up @@ -94,7 +101,7 @@ def get(self, resultSpec, kwargs):
num = kwargs['build_number']
dbdict = yield self.master.db.builds.getBuildByNumber(bldr, num)

data = yield self.db2data(dbdict) if dbdict else None
data = _db2data(dbdict) if dbdict else None
# In some cases, data could be None
if data:
filters = resultSpec.popProperties() if hasattr(resultSpec, 'popProperties') else []
Expand All @@ -116,7 +123,7 @@ def actionStop(self, args, kwargs):
bldr = kwargs['builderid']
num = kwargs['build_number']
dbdict = yield self.master.db.builds.getBuildByNumber(bldr, num)
buildid = dbdict['id']
buildid = dbdict.id
self.master.mq.produce(
("control", "builds", str(buildid), 'stop'),
{"reason": kwargs.get('reason', args.get('reason', 'no reason'))},
Expand Down Expand Up @@ -173,7 +180,7 @@ def get(self, resultSpec, kwargs):

buildscol = []
for b in builds:
data = yield self.db2data(b)
data = _db2data(b)
if kwargs.get('graphql'):
# let the graphql engine manage the properties
del data['properties']
Expand Down
4 changes: 2 additions & 2 deletions master/buildbot/data/logchunks.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def get_info():
if builder_dict is not None:
log_prefix += f'Builder: {builder_dict.name}\n'
if build_dict is not None:
log_prefix += f'Build number: {build_dict["number"]}\n'
log_prefix += f'Build number: {build_dict.number}\n'
if worker_dict is not None:
log_prefix += f'Worker name: {worker_dict["name"]}\n'

Expand All @@ -60,7 +60,7 @@ def get_info():
if builder_dict is not None:
informative_parts += [builder_dict.name]
if build_dict is not None:
informative_parts += ['build', str(build_dict['number'])]
informative_parts += ['build', str(build_dict.number)]
if step_dict is not None:
informative_parts += ['step', step_dict['name']]
informative_parts += ['log', log_dict['slug']]
Expand Down
2 changes: 1 addition & 1 deletion master/buildbot/data/resultspec.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def apply(self, data):
fld = self.field
v = self.values
f = self.getOperator()
return (d for d in data if f(d[fld], v))
return (d for d in data if f((d[fld] if isinstance(d, dict) else getattr(d, fld)), v))

def __repr__(self):
return f"resultspec.{self.__class__.__name__}('{self.field}','{self.op}',{self.values})"
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 @@ -54,7 +54,7 @@ def get(self, resultSpec, kwargs):
build_dbdict = yield self.master.db.builds.getBuild(step_dbdict['buildid'])

sets = yield self.master.db.test_result_sets.getTestResultSets(
build_dbdict['builderid'],
build_dbdict.builderid,
buildid=step_dbdict['buildid'],
stepid=kwargs['stepid'],
complete=complete,
Expand All @@ -64,7 +64,7 @@ def get(self, resultSpec, kwargs):
build_dbdict = yield self.master.db.builds.getBuild(kwargs['buildid'])

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

0 comments on commit 0af03b4

Please sign in to comment.