Skip to content

Commit 2987516

Browse files
authored
feat: Observation.actions to capture activity (#15768)
1 parent d0e44ea commit 2987516

File tree

4 files changed

+63
-1
lines changed

4 files changed

+63
-1
lines changed

tests/unit/admin/views/test_core.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ def test_dashboard(self, pyramid_request):
3131

3232
def test_dashboard_with_permission_and_observation(self, db_request):
3333
ProjectObservationFactory.create(kind="is_malware")
34+
ProjectObservationFactory.create(kind="is_malware", actions={"foo": "bar"})
35+
ProjectObservationFactory.create(kind="something_else")
3436
db_request.user = pretend.stub()
3537
db_request.has_permission = pretend.call_recorder(lambda perm: True)
3638

warehouse/admin/views/core.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ def dashboard(request):
2828
# Count how many Malware Project Observations are in the database
2929
malware_reports_count = (
3030
request.db.query(func.count(Observation.id)).filter(
31-
Observation.kind == ObservationKind.IsMalware.value[0]
31+
Observation.kind == ObservationKind.IsMalware.value[0],
32+
Observation.actions == {}, # No actions have been taken
3233
)
3334
).scalar()
3435
else:
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# Licensed under the Apache License, Version 2.0 (the "License");
2+
# you may not use this file except in compliance with the License.
3+
# You may obtain a copy of the License at
4+
#
5+
# http://www.apache.org/licenses/LICENSE-2.0
6+
#
7+
# Unless required by applicable law or agreed to in writing, software
8+
# distributed under the License is distributed on an "AS IS" BASIS,
9+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
# See the License for the specific language governing permissions and
11+
# limitations under the License.
12+
"""
13+
Add Observation.actions
14+
15+
Revision ID: 8673550a67a3
16+
Revises: 73c201ff90f3
17+
Create Date: 2024-04-11 20:45:05.218380
18+
"""
19+
20+
import sqlalchemy as sa
21+
22+
from alembic import op
23+
from sqlalchemy.dialects import postgresql
24+
25+
revision = "8673550a67a3"
26+
down_revision = "73c201ff90f3"
27+
28+
29+
def upgrade():
30+
op.add_column(
31+
"project_observations",
32+
sa.Column(
33+
"actions",
34+
postgresql.JSONB(astext_type=sa.Text()),
35+
server_default=sa.text("'{}'"),
36+
nullable=False,
37+
comment="Actions taken based on the observation",
38+
),
39+
)
40+
op.add_column(
41+
"release_observations",
42+
sa.Column(
43+
"actions",
44+
postgresql.JSONB(astext_type=sa.Text()),
45+
server_default=sa.text("'{}'"),
46+
nullable=False,
47+
comment="Actions taken based on the observation",
48+
),
49+
)
50+
51+
52+
def downgrade():
53+
op.drop_column("release_observations", "actions")
54+
op.drop_column("project_observations", "actions")

warehouse/observations/models.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,11 @@ class Observation(AbstractConcreteBase, db.Model):
156156
comment="Additional data for the observation",
157157
server_default=sql.text("'{}'"),
158158
)
159+
actions: Mapped[dict] = mapped_column(
160+
JSONB,
161+
comment="Actions taken based on the observation",
162+
server_default=sql.text("'{}'"),
163+
)
159164

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

0 commit comments

Comments
 (0)