Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OTT-1810: Update prebid-server version upgrade script #807

Merged
merged 2 commits into from
Jul 9, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
208 changes: 103 additions & 105 deletions scripts/upgrade-pbs.sh
Original file line number Diff line number Diff line change
@@ -1,50 +1,52 @@
#!/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

# --- start ---
CHECKLOG=/tmp/pbs-patch/checkpoints.log

trap 'clear_log' EXIT
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
}

log () {
printf "\n$(date): $1\n"
# 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 +56,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 +74,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 +97,105 @@ 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 ---

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

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
# 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
}

# Loop through each tag and merge it
tags=$(git tag --merged prebid-upstream/master --sort=v:refname)
# Initialize upgrade
initialize_upgrade() {
checkpoint_run clone_repo
cd /tmp/prebid-server
log "At $(pwd)"

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
# 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

if [[ "$found_latest_tag" = true && "$tag" =~ ^v[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
_upgrade_version=$tag
log "Final Upgrade Version: $VERSION"

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

log "Starting upgrade to version $_upgrade_version"
# Start validation
start_validation() {
go_mod
checkpoint_run "./validate.sh --race 5 --nofmt"
go_discard
}

tag_base_branch_name=prebid_$_upgrade_version-$attempt-tag
upgrade_branch_name=prebid_$_upgrade_version-$attempt
# 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
}

log "Merging master in $tag_base_branch_name"
# Merge branches
merge_branches() {
log "Merging master into $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"

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
}

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
# 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
}

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

# --- start ---
CHECKLOG=/tmp/pbs-patch/checkpoints.log
trap 'clear_log' EXIT

log "Attempt: $attempt"
main "$@"
Loading