Skip to content

Commit

Permalink
refactor(workflows/release): update tag.sh to work with TAGS_FILE (#12)
Browse files Browse the repository at this point in the history
* misc: vscode settings, empty .env-default

* refactor: remove .env local sourcing to avoid confusion

* fix(workflows/release): add TAGS_FILE env

* refactor(workflows/release): update tag.sh to work with TAGS_FILE

* misc: use fatal instead of exit 1

* feat: add LIB_LOADED checks and errors
  • Loading branch information
tcodes0 authored Sep 5, 2024
1 parent 67e1343 commit 4bb6707
Show file tree
Hide file tree
Showing 10 changed files with 90 additions and 56 deletions.
Empty file added .env-default
Empty file.
1 change: 1 addition & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@ jobs:
CHANGELOG_FILE: CHANGELOG.md
BASH_ENV: ./lib.sh
DRY_RUN: false
TAGS_FILE: tags.txt
run: ./workflows/release/tag.sh
1 change: 1 addition & 0 deletions .github/workflows/release_pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ jobs:
env:
CHANGELOG_FILE: CHANGELOG.md
BASH_ENV: ./lib.sh
TAGS_FILE: tags.txt
run: ./workflows/release/changelog.sh "${{ inputs.url }}" "${{ inputs.title }}" "${{ inputs.tag_prefix }}"

- name: Open PR
Expand Down
23 changes: 23 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
// - - - - - - - - YAML - - - - - - - - -
"prettier.configPath": ".prettierrc.yml",
"[yaml]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
// - - - - - - - - BASH - - - - - - - - -
"shellformat.flag": "-i 2 -ln bash",
"[shellscript]": {
"editor.defaultFormatter": "foxundermoon.shell-format"
},
"shellcheck.customArgs": ["-x"],
// - - - - - - - - OSX - - - - - - - - -
"terminal.integrated.defaultProfile.osx": "bash",
// - - - - - - - - CSPELL - - - - - - - - -
"cSpell.maxDuplicateProblems": 2,
"cSpell.showAutocompleteSuggestions": false,
"cSpell.enableFiletypes": ["shellscript"],
// "cSpell.spellCheckDelayMs": 2000, // needs to be set in user settings json
// - - - - - - - - MISC - - - - - - - - -
// https://github.com/github/vscode-github-actions/issues/222
"github-actions.use-enterprise": true
}
6 changes: 6 additions & 0 deletions go/generate_mocks.sh
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ rename_expect() {
### script ###
##############

if [ ! "${LIB_LOADED:-}" ]; then
echo -e "INFO ($0:$LINENO) BASH_ENV=${BASH_ENV:-}" >&2
echo -e "FATAL ($0:$LINENO) lib.sh not found. use 'export BASH_ENV=<lib.sh location>' or 'BASH_ENV=<lib.sh location> $0'" >&2
exit 1
fi

if requested_help "$*"; then
usage
exit 1
Expand Down
6 changes: 6 additions & 0 deletions go/lint_fix.sh
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,12 @@ lint() {
### script ###
##############

if [ ! "${LIB_LOADED:-}" ]; then
echo -e "INFO ($0:$LINENO) BASH_ENV=${BASH_ENV:-}" >&2
echo -e "FATAL ($0:$LINENO) lib.sh not found. use 'export BASH_ENV=<lib.sh location>' or 'BASH_ENV=<lib.sh location> $0'" >&2
exit 1
fi

if requested_help "$*"; then
usage
exit 1
Expand Down
10 changes: 4 additions & 6 deletions migration.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ EOF
}

# Description: Validates user input
# Globals : MIGRATIONS_DIR (github workflow or .env)
# Globals : MIGRATIONS_DIR (github workflow)
# Args : $@
# STDERR : Might print errors
# Returns : 1 if fail
Expand All @@ -31,10 +31,8 @@ validate() {
local name=${1-}

if [ ! "${MIGRATIONS_DIR:-}" ]; then
if ! source .env; then
err $LINENO "missing MIGRATIONS_DIR env variable"
return 1
fi
err $LINENO "missing MIGRATIONS_DIR env variable"
return 1
fi

if [ ! "$name" ]; then
Expand All @@ -45,7 +43,7 @@ validate() {
}

# Description: Create a new timestamped migration file
# Globals : MIGRATIONS_DIR (github workflow or .env)
# Globals : MIGRATIONS_DIR (github workflow)
# Args : 1=(up | down) 2=name
# STDOUT : Path to the new file
# Sideeffects: Creates a new file
Expand Down
6 changes: 6 additions & 0 deletions workflows/main/commit_lint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,12 @@ EOF
### script ###
##############

if [ ! "${LIB_LOADED:-}" ]; then
echo -e "INFO ($0:$LINENO) BASH_ENV=${BASH_ENV:-}" >&2
echo -e "FATAL ($0:$LINENO) lib.sh not found. use 'export BASH_ENV=<lib.sh location>' or 'BASH_ENV=<lib.sh location> $0'" >&2
exit 1
fi

validate
lint_title
lint_log
16 changes: 10 additions & 6 deletions workflows/release/changelog.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,13 @@ trap 'err $LINENO' ERR
##########################

# Description: Validates user input
# Globals : CHANGELOG_FILE (github workflow or .env)
# Globals : CHANGELOG_FILE (github workflow)
# Returns : 1 if fail
# Example : validate
validate() {
if [ ! "${CHANGELOG_FILE-}" ]; then
if ! source .env; then
err $LINENO "missing CHANGELOG_FILE env variable"
return 1
fi
err $LINENO "missing CHANGELOG_FILE env variable"
return 1
fi

if [ ! -f "$CHANGELOG_FILE" ]; then
Expand All @@ -34,7 +32,7 @@ validate() {
}

# Description: Calls changelog tool and updates CHANGELOG_FILE
# Globals : CHANGELOG_FILE, TAGS_FILE (github workflow or .env)
# Globals : CHANGELOG_FILE, TAGS_FILE (github workflow)
# Args : 1=url 2=title 3=prefixes
# STDERR : Might print errors
# Returns : 1 if fail
Expand Down Expand Up @@ -64,5 +62,11 @@ update_changelog() {
### script ###
##############

if [ ! "${LIB_LOADED:-}" ]; then
echo -e "INFO ($0:$LINENO) BASH_ENV=${BASH_ENV:-}" >&2
echo -e "FATAL ($0:$LINENO) lib.sh not found. use 'export BASH_ENV=<lib.sh location>' or 'BASH_ENV=<lib.sh location> $0'" >&2
exit 1
fi

validate
update_changelog "$@"
77 changes: 33 additions & 44 deletions workflows/release/tag.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,25 @@ trap 'err $LINENO' ERR
##########################

# Description: Exit the script if main head is doesn't match a release commit
# Globals : CHANGELOG_FILE (github workflow or .env)
# Globals : CHANGELOG_FILE, TAGS_FILE, DRY_RUN (github workflow)
# STDERR : Logs
# Returns : 1 if fail
# Sideeffects: Might exit the script with success
# Sideeffects: Might exit the script with success or failure
# Example : validate
validate() {
local main_head release_re="chore:[[:blank:]]+release"
local main_head release_re="^chore:[[:blank:]]+release"

if [ ! "${CHANGELOG_FILE-}" ]; then
if ! source .env; then
err $LINENO "missing CHANGELOG_FILE env variable"
return 1
fi
if [ "${DRY_RUN:-}" == "true" ]; then
DRY_RUN="echo"
else
DRY_RUN=""
fi

if [ ! "${CHANGELOG_FILE:-}" ]; then
fatal $LINENO "missing CHANGELOG_FILE env variable"
fi

if [ ! "${TAGS_FILE:-}" ]; then
fatal $LINENO "missing TAGS_FILE env variable"
fi

while read -r commit_line; do
Expand All @@ -43,39 +49,22 @@ validate() {
fi
}

# Description: Parses tags from changelog
# Globals : CHANGELOG_FILE (github workflow or .env)
# Description: Parses tags from TAGS_FILE
# Globals : TAGS_FILE (github workflow)
# STDOUT : Parsed tags
# Returns : Parsed tags
# Example : tags="$(parse_changelog_tags)"
parse_changelog_tags() {
local tags=() newest_date tag date
# https://regex101.com/r/67rzeb/6
local changelog_h1_re="#[[:blank:]]+([a-zA-Z0-9 !%&*()-+]+:)?[[:blank:]]*([a-zA-Z0-9\/.]+)[[:blank:]]+[*_]\(([[:digit:]]+-[[:digit:]]+-[[:digit:]]+)"
# Example : tags="$(parse_tags)"
parse_tags() {
local tags=() line

while read -r line; do
if ! [[ "$line" =~ $changelog_h1_re ]]; then
# want only release lines, h1 in md
# one or more lines with tags
if [[ "$line" =~ ^# ]]; then
# skip doc lines
continue
fi

tag="${BASH_REMATCH[2]}"
date="${BASH_REMATCH[3]}"

if [ ! "${newest_date:-}" ]; then
# want to push tags for the newest release only
# which will be in the top of the changelog
newest_date=$date
fi

if [ "$date" != "$newest_date" ]; then
# newest release is over
break
fi

tags+=("$tag")
done <"$CHANGELOG_FILE"
tags+=("$line")
done <"$TAGS_FILE"

printf %s "${tags[*]}"
}
Expand All @@ -101,19 +90,13 @@ validate_tags() {
}

# Description: Creates and pushes git tags
# Globals : DRY_RUN bool
# Globals : DRY_RUN (github workflow)
# Args : words separated by spaces
# STDOUT : Prints each tag created, plus git output
# STDERR : Git might output
# Sideeffects: Pushes git tags
# Example : tag_and_push foo/v1.1.1. bar/v1.2.3
# Example : tag_and_push v1.1.1. foo/v1.2.3
tag_and_push() {
if [ "${DRY_RUN:-}" == "true" ]; then
DRY_RUN="echo"
else
DRY_RUN=""
fi

for tag in "$@"; do
msgln "$tag"
$DRY_RUN git tag "$tag" HEAD
Expand All @@ -126,9 +109,15 @@ tag_and_push() {
### script ###
##############

if [ ! "${LIB_LOADED:-}" ]; then
echo -e "INFO ($0:$LINENO) BASH_ENV=${BASH_ENV:-}" >&2
echo -e "FATAL ($0:$LINENO) lib.sh not found. use 'export BASH_ENV=<lib.sh location>' or 'BASH_ENV=<lib.sh location> $0'" >&2
exit 1
fi

validate

parsed_tags="$(parse_changelog_tags)"
parsed_tags="$(parse_tags)"
# shellcheck disable=SC2068 # intentional splitting
validate_tags ${parsed_tags[@]}
# shellcheck disable=SC2068 # intentional splitting
Expand Down

0 comments on commit 4bb6707

Please sign in to comment.