Skip to content

Commit

Permalink
Add ScanModel
Browse files Browse the repository at this point in the history
  • Loading branch information
majamassarini committed Oct 11, 2024
1 parent 7a694cb commit 3ade7de
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 0 deletions.
44 changes: 44 additions & 0 deletions alembic/versions/a48ff5655c42_create_scans_table.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
"""Create scans table
Revision ID: a48ff5655c42
Revises: 3a7e4a388dd2
Create Date: 2024-10-11 09:03:17.476535
"""

from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = "a48ff5655c42"
down_revision = "3a7e4a388dd2"
branch_labels = None
depends_on = None


def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.create_table(
"scans",
sa.Column("id", sa.Integer(), nullable=False),
sa.Column("task_id", sa.Integer(), nullable=True),
sa.Column("issues_added_url", sa.String(), nullable=True),
sa.Column("issues_fixed_url", sa.String(), nullable=True),
sa.Column("scan_results_url", sa.String(), nullable=True),
sa.Column("copr_build_target_id", sa.Integer(), nullable=True),
sa.ForeignKeyConstraint(
["copr_build_target_id"],
["copr_build_targets.id"],
),
sa.PrimaryKeyConstraint("id"),
sa.UniqueConstraint("copr_build_target_id"),
sa.UniqueConstraint("task_id"),
)
# ### end Alembic commands ###


def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_table("scans")
# ### end Alembic commands ###
48 changes: 48 additions & 0 deletions packit_service/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1970,6 +1970,8 @@ class CoprBuildTargetModel(GroupAndTargetModelConnector, Base):
"CoprBuildGroupModel", back_populates="copr_build_targets"
)

scan = relationship("ScanModel", back_populates="copr_build_target")

def set_built_packages(self, built_packages):
with sa_session_transaction(commit=True) as session:
self.built_packages = built_packages
Expand Down Expand Up @@ -3824,6 +3826,52 @@ def get_by_koji_name(cls, koji_name: str) -> Optional["SidetagModel"]:
return session.query(SidetagModel).filter_by(koji_name=koji_name).first()


class ScanModel(Base):
__tablename__ = "scans"
id = Column(Integer, primary_key=True)
task_id = Column(Integer, unique=True) # open scan hub id
issues_added_url = Column(String)
issues_fixed_url = Column(String)
scan_results_url = Column(String)
copr_build_target_id = Column(
Integer, ForeignKey("copr_build_targets.id"), unique=True
)
copr_build_target = relationship(
"CoprBuildTargetModel", back_populates="scan", uselist=False
)

@classmethod
def get_or_create(cls, task_id: int, copr_build_target_id: int) -> "ScanModel":
with sa_session_transaction(commit=True) as session:
scan = cls.get_by_task_id(task_id)
if not scan:
scan = cls()
scan.task_id = task_id
scan.copr_build_target_id = copr_build_target_id
session.add(scan)
return scan

def set_issues_added_url(self, issues_added_url: str) -> None:
with sa_session_transaction(commit=True) as session:
self.issues_added_url = issues_added_url
session.add(self)

def set_issues_fixed_url(self, issues_fixed_url: str) -> None:
with sa_session_transaction(commit=True) as session:
self.issues_fixed_url = issues_fixed_url
session.add(self)

def set_scan_results_url(self, scan_results_url: str) -> None:
with sa_session_transaction(commit=True) as session:
self.scan_results_url = scan_results_url
session.add(self)

@classmethod
def get_by_task_id(cls, task_id: int) -> Optional["ScanModel"]:
with sa_session_transaction() as session:
return session.query(cls).filter_by(task_id=task_id).first()


@cached(cache=TTLCache(maxsize=2048, ttl=(60 * 60 * 24)))
def get_usage_data(datetime_from=None, datetime_to=None, top=10) -> dict:
"""
Expand Down
10 changes: 10 additions & 0 deletions tests_openshift/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
BodhiUpdateTargetModel,
BodhiUpdateGroupModel,
SyncReleasePullRequestModel,
ScanModel,
)
from packit_service.worker.events import InstallationEvent

Expand Down Expand Up @@ -2431,3 +2432,12 @@ def multiple_bodhi_update_runs(branch_project_event_model):
bodhi_update_group=group,
),
]


@pytest.fixture()
def a_scan(a_copr_build_for_pr):
scan = ScanModel.get_or_create(123, a_copr_build_for_pr.id)
scan.issues_added_url = "added issues"
scan.issues_fixed_url = "fixed issues"
scan.scan_results_url = "results"
yield scan
8 changes: 8 additions & 0 deletions tests_openshift/database/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1065,3 +1065,11 @@ def test_project_event_get_older_than_with_packages_config(
)
== 0
)


def test_create_scan(clean_before_and_after, a_scan):
assert a_scan.task_id == 123
assert a_scan.issues_added_url == "added issues"
assert a_scan.issues_fixed_url == "fixed issues"
assert a_scan.scan_results_url == "results"
assert a_scan.copr_build_target.build_id == "123456"

0 comments on commit 3ade7de

Please sign in to comment.