MarketLytics

Integrating GitLab Version Control in Apps Script Via Google Cloud Function

Google’s ecosystem is incredibly widespread, from its productivity suite like Google Docs to the powerful Google Compute Engine platform. We are big fans of Google’s ecosystem and have built our work around Google’s set of tools.

Google may be a little late to the productivity suite party but it has managed to build a solid product in the form of Google Sheets, thanks to its seamless collaborative features and incredibly powerful scripting abilities. This is possible thanks to the Google Apps Script platform, a rapid application development platform that makes it fast and easy to create business applications that integrate with different services.

Most of our project management tools are built inside Google Sheets using Google Apps Script. One issue we have found with Apps Script is the ability to integrate it with a proper code versioning tool. GitLab is our preferred choice and we wanted to be able to commit changes directly to our code repository. Thus, we set our brains to work and came up with a quick solution to use a Google Cloud Function to integrate Apps Script with GitLab.

Goal

  • Ability to commit changes to GitLab project directly from Google Apps Script
  • Get notifications on Slack for all successful commits

How To Use

  1. Open your Apps Script project.
  2. Paste the following code excerpt anywhere in the script.
function commit() {  
var commit_message = “apps script commit”   //the commit message  
var function_url = “” //The Trigger URL of the Cloud Function
var url = function_url+”script_id=”+ScriptApp.getScriptId()+”&message=”+commit_message;
var response = UrlFetchApp.fetch(url);
Logger.log(response);  }
  1. Execute the commit function to make a commit in the master branch.

It will create a scripts folder with all the scripts and the manifest from your Apps Script project. Any subsequent commits will update the folder with changes, as per the version control process.

By default, it will use the repository name and the access token that have been stored in Google Cloud Storage and can be configured by editing the gitlab_credentials.json file.

Workflow

  • Apps Script
    • Executing the commit function creates a REST GET request with the Script ID of the project and the provided commit message
    • Calls the Google Cloud Function using the Trigger URL, providing it with the commit message and script id variables
    • Prints the result (commit diff) in the log
  • Cloud Function
    • Retrieves Google Credentials from Cloud Storage
    • Uses Google Credentials and Apps Script ID to retrieve project script files and manifest from Apps Script project
    • Retrieves Gitlab Credentials from Cloud Storage
    • Establishes connectivity with Gitlab and pulls the list of existing files
    • Starts creating a JSON commit object with the following file actions
      • Create action for newly created files
      • Update action for already existing files
      • Delete action for existing files that have been removed in the new request
    • Sends a POST request to GitLab with the created JSON object
    • Retrieves the diff of the commit
    • Sends a new request to send to Slack using a webhook with the commit status, including the script URL and the GitLab commit URL (Optional)
    • Returns the result commit diff to the Apps Script project

Deployment 

Getting Credentials

To implement this, you will need two sets of credentials. Google Credentials that allow your script to access your Apps Script project and GitLab credentials that allow your script to commit to your GitLab repository.

Getting Credentials to Authenticate with Apps Script

Enable the Apps Script API for your project by going to Script Settings.

Open Google’s Quickstart guide for Python and execute Step 1 to download the Client Configuration File. Save it somewhere safe for now.

Getting Credentials to Authenticate with GitLab

GitLab allows you to create a Personal Access token that you can use to perform changes to your repository using a scripting tool. Here is how you can create one,

  1. Sign in to GitLab.
  2. In the upper-right corner, click your avatar and select Settings.
  3. On the User Settings menu, select Access Tokens.
  4. Choose a name and optional expiry date for the token.
  5. Choose the desired scopes. For our use, we require read as well as write access to allow us to execute commits so we are going to select all scopes.
  6. Click the Create personal access token button.
  7. Save the personal access token somewhere safe. If you navigate away or refresh your page, and you did not save the token, you must create a new one.

Now create a file in the following format, with the token in the token field and the name of the repositories you want to allow your function to commit to as a list in the repo field. This functionality has been added as an additional security feature to prevent accidental changes to other repositories.

{“token”:”**************”,”repo_list”:[“apps-script-project”]}


Save it in JSON format. For e.g. gitlab_credentials.json.

Slack Credentials 

One additional functionality that we have built-in this code is the ability to generate Slack alerts to notify in case of any error faced. You can choose to remove this functionality by simply commenting out the lines from 236-252.

If you want to use it, you can create an incoming Slack Webhook and store the URL in the following format on the same Cloud Storage bucket being used for the credentials files.

{“url”:””}

Storing Credentials

Now, you should have three files,

  • credentials.json
  • gitlab_credentials.json
  • slack_webhook.json (Optional)

We are going to store this file on Google Cloud Storage, which is a RESTful online file storage web service for storing and accessing data on Google Cloud Platform infrastructure. It allows us to safely store and access credentials without exposing them in our code files.

Visit Google Cloud Storage Console and create a new bucket and store this file there. Copy the Path for each file. You are going to configure these in the Cloud Function section later on.

Google Cloud Function

Creating Function

  1. Go to the Create a Google Cloud Function option menu.
  2. Enter the function name and preferred region.
  3. For Trigger type, choose HTTP.
  4. For Authentication, choose Allow unauthenticated access. For more security, you can also choose Require authentication but this will restrict your function in terms of accessibility. We suggest going with Allow unauthenticated access for testing purposes and changing it later to restricted.
  5. Click on Advanced and specify the configurations,
    1. Memory Allocated: 512 MB (Recommended)
    2. Timeout: 540 seconds (Recommended)
  1. Click on Variables, Networking and Advanced Settings to reveal more options. Here, you will create runtime environment variables which are variables accessible to your function using the os library at runtime. We are going to use the components of code that need to be changed i.e. the Cloud Storage paths for our credential files as well as the name of our project,
gitlab_credentials:cloud_storage_path/gitlab_credentials.json
google_credentials:cloud_storage_path/google_credentials.json
slack_webhook: cloud_storage_path/slack_webhook.json
scripts_folder: scripts/
project_name: google-cloud-project-name
  1. Once you are done, click on NEXT.

This will create your function and take you to the Source page where you can edit and add the code. There are two essential files you need to upload here,

  • main.py – Contains the Python script.
  • requirements.txt – Contains the packages required by the main.py script.

Both of these files are provided in the following code snippet.