Skip to content

Commit

Permalink
New settings for draft releases and automatic release notes (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
davegaeddert authored Oct 5, 2021
1 parent e714185 commit a036b64
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 11 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ jobs:
github_token: ${{ secrets.GITHUB_TOKEN }}
tag_prefix: v # default
next_branch: nextrelease # default
github_release: publish # or "draft", or "skip"
release_notes: "" # or "generate" to use https://docs.github.com/en/repositories/releasing-projects-on-github/automatically-generated-release-notes
```
### Using `prepare_cmd`
Expand Down
8 changes: 8 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ inputs:
github_token:
description: A GitHub token that can create and modify releases
required: true
github_release:
description: Should be "publish" to create and publish a GitHub Release, "draft" for a draft release that can be published manually, or "skip" for no release.
default: publish
release_notes:
description: Use "generate" to ask GitHub to generate the release notes automatically (using https://docs.github.com/en/rest/reference/repos#generate-release-notes-content-for-a-release).
default: ""
runs:
using: composite
steps:
Expand All @@ -32,3 +38,5 @@ runs:
PUBLISH_CMD: ${{ inputs.publish_cmd }}
PREPARE_CMD: ${{ inputs.prepare_cmd }}
GITHUB_TOKEN: ${{ inputs.github_token }}
GITHUB_RELEASE: ${{ inputs.github_release }}
RELEASE_NOTES: ${{ inputs.release_notes }}
71 changes: 60 additions & 11 deletions nextrelease/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,45 @@
cls_client.set_version(__version__)


def release_commit(requests_session, repo_full_name, tag_prefix, publish_cmd):
def release_commit(
requests_session,
repo_full_name,
tag_prefix,
publish_cmd,
github_release,
release_notes,
):
version = git.get_release_commit_version()
print(f"Releasing version {version} from commit")
version_semver = semver.VersionInfo.parse(version)
tag_name = git.tag_commit(version=version, tag_prefix=tag_prefix)

print("Creating GitHub release")
response = requests_session.post(
f"/repos/{repo_full_name}/releases",
json={
"tag_name": tag_name,
"prerelease": bool(version_semver.prerelease),
},
)
response.raise_for_status()
if github_release == "skip":
print("Skipping GitHub release")
else:
if release_notes == "generate":
response = requests_session.post(
f"/repos/{repo_full_name}/releases/generate-notes",
json={
"tag_name": tag_name,
"target_commitish": git.get_previous_commit_sha() # so we don't include the release PR,
# "previous_tag_name": "",
},
)
response.raise_for_status()
release_notes = response.json()["body"]

print("Creating GitHub release")
response = requests_session.post(
f"/repos/{repo_full_name}/releases",
json={
"tag_name": tag_name,
"prerelease": bool(version_semver.prerelease),
"body": release_notes,
"draft": github_release == "draft",
},
)
response.raise_for_status()

if publish_cmd:
print(f"Running publish command:\n{publish_cmd}")
Expand Down Expand Up @@ -87,14 +111,37 @@ def cli():
)
@click.option("--publish-cmd", envvar="PUBLISH_CMD")
@click.option("--prepare-cmd", envvar="PREPARE_CMD")
@click.option(
"--github-release",
default="publish",
type=click.Choice(["publish", "draft", "skip"]),
envvar="GITHUB_RELEASE",
)
@click.option(
"--release-notes",
default="",
type=click.Choice(["", "generate"]),
envvar="RELEASE_NOTES",
)
@cls_client.track_command(include_kwargs=["tag_prefix", "next_branch"])
def ci(tag_prefix, api_url, token, next_branch, publish_cmd, prepare_cmd):
def ci(
tag_prefix,
api_url,
token,
next_branch,
publish_cmd,
prepare_cmd,
github_release,
release_notes,
):

if "TAG_PREFIX" in os.environ:
# This way it can be an empty string, which isn't possible with click envvar
tag_prefix = os.environ["TAG_PREFIX"]

print("tag_prefix:", tag_prefix)
print("github_release:", github_release)
print("release_notes:", release_notes)
print("api_url:", api_url)
print("next_branch:", next_branch)
print("publish_cmd:", publish_cmd)
Expand Down Expand Up @@ -124,6 +171,8 @@ def ci(tag_prefix, api_url, token, next_branch, publish_cmd, prepare_cmd):
repo_full_name=gh_action.repo_full_name,
tag_prefix=tag_prefix,
publish_cmd=publish_cmd,
github_release=github_release,
release_notes=release_notes,
)
return

Expand Down
4 changes: 4 additions & 0 deletions nextrelease/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ def get_current_commit_message():
return check_output(["git", "log", "-1", "--pretty=%B"]).strip().decode("utf-8")


def get_previous_commit_sha():
return check_output(["git", "rev-parse", "HEAD^1"]).strip().decode("utf-8")


def tag_commit(version, tag_prefix="v"):
version_with_prefix = tag_prefix + version
check_call(["git", "tag", "-a", version_with_prefix, "-m", version_with_prefix])
Expand Down

0 comments on commit a036b64

Please sign in to comment.