commit
88bc7844e3
7 files changed
+446
No files matched your search
Executable
+114
@@ -0,0 +1,114 @@
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
|
||||
: "${INPUT_CONFIG_FILE:?config-file is required}"
|
||||
: "${INPUT_BASE_BRANCH:?base-branch is required}"
|
||||
: "${INPUT_TARGET_BRANCH:?target-branch is required}"
|
||||
: "${INPUT_BUMP:?bump is required}"
|
||||
: "${INPUT_REMOTE:?remote is required}"
|
||||
: "${INPUT_COMMIT_MESSAGE:?commit-message is required}"
|
||||
: "${INPUT_GIT_USER_NAME:?git-user-name is required}"
|
||||
: "${INPUT_GIT_USER_EMAIL:?git-user-email is required}"
|
||||
: "${INPUT_BUMP_MY_VERSION_VERSION:?bump-my-version-version is required}"
|
||||
: "${INPUT_PUSH:?push is required}"
|
||||
: "${GITHUB_OUTPUT:?GITHUB_OUTPUT is required}"
|
||||
|
||||
case "${INPUT_PUSH}" in
|
||||
true | false) ;;
|
||||
*) echo "push must be 'true' or 'false'" >&2; exit 1 ;;
|
||||
esac
|
||||
|
||||
case "${INPUT_BUMP}" in
|
||||
major | minor | patch) ;;
|
||||
*) echo "bump must be major, minor, or patch" >&2; exit 1 ;;
|
||||
esac
|
||||
|
||||
if [[ ! -f "${INPUT_CONFIG_FILE}" ]]; then
|
||||
echo "Configuration file not found: ${INPUT_CONFIG_FILE}" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if [[ -n "$(git status --porcelain)" ]]; then
|
||||
echo "Working tree must be clean before bumping the version" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
current_branch=$(git branch --show-current)
|
||||
if [[ "${current_branch}" != "${INPUT_TARGET_BRANCH}" ]]; then
|
||||
echo "Expected checked-out branch '${INPUT_TARGET_BRANCH}', found '${current_branch:-detached HEAD}'" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
git fetch "${INPUT_REMOTE}" \
|
||||
"${INPUT_BASE_BRANCH}:refs/remotes/${INPUT_REMOTE}/${INPUT_BASE_BRANCH}"
|
||||
|
||||
script_dir=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)
|
||||
base_ref="refs/remotes/${INPUT_REMOTE}/${INPUT_BASE_BRANCH}"
|
||||
current_version=$(python3 "${script_dir}/read_version.py" "${INPUT_CONFIG_FILE}")
|
||||
base_version=$(python3 "${script_dir}/read_version.py" "${INPUT_CONFIG_FILE}" --ref "${base_ref}")
|
||||
|
||||
comparison=$(python3 - "${current_version}" "${base_version}" <<'PY'
|
||||
import re
|
||||
import sys
|
||||
|
||||
pattern = re.compile(r"^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)$")
|
||||
versions = []
|
||||
for value in sys.argv[1:]:
|
||||
match = pattern.fullmatch(value)
|
||||
if not match:
|
||||
raise SystemExit(f"Version must use stable MAJOR.MINOR.PATCH format: {value}")
|
||||
versions.append(tuple(map(int, match.groups())))
|
||||
|
||||
print((versions[0] > versions[1]) - (versions[0] < versions[1]))
|
||||
PY
|
||||
)
|
||||
|
||||
if [[ "${comparison}" == "1" ]]; then
|
||||
echo "Version ${current_version} is already prepared (base: ${base_version})"
|
||||
{
|
||||
echo "bumped=false"
|
||||
echo "previous-version=${current_version}"
|
||||
echo "current-version=${current_version}"
|
||||
echo "commit="
|
||||
} >> "${GITHUB_OUTPUT}"
|
||||
exit 0
|
||||
fi
|
||||
|
||||
if [[ "${comparison}" == "-1" ]]; then
|
||||
echo "Target branch version ${current_version} is older than base branch version ${base_version}" >&2
|
||||
echo "Synchronize ${INPUT_TARGET_BRANCH} with ${INPUT_BASE_BRANCH} before releasing" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
venv_dir=$(mktemp -d "${RUNNER_TEMP:-${TMPDIR:-/tmp}}/bump-erp-app-version.XXXXXX")
|
||||
trap 'rm -rf "${venv_dir}"' EXIT
|
||||
python3 -m venv "${venv_dir}"
|
||||
"${venv_dir}/bin/pip" install --disable-pip-version-check \
|
||||
"bump-my-version==${INPUT_BUMP_MY_VERSION_VERSION}"
|
||||
|
||||
git config user.name "${INPUT_GIT_USER_NAME}"
|
||||
git config user.email "${INPUT_GIT_USER_EMAIL}"
|
||||
|
||||
message=${INPUT_COMMIT_MESSAGE//\{version\}/\{new_version\}}
|
||||
"${venv_dir}/bin/bump-my-version" bump \
|
||||
--config-file "${INPUT_CONFIG_FILE}" \
|
||||
--commit \
|
||||
--no-tag \
|
||||
--message "${message}" \
|
||||
"${INPUT_BUMP}"
|
||||
|
||||
new_version=$(python3 "${script_dir}/read_version.py" "${INPUT_CONFIG_FILE}")
|
||||
commit=$(git rev-parse HEAD)
|
||||
|
||||
if [[ "${INPUT_PUSH}" == "true" ]]; then
|
||||
git push "${INPUT_REMOTE}" "HEAD:${INPUT_TARGET_BRANCH}"
|
||||
fi
|
||||
|
||||
{
|
||||
echo "bumped=true"
|
||||
echo "previous-version=${current_version}"
|
||||
echo "current-version=${new_version}"
|
||||
echo "commit=${commit}"
|
||||
} >> "${GITHUB_OUTPUT}"
|
||||
|
||||
echo "Prepared ${current_version} -> ${new_version} (${commit})"
|
||||
Executable
+44
@@ -0,0 +1,44 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Read bump-my-version's current_version from a local file or Git ref."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
import subprocess
|
||||
import tomllib
|
||||
|
||||
|
||||
def parse_args() -> argparse.Namespace:
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument("config_file")
|
||||
parser.add_argument("--ref")
|
||||
return parser.parse_args()
|
||||
|
||||
|
||||
def main() -> None:
|
||||
args = parse_args()
|
||||
try:
|
||||
if args.ref:
|
||||
content = subprocess.check_output(
|
||||
["git", "show", f"{args.ref}:{args.config_file}"],
|
||||
)
|
||||
else:
|
||||
with open(args.config_file, "rb") as config_file:
|
||||
content = config_file.read()
|
||||
except (OSError, subprocess.CalledProcessError) as error:
|
||||
raise SystemExit(f"Could not load {args.config_file}: {error}") from error
|
||||
|
||||
try:
|
||||
version = tomllib.loads(content.decode())["tool"]["bumpversion"]["current_version"]
|
||||
except (KeyError, TypeError, UnicodeDecodeError, tomllib.TOMLDecodeError) as error:
|
||||
raise SystemExit(
|
||||
f"Could not read tool.bumpversion.current_version from {args.config_file}: {error}"
|
||||
) from error
|
||||
|
||||
if not isinstance(version, str):
|
||||
raise SystemExit("tool.bumpversion.current_version must be a string")
|
||||
print(version)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in new issue
Block a user