Skip to content

feat: Observation.actions to capture activity #15768

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

Merged
merged 3 commits into from
Apr 12, 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
2 changes: 2 additions & 0 deletions tests/unit/admin/views/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ def test_dashboard(self, pyramid_request):

def test_dashboard_with_permission_and_observation(self, db_request):
ProjectObservationFactory.create(kind="is_malware")
ProjectObservationFactory.create(kind="is_malware", actions={"foo": "bar"})
ProjectObservationFactory.create(kind="something_else")
db_request.user = pretend.stub()
db_request.has_permission = pretend.call_recorder(lambda perm: True)

Expand Down
3 changes: 2 additions & 1 deletion warehouse/admin/views/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ def dashboard(request):
# Count how many Malware Project Observations are in the database
malware_reports_count = (
request.db.query(func.count(Observation.id)).filter(
Observation.kind == ObservationKind.IsMalware.value[0]
Observation.kind == ObservationKind.IsMalware.value[0],
Observation.actions == {}, # No actions have been taken
)
).scalar()
else:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
Add Observation.actions

Revision ID: 8673550a67a3
Revises: 73c201ff90f3
Create Date: 2024-04-11 20:45:05.218380
"""

import sqlalchemy as sa

from alembic import op
from sqlalchemy.dialects import postgresql

revision = "8673550a67a3"
down_revision = "73c201ff90f3"


def upgrade():
op.add_column(
"project_observations",
sa.Column(
"actions",
postgresql.JSONB(astext_type=sa.Text()),
server_default=sa.text("'{}'"),
nullable=False,
comment="Actions taken based on the observation",
),
)
op.add_column(
"release_observations",
sa.Column(
"actions",
postgresql.JSONB(astext_type=sa.Text()),
server_default=sa.text("'{}'"),
nullable=False,
comment="Actions taken based on the observation",
),
)


def downgrade():
op.drop_column("release_observations", "actions")
op.drop_column("project_observations", "actions")
5 changes: 5 additions & 0 deletions warehouse/observations/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,11 @@ class Observation(AbstractConcreteBase, db.Model):
comment="Additional data for the observation",
server_default=sql.text("'{}'"),
)
actions: Mapped[dict] = mapped_column(
JSONB,
comment="Actions taken based on the observation",
server_default=sql.text("'{}'"),
)

def __repr__(self):
return f"<{self.__class__.__name__} {self.kind}>"
Expand Down