Skip to content

Commit

Permalink
Fix GITHUB_TOKEN usage for auto close inactive pr
Browse files Browse the repository at this point in the history
  • Loading branch information
lepapareil committed Nov 30, 2023
1 parent d977a3b commit 7558bf4
Show file tree
Hide file tree
Showing 5 changed files with 171 additions and 128 deletions.
2 changes: 0 additions & 2 deletions .github/workflows/auto-close-inactive-pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

19 changes: 9 additions & 10 deletions .github/workflows/bin/auto-close-inactive-pr.sh
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ function consume_args(){
case "$1" in
--help)
usage
return 0
exit 0
;;
--dry)
if [[ ${2:-} =~ true|false ]] ; then
Expand All @@ -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
Expand All @@ -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
Expand All @@ -82,27 +82,27 @@ 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
;;
esac
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
Expand All @@ -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}"
Expand All @@ -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
Expand Down
24 changes: 0 additions & 24 deletions .github/workflows/bin/common.functions.sh

This file was deleted.

92 changes: 0 additions & 92 deletions .github/workflows/bin/github.functions.sh

This file was deleted.

162 changes: 162 additions & 0 deletions .github/workflows/bin/shared.functions.sh
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 7558bf4

Please sign in to comment.