Skip to content

Commit

Permalink
Added auto-deploy and test in git
Browse files Browse the repository at this point in the history
Fixed issue with test in a non-namespaced environment
Added a namespace, because the framework will always have one
  • Loading branch information
rob-baillie-ortoo committed Dec 1, 2021
1 parent 8109082 commit 8654fd2
Show file tree
Hide file tree
Showing 7 changed files with 170 additions and 6 deletions.
76 changes: 76 additions & 0 deletions .github/workflows/create-org-and-deploy.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
name: Deploy and Run all Unit Tests
on:
push:
branches:
- '*'
- main
pull_request:
types: [ opened ]
branches:
- '*'
- main
jobs:
create-org-and-deploy:
runs-on: ubuntu-latest
env:
DEVHUB_ORG_ALIAS: OrtooISV
ORG_ALIAS_PREFIX: 'FTST'
steps:
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
- uses: actions/checkout@v2

- name: Salesforce SFDX CLI Action
uses: sfdx-actions/setup-sfdx@v1

# Update ./config/project-scratch-def.json

- name: Update project-scratch-def.json orgName
uses: jossef/action-set-json-field@6e6d7e639f24b3955ef682815317b5613ac6ca12
with:
file: ./config/project-scratch-def.json
field: orgName
value: "${{env.ORG_ALIAS_PREFIX}}${{github.run_number}}"

- name: Update project-scratch-def.json description
uses: jossef/action-set-json-field@6e6d7e639f24b3955ef682815317b5613ac6ca12
with:
file: ./config/project-scratch-def.json
field: description
value: "${{ github.repository }} , Org Alias ${{env.ORG_ALIAS_PREFIX}}${{github.run_number}}. Created by ${{ github.actor }}"

- name: Show project-scratch-def.json
run: 'cat ./config/project-scratch-def.json'

# sfdx-project.json file should be ok as is

- name: Show sfdx-project.json
run: 'cat ./sfdx-project.json'

# Dev Hub needed to create scratch org

- name: Create DevHub Auth File
run: echo "${{ secrets.AUTH_SECRET_ISV }}" > devhub_auth_file.txt

- name: Authorise Dev Hub Org
run: sfdx auth:sfdxurl:store -f devhub_auth_file.txt -d -a ${{ env.DEVHUB_ORG_ALIAS }}

# Create Scratch Org

- name: Run script CreateScratchOrgWithNamespaceOnlyHeadless.sh
run: ./scripts/std_batch/CreateScratchOrgWithNamespaceOnlyHeadless.sh "${{env.ORG_ALIAS_PREFIX}}${{github.run_number}}"
shell: sh

# Push code to org

- name: Deploy force-app to Org
run: sfdx force:source:deploy -p force-app --targetusername "${{env.ORG_ALIAS_PREFIX}}${{github.run_number}}"

# Run All Unit Tests

- name: Run All Unit Tests
run: sfdx force:apex:test:run -r human -u "${{env.ORG_ALIAS_PREFIX}}${{github.run_number}}" --wait 20 --codecoverage

# Delete Scratch Org

- name: Delete Scratch Org
run: sfdx force:org:delete --noprompt --targetusername "${{env.ORG_ALIAS_PREFIX}}${{github.run_number}}"
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,5 @@ ehthumbs.db
$RECYCLE.BIN/

# Local environment variables
.env
.env
.pmdCache
Binary file removed .pmdCache
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,18 @@ public inherited sharing class PackageUtils
String potentialNamespace = String.valueOf( classType ).substringBefore( '.' );
String remainingName = String.valueOf( classType ).substringAfter( '.' );

if ( potentialNamespace == 'System' )
{
return 'System';
}

if ( ! String.isEmpty( remainingName ) )
{
// We test this in case we were passed an inner class and the potentialNamespace
// is actually the name of the containing class, and there is no namespace
if ( classType == Type.forName( potentialNamespace, remainingName ) )
// is actually the name of the containing class, and there is no namespace.
// If that's the case, the potentialNamespace actually contains the name of a class
Type potentialClassType = Type.forName( potentialNamespace );
if ( potentialClassType != null )
{
namespacePrefix = potentialNamespace;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,27 @@
private without sharing class PackageUtilsTest
{
@isTest
private static void getNamespace_whenCalledWithASystemClass_willReturnSystem() // NOPMD: Test method name format
private static void getNamespace_whenCalledWithASystemClassFullyDescribed_willReturnSystem() // NOPMD: Test method name format
{
String packageUtilsNamespace = PackageUtils.getNamespace( System.Test.class );

System.assertEquals( 'System', packageUtilsNamespace, 'getNamespace, when called with a class, will the namespace it is defined in' );
System.assertEquals( 'System', packageUtilsNamespace, 'getNamespace, when called with a System class, will return System' );
}

@isTest
private static void getNamespace_whenCalledWithASystemClass_willReturnSystem() // NOPMD: Test method name format
{
String packageUtilsNamespace = PackageUtils.getNamespace( Test.class );

System.assertEquals( 'System', packageUtilsNamespace, 'getNamespace, when called with a System class, will return System' );
}

@isTest
private static void getNamespace_whenCalledWithAnCustomClass_willReturnAString() // NOPMD: Test method name format
{
String packageUtilsNamespace = PackageUtils.getNamespace( PackageUtils.class );
// we can't test more than this because we don't know what namespace we will be installed in
System.assertNotEquals( null, packageUtilsNamespace, 'getNamespace, when called with a custom class, will return a String' );
System.assertNotEquals( null, packageUtilsNamespace, 'getNamespace, when called with a custom class, will return the namespace it is in - cal only test it is a string though' );
}

@isTest
Expand Down
71 changes: 71 additions & 0 deletions scripts/std_batch/CreateScratchOrgWithNamespaceOnlyHeadless.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#!/bin/bash

# chmod u+x CreateScratchOrgWithNamespaceOnlyHeadless.sh
# run in Terminal window of SFDX project with ./scripts/std_batch/CreateScratchOrgWithNamespaceOnlyHeadless.sh OrtooQassignScratch

# CREATE SCRATCH ORG WITH NAMESPACE AND GENERATE PASSWORD
#
echo "Scratch Org Creation With Namespace ONLY (no data loaded) started."

ORG_ALIAS="OrtooQassignScratch"

if [ $# = 1 ]; then
ORG_ALIAS=$1
else
echo
echo "Script requires one parameter"
echo "1. Org Alias for created org. e.g. OrtooQassignScratch"
echo
echo "**** Remember to update config/project-scratch-def.json file as appropriate."
echo " Include your public facing IP Address to prevent two-factor authentication."
echo " Update Business Hours if necessary."
echo
echo "**** Remember to update sfdx-project.json file with Namespace to be used."
echo
exit 1
fi

echo
echo "Using $ORG_ALIAS for Org alias"
echo

echo "1. create"
sfdx force:org:create -f config/project-scratch-def.json --setalias "$ORG_ALIAS" --durationdays 30 --setdefaultusername --loglevel fatal
if [ $? = 0 ]; then
echo "Created Scratch Org with alias $ORG_ALIAS"
else
echo
echo "Non zero exit code: $?"
echo "Exiting script"
exit 1
fi

echo "2. password generation"
sfdx force:user:password:generate -u "$ORG_ALIAS"
if [ $? = 0 ]; then
echo "Password generated"
else
echo
echo "Non zero exit code: $?"
echo "Exiting script"
echo
exit 1
fi

echo "3. display Org details"
sfdx force:org:display -u "$ORG_ALIAS"
if [ $? = 0 ]; then
echo "Org details displayed"
else
echo
echo "Non zero exit code: $?"
echo "Exiting script"
echo
exit 1
fi

echo
echo "Scratch Org Creation With Namespace ONLY completed."
echo

exit 0
1 change: 1 addition & 0 deletions sfdx-project.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"default": true
}
],
"namespace": "ortoo_qra",
"sfdcLoginUrl": "https://login.salesforce.com",
"sourceApiVersion": "52.0"
}

0 comments on commit 8654fd2

Please sign in to comment.