Add _log and _set_github_output functions (#273)

* Add _set_github_output function

* Use _set_github_output in Action

* Add _log function

* Use _log in Action and fix Test Cases

* Update wording in log messages

* Update note about removal of old output syntax

Logic is now encapuslated in a single function. I don’t mind keeping it around until spring / June 2023
This commit is contained in:
Stefan Zweifel 2022-12-22 19:45:17 +01:00 committed by GitHub
parent 925bbcaef3
commit aeb1802648
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 39 additions and 46 deletions

View File

@ -6,6 +6,26 @@ if "$INPUT_DISABLE_GLOBBING"; then
set -o noglob; set -o noglob;
fi fi
_set_github_output() {
local name=${1}
local value=${2}
# Check if $GITHUB_OUTPUT is available
# (Feature detection will be removed in spring 2023)
if [ -z ${GITHUB_OUTPUT+x} ]; then
echo "::set-output name=$name::$value";
else
echo "$name=$value" >> $GITHUB_OUTPUT;
fi
}
_log() {
local level=${1}
local message=${2}
echo "::$level::$message";
}
_main() { _main() {
_check_if_git_is_available _check_if_git_is_available
@ -13,13 +33,7 @@ _main() {
if _git_is_dirty || "$INPUT_SKIP_DIRTY_CHECK"; then if _git_is_dirty || "$INPUT_SKIP_DIRTY_CHECK"; then
# Check if $GITHUB_OUTPUT is available _set_github_output "changes_detected" "true"
# (Feature detection will be removed in late December 2022)
if [ -z ${GITHUB_OUTPUT+x} ]; then
echo "::set-output name=changes_detected::true";
else
echo "changes_detected=true" >> $GITHUB_OUTPUT;
fi
_switch_to_branch _switch_to_branch
@ -36,26 +50,12 @@ _main() {
_push_to_github _push_to_github
else else
_set_github_output "changes_detected" "false"
# Check if $GITHUB_OUTPUT is available
# (Feature detection will be removed in late December 2022)
if [ -z ${GITHUB_OUTPUT+x} ]; then
echo "::set-output name=changes_detected::false";
else
echo "changes_detected=false" >> $GITHUB_OUTPUT;
fi
echo "Working tree clean. Nothing to commit."; echo "Working tree clean. Nothing to commit.";
fi fi
else else
_set_github_output "changes_detected" "false"
# Check if $GITHUB_OUTPUT is available
# (Feature detection will be removed in late December 2022)
if [ -z ${GITHUB_OUTPUT+x} ]; then
echo "::set-output name=changes_detected::false";
else
echo "changes_detected=false" >> $GITHUB_OUTPUT;
fi
echo "Working tree clean. Nothing to commit."; echo "Working tree clean. Nothing to commit.";
fi fi
@ -63,9 +63,9 @@ _main() {
_check_if_git_is_available() { _check_if_git_is_available() {
if hash -- "$INPUT_INTERNAL_GIT_BINARY" 2> /dev/null; then if hash -- "$INPUT_INTERNAL_GIT_BINARY" 2> /dev/null; then
echo "::debug::git binary found."; _log "debug" "git binary found.";
else else
echo "::error ::git-auto-commit could not find git binary. Please make sure git is available." _log "error" "git-auto-commit could not find git binary. Please make sure git is available."
exit 1; exit 1;
fi fi
} }
@ -77,7 +77,7 @@ _switch_to_repository() {
_git_is_dirty() { _git_is_dirty() {
echo "INPUT_STATUS_OPTIONS: ${INPUT_STATUS_OPTIONS}"; echo "INPUT_STATUS_OPTIONS: ${INPUT_STATUS_OPTIONS}";
echo "::debug::Apply status options ${INPUT_STATUS_OPTIONS}"; _log "debug" "Apply status options ${INPUT_STATUS_OPTIONS}";
echo "INPUT_FILE_PATTERN: ${INPUT_FILE_PATTERN}"; echo "INPUT_FILE_PATTERN: ${INPUT_FILE_PATTERN}";
read -r -a INPUT_FILE_PATTERN_EXPANDED <<< "$INPUT_FILE_PATTERN"; read -r -a INPUT_FILE_PATTERN_EXPANDED <<< "$INPUT_FILE_PATTERN";
@ -91,14 +91,14 @@ _switch_to_branch() {
# Fetch remote to make sure that repo can be switched to the right branch. # Fetch remote to make sure that repo can be switched to the right branch.
if "$INPUT_SKIP_FETCH"; then if "$INPUT_SKIP_FETCH"; then
echo "::debug::git-fetch has not been executed"; _log "debug" "git-fetch will not be executed.";
else else
git fetch --depth=1; git fetch --depth=1;
fi fi
# If `skip_checkout`-input is true, skip the entire checkout step. # If `skip_checkout`-input is true, skip the entire checkout step.
if "$INPUT_SKIP_CHECKOUT"; then if "$INPUT_SKIP_CHECKOUT"; then
echo "::debug::git-checkout has not been executed"; _log "debug" "git-checkout will not be executed.";
else else
# Create new local branch if `create_branch`-input is true # Create new local branch if `create_branch`-input is true
if "$INPUT_CREATE_BRANCH"; then if "$INPUT_CREATE_BRANCH"; then
@ -114,7 +114,7 @@ _switch_to_branch() {
_add_files() { _add_files() {
echo "INPUT_ADD_OPTIONS: ${INPUT_ADD_OPTIONS}"; echo "INPUT_ADD_OPTIONS: ${INPUT_ADD_OPTIONS}";
echo "::debug::Apply add options ${INPUT_ADD_OPTIONS}"; _log "debug" "Apply add options ${INPUT_ADD_OPTIONS}";
echo "INPUT_FILE_PATTERN: ${INPUT_FILE_PATTERN}"; echo "INPUT_FILE_PATTERN: ${INPUT_FILE_PATTERN}";
read -r -a INPUT_FILE_PATTERN_EXPANDED <<< "$INPUT_FILE_PATTERN"; read -r -a INPUT_FILE_PATTERN_EXPANDED <<< "$INPUT_FILE_PATTERN";
@ -125,7 +125,7 @@ _add_files() {
_local_commit() { _local_commit() {
echo "INPUT_COMMIT_OPTIONS: ${INPUT_COMMIT_OPTIONS}"; echo "INPUT_COMMIT_OPTIONS: ${INPUT_COMMIT_OPTIONS}";
echo "::debug::Apply commit options ${INPUT_COMMIT_OPTIONS}"; _log "debug" "Apply commit options ${INPUT_COMMIT_OPTIONS}";
# shellcheck disable=SC2206 # shellcheck disable=SC2206
INPUT_COMMIT_OPTIONS_ARRAY=( $INPUT_COMMIT_OPTIONS ); INPUT_COMMIT_OPTIONS_ARRAY=( $INPUT_COMMIT_OPTIONS );
@ -140,14 +140,7 @@ _local_commit() {
--author="$INPUT_COMMIT_AUTHOR" \ --author="$INPUT_COMMIT_AUTHOR" \
${INPUT_COMMIT_OPTIONS:+"${INPUT_COMMIT_OPTIONS_ARRAY[@]}"}; ${INPUT_COMMIT_OPTIONS:+"${INPUT_COMMIT_OPTIONS_ARRAY[@]}"};
_set_github_output "commit_hash" $(git rev-parse HEAD)
# Check if $GITHUB_OUTPUT is available
# (Feature detection will be removed in late December 2022)
if [ -z ${GITHUB_OUTPUT+x} ]; then
echo "::set-output name=commit_hash::$(git rev-parse HEAD)";
else
echo "commit_hash=$(git rev-parse HEAD)" >> $GITHUB_OUTPUT;
fi
} }
_tag_commit() { _tag_commit() {
@ -155,7 +148,7 @@ _tag_commit() {
if [ -n "$INPUT_TAGGING_MESSAGE" ] if [ -n "$INPUT_TAGGING_MESSAGE" ]
then then
echo "::debug::Create tag $INPUT_TAGGING_MESSAGE"; _log "debug" "Create tag $INPUT_TAGGING_MESSAGE";
git -c user.name="$INPUT_COMMIT_USER_NAME" -c user.email="$INPUT_COMMIT_USER_EMAIL" tag -a "$INPUT_TAGGING_MESSAGE" -m "$INPUT_TAGGING_MESSAGE"; git -c user.name="$INPUT_COMMIT_USER_NAME" -c user.email="$INPUT_COMMIT_USER_EMAIL" tag -a "$INPUT_TAGGING_MESSAGE" -m "$INPUT_TAGGING_MESSAGE";
else else
echo "No tagging message supplied. No tag will be added."; echo "No tagging message supplied. No tag will be added.";
@ -165,7 +158,7 @@ _tag_commit() {
_push_to_github() { _push_to_github() {
echo "INPUT_PUSH_OPTIONS: ${INPUT_PUSH_OPTIONS}"; echo "INPUT_PUSH_OPTIONS: ${INPUT_PUSH_OPTIONS}";
echo "::debug::Apply push options ${INPUT_PUSH_OPTIONS}"; _log "debug" "Apply push options ${INPUT_PUSH_OPTIONS}";
# shellcheck disable=SC2206 # shellcheck disable=SC2206
INPUT_PUSH_OPTIONS_ARRAY=( $INPUT_PUSH_OPTIONS ); INPUT_PUSH_OPTIONS_ARRAY=( $INPUT_PUSH_OPTIONS );
@ -175,15 +168,15 @@ _push_to_github() {
# Only add `--tags` option, if `$INPUT_TAGGING_MESSAGE` is set # Only add `--tags` option, if `$INPUT_TAGGING_MESSAGE` is set
if [ -n "$INPUT_TAGGING_MESSAGE" ] if [ -n "$INPUT_TAGGING_MESSAGE" ]
then then
echo "::debug::git push origin --tags"; _log "debug" "git push origin --tags";
git push origin --follow-tags --atomic ${INPUT_PUSH_OPTIONS:+"${INPUT_PUSH_OPTIONS_ARRAY[@]}"}; git push origin --follow-tags --atomic ${INPUT_PUSH_OPTIONS:+"${INPUT_PUSH_OPTIONS_ARRAY[@]}"};
else else
echo "::debug::git push origin"; _log "debug" "git push origin";
git push origin ${INPUT_PUSH_OPTIONS:+"${INPUT_PUSH_OPTIONS_ARRAY[@]}"}; git push origin ${INPUT_PUSH_OPTIONS:+"${INPUT_PUSH_OPTIONS_ARRAY[@]}"};
fi fi
else else
echo "::debug::Push commit to remote branch $INPUT_BRANCH"; _log "debug" "Push commit to remote branch $INPUT_BRANCH";
git push --set-upstream origin "HEAD:$INPUT_BRANCH" --follow-tags --atomic ${INPUT_PUSH_OPTIONS:+"${INPUT_PUSH_OPTIONS_ARRAY[@]}"}; git push --set-upstream origin "HEAD:$INPUT_BRANCH" --follow-tags --atomic ${INPUT_PUSH_OPTIONS:+"${INPUT_PUSH_OPTIONS_ARRAY[@]}"};
fi fi
} }

View File

@ -417,7 +417,7 @@ cat_github_output() {
assert_success assert_success
assert_line "::debug::git-fetch has not been executed" assert_line "::debug::git-fetch will not be executed."
} }
@test "If SKIP_CHECKOUT is true git-checkout will not be called" { @test "If SKIP_CHECKOUT is true git-checkout will not be called" {
@ -430,7 +430,7 @@ cat_github_output() {
assert_success assert_success
assert_line "::debug::git-checkout has not been executed" assert_line "::debug::git-checkout will not be executed."
} }
@test "It pushes generated commit and tag to remote and actually updates the commit shas" { @test "It pushes generated commit and tag to remote and actually updates the commit shas" {
@ -1090,5 +1090,5 @@ cat_github_output() {
run git_auto_commit run git_auto_commit
assert_failure; assert_failure;
assert_line "::error ::git-auto-commit could not find git binary. Please make sure git is available." assert_line "::error::git-auto-commit could not find git binary. Please make sure git is available."
} }