Skip to content

Commit

Permalink
ci: automate changelog additions (#8738)
Browse files Browse the repository at this point in the history
This change partially addresses #7227 by adding release notes to the
changelog file during the release process. This was tested first with
`DRY_RUN=1` and later without that flag during the 2.6.9 release
process. The pull request created during that test is #8737.

## Checklist

- [x] Change(s) are motivated and described in the PR description
- [x] Testing strategy is described if automated tests are not included
in the PR
- [x] Risks are described (performance impact, potential for breakage,
maintainability)
- [x] Change is maintainable (easy to change, telemetry, documentation)
- [x] [Library release note
guidelines](https://ddtrace.readthedocs.io/en/stable/releasenotes.html)
are followed or label `changelog/no-changelog` is set
- [x] Documentation is included (in-code, generated user docs, [public
corp docs](https://github.com/DataDog/documentation/))
- [x] Backport labels are set (if
[applicable](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting))
- [x] If this PR changes the public interface, I've notified
`@DataDog/apm-tees`.
- [x] If change touches code that signs or publishes builds or packages,
or handles credentials of any kind, I've requested a review from
`@DataDog/security-design-and-guidance`.

## Reviewer Checklist

- [x] Title is accurate
- [x] All changes are related to the pull request's stated goal
- [x] Description motivates each change
- [x] Avoids breaking
[API](https://ddtrace.readthedocs.io/en/stable/versioning.html#interfaces)
changes
- [x] Testing strategy adequately addresses listed risks
- [x] Change is maintainable (easy to change, telemetry, documentation)
- [x] Release note makes sense to a user of the library
- [x] Author has acknowledged and discussed the performance implications
of this PR as reported in the benchmarks PR comment
- [x] Backport labels are set in a manner that is consistent with the
[release branch maintenance
policy](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting)
  • Loading branch information
emmettbutler authored Mar 22, 2024
1 parent e95e097 commit 760d662
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions scripts/release.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
ReleaseParameters = namedtuple("ReleaseParameters", ["branch", "name", "tag", "dd_repo", "rn", "prerelease"])
DEFAULT_BRANCH = "main"
DRY_RUN = os.getenv("DRY_RUN", "0") == "1"
CHANGELOG_FILENAME = "../CHANGELOG.md"


def _ensure_current_checkout():
Expand Down Expand Up @@ -244,6 +245,44 @@ def get_ddtrace_repo():
return Github(gh_token).get_repo(full_name_or_id="DataDog/dd-trace-py")


def add_release_to_changelog(name: str, release_notes: str):
separator = "---"
with open(CHANGELOG_FILENAME, "r") as changelog_file:
contents = changelog_file.read()
existing_release_notes_lines = contents.split("\n")
insert_index = existing_release_notes_lines.index(separator)
new_notes = ["", separator, "", f"## {name}", ""] + release_notes.split("\n")
composite = (
existing_release_notes_lines[: insert_index - 1] + new_notes + existing_release_notes_lines[insert_index:]
)
with open(CHANGELOG_FILENAME, "w") as changelog_file:
changelog_file.write("\n".join(composite))


def commit_and_push(dd_repo, branch_name: str, release_name: str):
subprocess.check_output(f"git checkout -b {branch_name}", shell=True, cwd=os.pardir)
subprocess.check_output("git add -A", shell=True, cwd=os.pardir)
print(f"Committing changes to {CHANGELOG_FILENAME} on branch {branch_name}")
pr_body = f"update changelog for version {release_name}"
subprocess.check_output(f"git commit -m '{pr_body} via release script'", shell=True, cwd=os.pardir)
subprocess.check_output(f"git push origin {branch_name}", shell=True, cwd=os.pardir)
dd_repo.create_pull(DEFAULT_BRANCH, branch_name, title=f"chore: {pr_body}", body=f"- [x] {pr_body}", draft=False)


def create_changelog_pull_request(dd_repo, name: str, release_notes: str):
subprocess.check_output(f"git checkout {DEFAULT_BRANCH}", shell=True, cwd=os.pardir)
add_release_to_changelog(name, release_notes)
if not DRY_RUN:
commit_and_push(dd_repo, f"release.script/changelog-update-{name}", name)
else:
diff = subprocess.check_output("git diff", shell=True, cwd=os.pardir)
subprocess.check_output("git stash", shell=True, cwd=os.pardir)
diff = "\n".join(line.decode() for line in diff.split(b"\n"))
print(
f"\nDRY RUN: The following diff would be committed:\n\n{diff}\n\nDRY RUN: These changes have been stashed."
)


def create_notebook(dd_repo, name, rn, base):
dd_api_key = os.getenv("DD_API_KEY_STAGING")
dd_app_key = os.getenv("DD_APP_KEY_STAGING")
Expand Down Expand Up @@ -446,6 +485,8 @@ def _dry(fn: Callable, description: Optional[str] = None) -> Any:
"No notebook will be created at this time."
)
)
else:
create_changelog_pull_request(dd_repo, name, rn)

# switch back to original git branch
subprocess.check_output(
Expand Down

0 comments on commit 760d662

Please sign in to comment.