Skip to content
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

Keep more of GitHub release notes body #327

Merged
merged 1 commit into from
Mar 21, 2024
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
50 changes: 20 additions & 30 deletions scripts/release.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,11 @@ def verify_build(is_test: str) -> None:


def generate_github_release_notes_body(token: str, version: str) -> str:
"""Generate and grab release notes URL from Github."""
"""Generate and grab release notes URL from Github.

Delete their first paragraph, because we track its contents in a tighter
form in CHANGELOG.md. See `get_changelog_release_notes`.
"""
response = requests.post(
"https://api.github.com/repos/john-kurkowski/tldextract/releases/generate-notes",
headers={
Expand All @@ -100,24 +104,13 @@ def generate_github_release_notes_body(token: str, version: str) -> str:
file=sys.stderr,
)
return ""
return str(response.json()["body"])


def get_release_notes_url(body: str) -> str:
"""Parse the release notes content to get the changelog URL."""
url_pattern = re.compile(r"\*\*Full Changelog\*\*: (.*)$")
match = url_pattern.search(body)
if match:
return match.group(1)
else:
print(
"WARNING: Failed to parse release notes URL from GitHub response.",
file=sys.stderr,
)
return ""
body = str(response.json()["body"])
paragraphs = body.split("\n\n")
return "\n\n".join(paragraphs[1:])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So concise and it got rid of the REGEX-- love it. Still a little brittle if this format changes, but I think this is a great improvement.



def get_changelog_release_notes(release_notes_url: str, version: str) -> str:
def get_changelog_release_notes(version: str) -> str:
"""Get the changelog release notes.

Uses a regex starting on a heading beginning with the version number
Expand All @@ -131,25 +124,15 @@ def get_changelog_release_notes(release_notes_url: str, version: str) -> str:
if match:
return str(match.group(1)).strip()
else:
print(
f"WARNING: Failed to parse changelog release notes. Manually copy this version's notes from the CHANGELOG.md file to {release_notes_url}.",
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Haven't tripped on this error handler yet, but I think the URL in it was wrong. It's a compare link like 5.1.1...5.1.2, not a create-release link. I moved this error print to after we have the release link.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, good point. I think I garbled this warning when I added the release_notes_url. Your new placement is correct: it should be directing the user to the url of the newly created draft release to manually add the notes. Thanks for catching this!

file=sys.stderr,
)
return ""


def create_release_notes_body(token: str, version: str) -> str:
"""Compile the release notes."""
github_release_body = generate_github_release_notes_body(token, version)
release_notes_url = get_release_notes_url(github_release_body)
changelog_notes = get_changelog_release_notes(release_notes_url, version)
full_release_notes = f"{changelog_notes}\n\n**Full Changelog**: {release_notes_url}"
return full_release_notes


def create_github_release_draft(token: str, version: str) -> None:
"""Create a release on GitHub."""
release_body = create_release_notes_body(token, version)
github_release_body = generate_github_release_notes_body(token, version)
changelog_notes = get_changelog_release_notes(version)
release_body = f"{changelog_notes}\n\n{github_release_body}"

response = requests.post(
"https://api.github.com/repos/john-kurkowski/tldextract/releases",
headers={
Expand All @@ -174,8 +157,15 @@ def create_github_release_draft(token: str, version: str) -> None:
file=sys.stderr,
)
return

print(f'Release created successfully: {response.json()["html_url"]}')

if not changelog_notes:
print(
"WARNING: Failed to parse changelog release notes. Manually copy this version's notes from the CHANGELOG.md file to the above URL.",
file=sys.stderr,
)


def upload_build_to_pypi(is_test: str) -> None:
"""Upload the build to PyPI."""
Expand Down
3 changes: 3 additions & 0 deletions tests/__snapshots__/test_release.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@
* Misc.
* Increase typecheck aggression

## New Contributors
* @jdoe contributed

**Full Changelog**: fake-body
''',
'draft': True,
Expand Down
2 changes: 1 addition & 1 deletion tests/test_release.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def mock_post(*args: Any, **kwargs: Any) -> mock.Mock:
return mock.Mock(
json=mock.Mock(
return_value={
"body": "Body start **Full Changelog**: fake-body",
"body": "## What's Changed\nGitHub changelog here\n\n## New Contributors\n* @jdoe contributed\n\n**Full Changelog**: fake-body",
"html_url": "https://github.com/path/to/release",
}
),
Expand Down