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

Add support for a custom tag prefix #8

Merged
merged 7 commits into from
Dec 21, 2021
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ description: GitHub Action for creating GitHub Releases based on changelog

inputs:
changelog:
description: Path to changelog
description: Path to changelog (variables `$tag`, `$version`, `$prefix`, and any string)
required: false
title:
description: Format of title (variables `$tag`, `$version`, and any string)
description: Format of title (variables `$tag`, `$version`, `$prefix`, and any string)
required: false
default: '$tag'
draft:
Expand All @@ -16,6 +16,26 @@ inputs:
branch:
description: Reject releases from commits not contained in branches that match the specified pattern (regular expression)
required: false
prefix:
description: >
An optional pattern that matches a prefix for the release tag, before the
version number.

If a prefix is provided, an optional '-' character is permitted (but not
required) between the prefix and the version number. For example, the
tags 'my-crate-v0.1.2' and 'my-crate0.1.0' both have the prefix 'my-crate'.

This is interpreted as a bash-style regular expression, so it may be used
to match multiple prefix strings. Therefore, literal characters that
are part of bash's regular expression syntax, such as `$`, `^`, `[`, `]`,
`{`, `}`, `(`, `)`, and `|` must be escaped if they occur in the prefix
pattern.

If a prefix pattern is provided, the portion of the tag that matched the
prefix can be interpolated into `title` and `changelog` via the `$prefix`
variable.
required: false
default: ''

runs:
using: node12
Expand Down
24 changes: 19 additions & 5 deletions main.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ title="${INPUT_TITLE:?}"
changelog="${INPUT_CHANGELOG:-}"
draft="${INPUT_DRAFT:-}"
branch="${INPUT_BRANCH:-}"
prefix="${INPUT_PREFIX:-}"

if [[ -z "${GITHUB_TOKEN:-}" ]]; then
error "GITHUB_TOKEN not set"
Expand All @@ -29,18 +30,31 @@ if [[ "${GITHUB_REF:?}" != "refs/tags/"* ]]; then
fi
tag="${GITHUB_REF#refs/tags/}"

# TODO: Support custom prefix of tags https://github.com/taiki-e/create-gh-release-action/issues/1
if [[ ! "${tag}" =~ ^v?[0-9]+\.[0-9]+\.[0-9]+(-[a-zA-Z_0-9\.-]+)?(\+[a-zA-Z_0-9\.-]+)?$ ]]; then
if [[ ! "${tag}" =~ ^${prefix}-?v?[0-9]+\.[0-9]+\.[0-9]+(-[a-zA-Z_0-9\.-]+)?(\+[a-zA-Z_0-9\.-]+)?$ ]]; then
error "invalid tag format: '${tag}'"
exit 1
fi
if [[ "${tag}" =~ ^v?[0-9\.]+-[a-zA-Z_0-9\.-]+(\+[a-zA-Z_0-9\.-]+)?$ ]]; then
if [[ "${tag}" =~ ^${prefix}-?v?[0-9\.]+-[a-zA-Z_0-9\.-]+(\+[a-zA-Z_0-9\.-]+)?$ ]]; then
prerelease="--prerelease"
fi
version="${tag#v}"

version="${tag}"
# extract the portion of the tag matching the prefix pattern
if [[ ! "${prefix}" = "" ]]; then
prefix=$(grep <<<"${tag}" -Eo "^${prefix}")
version="${tag#${prefix}}"
taiki-e marked this conversation as resolved.
Show resolved Hide resolved
version="${version#-}"
fi
version="${version#v}"

# interpolate $tag, $version, and $prefix into the title string
title="${title/\$tag/${tag}}"
title="${title/\$version/${version}}"

title="${title/\$prefix/${prefix}}"
# interpolate $tag, $version, and $prefix into the changelog path
changelog="${changelog/\$tag/${tag}}"
changelog="${changelog/\$version/${version}}"
changelog="${changelog/\$prefix/${prefix}}"
case "${draft}" in
true) draft_option="--draft" ;;
false) ;;
Expand Down