def draft_changelog(version_spec, branch, repo, since, auth, changelog_path, dry_run): """Create a changelog entry PR""" repo = repo or util.get_repo() branch = branch or util.get_branch() version = util.get_version() tags = util.run("git --no-pager tag") if f"v{version}" in tags.splitlines(): raise ValueError(f"Tag v{version} already exists") # Check out any unstaged files from version bump util.run("git checkout -- .") current = changelog.extract_current(changelog_path) util.log(f"\n\nCurrent Changelog Entry:\n{current}") title = f"{changelog.PR_PREFIX} for {version} on {branch}" commit_message = f'git commit -a -m "{title}"' body = title # Check for multiple versions if util.PACKAGE_JSON.exists(): body += npm.get_package_versions(version) body += '\n\nAfter merging this PR run the "Draft Release" Workflow on your fork of `jupyter_releaser` with the following inputs' body += f""" | Input | Value | | ------------- | ------------- | | Target | {repo} | | Branch | {branch} | | Version Spec | {version_spec} | """ if since: body += f"| Since | {since} |" util.log(body) make_changelog_pr(auth, branch, repo, title, commit_message, body, dry_run=dry_run)
def forwardport_changelog( auth, ref, branch, repo, username, changelog_path, dry_run, git_url, release_url ): """Forwardport Changelog Entries to the Default Branch""" # Set up the git repo with the branch match = parse_release_url(release_url) gh = GhApi(owner=match["owner"], repo=match["repo"], token=auth) release = util.release_for_url(gh, release_url) tag = release.tag_name repo = f'{match["owner"]}/{match["repo"]}' # We want to target the main branch orig_dir = os.getcwd() branch = prep_git(None, None, repo, auth, username, git_url, install=False) os.chdir(util.CHECKOUT_NAME) # Bail if the tag has been merged to the branch tags = util.run(f"git --no-pager tag --merged {branch}") if tag in tags.splitlines(): util.log(f"Skipping since tag is already merged into {branch}") return # Get the entry for the tag util.run(f"git checkout {tag}") entry = changelog.extract_current(changelog_path) # Get the previous header for the branch full_log = Path(changelog_path).read_text(encoding="utf-8") start = full_log.index(changelog.END_MARKER) prev_header = "" for line in full_log[start:].splitlines(): if line.strip().startswith("#"): prev_header = line break if not prev_header: raise ValueError("No anchor for previous entry") # Check out the branch again util.run(f"git checkout -B {branch} origin/{branch}") default_entry = changelog.extract_current(changelog_path) # Look for the previous header default_log = Path(changelog_path).read_text(encoding="utf-8") if not prev_header in default_log: util.log( f'Could not find previous header "{prev_header}" in {changelog_path} on branch {branch}' ) return # If the previous header is the current entry in the default branch, we need to move the change markers if prev_header in default_entry: default_log = changelog.insert_entry(default_log, entry) # Otherwise insert the new entry ahead of the previous header else: insertion_point = default_log.index(prev_header) default_log = changelog.format( default_log[:insertion_point] + entry + default_log[insertion_point:] ) Path(changelog_path).write_text(default_log, encoding="utf-8") # Create a forward port PR title = f"{changelog.PR_PREFIX} Forward Ported from {tag}" commit_message = f'git commit -a -m "{title}"' body = title pr = make_changelog_pr( auth, ref, branch, repo, title, commit_message, body, dry_run=dry_run ) # Clean up after ourselves os.chdir(orig_dir) shutil.rmtree(util.CHECKOUT_NAME)
def draft_release( ref, branch, repo, auth, changelog_path, version_cmd, dist_dir, dry_run, post_version_spec, assets, ): """Publish Draft GitHub release and handle post version bump""" branch = branch or util.get_branch() repo = repo or util.get_repo() assets = assets or glob(f"{dist_dir}/*") version = util.get_version() body = changelog.extract_current(changelog_path) prerelease = util.is_prerelease(version) # Bump to post version if given if post_version_spec: post_version = bump_version(post_version_spec, version_cmd) util.log(f"Bumped version to {post_version}") util.run(f'git commit -a -m "Bump to {post_version}"') if dry_run: return owner, repo_name = repo.split("/") gh = GhApi(owner=owner, repo=repo_name, token=auth) # Remove draft releases over a day old if bool(os.environ.get("GITHUB_ACTIONS")): for release in gh.repos.list_releases(): if str(release.draft).lower() == "false": continue created = release.created_at d_created = datetime.strptime(created, r"%Y-%m-%dT%H:%M:%SZ") delta = datetime.utcnow() - d_created if delta.days > 0: gh.repos.delete_release(release.id) remote_url = util.run("git config --get remote.origin.url") if not os.path.exists(remote_url): util.run(f"git push origin HEAD:{branch} --follow-tags --tags") util.log(f"Creating release for {version}") util.log(f"With assets: {assets}") release = gh.create_release( f"v{version}", branch, f"Release v{version}", body, True, prerelease, files=assets, ) # Set the GitHub action output util.actions_output("release_url", release.html_url)