Skip to content

Instantly share code, notes, and snippets.

@martyychang
Last active December 28, 2018 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save martyychang/5c64deb18e2ddd28a62d23edea8ad888 to your computer and use it in GitHub Desktop.
Save martyychang/5c64deb18e2ddd28a62d23edea8ad888 to your computer and use it in GitHub Desktop.
Trailblazer .bin
# Check to require a username to be [given as argument][1]
#
# [1]: https://stackoverflow.com/questions/6482377/check-existence-of-input-argument-in-a-bash-shell-script
if [ -z "$1" ]; then
echo ERROR: Username argument required!
# [terminate and indicate error][3]
#
# [3]: https://stackoverflow.com/questions/4381618/exit-a-script-on-error
exit 1
fi
MDAPI_OUTPUT_DIR=mdapi_output
# Delete the converted metadata output directory [if it exists][2]
#
# [2]: https://stackoverflow.com/questions/59838/check-if-a-directory-exists-in-a-shell-script
if [ -d "$MDAPI_OUTPUT_DIR" ]; then
echo found: $MDAPI_OUTPUT_DIR
rm -Rv $MDAPI_OUTPUT_DIR
echo deleted
else
echo NOT found: $MDAPI_OUTPUT_DIR
fi
# Convert the source and deploy
sfdx force:source:convert -d $MDAPI_OUTPUT_DIR
sfdx force:mdapi:deploy -d $MDAPI_OUTPUT_DIR -w 5 -u "$1" \
-c -l "RunLocalTests"
# First attempt to get one string per duplicate file failed.
#
# for filepath in `find . -name *.dup`; do
# echo found: $filepath
# done
#
# The commands above gave the following output.
#
# found: ./force-app/main/default/profiles/Admin.profile-meta.xml.dup
# found: ./force-app/main/default/profiles/Marketing
# found: and
# found: Sales
# found: User.profile-meta.xml.dup
# Second attempt following suggestion [from Stack Exchange][1]
# was more successful. However the output literally has spaces, meaning
# the spaces still need to be handled when moving the .dup file.
#
# [1]: https://stackoverflow.com/questions/7039130/iterate-over-a-list-of-files-with-spaces
#
# find . -name *.dup | while read filepath
# do
# echo found: $filepath
# done
find . -name *.dup | while read filepath
do
# Point out the duplicate file that was found
echo found: $filepath
# Use a trick reported [on Stack Exchange][2] for getting rid of a known
# file extension by using the `%` string operator
#
# [2]: https://stackoverflow.com/questions/965053/extract-filename-and-extension-in-bash
mv "$filepath" "${filepath%.dup}"
# Assume the `mv` command was successful and print a confirmation message.
echo replaced!
done
# Check to require a username to be [given as argument][1]
#
# [1]: https://stackoverflow.com/questions/6482377/check-existence-of-input-argument-in-a-bash-shell-script
if [ -z "$1" ]; then
echo ERROR: Username argument required!
# [terminate and indicate error][3]
#
# [3]: https://stackoverflow.com/questions/4381618/exit-a-script-on-error
exit 1
fi
MDAPI_OUTPUT_DIR=mdapi_output
# Delete the converted metadata output directory [if it exists][2]
#
# [2]: https://stackoverflow.com/questions/59838/check-if-a-directory-exists-in-a-shell-script
if [ -d "$MDAPI_OUTPUT_DIR" ]; then
echo found: $MDAPI_OUTPUT_DIR
rm -Rv $MDAPI_OUTPUT_DIR
echo deleted
else
echo NOT found: $MDAPI_OUTPUT_DIR
fi
# Convert the source and deploy
sfdx force:source:convert -d $MDAPI_OUTPUT_DIR
sfdx force:mdapi:deploy -d $MDAPI_OUTPUT_DIR -w 5 -u "$1" \
-l "RunLocalTests"
# Create the org
echo "Creating the scratch org now..."
sfdx force:org:create -v mime -f config/project-scratch-def.json -s
# Open the org and prompt the user to make manual edits
echo # line break
echo "Please enable Opportunity Splits before continuing. (ref:TRAIL-1963)"
echo "Opening the scratch org now for you to perform the manual actions..."
sfdx force:org:open
read -p "Press [Enter] when done."
# Push source to the org
echo # line break
echo "Pushing source now..."
time sfdx force:source:push
# Assign the System Administrator permission set to yourself
echo # line break
echo "Assigning permission sets now..."
sfdx force:user:permset:assign -n SystemAdministrator
# Confirm readiness
echo # line break
echo "Remember to take a look for any push errors"
echo "to make sure nothing unexpected shows up."
echo "Otherwise..."
echo # line break
echo "You're all set and ready to go!"
if [ -z "$1" ]; then
echo ERROR: target username required
exit 1
fi
# Parse command arguments
TARGET_USERNAME=$1
PACKAGE_DIR=force-app
ASSIGNMENT_RULES_DIR=$PACKAGE_DIR/main/default/assignmentRules
QUEUES_DIR=$PACKAGE_DIR/main/default/queues
# Remove user references from queues
find . -name *.queue-meta.xml | while read filepath
do
# Point out the duplicate file that was found
echo found: $filepath
# Use a trick reported [on Stack Exchange][2] for getting rid of a known
# file extension by using the `%` string operator
#
# [2]: https://stackoverflow.com/questions/965053/extract-filename-and-extension-in-bash
mv "$filepath" "$filepath.tmp"
cat "$filepath.tmp" | grep -v "<user>" > "$filepath"
rm "$filepath.tmp"
# Assume the `mv` command was successful and print a confirmation message.
echo cleaned!
done
# Push source
sfdx force:source:push -u $TARGET_USERNAME
# Use Git to revert any removals above
git checkout -- "$QUEUES_DIR"
# retrieve-all.sh retrieves metadata based on the expected deployment manifest
# and then converts the metadata from Metadata API format to Salesforce DX
# format.
#
# After running retrieving the developer should also run dedupe.sh to resolve
# any changes that were not automatically converted and merged into the
# Salesforce DX metadata.
# deploy.sh performs a deployment to a target Salesforce org,
# requiring all local tests to pass as part of the deployment.
MDAPI_OUTPUT_DIR=mdapi_output
# Delete the converted metadata output directory [if it exists][2]
#
# [2]: https://stackoverflow.com/questions/59838/check-if-a-directory-exists-in-a-shell-script
if [ -d "$MDAPI_OUTPUT_DIR" ]; then
echo found: $MDAPI_OUTPUT_DIR
rm -R "$MDAPI_OUTPUT_DIR"
echo deleted: $MDAPI_OUTPUT_DIR
else
echo NOT found: $MDAPI_OUTPUT_DIR
fi
# Convert the source and deploy
sfdx force:source:convert -d "$MDAPI_OUTPUT_DIR"
# Define the Metadata API input directory
MDAPI_INPUT_DIR=mdapi_in
# Define the Salesforce DX source directory
SFDX_SOURCE_DIR=force-app
# Delete the existing directory... if it exists
if [ -d "$MDAPI_INPUT_DIR" ]; then
echo found: $MDAPI_INPUT_DIR
rm -R "$MDAPI_INPUT_DIR"
echo deleted: $MDAPI_INPUT_DIR
mkdir -p "$MDAPI_INPUT_DIR"
fi
# Require a username argument followed by a change set argument
if [ -z "$1" ]; then
echo ERROR: Username argument required!
# [terminate and indicate error][3]
#
# [3]: https://stackoverflow.com/questions/4381618/exit-a-script-on-error
exit 1
fi
# Retrieve the package metadata
sfdx force:mdapi:retrieve -r "$MDAPI_INPUT_DIR" \
-u $1 -k "$MDAPI_OUTPUT_DIR/package.xml"
# Extract the metadata contents
unzip "$MDAPI_INPUT_DIR/unpackaged.zip" -d "$MDAPI_INPUT_DIR"
# Convert the Metadata API source into Salesforce DX source
sfdx force:mdapi:convert \
-r "$MDAPI_INPUT_DIR/unpackaged" \
-d "$SFDX_SOURCE_DIR"
# Run the dedupe.sh script to "merge" the changes into the existing metadata
sh bin/dedupe.sh
# Define the Metadata API input directory
MDAPI_INPUT_DIR=mdapi_in
# Define the Salesforce DX source directory
SFDX_SOURCE_DIR=force-app
# Delete the existing directory... if it exists
if [ -d "$MDAPI_INPUT_DIR" ]; then
echo found: $MDAPI_INPUT_DIR
rm -Rv "$MDAPI_INPUT_DIR"
echo deleted
mkdir -p "$MDAPI_INPUT_DIR"
fi
# Require a username argument followed by a change set argument
if [ -z "$1" ]; then
echo ERROR: Username argument required!
# [terminate and indicate error][3]
#
# [3]: https://stackoverflow.com/questions/4381618/exit-a-script-on-error
exit 1
fi
if [ -z "$2" ]; then
echo ERROR: Change set name argument required!
# [terminate and indicate error][3]
#
# [3]: https://stackoverflow.com/questions/4381618/exit-a-script-on-error
exit 1
fi
# Retrieve the change set metadata
sfdx force:mdapi:retrieve -r "$MDAPI_INPUT_DIR" \
-u $1 -p "$2"
# Extract the metadata contents
unzip "$MDAPI_INPUT_DIR/unpackaged.zip" -d "$MDAPI_INPUT_DIR"
# Convert the Metadata API source into Salesforce DX source
sfdx force:mdapi:convert \
-r "$MDAPI_INPUT_DIR/$2" \
-d "$SFDX_SOURCE_DIR"
# Parameters for the script
USERNAME=mime-staging
CHANGE_SET="Trailblazer 2019.1.16"
MDAPI_OUTPUT_DIR=mdapi_output
# Git-friendly timestamp representation that can be used for branch name
TIMESTAMP=`date -u +"%Y.%m.%d-%H%M%S"`
# The name of the branch equivalent to develop in Gitflow
DEVELOP_BRANCH=staging
SNAPSHOT_PREFIX=snapshot
SNAPSHOT_BRANCH=$SNAPSHOT_PREFIX-$TIMESTAMP
# Check out the develop branch
echo # line break
echo "Checking out the $DEVELOP_BRANCH branch..."
git checkout $DEVELOP_BRANCH --force
# Pull to make sure it is current
echo # line break
echo "Pulling any new commits..."
git pull
# Create a new branch for the snapshot
echo # line break
echo "Snapshot branch: $SNAPSHOT_BRANCH"
echo "Creating snapshot branch..."
git checkout -b $SNAPSHOT_BRANCH
# Retrieve metadata for the snapshot
echo # line break
echo "Username: $USERNAME"
echo "Change set: $CHANGE_SET"
echo "Retrieving change set metadata..."
sh bin/retrieve.sh $USERNAME "$CHANGE_SET"
# Commit the changes
echo # line break
echo "Staging and committing changes now..."
git add .
git commit -m "Take snapshot of $CHANGE_SET from $USERNAME"
# Retrieve complete metadata based on current package to complete snapshot
echo #line break
echo "Retrieving all package metadata..."
sh .bin/retrieve-all.sh $USERNAME
# Commit the changes
echo # line break
echo "Staging and committing changes now..."
git add .
git commit -m "Retake package snapshot from $USERNAME"
# Push the changes to origin
echo # line break
echo "Pushing the snapshot to origin..."
git push origin $SNAPSHOT_BRANCH -u
# Open the branch on GitHub
echo # line break
echo "Opening GitHub on the web to facilitate a pull request..."
open https://github.com/Mimecast/trailblazer/compare/$DEVELOP_BRANCH...$SNAPSHOT_BRANCH
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment