diff --git a/.github/workflows/auto-close-inactive-pr.yml b/.github/workflows/auto-close-inactive-pr.yml index b33198a1a58..79d2d3e79b2 100644 --- a/.github/workflows/auto-close-inactive-pr.yml +++ b/.github/workflows/auto-close-inactive-pr.yml @@ -18,8 +18,6 @@ jobs: steps: - name: Checkout repository uses: actions/checkout@v4 - with: - ref: master - name: Auto close inactive PR run: .github/workflows/bin/auto-close-inactive-pr.sh --github-project-path "${REPO}" --github-token "${GITHUB_TOKEN}" --dry diff --git a/.github/workflows/bin/auto-close-inactive-pr.sh b/.github/workflows/bin/auto-close-inactive-pr.sh index 46cf8a90eeb..85d668ea385 100755 --- a/.github/workflows/bin/auto-close-inactive-pr.sh +++ b/.github/workflows/bin/auto-close-inactive-pr.sh @@ -42,7 +42,7 @@ function consume_args(){ case "$1" in --help) usage - return 0 + exit 0 ;; --dry) if [[ ${2:-} =~ true|false ]] ; then @@ -60,7 +60,7 @@ function consume_args(){ shift shift else - print_error "option $1" "can not be null" + log_error "option $1" "can not be null" usage >&2 return 1 fi @@ -71,7 +71,7 @@ function consume_args(){ shift shift else - print_error "option $1" "can not be null" + log_error "option $1" "can not be null" usage >&2 return 1 fi @@ -82,13 +82,13 @@ function consume_args(){ shift shift else - print_error "option $1" "can not be null" + log_error "option $1" "can not be null" usage >&2 return1 fi ;; *) - print_error "option $1" "is unknown" + log_error "option $1" "is unknown" usage >&2 return 1 ;; @@ -96,13 +96,13 @@ function consume_args(){ done for mandatory_option in github_project_path github_token ; do if [[ -z ${!mandatory_option} ]] ; then - print_error "option --${mandatory_option//_/-}" "is mandatory" + log_error "option --${mandatory_option//_/-}" "is mandatory" usage >&2 return 1 fi done if ! (command -v gh >/dev/null 2>&1) ; then - print_error "packages prerequisites" "github client (gh) has to be installed on your system (https://github.com/cli/cli)" + log_error "packages prerequisites" "github client (gh) has to be installed on your system (https://github.com/cli/cli)" usage >&2 return 1 fi @@ -123,8 +123,7 @@ function is_timestamp_young(){ # main script_dir=$(dirname "$0") -source "${script_dir}/github.functions.sh" -source "${script_dir}/common.functions.sh" +source "${script_dir}/shared.functions.sh" init_terminal_colors consume_args "$@" github_connect "${github_token}" @@ -142,7 +141,7 @@ else comment="📆 This PR has been closed because there is no activity (commits/comments) for more than ${max_days_of_inactivity} days 😥. Feel free to reopen it with new commits/comments." if [[ ${dry} == false ]] ; then if ! result=$(github_close_pr "${github_project_path}" "${pr_number}" "${comment}" 2>&1) ; then - print_error "${FUNCNAME[0]}" "$(head -1 <<< "${result}")" + log_error "${FUNCNAME[0]}" "$(head -1 <<< "${result}")" return 1 fi fi diff --git a/.github/workflows/bin/common.functions.sh b/.github/workflows/bin/common.functions.sh deleted file mode 100755 index 395f881ce48..00000000000 --- a/.github/workflows/bin/common.functions.sh +++ /dev/null @@ -1,24 +0,0 @@ -#!/bin/bash -set -Eeuo pipefail - -# functions - -function init_terminal_colors(){ - color_black=$(echo -ne "\033[1;30m") - color_red=$(echo -ne "\033[1;31m") - color_green=$(echo -ne "\033[1;32m") - color_yellow=$(echo -ne "\033[1;33m") - color_blue=$(echo -ne "\033[1;34m") - color_purple=$(echo -ne "\033[1;34m") - color_cyan=$(echo -ne "\033[1;36m") - color_backwhite=$(echo -ne "\033[1;47m") - color_reset=$(echo -ne "\033[0m") - export color_black color_red color_green color_yellow color_blue color_purple color_cyan color_backwhite color_reset -} - -function print_error(){ - title=$1 - message=$2 - echo "${color_red}$(basename "$0"): ${title}: ${message}${color_reset}" -} - diff --git a/.github/workflows/bin/github.functions.sh b/.github/workflows/bin/github.functions.sh deleted file mode 100755 index a52486660b4..00000000000 --- a/.github/workflows/bin/github.functions.sh +++ /dev/null @@ -1,92 +0,0 @@ -#!/bin/bash -set -Eeuo pipefail - -# functions - -function github_connect(){ - if [[ $# -ne 1 ]] ; then - print_error "internal function ${FUNCNAME[0]}" "please provide one parameter, ${FUNCNAME[0]} \$github_token" - return 1 - else - token=$1 - if ! result=$(gh auth login --with-token 2>&1 <<< "${token}") ; then - print_error "${FUNCNAME[0]}" "$(head -1 <<< "${result}")" - return 1 - fi - fi -} - -function github_test_repo(){ - if [[ $# -ne 1 ]] ; then - print-eror "internal function ${FUNCNAME[0]}" "please provide one parameter, ${FUNCNAME[0]} \$github_project_path" - return 1 - else - project_path=$1 - if ! result=$(gh repo view "${project_path}" 2>&1) ; then - print_error "${FUNCNAME[0]}" "$(head -1 <<< "${result}")" - return 1 - fi - fi -} - -function github_get_pr_number_list(){ - if [[ $# -ne 1 ]] ; then - print_error "internal function ${FUNCNAME[0]}" "please provide one parameter, ${FUNCNAME[0]} \$github_project_path" - return 1 - else - project_path=$1 - if result=$(gh pr list --state open --json number --jq '.[]|.number' --repo "${project_path}" 2>&1) ; then - sort -V <<< "${result}" - else - print_error "${FUNCNAME[0]}" "$(head -1 <<< "${result}")" - return 1 - fi - fi -} - -function github_get_pr_last_update_timestamp(){ - if [[ $# -ne 2 ]] ; then - print_error "internal function ${FUNCNAME[0]}" "please provide one parameter, ${FUNCNAME[0]} \$github_project_path \$pr_number" - return 1 - else - project_path=$1 - number=$2 - if result=$(gh pr view --json updatedAt --jq .updatedAt --jq '.updatedAt|fromdate|tostring' "https://github.com/${project_path}/pull/${number}" 2>&1) ; then - echo "${result}" - else - print_error "${FUNCNAME[0]}" "$(head -1 <<< "${result}")" - return 1 - fi - fi -} - -function github_comment_pr(){ - if [[ $# -ne 3 ]] ; then - print_error "internal function ${FUNCNAME[0]}" "please provide one parameter, ${FUNCNAME[0]} \$github_project_path \$pr_number \$pr_comment" - return 1 - else - project_path=$1 - pr_number=$2 - comment=$3 - if ! result=$(gh pr comment "${pr_number}" --repo "${project_path}" --body "${comment}") ; then - print_error "${FUNCNAME[0]}" "$(head -1 <<< "${result}")" - return 1 - fi - fi -} - -function github_close_pr(){ - if [[ $# -ne 3 ]] ; then - print_error "internal function ${FUNCNAME[0]}" "please provide one parameter, ${FUNCNAME[0]} \$github_project_path \$pr_number \$pr_comment" - return 1 - else - project_path=$1 - pr_number=$2 - comment=$3 - if ! result=$(gh pr close "${pr_number}" --repo "${project_path}" --comment "${comment}") ; then - print_error "${FUNCNAME[0]}" "$(head -1 <<< "${result}")" - return 1 - fi - fi -} - diff --git a/.github/workflows/bin/shared.functions.sh b/.github/workflows/bin/shared.functions.sh new file mode 100755 index 00000000000..b3afb411e51 --- /dev/null +++ b/.github/workflows/bin/shared.functions.sh @@ -0,0 +1,162 @@ +#!/bin/bash +set -Eeuo pipefail + +# functions + +function init_terminal_colors(){ + color_red=$(echo -ne "\033[1;31m") + color_green=$(echo -ne "\033[1;32m") + color_yellow=$(echo -ne "\033[1;33m") + color_cyan=$(echo -ne "\033[1;36m") + color_reset=$(echo -ne "\033[0m") +} + +function log(){ + color="$1" + title="$2" + message="$3" + if basename "$0" >/dev/null 2>&1 ; then + parent=$(basename "$0") + else + parent="." + fi + echo "${color}${parent}: ${title}: ${message}${color_reset}" +} + +function log_error(){ + title="$1" + message="$2" + log "${color_red}" "${title}" "${message}" +} + +function log_warning(){ + title=$1 + message=$2 + log "${color_yellow}" "${title}" "${message}" +} + +function log_success(){ + title=$1 + message=$2 + log "${color_green}" "${title}" "${message}" +} + +function log_running(){ + title=$1 + message=$2 + log "${color_cyan}" "${title}" "${message}" +} + +function github_connect(){ + if [[ -n ${GITHUB_TOKEN:-} ]] ; then + connect=$(gh auth login 2>&1 || true) + if ! result=$(gh auth status 2>&1) ; then + log_error "${FUNCNAME[0]}" "$(head -3 <<< "${connect}")" + log_error "${FUNCNAME[0]}" "$(head -3 <<< "${result}")" + return 1 + else + log_warning "${FUNCNAME[0]}" "As \$GITHUB_TOKEN is set, it has been used for github auth" + return 0 + fi + else + if [[ $# -ne 1 ]] ; then + log_error "internal function ${FUNCNAME[0]}" "please provide one parameter, ${FUNCNAME[0]} \$github_token" + return 1 + else + token=$1 + connect=$(gh auth login --with-token 2>&1 <<< "${token}" || true) + if ! result=$(gh auth status 2>&1) ; then + log_error "${FUNCNAME[0]}" "$(head -1 <<< "${connect}")" + log_error "${FUNCNAME[0]}" "$(head -1 <<< "${result}")" + return 1 + else + return 0 + fi + fi + fi +} + +function github_test_repo(){ + if [[ $# -ne 1 ]] ; then + print-eror "internal function ${FUNCNAME[0]}" "please provide one parameter, ${FUNCNAME[0]} \$github_project_path" + return 1 + else + project_path=$1 + if ! result=$(gh repo view "${project_path}" 2>&1) ; then + log_error "${FUNCNAME[0]}" "$(head -1 <<< "${result}")" + return 1 + else + return 0 + fi + fi +} + +function github_get_pr_number_list(){ + if [[ $# -ne 1 ]] ; then + log_error "internal function ${FUNCNAME[0]}" "please provide one parameter, ${FUNCNAME[0]} \$github_project_path" + return 1 + else + project_path=$1 + if result=$(gh pr list --state open --json number --jq '.[]|.number' --repo "${project_path}" 2>&1) ; then + sort -V <<< "${result}" + return 0 + else + log_error "${FUNCNAME[0]}" "$(head -1 <<< "${result}")" + return 1 + fi + fi +} + +function github_get_pr_last_update_timestamp(){ + if [[ $# -ne 2 ]] ; then + log_error "internal function ${FUNCNAME[0]}" "please provide one parameter, ${FUNCNAME[0]} \$github_project_path \$pr_number" + return 1 + else + project_path=$1 + number=$2 + if result=$(gh pr view --json updatedAt --jq .updatedAt --jq '.updatedAt|fromdate|tostring' "https://github.com/${project_path}/pull/${number}" 2>&1) ; then + echo "${result}" + return 0 + else + log_error "${FUNCNAME[0]}" "$(head -1 <<< "${result}")" + return 1 + fi + fi +} + +function github_comment_pr(){ + if [[ $# -ne 3 ]] ; then + log_error "internal function ${FUNCNAME[0]}" "please provide one parameter, ${FUNCNAME[0]} \$github_project_path \$pr_number \$pr_comment" + return 1 + else + project_path=$1 + pr_number=$2 + comment=$3 + if ! result=$(gh pr comment "${pr_number}" --repo "${project_path}" --body "${comment}") ; then + log_error "${FUNCNAME[0]}" "$(head -1 <<< "${result}")" + return 1 + else + return 0 + fi + fi +} + +function github_close_pr(){ + if [[ $# -ne 3 ]] ; then + log_error "internal function ${FUNCNAME[0]}" "please provide one parameter, ${FUNCNAME[0]} \$github_project_path \$pr_number \$pr_comment" + return 1 + else + project_path=$1 + pr_number=$2 + comment=$3 + if ! result=$(gh pr close "${pr_number}" --repo "${project_path}" --comment "${comment}") ; then + log_error "${FUNCNAME[0]}" "$(head -1 <<< "${result}")" + return 1 + else + return 0 + fi + fi +} + +# main +init_terminal_colors