Skip to content
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

cherry-pick #7032 fix updated_at field in cicd_tasks and cicd_pipelines #7041

Merged
merged 1 commit into from
Feb 28, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
class Builds(Stream):
tool_model = Build
domain_types = [DomainType.CICD]
domain_models = [devops.CiCDPipelineCommit, devops.CICDPipeline]

def collect(self, state, context) -> Iterable[tuple[object, dict]]:
repo: GitRepository = context.scope
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class Jobs(Substream):
tool_model = Job
domain_types = [DomainType.CICD]
parent_stream = Builds
domain_models = [devops.CICDTask]

def collect(self, state, context, parent: Build) -> Iterable[tuple[object, dict]]:
repo: GitRepository = context.scope
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class GitPullRequestCommits(Substream):
tool_model = GitPullRequestCommit
domain_types = [DomainType.CODE]
parent_stream = GitPullRequests
domain_models = [code.PullRequestCommit]

def should_run_on(self, scope: GitRepository) -> bool:
return not scope.is_external()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
class GitPullRequests(Stream):
tool_model = GitPullRequest
domain_types = [DomainType.CODE]
domain_models = [code.PullRequest]

def should_run_on(self, scope: GitRepository) -> bool:
return not scope.is_external()
Expand Down
9 changes: 8 additions & 1 deletion backend/python/pydevlake/pydevlake/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
from sqlmodel import SQLModel
from pydevlake import Field


inflect_engine = inflect.engine()


Expand All @@ -42,6 +41,10 @@ class Model(SQLModel):
sa_column=Column(DateTime(), default=datetime.utcnow, onupdate=datetime.utcnow)
)

def set_updated_at(self):
if self.updated_at is None:
self.updated_at = datetime.utcnow()


class ToolTable(SQLModel):
@declared_attr
Expand Down Expand Up @@ -132,6 +135,10 @@ class NoPKModel(RawDataOrigin):
sa_column=Column(DateTime(), default=datetime.utcnow, onupdate=datetime.utcnow)
)

def set_updated_at(self):
if self.updated_at is None:
self.updated_at = datetime.utcnow()


class ToolModel(ToolTable, NoPKModel):
connection_id: Optional[int] = Field(primary_key=True, auto_increment=False)
Expand Down
7 changes: 6 additions & 1 deletion backend/python/pydevlake/pydevlake/stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
# limitations under the License.


from typing import Iterable, Type
from typing import Iterable, Type, Optional
from abc import abstractmethod

from pydevlake.context import Context
Expand Down Expand Up @@ -52,6 +52,11 @@ def tool_model(self) -> Type[ToolModel]:
def domain_types(self) -> list[DomainType]:
pass

@property
@abstractmethod
def domain_models(self) -> Optional[list[Type[DomainModel]]]:
pass

def raw_model(self, session) -> Type[RawModel]:
if self._raw_model is not None:
return self._raw_model
Expand Down
7 changes: 6 additions & 1 deletion backend/python/pydevlake/pydevlake/subtasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ def process(self, raw: RawModel, session: Session, ctx: Context):
tool_model = self.stream.extract(json.loads(raw.data))
tool_model.set_raw_origin(raw)
tool_model.connection_id = ctx.connection.id
tool_model.set_updated_at()
session.merge(tool_model)

def delete(self, session, ctx):
Expand Down Expand Up @@ -206,9 +207,13 @@ def process(self, tool_model: ToolModel, session: Session, ctx: Context):

def _save(self, tool_model: ToolModel, domain_model: DomainModel, session: Session, connection_id: int):
domain_model.set_tool_origin(tool_model)
domain_model.set_updated_at()
if isinstance(domain_model, DomainModel):
domain_model.id = tool_model.domain_id()
session.merge(domain_model)

def delete(self, session, ctx):
pass
domain_models = self.stream.domain_models
if domain_models is not None:
for domain_model in domain_models:
session.execute(sql.delete(domain_model).where(domain_model.raw_data_params == self._params(ctx)))
6 changes: 5 additions & 1 deletion backend/python/pydevlake/pydevlake/testing/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,11 @@ def assert_stream_convert(plugin: Union[Plugin, Type[Plugin]], stream_name: str,
if not isinstance(domain_models, (Iterable, Generator)):
domain_models = [domain_models]
for res, exp in zip(domain_models, expected):
assert res == exp
if (hasattr(res, "__getitem__") and res[0] == "updated_at") or (
hasattr(exp, "__getitem__") and exp[0] == "updated_at"):
pass
else:
assert res == exp


def assert_stream_run(stream: Stream, connection: Connection, scope: ToolScope, scope_config: ScopeConfig):
Expand Down
Loading