Skip to content

#47 - Apply since logic to new architecture solution #63

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ Generate Release Notes action is dedicated to enhance the quality and organizati
- **Required**: Yes

### `published-at`
- **Description**: Set to true to enable the use of the `published-at` timestamp as the reference point for searching closed issues and PRs, instead of the `created-at` date of the latest release.
- **Description**: Set to true to enable the use of the `published-at` timestamp as the reference point for searching closed issues and PRs, instead of the `created-at` date of the latest release. If first release, repository creation date is used.
- **Required**: No
- **Default**: false

Expand Down
2 changes: 1 addition & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ inputs:
description: 'JSON string defining chapters and corresponding labels for categorization.'
required: false
published-at:
description: 'Use `published-at` timestamp instead of `created-at` as the reference point.'
description: 'Use `published-at` timestamp instead of `created-at` as the reference point of previous Release.'
required: false
default: 'false'
skip-release-notes-label:
Expand Down
22 changes: 18 additions & 4 deletions release_notes_generator/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def generate(self) -> Optional[str]:
"""
Generates the release notes for a given repository.

:return: The generated release notes as a string, or None if the repository could not be found.
@return: The generated release notes as a string, or None if the repository could not be found.
"""
repo = self.safe_call(self.github_instance.get_repo)(ActionInputs.get_github_repository())
if repo is None:
Expand All @@ -56,10 +56,24 @@ def generate(self) -> Optional[str]:
if rls is None:
logging.info(f"Latest release not found for {repo.full_name}. 1st release for repository!")

since = rls.published_at if rls else repo.created_at
# default is repository creation date if no releases OR created_at of latest release
since = rls.created_at if rls else repo.created_at
if rls and ActionInputs.get_published_at():
since = rls.published_at

issues = self.safe_call(repo.get_issues)(state=Constants.ISSUE_STATE_ALL, since=since)
pulls = self.safe_call(repo.get_pulls)(state='closed')
commits = self.safe_call(repo.get_commits)()
pulls = pulls_all = self.safe_call(repo.get_pulls)(state='closed')
commits = commits_all = list(self.safe_call(repo.get_commits)())

if rls is not None:
logging.info(f"Count of issues: {len(list(issues))}")

# filter out merged PRs and commits before the since date
pulls = list(filter(lambda pull: pull.merged_at is not None and pull.merged_at > since, list(pulls_all)))
logging.debug(f"Count of pulls reduced from {len(list(pulls_all))} to {len(pulls)}")

commits = list(filter(lambda commit: commit.commit.author.date > since, list(commits_all)))
logging.debug(f"Count of commits reduced from {len(list(commits_all))} to {len(commits)}")

changelog_url = get_change_url(tag_name=ActionInputs.get_tag_name(), repository=repo, git_release=rls)

Expand Down
2 changes: 1 addition & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ def mock_issue_closed(mocker):
label2.name = 'label2'
issue.labels = [label1, label2]
issue.title = 'Fix the bug'
issue.number = 122
issue.number = 121
return issue


Expand Down
4 changes: 2 additions & 2 deletions tests/release_notes/model/test_record.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ def test_to_chapter_row_with_pull(record_with_no_issue_one_pull_closed):


def test_to_chapter_row_with_issue(record_with_issue_closed_one_pull):
expected_row = "#122 _Fix the bug_ in [#123](https://github.com/org/repo/pull/123)\n - Fixed bug\n - Improved performance"
expected_row = "#121 _Fix the bug_ in [#123](https://github.com/org/repo/pull/123)\n - Fixed bug\n - Improved performance"
assert expected_row == record_with_issue_closed_one_pull.to_chapter_row()


Expand All @@ -143,7 +143,7 @@ def test_to_chapter_row_with_pull_no_rls_notes(record_with_no_issue_one_pull_clo


def test_to_chapter_row_with_issue_no_rls_notes(record_with_issue_closed_one_pull_no_rls_notes):
expected_row = "#122 _Fix the bug_ in [#123](https://github.com/org/repo/pull/123)"
expected_row = "#121 _Fix the bug_ in [#123](https://github.com/org/repo/pull/123)"
assert expected_row == record_with_issue_closed_one_pull_no_rls_notes.to_chapter_row()


Expand Down
2 changes: 1 addition & 1 deletion tests/release_notes/test_record_formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
def test_format_record_with_issue(record_with_issue_closed_one_pull):
formatter = RecordFormatter()
formatted = formatter.format(record_with_issue_closed_one_pull)
expected_output = "- #122 _Fix the bug_ implemented by developers in [#123](http://example.com/pull/123)\n - release notes 1\n - release notes 2"
expected_output = "- #121 _Fix the bug_ implemented by developers in [#123](http://example.com/pull/123)\n - release notes 1\n - release notes 2"

assert expected_output == formatted

Expand Down
8 changes: 4 additions & 4 deletions tests/release_notes/test_release_notes_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,10 +172,10 @@ def __init__(self, name):
"""

release_notes_data_service_chapters_closed_issue_no_pr_no_user_labels = """### Closed Issues without Pull Request ⚠️
- #122 _Fix the bug_
- #121 _Fix the bug_

### Closed Issues without User Defined Labels ⚠️
- #122 _Fix the bug_
- #121 _Fix the bug_

#### Full Changelog
http://example.com/changelog
Expand Down Expand Up @@ -222,7 +222,7 @@ def __init__(self, name):
"""

release_notes_data_closed_issue_no_pr_with_user_labels = """### Closed Issues without Pull Request ⚠️
- #122 _Fix the bug_
- #121 _Fix the bug_

#### Full Changelog
http://example.com/changelog
Expand Down Expand Up @@ -270,7 +270,7 @@ def __init__(self, name):
"""

release_notes_data_closed_issue_with_merged_prs_without_user_labels = """### Closed Issues without User Defined Labels ⚠️
- #122 _Fix the bug_ in [#123](https://github.com/org/repo/pull/123)
- #121 _Fix the bug_ in [#123](https://github.com/org/repo/pull/123)
- Fixed bug
- Improved performance

Expand Down
101 changes: 93 additions & 8 deletions tests/test_release_notes_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#

import time
from datetime import datetime
from datetime import datetime, timedelta

from github import Github

Expand All @@ -41,24 +41,109 @@ def test_generate_release_notes_repository_not_found(mocker):
assert release_notes is None


def test_generate_release_notes_latest_release_not_found(mocker, mock_repo, mock_issue_closed_i1_bug,
mock_pull_closed_with_rls_notes_101, mock_commit):
def test_generate_release_notes_latest_release_not_found(
mocker, mock_repo, mock_git_release, mock_issue_closed, mock_issue_closed_i1_bug,
mock_pull_closed_with_rls_notes_101, mock_pull_closed_with_rls_notes_102, mock_commit
):
github_mock = mocker.Mock(spec=Github)
github_mock.get_repo.return_value = mock_repo
mock_repo.created_at = datetime.now()
mock_repo.get_issues.return_value = [mock_issue_closed_i1_bug]
mock_repo.get_pulls.return_value = [mock_pull_closed_with_rls_notes_101]
mock_repo.created_at = datetime.now() - timedelta(days=10)
mock_repo.get_issues.return_value = [mock_issue_closed, mock_issue_closed_i1_bug]
mock_repo.get_pulls.return_value = [mock_pull_closed_with_rls_notes_101, mock_pull_closed_with_rls_notes_102]
mock_repo.get_commits.return_value = [mock_commit]

mock_issue_closed.created_at = mock_repo.created_at + timedelta(days=2)
mock_issue_closed_i1_bug.created_at = mock_repo.created_at + timedelta(days=7)
mock_pull_closed_with_rls_notes_101.merged_at = mock_repo.created_at + timedelta(days=2)
mock_pull_closed_with_rls_notes_102.merged_at = mock_repo.created_at + timedelta(days=7)

github_mock.get_repo().get_latest_release.return_value = None

mock_rate_limit = mocker.Mock()
mock_rate_limit.core.remaining = 10
mock_rate_limit.core.reset.timestamp.return_value = time.time() + 3600
mock_rate_limit.core.remaining = 1000
github_mock.get_rate_limit.return_value = mock_rate_limit

custom_chapters = CustomChapters(print_empty_chapters=True)

release_notes = ReleaseNotesGenerator(github_mock, custom_chapters).generate()

print(release_notes)
assert release_notes is not None
assert "- #121 _Fix the bug_" in release_notes
assert "- #122 _I1+bug_" in release_notes
assert "- PR: #101 _Fixed bug_" in release_notes
assert "- PR: #102 _Fixed bug_" in release_notes


def test_generate_release_notes_latest_release_found_by_created_at(
mocker, mock_repo, mock_git_release, mock_issue_closed_i1_bug,
mock_pull_closed_with_rls_notes_101, mock_pull_closed_with_rls_notes_102, mock_commit
):
github_mock = mocker.Mock(spec=Github)
github_mock.get_repo.return_value = mock_repo
mock_repo.created_at = datetime.now() - timedelta(days=10)

mock_repo.get_issues.return_value = [mock_issue_closed_i1_bug]
mock_repo.get_pulls.return_value = [mock_pull_closed_with_rls_notes_101, mock_pull_closed_with_rls_notes_102]
mock_repo.get_commits.return_value = [mock_commit]
mock_commit.commit.author.date = mock_repo.created_at + timedelta(days=1)

mock_issue_closed_i1_bug.created_at = mock_repo.created_at + timedelta(days=7)
mock_pull_closed_with_rls_notes_101.merged_at = mock_repo.created_at + timedelta(days=2)
mock_pull_closed_with_rls_notes_102.merged_at = mock_repo.created_at + timedelta(days=7)

github_mock.get_repo().get_latest_release.return_value = mock_git_release
mock_git_release.created_at = mock_repo.created_at + timedelta(days=5)
mock_git_release.published_at = mock_repo.created_at + timedelta(days=5)

mock_rate_limit = mocker.Mock()
mock_rate_limit.core.remaining = 1000
github_mock.get_rate_limit.return_value = mock_rate_limit

custom_chapters = CustomChapters(print_empty_chapters=True)

release_notes = ReleaseNotesGenerator(github_mock, custom_chapters).generate()

assert release_notes is not None
assert "- #122 _I1+bug_" in release_notes
assert "- PR: #101 _Fixed bug_" not in release_notes
assert "- PR: #102 _Fixed bug_" in release_notes


def test_generate_release_notes_latest_release_found_by_published_at(
mocker, mock_repo, mock_git_release, mock_issue_closed_i1_bug,
mock_pull_closed_with_rls_notes_101, mock_pull_closed_with_rls_notes_102, mock_commit
):
github_mock = mocker.Mock(spec=Github)
github_mock.get_repo.return_value = mock_repo
mock_repo.created_at = datetime.now() - timedelta(days=10)

mocker.patch('release_notes_generator.action_inputs.ActionInputs.get_published_at',
return_value='true')

mock_repo.get_issues.return_value = [mock_issue_closed_i1_bug]
mock_repo.get_pulls.return_value = [mock_pull_closed_with_rls_notes_101, mock_pull_closed_with_rls_notes_102]
mock_repo.get_commits.return_value = [mock_commit]
mock_commit.commit.author.date = mock_repo.created_at + timedelta(days=1)

mock_issue_closed_i1_bug.created_at = mock_repo.created_at + timedelta(days=7)
mock_issue_closed_i1_bug.published_at = mock_repo.created_at + timedelta(days=8)
mock_pull_closed_with_rls_notes_101.merged_at = mock_repo.created_at + timedelta(days=2)
mock_pull_closed_with_rls_notes_102.merged_at = mock_repo.created_at + timedelta(days=7)

github_mock.get_repo().get_latest_release.return_value = mock_git_release
mock_git_release.created_at = mock_repo.created_at + timedelta(days=5)
mock_git_release.published_at = mock_repo.created_at + timedelta(days=5)

mock_rate_limit = mocker.Mock()
mock_rate_limit.core.remaining = 1000
github_mock.get_rate_limit.return_value = mock_rate_limit

custom_chapters = CustomChapters(print_empty_chapters=True)

release_notes = ReleaseNotesGenerator(github_mock, custom_chapters).generate()

assert release_notes is not None
assert "- #122 _I1+bug_" in release_notes
assert "- PR: #101 _Fixed bug_" not in release_notes
assert "- PR: #102 _Fixed bug_" in release_notes
Loading