Skip to content

Commit

Permalink
script: refactor (2.1) (#474)
Browse files Browse the repository at this point in the history
* script: refactor (2.1)

script: fixed argument handling

-z requires no argument
-p requires no argument
-f requires no argument

script: refactor and improve usage with default values

* misc and logging
  • Loading branch information
bakerboy448 authored Jul 7, 2024
1 parent a48b33c commit f871d82
Showing 1 changed file with 63 additions and 24 deletions.
87 changes: 63 additions & 24 deletions scripts/indexer-sync-v2.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,30 @@
### Typically only prowlarr_git_path would be needed to be set
## Using the Script
### Suggested to run from the current directory being Prowlarr/Indexers local Repo using Git Bash `./scripts/prowlarr-indexers-jackettpull.sh`
# Default values
prowlarr_remote_name="origin"
prowlarr_target_branch="master"
mode_choice="normal"
push_mode=false
push_mode_force=false
PROWLARR_COMMIT_TEMPLATE="jackett indexers as of"
PROWLARR_COMMIT_TEMPLATE_APPEND=""
PROWLARR_REPO_URL="https://github.com/Prowlarr/Indexers.git"
JACKETT_REPO_URL="https://github.com/Jackett/Jackett.git"
PROWLARR_RELEASE_BRANCH="master"
JACKETT_BRANCH="master"
JACKETT_REMOTE_NAME="z_Jackett"
SKIP_BACKPORT=false
is_dev_exec=false
pulls_exists=false
local_exist=false
# Initialize Defaults
removed_indexers=""
added_indexers=""
modified_indexers=""
newschema_indexers=""

usage() {
echo "Usage: $0 [-r remote] [-b branch] [-m mode (normal or dev)] [-p push to remote] [-f force push if pushing] [-c commit_template] [-u prowlarr_repo_url] [-j jackett_repo_url] [-R release_branch] [-J jackett_branch] [-n jackett_remote_name] [-z skip_backport]"
exit 1
}

# Default values
prowlarr_remote_name="origin"
prowlarr_target_branch="master"
Expand All @@ -37,6 +55,23 @@ removed_indexers=""
added_indexers=""
modified_indexers=""
newschema_indexers=""
usage() {
echo "Usage: $0 [options]
Options:
-r <remote> Set the Prowlarr remote name. Default: $prowlarr_remote_name
-b <branch> Set the Prowlarr target branch. Default: $prowlarr_target_branch
-m <mode> Set the mode ('normal' or 'dev'). Default: $mode_choice
-p Enable push to remote. Default: $push_mode
-f Force push if pushing. Default: $push_mode_force
-c <commit_template> Set the commit template for Prowlarr. Default: $PROWLARR_COMMIT_TEMPLATE
-u <repo_url> Set the Prowlarr repository URL. Default: $PROWLARR_REPO_URL
-j <repo_url> Set the Jackett repository URL. Default: $JACKETT_REPO_URL
-R <release_branch> Set the Prowlarr release branch. Default: $PROWLARR_RELEASE_BRANCH
-J <jackett_branch> Set the Jackett branch. Default: $JACKETT_BRANCH
-n <remote_name> Set the Jackett remote name. Default: $JACKETT_REMOTE_NAME
-z Skip backporting. Default: $SKIP_BACKPORT"
exit 1
}

# Prowlarr Schema Versions
## v1 frozen 2021-10-13
Expand Down Expand Up @@ -152,11 +187,12 @@ initialize_script() {
fi
}

while getopts ":f:r:b:m:p:c:u:j:R:J:n:z:" opt; do
while getopts "frpzb:m:c:u:j:R:J:n:" opt; do
case ${opt} in
f)
# No Arg
push_mode_force=true
log "DEBUG" "push_mode_force using argument $push_mode_force"
log "DEBUG" "push_mode_force is $push_mode_force"
;;
r)
prowlarr_remote_name=$OPTARG
Expand All @@ -175,7 +211,7 @@ while getopts ":f:r:b:m:p:c:u:j:R:J:n:z:" opt; do
;;
development | d | D)
is_dev_exec=true
log "INFO" "Skipping upstream reset to local. Also Skip checking out the local branch and log an info message."
log "WARN" "Skipping upstream reset to local. Also Skip checking out the local branch and output thr details."
log "INFO" "This will not reset branch from upstream/master and will ONLY checkout the selected branch to use."
log "INFO" "This will pause at various debugging points for human review"
;;
Expand All @@ -185,8 +221,9 @@ while getopts ":f:r:b:m:p:c:u:j:R:J:n:z:" opt; do
esac
;;
p)
# No Arg
push_mode=true
log "DEBUG" "push_mode using argument $push_mode"
log "DEBUG" "push_mode is $push_mode"
;;
c)
PROWLARR_COMMIT_TEMPLATE=$OPTARG
Expand All @@ -213,9 +250,10 @@ while getopts ":f:r:b:m:p:c:u:j:R:J:n:z:" opt; do
log "DEBUG" "JACKETT_REMOTE_NAME using argument $JACKETT_REMOTE_NAME"
;;
z)
# No Arg
SKIP_BACKPORT=true
PROWLARR_COMMIT_TEMPLATE_APPEND="[backports skipped - TODO]"
log "DEBUG" "SKIP_BACKPORT using argument $SKIP_BACKPORT"
log "DEBUG" "SKIP_BACKPORT is $SKIP_BACKPORT. Commit Template will be appended with 'PROWLARR_COMMIT_TEMPLATE_APPEND'"
;;
\?)
usage
Expand Down Expand Up @@ -252,15 +290,15 @@ check_branches() {

if [ -z "$local_pulls_check" ]; then
local_exist=false
log "INFO" "local branch [$prowlarr_target_branch] does not exist"
log "WARN" "local branch [$prowlarr_target_branch] does not exist"
else
local_exist=true
log "INFO" "local branch [$prowlarr_target_branch] does exist"
fi

if [ -z "$remote_pulls_check" ]; then
pulls_exists=false
log "INFO" "remote repo/branch [$prowlarr_remote_name/$prowlarr_target_branch] does not exist"
log "WARN" "remote repo/branch [$prowlarr_remote_name/$prowlarr_target_branch] does not exist"
else
pulls_exists=true
log "INFO" "remote repo/branch [$prowlarr_remote_name/$prowlarr_target_branch] does exist"
Expand All @@ -271,7 +309,7 @@ handle_branch_reset() {
if [ "$pulls_exists" = false ]; then
if [ "$local_exist" = true ]; then
if [ "$is_dev_exec" = true ]; then
log "INFO" "[$is_dev_exec] skipping reset to [$prowlarr_remote_name/$PROWLARR_RELEASE_BRANCH] and checking out local branch [$prowlarr_target_branch]"
log "DEBUG" "[$is_dev_exec] skipping reset to [$prowlarr_remote_name/$PROWLARR_RELEASE_BRANCH] and checking out local branch [$prowlarr_target_branch]"
git checkout -B "$prowlarr_target_branch"
else
git reset --hard "$prowlarr_remote_name"/"$PROWLARR_RELEASE_BRANCH"
Expand Down Expand Up @@ -311,10 +349,10 @@ pull_cherry_and_merge() {
jackett_recent_commit=$(git rev-parse "$JACKETT_REMOTE_NAME/$JACKETT_BRANCH")
log "INFO" "most recent Jackett commit is: [$jackett_recent_commit] from [$JACKETT_REMOTE_NAME/$JACKETT_BRANCH]"
recent_pulled_commit=$(echo "$prowlarr_commits" | awk 'NR==1{print $5}')
log "INFO" "most recent Prowlarr jackett commit is: [$recent_pulled_commit] from [$prowlarr_remote_name/$prowlarr_target_branch]"
log "INFO" "most recent Prowlarr pulled jackett commit is: [$recent_pulled_commit] from [$prowlarr_remote_name/$prowlarr_target_branch]"

if [ "$jackett_recent_commit" = "$recent_pulled_commit" ]; then
log "SUCCESS" "we are current with jackett; nothing to do"
log "SUCCESS" "--- we are current with jackett; nothing to do ---"
exit 0
fi

Expand All @@ -334,9 +372,10 @@ pull_cherry_and_merge() {
# read -r -p "Pausing to review commits. Press any key to continue." -n1 -s
fi
log "INFO" "Commit Range is: [$commit_range]"
log "INFO" "Beginning Cherrypicking"
log "INFO" "-- Beginning Cherrypicking ---"
git config merge.directoryRenames true
git config merge.verbosity 0
sleep 5

for pick_commit in ${commit_range}; do
has_conflicts=$(git ls-files --unmerged)
Expand All @@ -345,7 +384,7 @@ pull_cherry_and_merge() {
read -r -p "Pausing due to conflicts. Press any key to continue when resolved." -n1 -s
log "INFO" "Continuing Cherrypicking"
fi
log "INFO" "cherrypicking [$pick_commit]"
log "INFO" "cherrypicking Jackett commit [$pick_commit]"
git cherry-pick --no-commit --rerere-autoupdate --allow-empty --keep-redundant-commits "$pick_commit"
has_conflicts=$(git ls-files --unmerged)
if [ -n "$has_conflicts" ]; then
Expand All @@ -355,7 +394,7 @@ pull_cherry_and_merge() {
git config merge.verbosity 2
done

log "SUCCESS" "Completed cherry picking"
log "SUCCESS" "--- Completed cherry picking ---"
log "INFO" "Evaluating and Reviewing Changes"

git checkout HEAD -- "definitions/v*/schema.json"
Expand All @@ -377,18 +416,18 @@ resolve_conflicts() {

log "WARN" "conflicts exist"
if [ -n "$readme_conflicts" ]; then
log "INFO" "README conflict exists; using Prowlarr README"
log "DEBUG" "README conflict exists; using Prowlarr README"
git checkout --ours "README.md"
git add --f "README.md"
fi
if [ -n "$schema_conflicts" ]; then
log "INFO" "Schema conflict exists; using Prowlarr schema"
log "DEBUG" "Schema conflict exists; using Prowlarr schema"
git checkout --ours "*schema.json"
git add --f "*schema.json"
fi

if [ -n "$nonyml_conflicts" ]; then
log "INFO" "Non-YML conflicts exist; removing cs, js, iss, html"
log "DEBUG" "Non-YML conflicts exist; removing cs, js, iss, html"
git rm --f --q --ignore-unmatch "*.cs*"
git rm --f --q --ignore-unmatch "*.js"
git rm --f --q --ignore-unmatch "*.iss*"
Expand All @@ -398,15 +437,15 @@ resolve_conflicts() {
git checkout --ours ".editorconfig"
fi
if [ -n "$yml_conflicts" ]; then
log "INFO" "YML conflict exists; [$yml_conflicts]"
log "DEBUG" "YML conflict exists; [$yml_conflicts]"
handle_yml_conflicts
fi
}

handle_yml_conflicts() {
yml_remove=$(git status --porcelain | grep yml | grep -v "definitions/" | awk -F '[ADUMRC]{1,2} ' '{print $2}' | awk '{ gsub(/^[ \t]+|[ \t]+$/, ""); print }')
for def in $yml_remove; do
log "INFO" "Removing non-definition yml; [$yml_remove]"
log "DEBUG" "Removing non-definition yml; [$yml_remove]"
git rm --f --ignore-unmatch "$yml_remove"
yml_conflicts=$(git diff --cached --name-only | grep ".yml")
done
Expand Down Expand Up @@ -500,7 +539,7 @@ handle_modified_indexers() {
unset indexer
unset test
fi
log "SUCCESS" "completed changed indexers"
log "SUCCESS" "--- completed changed indexers ---"
}

handle_backporting_indexers() {
Expand Down Expand Up @@ -544,7 +583,7 @@ handle_backporting_indexers() {
unset indexer
unset indexer_check
fi
log "SUCCESS" "completed backporting indexers"
log "SUCCESS" "--- completed backporting indexers ---"
}

cleanup_and_commit() {
Expand Down

0 comments on commit f871d82

Please sign in to comment.