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 <dev.aditya.alok@gmail.com>
This commit is contained in:
Aditya Alok 2022-04-27 01:03:01 +05:30
parent 371bf382fe
commit 81007d0053
No known key found for this signature in database
GPG Key ID: 345AE134142077D8
1 changed files with 107 additions and 92 deletions

View File

@ -1,5 +1,5 @@
#!/usr/bin/env bash #!/usr/bin/env bash
# shellcheck source-path=/data/data/com.termux/files/home/termux-packages
set -u set -u
# Following variables should be set in environment outside of this script. # Following variables should be set in environment outside of this script.
@ -104,6 +104,24 @@ _update() {
fi 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=() declare -A _FAILED_UPDATES=()
_run_update() { _run_update() {
@ -112,6 +130,10 @@ _run_update() {
if [[ ! -f "${pkg_dir}/build.sh" ]]; then if [[ ! -f "${pkg_dir}/build.sh" ]]; then
# Fail if detected a non-package directory. # Fail if detected a non-package directory.
termux_error_exit "ERROR: directory '${pkg_dir}' is not a package." 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 fi
# Run each package update in separate process since we include their environment variables. # Run each package update in separate process since we include their environment variables.
local output="" local output=""
@ -128,88 +150,106 @@ _run_update() {
fi fi
} }
_gh_issue() { echo "INFO: Running update for: $*"
local pkg_name="$1"
local header="$2"
local issue_number="${3:-}"
local max_comment_length=65536 # Max comment length in one request.
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 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: Here's the output of the update script:
<details>
<details><summary>Output log</summary> <summary>Show log</summary>
<pre lang="bash"> <pre>${_FAILED_UPDATES["${pkg_name}"]}</pre>
${_FAILED_UPDATES[$pkg_name]}
</pre>
</details> </details>
Thanks! <hr>
<i> <i>
Run ID: <a href="${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}/actions/runs/${GITHUB_RUN_ID}">${GITHUB_RUN_ID}</a><br> Above error occured when I last tried to update at $(date -u +"%Y-%m-%d %H:%M:%S UTC").<br>
Timestamp: $(date -u +"%Y-%m-%d %H:%M:%S UTC")<br> Run ID: <a href="${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}/actions/runs/${GITHUB_RUN_ID}">${GITHUB_RUN_ID}</a><br><br>
<b>Note:</b> Automatic updates will be disabled until this issue is resolved.<br>
</i> </i>
EOF EOF
)" )"
issue_number=$(
if [[ -z "${issue_number}" ]]; then gh issue create \
issue_number="$( --title "Auto update failing for ${pkg_name}" \
gh issue create \ --body "${body:0:${max_body_length}}" \
--label "auto update failing" --label "bot" \ --label "auto update failing" --label "bot" \
--title "Auto update failing for ${pkg_name}" \ --assignee ${assignee} |
--body "" | # Body is empty, since we will append the logs later. grep -oE "[0-9]+" # Last component of the URL returned is the issue number.
grep -oE '[0-9]+' # Last component in url returned is issue number. )
)" if [ -z "${issue_number}" ]; then
echo "ERROR: Failed to create issue."
return 1
fi fi
while true; do echo "INFO: Created issue ${issue_number} for ${pkg_name}."
local comment_part="${body:0:$max_comment_length}"
body="${body:$max_comment_length}" if [[ -n "${body:${max_body_length}}" ]]; then
sleep 5 # Otherwise we might get rate limited. # The body was too long, so we need to append the rest.
gh issue edit "$issue_number" \ while true; do
--body-file - <<<"$(gh issue view "$issue_number" --json body --jq '.body')<br>$comment_part" body="${body:${max_body_length}}"
# NOTE: we use --body-file instead of --body to avoid shell error 'argument list too long'. if [[ -z "${body}" ]]; then
if [[ -z "${body}" ]]; then break
break fi
fi sleep 5 # Otherwise we might get rate limited.
done 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() { _handle_failure() {
echo # Newline.
if [[ "${GITHUB_ACTIONS:-}" == "true" ]]; then if [[ "${GITHUB_ACTIONS:-}" == "true" ]]; then
echo # Newline. echo "INFO: Creating issue for failed updates...(if any)"
echo "INFO: Creating/Updating issue for failed updates."
for pkg_name in "${!_FAILED_UPDATES[@]}"; do for pkg_name in "${!_FAILED_UPDATES[@]}"; do
local issue_number _gh_create_new_issue "${pkg_name}"
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="<h4>EDIT:</h4><br>It failed again."
fi
_gh_issue "${pkg_name}" "${header}" ${issue_number}
done done
else else
echo # Newline.
echo "==> Failed updates:" echo "==> Failed updates:"
local count=0 local count=0
for pkg_name in "${!_FAILED_UPDATES[@]}"; do for pkg_name in "${!_FAILED_UPDATES[@]}"; do
@ -220,31 +260,6 @@ _handle_failure() {
fi fi
} }
main() { if [[ ${#_FAILED_UPDATES[@]} -gt 0 ]]; then
echo "INFO: Running update for: $*" _handle_failure
fi
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 "$@"