back to blog
Code Review Process using Git - Salesforce
January 04 2023 • 15 min read
  1. Download and install Visual Studio Code
  2. Setup Visual Studio Code
  3. Clone the repository
  4. Create a Project and Authorize the Org
  5. Retrieve Metadata from the org
  6. Push Metadata to Master/Main
  7. Create a branch from master in GIT and push the changes
  8. Review the changes

DOWNLOAD AND INSTALL VISUAL STUDIO CODE

Install Vs Code Download Visual Studio Code - Mac, Linux, Windows

SETUP VISUAL STUDIO CODE

  1. Install GIT
  2. Install Salesforce CLI Salesforce CLI
  3. Install JDK version 17
  4. Click on gear symbol at the bottom left corner of VS Code and select Settings.

screenshot 6

  1. in the  search bar type java home. Under java home you will find a blank bar. Head to the Java folder in your system where JDK 17 has been installed, copy the path(C:\Program Files\Java\jdk-17.0.2) and paste it as java home value.
  2. Another way to do it is by searching for the settings.json file from the search bar and changing the java home path

screenshot 20230104 033804

  1. Install the following extensions in VS Code
  1. Restart VS Code

CLONE THE REPOSITORY

  1. Create a GitHub account, if you don’t have one.
  2. If you have an account already, login and Create a new Repository(if you do not have any existing repository).
  3. Click New.

screenshot 35

  1. Enter a name for the repository, type an optional description, choose whether you want it to be public or private, check the Add a README file option if required and click on Create repository.

screenshot 36

screenshot 37

  1. If you have created a new repository, copy the link of the repository.

screenshot 38

  1. If you already have an existing repository, open the repository and copy the link from the address bar of the browser.

screenshot 63

  1. Open VS Code and open the terminal using the command Ctrl + ` .

  2. Configure your user email using the below command.

    git config --global user.email <user_email>
  3. Use the below command to clone the repository.

    git clone <repository_link>
  4. Sign in with your browser or use a personal access token.

screenshot 0 29

  1. Authorize GitCredentialManager when prompted.

crp 30

  1. Confirm access with your password when prompted.

crp 34

  1. You will see a screen mentioning the Authentication Succeeded.

crp 32

  1. Now the repository will be cloned into your local.

CREATE A PROJECT AND AUTHORIZE THE ORG

  1. Press Ctrl+Shift+p in windows and Cmd+Shift+p in mac to open the command palette and search for SFDX: Create Project with Manifest and select it.

screenshot 20221125 053542

  1. Choose the Standard template, enter the Project name and choose the destination folder as the cloned repository folder to save the files.

screenshot 20221125 053615

screenshot 20221125 053650

  1. It should take around 5 minutes for all the extensions to get ready before authorizing the org.
  2. Now, open the command palette and search for SFDX: Authorize an Org and select it.

screenshot 20221125 053753

  1. Choose the type of Org i.e. sandbox or production or custom. Here we pull metadata from Production, so we choose production.

screenshot 20221125 053832

  1. Type an Org alias and hit enter.

screenshot 20221125 053906

  1. You will be redirected to Salesforce login page, enter the credentials of the target org and click allow when prompted.

screenshot 20221125 054106

crp 25

  1. Now head back to the VS Code and you will see the notification SFDX: Authorize an Org successfully ran on the bottom right corner of the screen.

RETRIEVE METADATA FROM THE ORG

  1. The file where we define what metadata to be pulled from the Org is the package.xml.
  2. A single package.xml file cannot pull more than 10,000 files from the Org. We may have to use another package.xml file to pull the remaining metadata.
  3. Open the command palette and search for SFDX Package.xml Generator: Choose Metadata Components and select it.
  4. You will see a new tab and the metadata from your org will be processed for a few seconds.
  5. Click Select All next to Metadata Types and most of the metadata from your Org will be selected and the data will be processed in alphabetical order for a few minutes. You will receive a notification when all the metadata types are imported.

screenshot 20221125 054547

  1. Metadata types can also be selected manually.

  2. Click UPDATE PACKAGE.XML present on the blue header of the screen and your package.xml file will be updated.

  3. We have the default package.xml file. If you have more than 10,000 files to be retrieved, under the manifest folder add another file and name it as package2.xml.

  4. Copy the first two lines and last two lines from the package.xml file and paste it in the package2.xml file as shown below.

    <?xml version="1.0" encoding="UTF-8"?>
    <Package xmlns="http://soap.sforce.com/2006/04/metadata">
    
        <version>56.0</version>
    </Package>
    
  5. Split the components in the types tags between the two xml files.

  6. Right click on the package.xml file and select SFDX: Retrieve Source in Manifest from Org.

crp 45

  1. All the metadata from the package.xml file will be retrieved into the Project.
  2. Similarly retrieve the metadata from package1.xml file.

PUSH METADATA TO THE MASTER/MAIN (From Production)

  1. Check for the files to add to GIT

    git status
  2. Add all the files to the staging area for GIT using the below command.

    git add .
  3. Check whether all the files are added again, the file name displays in green if already added.

    git status
  4. Commit records the changes made to the files in the local repository, use the below command to commit changes to the local repository.

    git commit -m “commit message in quotes”
  5. To check the current state of the repository use the below command.

    git status
  6. Push the changes to the remote repository using the below command.

    git push
  7. We may need to provide Password (which we can get/generate from GIT) when prompted

CREATE A BRANCH FROM MASTER IN GIT AND PUSH CHANGES

  1. Authorize the VS Code to the Org where the new feature has been developed.

  2. Create a new branch and check out to the branch using the below command.

    git checkout -b <feature_branch_name>
  3. Retrieve the metadata from the Org using the package.xml files.

  4. Check the status of the files.

    git status
  5. Add all the metadata changes using the below command to the staging area for GIT using the below command.

    git add .
  6. Check the status again to make sure all the files are added.

  7. Commit all the changes to the local repository using the below command.

    git commit -m “commit message in quotes”
  8. Check the status of the files after committing to ensure all files are added.

    git status
  9. Push metadata to the new branch using the below command

    git push --set-upstream origin <feature_branch_name>

REVIEW THE CHANGES

  1. Create a new pull request.

crp 57

  1. From the compare dropdown choose the feature branch created.

crp 58

  1. You can see the number of commits, number of files changed and also the number of contributors.
  2. Now click on the Id where you can see View commit details text when hovered over.

crp 64

  1. All the GIT user interfaces(Bitbucket/Azure Devops/GitHub) allow us to view the differences between the master and the branch.

crp 61

  1. The GitHub interface displays the changed files with the number of additions and deletions
  2. The code can be viewed in split or unified mode.
  3. We can enter comments for a specific line of code by hovering over it and clicking on the + icon that pops up.
  4. Developer fixes the code in VS Code and pushes the changes to the feature branch.
  5. After the final review, the branch can be merged with the master.

crp 65

crp 66

crp 67

  1. The code is now ready to be pushed to production.

WRAPPING IT UP

In this blog we have explained how to establish a code review process using Git and VS Code.

Leave a Comment

Your email address will not be published

© 2024 Digital Biz Tech