Skip to content

Instantly share code, notes, and snippets.

View douglascayers's full-sized avatar

Doug Ayers douglascayers

View GitHub Profile
@douglascayers
douglascayers / JWT.cls
Last active October 21, 2022 19:19
Sign a JWT token with only a private key
/**
* Inspired by the JWT repo by Salesforce Identity
* https://github.com/salesforceidentity/jwt/
*
* Inspired by the JWT repo by Auth0
* https://github.com/auth0/java-jwt
*
* Learn more about JWT at https://jwt.io
*/
public inherited sharing class JWT {
@douglascayers
douglascayers / create-package-version.sh
Last active February 22, 2021 02:44
Bash script to create a new managed package version from source control and no maintenance of sfdx-project.json
#!/usr/bin/env bash
# =========================================================================== #
# USAGE
# -----
# create-package-version.sh [branchName]
#
# The $1 argument (optional) indicates the git branch to use.
# If not set then script defaults to 'develop' branch.
# =========================================================================== #
@douglascayers
douglascayers / delete-local-branches.sh
Last active March 10, 2020 21:25
Delete local and remote branches
# You'll need to keep one branch.
# Specify that branch's name here.
BRANCH_TO_KEEP=master
# Switch to the branch you're keeping
git checkout ${BRANCH_TO_KEEP}
# Delete all other local branches
git branch -D $(git branch | grep -v -e ${BRANCH_TO_KEEP})
@douglascayers
douglascayers / delete-org.sh
Created August 30, 2019 03:41
Deletes a scratch org when `force:org:delete` fails for any reason
org_list_json=$(sfdx force:org:list --json)
scratch_org_username=$1
devhub_username=$2
# if no scratch org username given, use default
if [ -z "$1" ]; then
scratch_org_username=$(echo $org_list_json | jq -r '.result.scratchOrgs[] | select(.isDefaultUsername) | .signupUsername')
else
# else check if argument is an org alias and get signup username from that
scratch_org_username=$(echo $org_list_json | jq -r --arg ALIAS "$1" '.result.scratchOrgs[] | select(.alias==$ALIAS) | .signupUsername')
@douglascayers
douglascayers / rename.sh
Created August 18, 2019 02:15
Rename files in all subdirectories.
# Finds all files in the current directory and subdirectories
# whose filenames match the expression "*.js" (ends with .js)
# then renames the files, changing their extension to ".ts".
# https://stackoverflow.com/questions/7450818/rename-all-files-in-directory-from-filename-h-to-filename-half
for file in $(find . -type f -name "*.js")
do
mv "$file" "${file/.js/.ts}"
done
@douglascayers
douglascayers / load_properties.sh
Created August 17, 2019 03:23
Bash: Read a properties file and set variables
# Read a property file line by line and
# set key=value pairs as bash variables
# https://stackoverflow.com/a/28831442/470818
FILEPATH=$1
while IFS='=' read -r k v; do
eval ${k}=${v}
done < $FILEPATH
@douglascayers
douglascayers / github-copy-labels.sh
Last active December 22, 2023 08:16
Export and import GitHub labels between projects by running bash script with jq and curl. Uses GitHub REST API. Requires personal access token.
# This script uses the GitHub Labels REST API
# https://developer.github.com/v3/issues/labels/
# Provide a personal access token that can
# access the source and target repositories.
# This is how you authorize with the GitHub API.
# https://help.github.com/en/articles/creating-a-personal-access-token-for-the-command-line
GH_TOKEN="YOUR_TOKEN"
# If you use GitHub Enterprise, change this to "https://<your_domain>/api/v3"
@douglascayers
douglascayers / github-export-labels.js
Last active September 14, 2023 15:30
Export and import GitHub labels between projects by running JavaScript in the browser console to automate clicks.
/**
* Inspired by @MoOx original script: https://gist.github.com/MoOx/93c2853fee760f42d97f
* Adds file download per @micalevisk https://gist.github.com/MoOx/93c2853fee760f42d97f#gistcomment-2660220
*
* Changes include:
* - Get the description from the `title` attribute instead of `aria-label` (doesn't exist anymore)
* - Use style.backgroundColor and parse the rgb(...) to hex (rather than regex parsing of 'style' string)
* - Downloads labels to a JSON file named after the webpage to know which GitHub repo they came from.
*
* Last tested 2019-July-27:
@douglascayers
douglascayers / DemoBatchJob.cls
Last active February 17, 2023 00:47
Demo of Apex Batch Job's finish method not running if exception occurs in its execute method (API v46.0)
public class DemoBatchJob implements Database.Batchable<SObject> {
public Database.QueryLocator start( Database.BatchableContext context ) {
System.debug( 'DemoBatchJob.start: ' + context );
return Database.getQueryLocator([ SELECT Id FROM Organization ]);
}
public void execute( Database.BatchableContext context, List<SObject> records ) {
System.debug( 'DemoBatchJob.execute: ' + context );
Integer i = 1 / 0; // cause exception
@douglascayers
douglascayers / event-start-time-utc.txt
Created June 23, 2019 03:16
Format Event Start Time in UTC in ISO-8601 Format (no dashes)
TEXT( YEAR( ActivityDate ) ) &
LPAD( TEXT( MONTH( ActivityDate ) ), 2, '0' ) &
LPAD( TEXT( DAY( ActivityDate ) ), 2, '0' ) &
'T' &
LPAD( TEXT( HOUR( TIMEVALUE( ActivityDateTime ) ) ), 2, '0' ) &
LPAD( TEXT( MINUTE( TIMEVALUE( ActivityDateTime ) ) ), 2, '0' ) &
LPAD( TEXT( SECOND( TIMEVALUE( ActivityDateTime ) ) ), 2, '0' ) &
'Z';