From 81007d005392798e5a57c38010d18643026af1ac Mon Sep 17 00:00:00 2001 From: Aditya Alok Date: Wed, 27 Apr 2022 01:03:01 +0530 Subject: [PATCH] refactor(auto-update): do not append same log again - Now auto update will be disable until issue is closed. - Assign to GITHUB_ACTOR. Myself if GITHUB_ACTOR is Termux bot. Signed-off-by: Aditya Alok --- scripts/bin/update-packages | 199 +++++++++++++++++++----------------- 1 file changed, 107 insertions(+), 92 deletions(-) diff --git a/scripts/bin/update-packages b/scripts/bin/update-packages index 288500738..6f0e203d6 100755 --- a/scripts/bin/update-packages +++ b/scripts/bin/update-packages @@ -1,5 +1,5 @@ #!/usr/bin/env bash -# shellcheck source-path=/data/data/com.termux/files/home/termux-packages + set -u # Following variables should be set in environment outside of this script. @@ -104,6 +104,24 @@ _update() { fi } +# Check if an issue with same title already exists and is open. +_gh_check_issue_exists() { + local pkg_name="$1" + while read -r title; do + # Check for exact title match. + if [[ "${title}" == "Auto update failing for ${pkg_name}" ]]; then + return 0 + fi + done <<<"$( + gh issue list \ + --label "auto update failing" --label "bot" \ + --state open \ + --search "Auto update failing for ${pkg_name} in:title type:issue" \ + --json title | jq -r '.[] | .title' + )" + return 1 +} + declare -A _FAILED_UPDATES=() _run_update() { @@ -112,6 +130,10 @@ _run_update() { if [[ ! -f "${pkg_dir}/build.sh" ]]; then # Fail if detected a non-package directory. termux_error_exit "ERROR: directory '${pkg_dir}' is not a package." + elif [[ "${GITHUB_ACTIONS:-}" == "true" ]] && _gh_check_issue_exists "$(basename "${pkg_dir}")"; then + # Skip if issue with same title already exists. + echo "INFO: Skipping '$(basename "${pkg_dir}")', an update issue for it hasn't been resolved yet." + return fi # Run each package update in separate process since we include their environment variables. local output="" @@ -128,88 +150,106 @@ _run_update() { fi } -_gh_issue() { - local pkg_name="$1" - local header="$2" - local issue_number="${3:-}" - local max_comment_length=65536 # Max comment length in one request. +echo "INFO: Running update for: $*" - local body="$( +if [[ "$1" == "@all" ]]; then + for repo_dir in $(jq --raw-output 'keys | .[]' "${TERMUX_SCRIPTDIR}/repo.json"); do + for pkg_dir in "${repo_dir}"/*; do + _run_update "${pkg_dir}" + done + done +else + for pkg in "$@"; do + if [ ! -d "${pkg}" ]; then # If only package name is given, try to find it's directory. + for repo_dir in $(jq --raw-output 'keys | .[]' "${TERMUX_SCRIPTDIR}/repo.json"); do + if [ -d "${repo_dir}/${pkg}" ]; then + pkg="${repo_dir}/${pkg}" + break + fi + done + fi + _run_update "${pkg}" # Here, `pkg` is a directory. + done +fi + +################################################Failure handling################################################# + +_gh_create_new_issue() { + local pkg_name="$1" + + local max_body_length=65536 # Max length of the body for one request. + local assignee="${GITHUB_ACTOR:-}" + local issue_number + local body + + if [[ "${assignee:-termuxbot2}" == "termuxbot2" ]]; then + assignee="MrAdityaAlok" # Assign myself if termuxbot2 is the actor. + fi + + body="$( cat <<-EOF - ${header} + Hi, I'm Termux 🤖. + + I'm here to help you update your Termux packages. + + I've tried to update the ${pkg_name} package, but it failed. Here's the output of the update script: - -
Output log -
-				${_FAILED_UPDATES[$pkg_name]}
-			
+
+ Show log +
${_FAILED_UPDATES["${pkg_name}"]}
- Thanks! - +
- Run ID: ${GITHUB_RUN_ID}
- Timestamp: $(date -u +"%Y-%m-%d %H:%M:%S UTC")
+ Above error occured when I last tried to update at $(date -u +"%Y-%m-%d %H:%M:%S UTC").
+ Run ID: ${GITHUB_RUN_ID}

+ Note: Automatic updates will be disabled until this issue is resolved.
EOF )" - - if [[ -z "${issue_number}" ]]; then - issue_number="$( - gh issue create \ - --label "auto update failing" --label "bot" \ - --title "Auto update failing for ${pkg_name}" \ - --body "" | # Body is empty, since we will append the logs later. - grep -oE '[0-9]+' # Last component in url returned is issue number. - )" + issue_number=$( + gh issue create \ + --title "Auto update failing for ${pkg_name}" \ + --body "${body:0:${max_body_length}}" \ + --label "auto update failing" --label "bot" \ + --assignee ${assignee} | + grep -oE "[0-9]+" # Last component of the URL returned is the issue number. + ) + if [ -z "${issue_number}" ]; then + echo "ERROR: Failed to create issue." + return 1 fi - while true; do - local comment_part="${body:0:$max_comment_length}" - body="${body:$max_comment_length}" - sleep 5 # Otherwise we might get rate limited. - gh issue edit "$issue_number" \ - --body-file - <<<"$(gh issue view "$issue_number" --json body --jq '.body')
$comment_part" - # NOTE: we use --body-file instead of --body to avoid shell error 'argument list too long'. - if [[ -z "${body}" ]]; then - break - fi - done + echo "INFO: Created issue ${issue_number} for ${pkg_name}." + + if [[ -n "${body:${max_body_length}}" ]]; then + # The body was too long, so we need to append the rest. + while true; do + body="${body:${max_body_length}}" + if [[ -z "${body}" ]]; then + break + fi + sleep 5 # Otherwise we might get rate limited. + gh issue edit "$issue_number" \ + --body-file - <<<"$( + gh issue view "$issue_number" \ + --json body \ + --jq '.body' + )${body:0:${max_body_length}}" >/dev/null + # NOTE: we use --body-file instead of --body to avoid shell error 'argument list too long'. + done + fi } _handle_failure() { + echo # Newline. if [[ "${GITHUB_ACTIONS:-}" == "true" ]]; then - echo # Newline. - echo "INFO: Creating/Updating issue for failed updates." + echo "INFO: Creating issue for failed updates...(if any)" for pkg_name in "${!_FAILED_UPDATES[@]}"; do - local issue_number - issue_number=$( - gh issue list \ - --label "auto update failing" --label "bot" \ - --state open \ - --search "Auto update failing for ${pkg_name} in:title type:issue" \ - --json number --jq '.[0].number' # Assume only one issue (and it should be too). - ) - local header - if [[ -z "${issue_number}" ]]; then - header="$( - cat <<-EOF - Hi, I'm Termux 🤖. - - I'm here to help you update your Termux packages. - - I've tried to update the ${pkg_name} package, but it failed. - EOF - )" - else - header="

EDIT:


It failed again." - fi - _gh_issue "${pkg_name}" "${header}" ${issue_number} + _gh_create_new_issue "${pkg_name}" done - else - echo # Newline. echo "==> Failed updates:" local count=0 for pkg_name in "${!_FAILED_UPDATES[@]}"; do @@ -220,31 +260,6 @@ _handle_failure() { fi } -main() { - echo "INFO: Running update for: $*" - - if [[ "$1" == "@all" ]]; then - for repo_dir in $(jq --raw-output 'keys | .[]' "${TERMUX_SCRIPTDIR}/repo.json"); do - for pkg_dir in "${repo_dir}"/*; do - _run_update "${pkg_dir}" - done - done - else - for pkg in "$@"; do - if [ ! -d "${pkg}" ]; then # If only package name is given, try to find it's directory. - for repo_dir in $(jq --raw-output 'keys | .[]' "${TERMUX_SCRIPTDIR}/repo.json"); do - if [ -d "${repo_dir}/${pkg}" ]; then - pkg="${repo_dir}/${pkg}" - break - fi - done - fi - _run_update "${pkg}" # Here, `pkg` is a directory. - done - fi - if [[ ${#_FAILED_UPDATES[@]} -gt 0 ]]; then - _handle_failure - fi -} - -main "$@" +if [[ ${#_FAILED_UPDATES[@]} -gt 0 ]]; then + _handle_failure +fi