Skip to content

Commit 64007c8

Browse files
committed
Fix scan timestamp
1 parent 29f3276 commit 64007c8

6 files changed

Lines changed: 84 additions & 46 deletions

File tree

api/projects/code_references/migrations/0003_introduce_per_feature_scanned_references.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ def migrate_scans_forward(apps: Apps, _: object) -> None:
3535
PerFeatureScan = apps.get_model("code_references", "ScannedCodeReferences")
3636
Repository = apps.get_model("code_references", "VCSRepository")
3737
Feature = apps.get_model("features", "Feature")
38-
PerFeatureScan._meta.get_field("created_at").auto_now_add = False
3938

4039
legacy_scans_summaries = LegacyScan.objects.values(
4140
"project_id",
@@ -179,7 +178,7 @@ class Migration(migrations.Migration):
179178
verbose_name="ID",
180179
),
181180
),
182-
("created_at", models.DateTimeField(auto_now_add=True)),
181+
("created_at", models.DateTimeField()),
183182
("revision", models.CharField(max_length=100)),
184183
("code_references", models.JSONField(default=list)),
185184
("code_references_hash", models.CharField(max_length=32)),

api/projects/code_references/models.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class ScannedCodeReferences(models.Model):
4040
A list of code references for a feature scanned from a VCS repository
4141
"""
4242

43-
created_at = models.DateTimeField(auto_now_add=True)
43+
created_at = models.DateTimeField()
4444

4545
feature = models.ForeignKey(
4646
"features.Feature",

api/projects/code_references/services.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ def record_scan(
141141
revision=revision,
142142
code_references=references,
143143
code_references_hash=_hash_references(references),
144+
created_at=scanned_at,
144145
)
145146
for feature_name, references in references_by_feature.items()
146147
if (feature := features_by_name.get(feature_name)) is not None

api/tests/unit/features/test_unit_features_views.py

Lines changed: 69 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -3670,41 +3670,39 @@ def test_list_features__with_code_references__returns_counts(
36703670
) -> None:
36713671
# Given
36723672
with_project_permissions([VIEW_PROJECT]) # type: ignore[call-arg]
3673-
with freeze_time("2099-01-01T10:00:00-0300"):
3674-
github_repository = VCSRepository.objects.create(
3675-
project=project,
3676-
url="https://github.flagsmith.com/backend/",
3677-
vcs_provider="github",
3678-
last_scanned_at=timezone.now(),
3679-
)
3680-
ScannedCodeReferences.objects.create(
3681-
feature=feature,
3682-
repository=github_repository,
3683-
revision="backend-1",
3684-
code_references=[
3685-
{"file_path": "path/to/file.py", "line_number": 42},
3686-
],
3687-
code_references_hash="hash-backend-1",
3688-
)
3689-
with freeze_time("2099-01-02T11:00:00-0300"):
3690-
github_repository.last_scanned_at = timezone.now()
3691-
github_repository.save()
3692-
gitlab_repository = VCSRepository.objects.create(
3693-
project=project,
3694-
url="https://gitlab.flagsmith.com/frontend/",
3695-
vcs_provider="github",
3696-
last_scanned_at=timezone.now(),
3697-
)
3698-
ScannedCodeReferences.objects.create(
3699-
feature=feature,
3700-
repository=gitlab_repository,
3701-
revision="frontend-2",
3702-
code_references=[
3703-
{"file_path": "path/to/file.js", "line_number": 23},
3704-
{"file_path": "path/to/another/file.js", "line_number": 50},
3705-
],
3706-
code_references_hash="hash-frontend-2",
3707-
)
3673+
github_repository = VCSRepository.objects.create(
3674+
project=project,
3675+
url="https://github.flagsmith.com/backend/",
3676+
vcs_provider="github",
3677+
last_scanned_at="2099-01-02T14:00:00+00:00",
3678+
)
3679+
ScannedCodeReferences.objects.create(
3680+
feature=feature,
3681+
repository=github_repository,
3682+
revision="backend-1",
3683+
code_references=[
3684+
{"file_path": "path/to/file.py", "line_number": 42},
3685+
],
3686+
code_references_hash="hash-backend-1",
3687+
created_at="2099-01-01T13:00:00+00:00",
3688+
)
3689+
gitlab_repository = VCSRepository.objects.create(
3690+
project=project,
3691+
url="https://gitlab.flagsmith.com/frontend/",
3692+
vcs_provider="github",
3693+
last_scanned_at="2099-01-02T14:00:00+00:00",
3694+
)
3695+
ScannedCodeReferences.objects.create(
3696+
feature=feature,
3697+
repository=gitlab_repository,
3698+
revision="frontend-2",
3699+
code_references=[
3700+
{"file_path": "path/to/file.js", "line_number": 23},
3701+
{"file_path": "path/to/another/file.js", "line_number": 50},
3702+
],
3703+
code_references_hash="hash-frontend-2",
3704+
created_at="2099-01-02T14:00:00+00:00",
3705+
)
37083706

37093707
# When
37103708
response = staff_client.get(f"/api/v1/projects/{project.pk}/features/")
@@ -3727,6 +3725,42 @@ def test_list_features__with_code_references__returns_counts(
37273725
]
37283726

37293727

3728+
def test_list_features__scan_recorded_via_api__count_reflects_references(
3729+
feature: Feature,
3730+
project: Project,
3731+
admin_client_new: APIClient,
3732+
staff_client: APIClient,
3733+
with_project_permissions: WithProjectPermissionsCallable,
3734+
) -> None:
3735+
# Given
3736+
with_project_permissions([VIEW_PROJECT]) # type: ignore[call-arg]
3737+
admin_client_new.post(
3738+
f"/api/v1/projects/{project.pk}/code-references/",
3739+
data={
3740+
"repository_url": "https://github.flagsmith.com/backend/",
3741+
"revision": "rev-1",
3742+
"code_references": [
3743+
{
3744+
"feature_name": feature.name,
3745+
"file_path": "path/to/file.py",
3746+
"line_number": 42,
3747+
},
3748+
],
3749+
},
3750+
format="json",
3751+
)
3752+
3753+
# When
3754+
response = staff_client.get(f"/api/v1/projects/{project.pk}/features/")
3755+
3756+
# Then
3757+
assert response.status_code == status.HTTP_200_OK
3758+
counts = response.json()["results"][0]["code_references_counts"]
3759+
assert len(counts) == 1
3760+
assert counts[0]["repository_url"] == "https://github.flagsmith.com/backend/"
3761+
assert counts[0]["count"] == 1
3762+
3763+
37303764
@pytest.mark.usefixtures("feature")
37313765
def test_list_features__without_code_references__returns_empty_counts(
37323766
environment: Environment,

api/tests/unit/projects/code_references/test_unit_projects_code_references_0003_introduce_per_feature_scanned_references.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,7 @@ def test_introduce_per_feature_scanned_references_backward__per_feature_row__reb
374374
revision="rev-1",
375375
code_references=[{"file_path": file_path, "line_number": 1}],
376376
code_references_hash=hash_id,
377+
created_at=timezone.now(),
377378
)
378379

379380
# When
@@ -428,14 +429,14 @@ def test_introduce_per_feature_scanned_references_backward__legacy_row__preserve
428429
url="https://github.flagsmith.com/backend",
429430
vcs_provider="github",
430431
)
431-
with freezegun.freeze_time("2099-01-01T10:00:00+00:00"):
432-
PerFeatureScan.objects.create(
433-
feature=feature,
434-
repository=repository,
435-
revision="rev-1",
436-
code_references=[{"file_path": "a.py", "line_number": 1}],
437-
code_references_hash="hash-1",
438-
)
432+
PerFeatureScan.objects.create(
433+
feature=feature,
434+
repository=repository,
435+
revision="rev-1",
436+
code_references=[{"file_path": "a.py", "line_number": 1}],
437+
code_references_hash="hash-1",
438+
created_at="2099-01-01T10:00:00+00:00",
439+
)
439440

440441
# When
441442
reverted_state = migrator.apply_tested_migration(_INITIAL)

api/tests/unit/projects/code_references/test_unit_projects_code_references_views.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,7 @@ def test_get_feature_code_references__multiple_scans_exist__returns_latest_per_r
239239
{"file_path": "backend/file1.py", "line_number": 20},
240240
],
241241
code_references_hash="hash-backend-1",
242+
created_at=timezone.now(),
242243
)
243244
with freezegun.freeze_time("2099-01-02T11:00:00-0300"):
244245
frontend_repository = VCSRepository.objects.create(
@@ -256,6 +257,7 @@ def test_get_feature_code_references__multiple_scans_exist__returns_latest_per_r
256257
{"file_path": "frontend/file2.js", "line_number": 5},
257258
],
258259
code_references_hash="hash-frontend-2",
260+
created_at=timezone.now(),
259261
)
260262

261263
# When
@@ -332,6 +334,7 @@ def test_get_feature_code_references__feature_flag_removed__returns_no_entry(
332334
{"file_path": "path/to/file1.py", "line_number": 10},
333335
],
334336
code_references_hash="hash-1",
337+
created_at=timezone.now(),
335338
)
336339
with freezegun.freeze_time("2099-01-02T11:00:00-0300"):
337340
repository.last_scanned_at = timezone.now()

0 commit comments

Comments
 (0)