Skip to content

Commit

Permalink
Merge pull request #7 from Purpose-Green/feat/ask-extra-confirmation
Browse files Browse the repository at this point in the history
Ask extra confirmation
  • Loading branch information
Chemaclass authored Nov 6, 2024
2 parents 62eaf59 + 4c15bb6 commit b4bcefe
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 1 deletion.
1 change: 1 addition & 0 deletions .env.dist
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

# release
RELEASE_SUCCESSFUL_TEXT=https://github.com/Purpose-Green/release/releases
RELEASE_EXTRA_CONFIRMATION='{"src/dev":"Are you sure sure?"}'

# slack
SLACK_CHANNEL_ID=
Expand Down
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,22 @@ Examples:

![](demo/creating-release.gif)

## Env variables

### RELEASE_SUCCESSFUL_TEXT

Display a text at the very end of the release.
Useful to have a link directly to the releases page to validate everything was good.

> Example: RELEASE_SUCCESSFUL_TEXT=https://github.com/Purpose-Green/release/releases
### RELEASE_EXTRA_CONFIRMATION

Force asking for a new dialog when a filepath is found on such a key.
The value is the question forced to be asked. It must be [y/Y] to continue the release.

> Example: RELEASE_EXTRA_CONFIRMATION='{"migrations": "Migrations found! Remember to create a DB backup!"}'
## Development

#### Env
Expand Down
1 change: 1 addition & 0 deletions release
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ source "$RELEASE_ROOT_DIR/src/io.sh"
source "$RELEASE_ROOT_DIR/src/release.sh"
source "$RELEASE_ROOT_DIR/src/validate.sh"
source "$RELEASE_ROOT_DIR/src/slack.sh"
source "$RELEASE_ROOT_DIR/src/json.sh"
source "$RELEASE_ROOT_DIR/src/main.sh"

# Check if at least one argument (branch name) is passed
Expand Down
38 changes: 38 additions & 0 deletions src/json.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#!/bin/bash
# shellcheck disable=SC2155
set -euo pipefail

# Render the value str where the key is matching on the given json
#
# $1 full json as str
# $2 the filepath
#
# Example: '{"src/dev": "are you sure you want to change the files in this dir?"}'
function json::parse_text() {
local json_str="$1"
local filepath="$2"

# Start with the full path and progressively reduce it to parent directories
local dir_path="$filepath"

while [[ -n "$dir_path" ]]; do
# Escape special characters in the directory path for `sed`
local escaped_dir_path=$(echo "$dir_path" | sed 's/[\/&]/\\&/g')

# Extract and return the message for the current directory path if it exists in the JSON string
local text=$(echo "$json_str" | sed -n "s/.*\"$escaped_dir_path\":\"\([^\"]*\)\".*/\1/p")

if [[ -n "$text" ]]; then
echo "$text"
return
fi

# Remove the last directory level
dir_path=$(dirname "$dir_path")

# Break if we reach the root
if [[ "$dir_path" == "." ]]; then
break
fi
done
}
27 changes: 26 additions & 1 deletion src/main.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/bin/bash
# shellcheck disable=SC2155
set -euo pipefail

# shellcheck disable=SC2155
function main::action() {
local source=${1:-main}
local target=${2:-prod}
Expand Down Expand Up @@ -42,6 +42,8 @@ function main::action() {
exit 0
fi

main::extra_confirmation_texts "$changed_files"

main::force_checkout "$target"
main::merge_source_to_target "$source" "$target"

Expand All @@ -51,6 +53,29 @@ function main::action() {
main::update_develop "$develop" "$target"
}

function main::extra_confirmation_texts() {
local changed_files=$1

if [[ -z "${RELEASE_EXTRA_CONFIRMATION:-}" ]]; then
return 0
fi

local confirmation_msg filepath
while IFS= read -r filepath; do
confirmation_msg=$(json::parse_text "$RELEASE_EXTRA_CONFIRMATION" "$filepath")
if [[ -n "$confirmation_msg" ]]; then
break
fi
done <<< "$changed_files"

if [[ -n "$confirmation_msg" ]]; then
echo "Psst, due to '$filepath'..."
# shellcheck disable=SC2116
local question="$(echo "${COLOR_RED}$confirmation_msg${COLOR_RESET}")"
io::confirm_or_exit "$question"
fi
}

function main::render_steps() {
local source=$1
local target=$1
Expand Down

0 comments on commit b4bcefe

Please sign in to comment.