Skip to content

Commit e6a032c

Browse files
authored
✨ Extended cd-create-news to specify reference number (#77)
<!-- Copyright (C) 2020-2022 Arm Limited or its affiliates and Contributors. All rights reserved. SPDX-License-Identifier: Apache-2.0 --> ### Description - the reference number used in the news file name can now be specified from the command line ### 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 9084495 commit e6a032c

File tree

4 files changed

+39
-11
lines changed

4 files changed

+39
-11
lines changed

continuous_delivery_scripts/create_news_file.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,10 @@ def main() -> int:
2929
parser.add_argument(
3030
"-t", "--type", help="News type to create.", choices=[t.name for t in NewsType], default="feature"
3131
)
32+
parser.add_argument("-n", "--ref-number", help="Reference number of the news file to use", required=False)
3233

3334
args = parser.parse_args()
34-
created_file = create_news_file(str(NEWS_DIR), args.news_text, NewsType[args.type])
35+
created_file = create_news_file(str(NEWS_DIR), args.ref_number, args.news_text, NewsType[args.type])
3536

3637
try:
3738
validate_news_file(created_file)

continuous_delivery_scripts/utils/news_file.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import pathlib
88
import logging
99
from datetime import datetime
10-
from typing import Any
10+
from typing import Any, Optional
1111

1212
logger = logging.getLogger(__name__)
1313

@@ -23,21 +23,21 @@ class NewsType(enum.Enum):
2323
removal = 5
2424

2525

26-
def create_news_file(news_dir: str, news_text: str, news_type: Any) -> pathlib.Path:
26+
def create_news_file(news_dir: str, news_ref: Optional[str], news_text: str, news_type: Any) -> pathlib.Path:
2727
"""Facilitates creating a news file, determining its file name based on the type."""
2828
message_type = NewsType.misc
2929
if isinstance(news_type, str):
3030
message_type = NewsType[news_type]
3131
elif isinstance(news_type, NewsType):
3232
message_type = news_type
33-
file_path = determine_news_file_path(news_dir, message_type)
33+
file_path = determine_news_file_path(news_dir, news_ref, message_type)
3434
_write_file(file_path, news_text)
3535
return file_path
3636

3737

38-
def determine_news_file_path(news_dir: str, news_type: NewsType) -> pathlib.Path:
38+
def determine_news_file_path(news_dir: str, news_ref: Optional[str], news_type: NewsType) -> pathlib.Path:
3939
"""Returns an available file path for given news type."""
40-
news_file_name = determine_basic_new_news_file_name()
40+
news_file_name = news_ref if news_ref else determine_basic_new_news_file_name()
4141
news_file_path = pathlib.Path(news_dir, f"{news_file_name}.{news_type.name}")
4242
inc = 0
4343
while news_file_path.exists():

news/20230301133846.feature

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
:sparkles: Extended `create-news` so that the reference number can be specified

tests/news_file/test_create_news_file.py

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ def test_creates_a_file_with_available_file_name_and_returns_its_path(self, _wri
2626
news_text = "Cool feature"
2727
news_type = NewsType.feature
2828

29-
file_path = create_news_file(NEWS_DIR, news_text, news_type)
29+
file_path = create_news_file(NEWS_DIR, None, news_text, news_type)
3030

3131
self.assertEqual(file_path, news_file_path)
32-
determine_news_file_path.assert_called_once_with(NEWS_DIR, news_type)
32+
determine_news_file_path.assert_called_once_with(NEWS_DIR, None, news_type)
3333
_write_file.assert_called_once_with(file_path, news_text)
3434

3535
@mock.patch("continuous_delivery_scripts.utils.news_file.determine_news_file_path")
@@ -40,10 +40,36 @@ def test_creates_a_file_with_message_type_string(self, _write_file, determine_ne
4040
news_text = "Cool feature"
4141
news_type = NewsType.feature
4242

43-
file_path = create_news_file(NEWS_DIR, news_text, news_type.name)
43+
file_path = create_news_file(NEWS_DIR, None, news_text, news_type.name)
4444

4545
self.assertEqual(file_path, news_file_path)
46-
determine_news_file_path.assert_called_once_with(NEWS_DIR, news_type)
46+
determine_news_file_path.assert_called_once_with(NEWS_DIR, None, news_type)
47+
_write_file.assert_called_once_with(file_path, news_text)
48+
49+
@mock.patch("continuous_delivery_scripts.utils.news_file.determine_basic_new_news_file_name")
50+
@mock.patch("continuous_delivery_scripts.utils.news_file._write_file")
51+
def test_creates_a_file_with_news_reference(self, _write_file, determine_basic_new_news_file_name):
52+
determine_basic_new_news_file_name.return_value = "1234501.feature"
53+
news_text = "Cool feature"
54+
news_type = NewsType.feature
55+
ref = "123456"
56+
expected_file_path = pathlib.Path(NEWS_DIR, f"{ref}.feature")
57+
file_path = create_news_file(NEWS_DIR, ref, news_text, news_type.name)
58+
59+
self.assertEqual(file_path, expected_file_path)
60+
_write_file.assert_called_once_with(file_path, news_text)
61+
62+
@mock.patch("continuous_delivery_scripts.utils.news_file.determine_basic_new_news_file_name")
63+
@mock.patch("continuous_delivery_scripts.utils.news_file._write_file")
64+
def test_creates_a_file_without_news_reference(self, _write_file, determine_basic_new_news_file_name):
65+
expected_file_name = "1234501"
66+
determine_basic_new_news_file_name.return_value = expected_file_name
67+
news_text = "Cool feature"
68+
news_type = NewsType.feature
69+
70+
file_path = create_news_file(NEWS_DIR, None, news_text, news_type.name)
71+
72+
self.assertEqual(file_path, pathlib.Path(NEWS_DIR, f"{expected_file_name}.feature"))
4773
_write_file.assert_called_once_with(file_path, news_text)
4874

4975

@@ -59,7 +85,7 @@ def test_finds_first_available_file_path_in_news_dir(self):
5985
pathlib.Path(tmp_dir, f"{news_file_path_today}.{news_type.name}").touch()
6086
pathlib.Path(tmp_dir, f"{news_file_path_today}01.{news_type.name}").touch()
6187

62-
file_path = determine_news_file_path(NEWS_DIR, news_type)
88+
file_path = determine_news_file_path(NEWS_DIR, None, news_type)
6389

6490
self.assertEqual(file_path, pathlib.Path(news_dir, f"{news_file_name_today}02.{news_type.name}"))
6591

0 commit comments

Comments
 (0)