Skip to content

Commit 9084495

Browse files
authored
⚡ Improved assert news performance (#75)
<!-- Copyright (C) 2020-2022 Arm Limited or its affiliates and Contributors. All rights reserved. SPDX-License-Identifier: Apache-2.0 --> ### Description Improved `assert news` performance by first looking for news file in the current commit before exploring the whole branch. ### Test Coverage <!-- Please put an `x` in the correct box e.g. `[x]` to indicate the testing coverage of this change. --> - [x] This change is covered by existing or additional automated tests. - [ ] Manual testing has been performed (and evidence provided) as automated testing was not feasible. - [ ] Additional tests are not required for this change (e.g. documentation update).
1 parent 52febee commit 9084495

File tree

6 files changed

+61
-12
lines changed

6 files changed

+61
-12
lines changed

.secrets.baseline

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@
116116
"filename": "continuous_delivery_scripts/utils/git_helpers.py",
117117
"hashed_secret": "9af3842326eadff7d85a55edfec67f6d8a3a259f",
118118
"is_verified": false,
119-
"line_number": 49
119+
"line_number": 48
120120
}
121121
],
122122
"tests/git_helper/htmlcov/status.json": [
@@ -425,5 +425,5 @@
425425
}
426426
]
427427
},
428-
"generated_at": "2022-12-22T00:31:43Z"
428+
"generated_at": "2023-02-28T00:31:43Z"
429429
}

continuous_delivery_scripts/assert_news.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
"""Checks if valid news files are created for changes in the project."""
66
import argparse
77
import logging
8+
import pathlib
89
import re
910
import sys
1011
from typing import List, Union
11-
import pathlib
1212

1313
from continuous_delivery_scripts.utils.configuration import configuration, ConfigurationVariable
1414
from continuous_delivery_scripts.utils.git_helpers import ProjectTempClone, LocalProjectRepository, GitWrapper
@@ -71,10 +71,15 @@ def find_news_files(git: GitWrapper, root_dir: str, news_dir: str) -> List[str]:
7171
Returns:
7272
list: list of absolute paths to news files
7373
"""
74-
files_changed = git.list_files_added_on_current_branch()
74+
files_changed = git.list_files_added_to_current_commit()
75+
# To speed up the process, we first look at files added to the current commit.
76+
# If no news files were added, then we check for addition on the branch.
7577
# Relies on the fact GitWrapper returns paths that are always relative
7678
# to the project root.
7779
added_news_files = [file_path for file_path in files_changed if file_path.startswith(news_dir)]
80+
if len(added_news_files) == 0:
81+
files_changed = git.list_files_added_on_current_branch()
82+
added_news_files = [file_path for file_path in files_changed if file_path.startswith(news_dir)]
7883
return [str(pathlib.Path(root_dir, file_path)) for file_path in added_news_files]
7984

8085

continuous_delivery_scripts/utils/git_helpers.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,10 @@
77
import os
88
import re
99
import shutil
10-
from pathlib import Path
11-
from typing import Optional, List, Union, Any, Tuple
12-
1310
from git import Repo, Actor, GitCommandError
1411
from packaging import version
12+
from pathlib import Path
13+
from typing import Optional, List, Union, Any, Tuple
1514

1615
from .configuration import configuration, ConfigurationVariable
1716
from .filesystem_helpers import TemporaryDirectory
@@ -401,7 +400,13 @@ def remote_branch_exists(self, branch_name: str) -> bool:
401400
return self.get_remote_branch(branch_name) is not None
402401

403402
def _get_specific_changes(self, change_type: Optional[str], commit1: Any, commit2: Any) -> List[str]:
404-
diff = commit1.diff(commit2)
403+
diff = None
404+
if commit1:
405+
diff = commit1.diff(commit2) if commit2 else commit1.diff()
406+
elif commit2:
407+
diff = commit2.diff()
408+
if not diff:
409+
return []
405410
if change_type:
406411
change_type = change_type.upper()
407412
change_type = change_type if change_type in diff.change_type else None
@@ -547,6 +552,14 @@ def _get_remote(self) -> Optional[Any]:
547552
logger.warning(e)
548553
return None
549554

555+
def list_files_added_to_current_commit(self) -> List[str]:
556+
"""Returns a list of files added in the current commit."""
557+
current_commit = self.repo.head.commit
558+
previous_commit = self.repo.commit("HEAD~1")
559+
if not current_commit:
560+
current_commit = self.get_current_commit()
561+
return self.get_changes_list(previous_commit, current_commit, change_type="a")
562+
550563
def list_files_added_on_current_branch(self) -> List[str]:
551564
"""Returns a list of files changed against master branch."""
552565
master_branch = self.get_master_branch()
@@ -565,8 +578,7 @@ def list_files_added_on_current_branch(self) -> List[str]:
565578
# The branch point off `beta` is more recent than off `master`.
566579
# Hence, the difference between current and `beta` should be considered.
567580
branch_point = beta_branch_point
568-
changes = self.get_changes_list(branch_point, current_branch_commit, change_type="a")
569-
return changes
581+
return self.get_changes_list(branch_point, current_branch_commit, change_type="a")
570582

571583
def is_current_branch_feature(self) -> bool:
572584
"""Returns boolean indicating if current branch is considered a feature."""

news/20230228103345.bugfix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
:zap: Improved the performance of `assert news` for cases where news files are added to the current commit

tests/assert_news/test_news_files_validation.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,31 @@ class TestFindNewsFiles(TestCase):
1313
def test_returns_newly_added_news_files(self):
1414
"""Given added files in git, it returns absolute paths to added news files."""
1515
fake_git_wrapper = mock.Mock(spec_set=GitWrapper)
16+
fake_git_wrapper.list_files_added_to_current_commit.return_value = [
17+
"foo/bar.py",
18+
"news/1234.txt",
19+
"news/wat.html",
20+
]
21+
fake_git_wrapper.list_files_added_on_current_branch.return_value = [
22+
"foo/bar.py",
23+
"news/1234.txt",
24+
"news/wat.html",
25+
]
26+
news_dir = "news/"
27+
root_dir = "/root/"
28+
29+
subject = find_news_files(git=fake_git_wrapper, root_dir=root_dir, news_dir=news_dir)
30+
31+
fake_git_wrapper.list_files_added_to_current_commit.assert_called_once()
32+
fake_git_wrapper.list_files_added_on_current_branch.assert_not_called()
33+
self.assertEqual(
34+
subject, [str(pathlib.Path(root_dir, "news/1234.txt")), str(pathlib.Path(root_dir, "news/wat.html"))]
35+
)
36+
37+
def test_returns_previously_added_news_files(self):
38+
"""Given added files in git, it returns absolute paths to added news files."""
39+
fake_git_wrapper = mock.Mock(spec_set=GitWrapper)
40+
fake_git_wrapper.list_files_added_to_current_commit.return_value = []
1641
fake_git_wrapper.list_files_added_on_current_branch.return_value = [
1742
"foo/bar.py",
1843
"news/1234.txt",
@@ -23,6 +48,8 @@ def test_returns_newly_added_news_files(self):
2348

2449
subject = find_news_files(git=fake_git_wrapper, root_dir=root_dir, news_dir=news_dir)
2550

51+
fake_git_wrapper.list_files_added_to_current_commit.assert_called_once()
52+
fake_git_wrapper.list_files_added_on_current_branch.assert_called_once()
2653
self.assertEqual(
2754
subject, [str(pathlib.Path(root_dir, "news/1234.txt")), str(pathlib.Path(root_dir, "news/wat.html"))]
2855
)

tests/git_helper/test_git_helpers.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,10 @@ def test_file_addition(self):
115115
clone.commit("Test commit")
116116
self.assertNotEqual(previous_hash, clone.get_commit_hash())
117117
self.assertEqual(previous_count + 1, clone.get_commit_count())
118-
added_files = [Path(clone.root).joinpath(f) for f in clone.list_files_added_on_current_branch()]
119-
self.assertTrue(test_file in added_files)
118+
added_files_to_commit = [Path(clone.root).joinpath(f) for f in clone.list_files_added_to_current_commit()]
119+
self.assertTrue(test_file in added_files_to_commit)
120+
added_files_to_branch = [Path(clone.root).joinpath(f) for f in clone.list_files_added_on_current_branch()]
121+
self.assertTrue(test_file in added_files_to_branch)
120122

121123
def test_repo_clean(self):
122124
"""Test basic git clean on the clone."""
@@ -166,5 +168,7 @@ def test_file_addition_with_paths_to_initial_repository(self):
166168
self.assertTrue(clone.get_corresponding_path(test_file) in uncommitted_changes)
167169
clone.add(test_file)
168170
clone.commit("Test commit")
171+
added_files_to_commit = [Path(git.root).joinpath(f) for f in clone.list_files_added_to_current_commit()]
169172
added_files = [Path(git.root).joinpath(f) for f in clone.list_files_added_on_current_branch()]
170173
self.assertTrue(test_file in added_files)
174+
self.assertTrue(test_file in added_files_to_commit)

0 commit comments

Comments
 (0)