Skip to content

Commit 5b0386e

Browse files
#152 - Refactor Record call (#154)
* #152 - Refactor Record call - Review and update of unit tests related to Record and its child subclasses.
1 parent 639531a commit 5b0386e

16 files changed

+1345
-874
lines changed

release_notes_generator/miner.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,9 @@
2929
from github.Repository import Repository
3030

3131
from release_notes_generator.action_inputs import ActionInputs
32+
from release_notes_generator.model.issue_record import IssueRecord
3233
from release_notes_generator.model.mined_data import MinedData
33-
from release_notes_generator.utils.constants import ISSUE_STATE_ALL, PR_STATE_CLOSED
34+
from release_notes_generator.model.pull_request_record import PullRequestRecord
3435
from release_notes_generator.utils.decorators import safe_call_decorator
3536
from release_notes_generator.utils.github_rate_limiter import GithubRateLimiter
3637

@@ -63,7 +64,7 @@ def mine_data(self) -> MinedData:
6364
self._get_issues(data)
6465

6566
# pulls and commits, and then reduce them by the latest release since time
66-
data.pull_requests = list(self._safe_call(data.repository.get_pulls)(state=PR_STATE_CLOSED))
67+
data.pull_requests = list(self._safe_call(data.repository.get_pulls)(state=PullRequestRecord.PR_STATE_CLOSED))
6768
data.commits = list(self._safe_call(data.repository.get_commits)())
6869

6970
logger.info("Data mining from GitHub completed.")
@@ -117,14 +118,16 @@ def _get_issues(self, data: MinedData):
117118
logger.info("Fetching issues from repository...")
118119
# get all issues
119120
if data.release is None:
120-
data.issues = list(self._safe_call(data.repository.get_issues)(state=ISSUE_STATE_ALL))
121+
data.issues = list(self._safe_call(data.repository.get_issues)(state=IssueRecord.ISSUE_STATE_ALL))
121122
else:
122123
# default is repository creation date if no releases OR created_at of latest release
123124
data.since = data.release.created_at if data.release else data.repository.created_at
124125
if data.release and ActionInputs.get_published_at():
125126
data.since = data.release.published_at
126127

127-
data.issues = list(self._safe_call(data.repository.get_issues)(state=ISSUE_STATE_ALL, since=data.since))
128+
data.issues = list(
129+
self._safe_call(data.repository.get_issues)(state=IssueRecord.ISSUE_STATE_ALL, since=data.since)
130+
)
128131

129132
logger.info("Fetched %d issues", len(data.issues))
130133

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
"""
2+
A module that defines the CommitRecord class, which represents a direct commit record in the release notes.
3+
"""
4+
5+
from typing import Optional
6+
7+
from github.Commit import Commit
8+
9+
from release_notes_generator.action_inputs import ActionInputs
10+
from release_notes_generator.model.record import Record
11+
12+
13+
class CommitRecord(Record):
14+
"""
15+
A class used to represent a direct commit record in the release notes.
16+
Inherits from Record and provides additional functionality specific to direct commits.
17+
"""
18+
19+
def __init__(self, commit: Commit, skip: bool = False):
20+
super().__init__(skip=skip)
21+
22+
self._commit: Commit = commit
23+
24+
# properties - override Record properties
25+
26+
@property
27+
def record_id(self) -> int | str:
28+
return self._commit.sha
29+
30+
@property
31+
def is_closed(self) -> bool:
32+
return True
33+
34+
@property
35+
def is_open(self) -> bool:
36+
return False
37+
38+
@property
39+
def labels(self):
40+
return []
41+
42+
@property
43+
def authors(self) -> list[str]:
44+
return [self._commit.author.login] if self._commit.author else []
45+
46+
# properties - specific to CommitRecord
47+
48+
@property
49+
def commit(self) -> Commit:
50+
"""
51+
Gets the commits associated with the record.
52+
Returns: The commits associated with the record.
53+
"""
54+
return self._commit
55+
56+
# methods - override Record methods
57+
58+
def to_chapter_row(self) -> str:
59+
self.added_into_chapters()
60+
row_prefix = f"{ActionInputs.get_duplicity_icon()} " if self.present_in_chapters() > 1 else ""
61+
62+
# collecting values for formatting
63+
commit_message = self._commit.commit.message.replace("\n", " ")
64+
row = f"{row_prefix}Commit: {self._commit.sha[:7]} - {commit_message}"
65+
66+
if self.contains_release_notes():
67+
row = f"{row}\n{self.get_rls_notes()}"
68+
69+
return row
70+
71+
def get_rls_notes(self, line_marks: Optional[list[str]] = None) -> str:
72+
# TODO - in this version - direct commits does not support release notes
73+
return ""
74+
75+
# methods - specific to CommitRecord

release_notes_generator/model/custom_chapters.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,14 @@
1818
This module contains the CustomChapters class which is responsible for representing the custom chapters in the release
1919
notes.
2020
"""
21+
from typing import cast
2122

2223
from release_notes_generator.action_inputs import ActionInputs
2324
from release_notes_generator.model.base_chapters import BaseChapters
2425
from release_notes_generator.model.chapter import Chapter
25-
from release_notes_generator.model.record import Record, CommitRecord
26+
from release_notes_generator.model.commit_record import CommitRecord
27+
from release_notes_generator.model.issue_record import IssueRecord
28+
from release_notes_generator.model.record import Record
2629
from release_notes_generator.utils.enums import DuplicityScopeEnum
2730

2831

@@ -55,7 +58,11 @@ def populate(self, records: dict[int | str, Record]) -> None:
5558
continue
5659

5760
for record_label in records[record_id].labels: # iterate all labels of the record (issue, or 1st PR)
58-
if record_label in ch.labels and records[record_id].pulls_count > 0:
61+
pulls_count = 1
62+
if isinstance(records[record_id], IssueRecord):
63+
pulls_count = cast(IssueRecord, records[record_id]).pull_requests_count()
64+
65+
if record_label in ch.labels and pulls_count > 0:
5966
if not records[record_id].is_present_in_chapters:
6067
ch.add_row(record_id, records[record_id].to_chapter_row())
6168
self.populated_record_numbers_list.append(record_id)

0 commit comments

Comments
 (0)