Skip to content

Commit

Permalink
Keep more of GitHub release notes body (#327)
Browse files Browse the repository at this point in the history
Only omit the first paragraph, "What's Changed." Keep the e.g. "New Contributors" paragraph and "Full Changelog" link.
  • Loading branch information
john-kurkowski committed Mar 21, 2024
1 parent f4e993f commit 1d40c76
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 31 deletions.
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:])


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}.",
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

0 comments on commit 1d40c76

Please sign in to comment.