Skip to content

Commit

Permalink
Extend Bodhi and pipeline API (#2311)
Browse files Browse the repository at this point in the history
Extend Bodhi and pipeline API

Extend Bodhi API with info about times and pipelines API with info about Bodhi updates.

RELEASE NOTES BEGIN
N/A
RELEASE NOTES END

Reviewed-by: Matej Focko
  • Loading branch information
softwarefactory-project-zuul[bot] authored Jan 22, 2024
2 parents 61cd5be + 89d2b29 commit a575cc1
Show file tree
Hide file tree
Showing 9 changed files with 147 additions and 2 deletions.
114 changes: 114 additions & 0 deletions alembic/versions/a3a17014c282_add_times_for_bodhi_update_targets.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
"""Add times for Bodhi update targets
Revision ID: a3a17014c282
Revises: 8ba13238bfa4
Create Date: 2024-01-20 22:05:46.749382
"""
from datetime import datetime
from typing import TYPE_CHECKING

import sqlalchemy as sa
from alembic import op
from sqlalchemy import orm, Column, Integer, String, JSON, DateTime, ForeignKey
from sqlalchemy.orm import relationship, declarative_base

from packit_service.models import (
GroupAndTargetModelConnector,
ProjectAndEventsConnector,
GroupModel,
)

# revision identifiers, used by Alembic.
revision = "a3a17014c282"
down_revision = "8ba13238bfa4"
branch_labels = None
depends_on = None

if TYPE_CHECKING:
Base = object
else:
Base = declarative_base()


class PipelineModel(Base):
"""
Represents one pipeline.
Connects ProjectEventModel (and project events like PullRequestModel via that model) with
build/test models like SRPMBuildModel, CoprBuildTargetModel, KojiBuildTargetModel,
and TFTTestRunGroupModel.
* One model of each build/test target/group model can be connected.
* Each build/test model can be connected to multiple PipelineModels (e.g. on retrigger).
* Each PipelineModel has to be connected to exactly one ProjectEventModel.
* There can be multiple PipelineModels for one ProjectEventModel.
(e.g. For each push to PR, there will be new PipelineModel, but same ProjectEventModel.)
"""

__tablename__ = "pipelines"
id = Column(Integer, primary_key=True) # our database PK
datetime = Column(DateTime, default=datetime.utcnow)
bodhi_update_group_id = Column(
Integer, ForeignKey("bodhi_update_groups.id"), index=True
)
bodhi_update_group = relationship("BodhiUpdateGroupModel", back_populates="runs")


class BodhiUpdateGroupModel(ProjectAndEventsConnector, GroupModel, Base):
__tablename__ = "bodhi_update_groups"
id = Column(Integer, primary_key=True)
submitted_time = Column(DateTime, default=datetime.utcnow)

runs = relationship("PipelineModel", back_populates="bodhi_update_group")
bodhi_update_targets = relationship(
"BodhiUpdateTargetModel", back_populates="group_of_targets"
)


class BodhiUpdateTargetModel(GroupAndTargetModelConnector, Base):
__tablename__ = "bodhi_update_targets"
id = Column(Integer, primary_key=True)
status = Column(String)
target = Column(String)
web_url = Column(String)
koji_nvr = Column(String)
alias = Column(String)
submitted_time = Column(DateTime, default=datetime.utcnow)
update_creation_time = Column(DateTime)
data = Column(JSON)
bodhi_update_group_id = Column(Integer, ForeignKey("bodhi_update_groups.id"))

group_of_targets = relationship(
"BodhiUpdateGroupModel", back_populates="bodhi_update_targets"
)


def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.add_column(
"bodhi_update_targets",
sa.Column("submitted_time", sa.DateTime(), nullable=True),
)
op.add_column(
"bodhi_update_targets",
sa.Column("update_creation_time", sa.DateTime(), nullable=True),
)

bind = op.get_bind()
session = orm.Session(bind=bind)

for bodhi_update in session.query(BodhiUpdateTargetModel).all():
bodhi_update.submitted_time = bodhi_update.group_of_targets.submitted_time
session.add(bodhi_update)

session.commit()

# ### end Alembic commands ###


def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_column("bodhi_update_targets", "update_creation_time")
op.drop_column("bodhi_update_targets", "submitted_time")
# ### end Alembic commands ###
10 changes: 10 additions & 0 deletions packit_service/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1393,6 +1393,9 @@ def __query_merged_runs(cls):
func.array_agg(psql_array([PipelineModel.sync_release_run_id])).label(
"sync_release_run_id",
),
func.array_agg(psql_array([PipelineModel.bodhi_update_group_id])).label(
"bodhi_update_group_id",
),
)

@classmethod
Expand Down Expand Up @@ -1809,6 +1812,8 @@ class BodhiUpdateTargetModel(GroupAndTargetModelConnector, Base):
web_url = Column(String)
koji_nvr = Column(String)
alias = Column(String)
submitted_time = Column(DateTime, default=datetime.utcnow)
update_creation_time = Column(DateTime)
data = Column(JSON)
bodhi_update_group_id = Column(Integer, ForeignKey("bodhi_update_groups.id"))

Expand Down Expand Up @@ -1836,6 +1841,11 @@ def set_data(self, data: dict):
self.data = data
session.add(self)

def set_update_creation_time(self, time: datetime):
with sa_session_transaction() as session:
self.update_creation_time = time
session.add(self)

@classmethod
def create(
cls,
Expand Down
4 changes: 4 additions & 0 deletions packit_service/service/api/bodhi_updates.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ def get(self):
"pr_id": update.get_pr_id(),
"branch_name": update.get_branch_name(),
"release": update.get_release_tag(),
"submitted_time": optional_timestamp(update.submitted_time),
"update_creation_time": optional_timestamp(update.update_creation_time),
}

if project := update.get_project():
Expand Down Expand Up @@ -77,6 +79,8 @@ def get(self, id):
"web_url": update.web_url,
"koji_nvr": update.koji_nvr,
"alias": update.alias,
"submitted_time": optional_timestamp(update.submitted_time),
"update_creation_time": optional_timestamp(update.update_creation_time),
"run_ids": sorted(run.id for run in update.group_of_targets.runs),
}

Expand Down
12 changes: 10 additions & 2 deletions packit_service/service/api/runs.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
optional_timestamp,
BuildStatus,
TFTTestRunGroupModel,
BodhiUpdateGroupModel,
BodhiUpdateTargetModel,
)
from packit_service.service.api.parsers import indices, pagination_arguments
from packit_service.service.api.utils import (
Expand Down Expand Up @@ -72,6 +74,7 @@ def process_runs(runs):
"test_run": [],
"propose_downstream": [],
"pull_from_upstream": [],
"bodhi_update": [],
}

if srpm_build := SRPMBuildModel.get_by_id(pipeline.srpm_build_id):
Expand All @@ -88,6 +91,7 @@ def process_runs(runs):
("copr", CoprBuildGroupModel, pipeline.copr_build_group_id),
("koji", KojiBuildGroupModel, pipeline.koji_build_group_id),
("test_run", TFTTestRunGroupModel, pipeline.test_run_group_id),
("bodhi_update", BodhiUpdateGroupModel, pipeline.bodhi_update_group_id),
):
for packit_id in set(flatten_and_remove_none(packit_ids)):
group_row = Model.get_by_id(packit_id)
Expand All @@ -104,9 +108,12 @@ def process_runs(runs):
if "trigger" not in response_dict:
submitted_time = (
row.submitted_time
if isinstance(row, TFTTestRunTargetModel)
if isinstance(
row, (TFTTestRunTargetModel, BodhiUpdateTargetModel)
)
else row.build_submitted_time
)

response_dict["time_submitted"] = optional_timestamp(
submitted_time
)
Expand Down Expand Up @@ -171,11 +178,12 @@ def get(self, id):
result = {
"run_id": run.id,
"trigger": get_project_info_from_build(
run.srpm_build or run.sync_release_run
run.srpm_build or run.sync_release_run or run.bodhi_update_group
),
"srpm_build_id": run.srpm_build_id,
"copr_build_group_id": run.copr_build_group_id,
"koji_build_group_id": run.koji_build_group_id,
"test_run_group_id": run.test_run_group_id,
"bodhi_update_group_id": run.bodhi_update_group_id,
}
return response_maker(result)
2 changes: 2 additions & 0 deletions packit_service/worker/handlers/bodhi.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"""
import abc
import logging
from datetime import datetime
from os import getenv
from typing import Tuple, Type, Optional

Expand Down Expand Up @@ -114,6 +115,7 @@ def run(self) -> TaskResults:
target_model.set_status("success")
target_model.set_alias(alias)
target_model.set_web_url(url)
target_model.set_update_creation_time(datetime.now())

except PackitException as ex:
logger.debug(f"Bodhi update failed to be created: {ex}")
Expand Down
3 changes: 3 additions & 0 deletions tests/integration/test_bodhi_update.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ def test_bodhi_update_for_unknown_koji_build(koji_build_completed_old_format):
set_data=lambda x: None,
set_web_url=lambda x: None,
set_alias=lambda x: None,
set_update_creation_time=lambda x: None,
)
],
)
Expand Down Expand Up @@ -664,6 +665,7 @@ def test_bodhi_update_for_known_koji_build(koji_build_completed_old_format):
set_data=lambda x: None,
set_web_url=lambda x: None,
set_alias=lambda x: None,
set_update_creation_time=lambda x: None,
)
]
)
Expand Down Expand Up @@ -802,6 +804,7 @@ def test_bodhi_update_fedora_stable_by_default(koji_build_completed_f36):
set_data=lambda x: None,
set_web_url=lambda x: None,
set_alias=lambda x: None,
set_update_creation_time=lambda x: None,
)
]
)
Expand Down
2 changes: 2 additions & 0 deletions tests/integration/test_issue_comment.py
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,7 @@ def test_issue_comment_retrigger_bodhi_update_handler(
set_data=lambda x: None,
set_web_url=lambda x: None,
set_alias=lambda x: None,
set_update_creation_time=lambda x: None,
),
flexmock(
target="f38",
Expand All @@ -453,6 +454,7 @@ def test_issue_comment_retrigger_bodhi_update_handler(
set_data=lambda x: None,
set_web_url=lambda x: None,
set_alias=lambda x: None,
set_update_creation_time=lambda x: None,
),
],
)
Expand Down
1 change: 1 addition & 0 deletions tests/integration/test_pr_comment.py
Original file line number Diff line number Diff line change
Expand Up @@ -2523,6 +2523,7 @@ def test_bodhi_update_retrigger_via_dist_git_pr_comment(pagure_pr_comment_added)
set_data=lambda x: None,
set_web_url=lambda x: None,
set_alias=lambda x: None,
set_update_creation_time=lambda x: None,
)
],
)
Expand Down
1 change: 1 addition & 0 deletions tests_openshift/service/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -869,3 +869,4 @@ def test_bodhi_update_info(
assert response_dict["repo_name"] == SampleValues.repo_name
assert response_dict["repo_namespace"] == SampleValues.repo_namespace
assert response_dict["status"] == "error"
assert response_dict["submitted_time"] is not None

0 comments on commit a575cc1

Please sign in to comment.