Skip to content

Commit

Permalink
Merge pull request #20 from MervmessInc/update-workflows
Browse files Browse the repository at this point in the history
Update workflows
  • Loading branch information
nigelhu authored Mar 23, 2023
2 parents ba5ae92 + b1a8714 commit 5b0b4e0
Show file tree
Hide file tree
Showing 13 changed files with 957 additions and 727 deletions.
12 changes: 7 additions & 5 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ jobs:
# This workflow contains a single job called "build"
build:
# The type of runner that the job will run on
runs-on: ubuntu-18.04
runs-on: ubuntu-latest

# Steps represent a sequence of tasks that will be executed as part of the job
steps:
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
- uses: actions/checkout@v2
- uses: actions/checkout@v3

# RUN PMD ~ pmd -language apex -d . -R pmd-apex-ruleset.xml -f text --failOnViolation false
- name: Run PMD
Expand Down Expand Up @@ -55,11 +55,13 @@ jobs:
# Build Scratch Org
- name: Run org_builder
uses: actions/setup-python@v2
uses: actions/setup-python@v3
with:
python-version: "3.8"
python-version: "3.10"
cache: 'pip'
- run: pip install -r requirements.txt
- run: python org_builder.py -a sfdx-example -d 1 -v hub-org --debug

# Run tests
- name: Run Tests
run: sfdx force:apex:test:run -r human -w 5 -l RunLocalTests --codecoverage
run: sfdx apex:run:test -r human -w 5 -l RunLocalTests -c -v
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# For useful gitignore templates see: https://github.com/github/gitignore

# Salesforce cache
.sf/
.sfdx/
.localdevserver/

Expand Down Expand Up @@ -36,7 +37,13 @@ Thumbs.db
ehthumbs.db
[Dd]esktop.ini
$RECYCLE.BIN/

# Project specific files
org_list.json
.history/
__pycache__/
force-app/main/default/profiles/
JWT/server.crt
JWT/server.csr
JWT/server.key
JWT/server.pass.key
11 changes: 11 additions & 0 deletions JWT/create_key.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
https://developer.salesforce.com/docs/atlas.en-us.sfdx_dev.meta/sfdx_dev/sfdx_dev_auth_key_and_cert.htm

$ which openssl

$ openssl genpkey -des3 -algorithm RSA -pass pass:SomePassword -out server.pass.key -pkeyopt rsa_keygen_bits:2048

$ openssl rsa -passin pass:SomePassword -in server.pass.key -out server.key

$ openssl req -new -key server.key -out server.csr

$ openssl x509 -req -sha256 -days 365 -in server.csr -signkey server.key -out server.crt
34 changes: 0 additions & 34 deletions config.py

This file was deleted.

267 changes: 2 additions & 265 deletions org_builder.py
Original file line number Diff line number Diff line change
@@ -1,266 +1,3 @@
# org_builder.py
__version__ = '0.0.2'
import sf_org_manager.org_builder as org_builder

import argparse
import logging
import os
import sys

import config
import sfdx_cli_utils as sfdx

# Set the working directory to the location of the file.
#
dir_path = os.path.dirname(os.path.realpath(__file__))
os.chdir(dir_path)

# Set the Log level
#
logging.basicConfig(
level=logging.ERROR,
format='%(asctime)s - %(message)s',
datefmt='%d-%b-%y %H:%M:%S')
logger = logging.getLogger()

# Config
#
DURATION = config.DURATION
DEVHUB = config.DEVHUB
PACKAGE_IDS = config.PACKAGE_IDS
PACKAGE_P_SETS = config.PACKAGE_P_SETS
PRE_DEPLOY = config.PRE_DEPLOY
SRC_FOLDERS = config.SRC_FOLDERS
BUILD_DATA_CMD = config.BUILD_DATA_CMD
P_SETS = config.P_SETS
#

parser = argparse.ArgumentParser(
prog='org_builder',
description='''
Python wrapper for a number of Salesforce CLI (sfdx) commands, to build and setup Scratch Orgs.
''')


def setup_args():
logging.debug("setup_args()")

parser.add_argument(
'-a', '--alias',
help="Scratch Org user alias"
)
parser.add_argument(
'-d', '--duration',
help=f"Number of days org will last [1..30]. Default: {DURATION}",
default=DURATION
)
parser.add_argument(
'-v', '--devhub',
help=f"Target dev hub username or alias. Default: {DEVHUB}",
default=DEVHUB
)
parser.add_argument(
'--debug',
help="Turn on debug messages",
action='store_true'
)


def check_install(org_alias, status_id):

py_obj = sfdx.check_install(org_alias, status_id)

if py_obj['status'] == 1:
message = py_obj['message']
logging.error(f"MESSAGE: {message}")
logging.warning(f"{py_obj}")
sys.exit(1)

if py_obj['status'] == 0:
status = py_obj['result']['Status']

logging.error(f"Checking package install status ~ {status}")

return status


def check_org(org_alias):

py_obj = sfdx.org_list()

scratch_orgs = py_obj['result']['scratchOrgs']
for org in scratch_orgs:
try:
if org_alias == org['alias'] and not org['isExpired']:
logging.debug(
f"Alias : {org['alias']}, Username : {org['username']}, End Date : {org['expirationDate']}")
return org['username'], True

except KeyError:
pass

return '', False


def create_sratch_org(org_alias, duration, devhub):

py_obj = sfdx.create_sratch_org(org_alias, duration, devhub)

if py_obj['status'] == 1:
message = py_obj['message']
logging.error(f"MESSAGE: {message}")
logging.warning(f"{py_obj}")
sys.exit(1)

if py_obj['status'] == 0:
username = py_obj['result']['username']

return username


def execute_script(org_alias, apex_file):

py_obj = sfdx.execute_script(org_alias, apex_file)

if py_obj['status'] == 1:
message = py_obj['message']
logging.error(f"MESSAGE: {message}")
logging.error(
f"CompileProblem: {py_obj['result']['compileProblem']}\nExceptionMessage: {py_obj['result']['exceptionMessage']}")
logging.warning(f"{py_obj}")
sys.exit(1)

if py_obj['status'] == 0:
logging.warning(f"{py_obj}")

return True


def install_package(org_alias, package_id):

py_obj = sfdx.install_package(org_alias, package_id)

if py_obj['status'] == 1:
message = py_obj['message']
logging.error(f"MESSAGE: {message}")
logging.warning(f"{py_obj}")
sys.exit(1)

if py_obj['status'] == 0:
install_status = py_obj['result']['Status']
while install_status == "IN_PROGRESS":
install_status = check_install(org_alias, py_obj['result']['Id'])

return True


def install_permission_set(org_alias, pset):

py_obj = sfdx.install_permission_set(org_alias, pset)

if py_obj['status'] == 1:
message = py_obj['result']['failures'][0]['message']
logging.error(f"MESSAGE: {message}")
logging.warning(f"{py_obj}")

if py_obj['status'] == 0:
logging.info(f"{py_obj}")

return True


def install_source(org_alias, src_folder):

py_obj = sfdx.install_source(org_alias, src_folder)

if py_obj['status'] == 1:
logging.warning(f"{py_obj}")
message = py_obj['message']
logging.error(f"MESSAGE: {message}")
sys.exit(1)

if py_obj['status'] == 0:
for item in py_obj['result']['deployedSource']:
logging.info(
f"Type: {item['type']}, State: {item['state']}, Name: {item['fullName']}")

return True


def user_details(org_alias):

py_obj = sfdx.user_details(org_alias)

if py_obj['status'] == 1:
message = py_obj['message']
logging.error(f"MESSAGE: {message}")
logging.warning(f"{py_obj}")
sys.exit(1)

if py_obj['status'] == 0:
logging.error(f"OrgId \t: {py_obj['result']['orgId']}")
logging.error(f"Username \t: {py_obj['result']['username']}")
logging.error(f"Url \t: {py_obj['result']['instanceUrl']}")
logging.error(f"Alias \t: {py_obj['result']['alias']}")


def main():
logging.debug("main()")

setup_args()
args = parser.parse_args()

if args.alias is None:
parser.print_help()
sys.exit(0)

if args.debug:
logging.error("~~~ Setting up DEBUG ~~~")
logger.setLevel(logging.INFO)

logging.error("~~~ Setting up Scratch Org ~~~")
logging.error(f"{args}")

logging.error("~~~ Check if Org Already Exists ~~~")
username, org_exists = check_org(args.alias)

if not org_exists:
logging.error("~~~ Create New Scratch Org ~~~")
username = create_sratch_org(args.alias, args.duration, args.devhub)

if PACKAGE_IDS:
for pckg in PACKAGE_IDS:
logging.error(f"~~~ Installing Packages {pckg} ~~~")
install_package(args.alias, pckg)

if PRE_DEPLOY:
for fldr in PRE_DEPLOY:
logging.error(f"~~~ Installing Source ({fldr}) ~~~")
install_source(args.alias, f"{dir_path}/{fldr}")

if PACKAGE_P_SETS:
for pset in PACKAGE_P_SETS:
logging.error(f"~~~ Installing Permission Set ({pset}) ~~~")
install_permission_set(args.alias, pset)

if SRC_FOLDERS:
for fldr in SRC_FOLDERS:
logging.error(f"~~~ Installing Source ({fldr}) ~~~")
install_source(args.alias, f"{dir_path}/{fldr}")

if P_SETS:
for pset in P_SETS:
logging.error(f"~~~ Installing Permission Set ({pset}) ~~~")
install_permission_set(args.alias, pset)

if BUILD_DATA_CMD:
logging.error("~~~ Running Build data ~~~")
execute_script(args.alias, BUILD_DATA_CMD)

logging.error("~~~ Details ~~~")
user_details(args.alias)

logging.error("~~~ Scratch Org Complete ~~~")


if __name__ == '__main__':
main()
org_builder.main()
Loading

0 comments on commit 5b0b4e0

Please sign in to comment.