Example Scripts and Commands for Functions

Updated 15 hours ago by Copado Solutions

In this article, you will find some out-of-the-box scripts you can execute as part of a function to complete different tasks.

Before you get to executing advanced scripts, it is a good practice to do a test and ensure you have all the necessary permissions to run a function. You can use the below basic script:

echo hello world $name

If there are any problems, you can troubleshoot them before moving on to running more advanced functions. 

Clone a public Git repository and review its size:

copado -p 'Cloning the repository' 
mkdir repo 
cd repo 
git clone "$giturl" . 
copado -p 'Running git-sizer'
git-sizer --verbose > git-sizer.txt 
git-sizer --json > git-sizer.json 
copado -p 'Attaching results' 
copado -u git-sizer.txt 
copado -u git-sizer.json

To clone a public repository, you need to add the URL in the Parameters section.

Clone a private repository and review its size:

# 1 - Clone Git Branch
echo "$provider"
copado --progress "Cloning the repository"
copado-git-get -d . "$branchName"

# 2 - Run Git Sizer Library
copado --progress "Running git-sizer"
git-sizer --verbose > git-sizer.txt
git-sizer --json > git-sizer.json

# 3 - Attach Output Files in the Copado Result record
copado --progress "Attaching results"
copado --u git-sizer.txt
copado --u git-sizer.json

In the case of a private repository, you need to provide the credentials. To avoid exposing these credentials, use the following dynamic parameter:

{$RecordId.Repository.Credential}

To run this script, you also need to provide the branch name and the Git provider as parameters, so configure them as follows:

Branch:

  • Parameter Name: branchName
  • Default Value: master (main if yuo are working with GitHub).

Provider:

  • Parameter Name: provider
  • Default Value: {$RecordId.Gt_Provider__c}

This parameter needs to be sent as a git_json, otherwise, the backend won’t understand it and the copado-git-get command will fail.

In addition to these scripts, there are some out-of-the-box commands you can include in your scripts. Let’s take a look at some examples below.

You can call the copado command to set the progress of the job, optionally adding an error message or the result information:

usage: copado [-h] [-p PROGRESS_MESSAGE] [-e ERROR_MESSAGE] [-r RESULT_DATA]
-p PROGRESS_MESSAGE, --progress PROGRESS_MESSAGE
-e ERROR_MESSAGE, --error-message ERROR_MESSAGE
-r RESULT_DATA, --result-data RESULT_DATA
Examples:
copado --progress "concise progress message..."
copado -p "unexpected error" --error-message "there was an error attempting the connection... error=${error_messsage}"
copado --result-data "{"result": "success", ...}"

You can also call the copado command to attach local files as Salesforce files associated with the Result object, and you can give them an optional name in Salesforce different from the local file: 

usage: copado [-h] [-u UPLOAD_FILE] [-n NAME]

Examples:
copado --uploadfile /tmp/result.txt
copado -u /tmp/tmp.png --name "Chart of sales.png"

The copado-git-get command is a utility function to simplify Git initialization and other read operations. It can be called multiple times in the same directory, or in separate directories, in order to initialize, fetch and checkout any branch:

usage: copado-git-get [-h] [-d DIRECTORY] [--depth DEPTH] [-v] [-c] BRANCH
-d DIRECTORY, --directory DIRECTORY
Root directory of the repository. Defaults to the current directory
--depth DEPTH     git DEPTH for shallow cloning. Default is no shallow cloning
-c, --create      if BRANCH does not exist, create it locally instead of throwing an error
-v, --verbose         enable git verbose / progress output

Example 1:
copado-git-get main
# fetch or create the branch "testing-branch" from the previous branch "main"
copado-git-get -c "testing-branch"

Example 2:
# create the needed directories
mkdir /tmp/repository_main /tmp/repository_testing

# clone the repository twice, fetching only the last commit of each branch
copado-git-get --depth 1 -d /tmp/repository_main main
copado-git-get --depth 1 -d /tmp/repository_testing testing

# show the different file names between the two branches.
diff -q /tmp/repository_main /tmp/repository_testing 


How did we do?