Skip to content

Commit

Permalink
OTT-1810: Update prebid-server version upgrade script (#807)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ankit-Pinge authored Jul 9, 2024
1 parent 08940f6 commit 4f724cb
Showing 1 changed file with 117 additions and 119 deletions.
236 changes: 117 additions & 119 deletions scripts/upgrade-pbs.sh
Original file line number Diff line number Diff line change
@@ -1,50 +1,114 @@
#!/bin/bash -e


attempt=1

usage="
Script starts or continues prebid upgrade to version set in 'to_minor' variable. Workspace is at /tmp/prebid-server and /tmp/pbs-patch
./upgrade-pbs.sh [--restart]
./upgrade-pbs.sh [--restart] [--version=VERSION]
--restart Restart the upgrade (deletes /tmp/prebid-server and /tmp/pbs-patch)
--version=VERSION Specify a particular version to upgrade to (optional)
-h Help
TODO:
- paramertrize the script
- create ci branch PR
- create header-bidding PR"

RESTART=0
for i in "$@"; do
case $i in
--restart)
RESTART=1
shift
;;
-h)
echo "$usage"
exit 0
;;
esac
done
VERSION=""

# Process arguments
process_arguments() {
for i in "$@"; do
case $i in
--restart)
RESTART=1
;;
-h)
echo "$usage"
exit 0
;;
--version=*)
VERSION="${i#*=}"
;;
esac
done
}

# --- start ---
CHECKLOG=/tmp/pbs-patch/checkpoints.log
# Restart upgrade
restart_upgrade() {
if [ "$RESTART" -eq "1" ]; then
log "Restarting the upgrade: rm -rf /tmp/prebid-server /tmp/pbs-patch/"
rm -rf /tmp/prebid-server /tmp/pbs-patch/
mkdir -p /tmp/pbs-patch/
fi
}

trap 'clear_log' EXIT
# Initialize upgrade
initialize_upgrade() {
checkpoint_run clone_repo
cd /tmp/prebid-server
log "At $(pwd)"

# Get the latest tag if VERSION is not specified
if [ -z "$VERSION" ]; then
VERSION=$(git describe --tags $(git rev-list --tags --max-count=1))
fi

log "Final Upgrade Version: $VERSION"

git diff tags/$VERSION..origin/master > /tmp/pbs-patch/current_ow_patch-$VERSION-origin_master-$attempt.diff
}

# Start validation
start_validation() {
go_mod
checkpoint_run "./validate.sh --race 5 --nofmt"
go_discard
}

log () {
printf "\n$(date): $1\n"
# Setup branches
setup_branches() {
tag_base_branch_name=prebid_$VERSION-$attempt-tag
upgrade_branch_name=prebid_$VERSION-$attempt
log "Reference tag branch: $tag_base_branch_name"
log "Upgrade branch: $upgrade_branch_name"

checkpoint_run checkout_branch
}

# Merge branches
merge_branches() {
log "Merging master into $tag_base_branch_name"
checkpoint_run git merge master --no-edit

log "Validating the master merge into current tag. Fix and commit changes if required."
go_mod
checkpoint_run "./validate.sh --race 5 --nofmt"
go_discard

checkpoint_run git checkout master
checkpoint_run git merge $upgrade_branch_name --no-edit
}

# Generate patch file
generate_patch() {
log "Generating patch file at /tmp/pbs-patch/ for $VERSION"
git diff tags/$VERSION..master > /tmp/pbs-patch/new_ow_patch_$VERSION-master-1.diff
}

# Log message
log() {
printf "\n$(date): $1\n"
}

# Clear log on exit
clear_log() {
current_fork_at_version=$(git describe --tags --abbrev=0)
if [ "$current_fork_at_version" == "$upgrade_version" ] ; then
if [ "$current_fork_at_version" == "$VERSION" ]; then
log "Upgraded to $current_fork_at_version"
rm -f "$CHECKLOG"

log "Last validation before creating PR"
go_mod
checkpoint_run "./validate.sh --race 5 --nofmt"
Expand All @@ -54,13 +118,14 @@ clear_log() {
log "Commit final go.mod and go.sum"
git commit go.mod go.sum --amend --no-edit
set -e
else

else
log "Exiting with failure!!!"
exit 1
fi
}


# Clone repository
clone_repo() {
if [ -d "/tmp/prebid-server" ]; then
log "Code already cloned. Attempting to continue the upgrade!!!"
Expand All @@ -71,28 +136,20 @@ clone_repo() {
cd prebid-server

git remote add prebid-upstream https://github.com/prebid/prebid-server.git
git remote -v
git fetch --all --tags --prune
fi
}

# Checkout branch
checkout_branch() {
set +e
git checkout tags/$_upgrade_version -b $tag_base_branch_name
# git push origin $tag_base_branch_name

set +e
git checkout tags/$VERSION -b $tag_base_branch_name
git checkout -b $upgrade_branch_name
git checkout $upgrade_branch_name
# git push origin $upgrade_branch_name

set -e
# if [ "$?" -ne 0 ]
# then
# log "Failed to create branch $upgrade_branch_name. Already working on it???"
# exit 1
# fi
}

# Execute command
cmd_exe() {
cmd=$*
if ! $cmd; then
Expand All @@ -102,102 +159,43 @@ cmd_exe() {
fi
}

# Run checkpoint
checkpoint_run() {
cmd=$*
if [ -f $CHECKLOG ] ; then
if grep -q "$cmd" "$CHECKLOG"; then
log "Retry this checkpoint: $cmd"
cmd_exe $cmd
rm "$CHECKLOG"
elif grep -q "./validate.sh --race 5 --nofmt" "$CHECKLOG"; then
log "Special checkpoint. ./validate.sh --race 5 --nofmt failed for last tag update. Hence, only fixes are expected in successfully upgraded branch. (change in func() def, wrong conflict resolve, etc)"
cmd_exe $cmd
rm "$CHECKLOG"
else
log "Skip this checkpoint: $cmd"
return
fi
if [ -f $CHECKLOG ] && grep -q "$cmd" "$CHECKLOG"; then
log "Retrying checkpoint: $cmd"
cmd_exe $cmd
rm "$CHECKLOG"
else
cmd_exe $cmd
fi
cmd_exe $cmd
}

# Manage Go modules
go_mod() {
go mod download all
go mod tidy
go mod tidy
go mod download all
}

# Discard Go module changes
go_discard() {
git checkout go.mod go.sum
}

# --- main ---
# Main script
main() {
process_arguments "$@"
restart_upgrade
initialize_upgrade
start_validation
setup_branches
merge_branches
generate_patch
}

if [ "$RESTART" -eq "1" ]; then
log "Restarting the upgrade: rm -rf /tmp/prebid-server /tmp/pbs-patch/"
rm -rf /tmp/prebid-server /tmp/pbs-patch/
mkdir -p /tmp/pbs-patch/
fi
# --- start ---
CHECKLOG=/tmp/pbs-patch/checkpoints.log
trap 'clear_log' EXIT

log "Final Upgrade Version: $upgrade_version"
log "Attempt: $attempt"

checkpoint_run clone_repo
cd /tmp/prebid-server
log "At $(pwd)"

# Get the latest tag
latest_tag=$(git describe --tags --abbrev=0)

git diff tags/$latest_tag..origin/master > /tmp/pbs-patch/current_ow_patch-$latest_tag-origin_master-$attempt.diff

log "Starting with version :$latest_tag"

log "Checking if last failure was for test case. Need this to pick correct"
go_mod
checkpoint_run "./validate.sh --race 5 --nofmt"
go_discard

# Loop through each tag and merge it
tags=$(git tag --merged prebid-upstream/master --sort=v:refname)

log "Starting upgrade loop..."
for tag in $tags
do
if [[ "$tag" == "$latest_tag" ]]; then
found_latest_tag=true
if [[ -f $CHECKLOG ]]; then
log "At tag: $tag but $CHECKLOG exists. Continue last failed checkpoint."
else
continue
fi
fi

if [[ "$found_latest_tag" = true && "$tag" =~ ^v[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
_upgrade_version=$tag

log "Starting upgrade to version $_upgrade_version"

tag_base_branch_name=prebid_$_upgrade_version-$attempt-tag
upgrade_branch_name=prebid_$_upgrade_version-$attempt
log "Reference tag branch: $tag_base_branch_name"
log "Upgrade branch: $upgrade_branch_name"

checkpoint_run checkout_branch

log "Merging master in $tag_base_branch_name"
checkpoint_run git merge master --no-edit
# Use `git commit --amend --no-edit` if you had to fix test cases, etc for wrong merge conflict resolve, etc.
log "Validating the master merge into current tag. Fix and commit changes if required. Use 'git commit --amend --no-edit' for consistency"
go_mod
checkpoint_run "./validate.sh --race 5 --nofmt"
go_discard

checkpoint_run git checkout master
checkpoint_run git merge $upgrade_branch_name --no-edit

log "Generating patch file at /tmp/pbs-patch/ for $_upgrade_version"
git diff tags/$_upgrade_version..master > /tmp/pbs-patch/new_ow_patch_$upgrade_version-master-1.diff
fi
done
main "$@"

0 comments on commit 4f724cb

Please sign in to comment.