As a developer, you understand the importance of version control and maintaining a consistent flow of commits to your GitHub repository. However, manually committing changes to your repository can be time-consuming and tedious, especially if you need to make multiple commits in a short amount of time. That’s where Google Apps Script comes in – it offers a powerful and simple automation tool to help you automate your GitHub commits. In this tutorial, we’ll take a look at how you can use Google Apps Script to automate your GitHub commits.

Setting Up Your GitHub Repository

Before we dive into the code, let’s first set up our GitHub repository. If you haven’t created a repository yet, head to your GitHub account and create a new repository. Once you’ve set up your repository, you’ll need to create a Personal Access Token. Personal Access Tokens are used to authenticate API requests to GitHub, and we’ll use it in our Google Apps Script code to automate our commits.

To create a Personal Access Token, head to your GitHub account, click on your profile picture in the top-right corner, then click on Settings. Under Developer settings, select Personal access tokens and click on Generate new token. Give your token a name, select the repo scope, then click Generate token. Make sure to copy your token – we’ll use it in our Google Apps Script code.

Creating Your Google Apps Script Project

Now that you’ve set up your GitHub repository and created a Personal Access Token, let’s create our Google Apps Script project. Open up Google Drive, create a new document, then click on Tools > Script editor.

In your Google Apps Script project, you’ll see a code editor where you can write your code. Let’s start by writing a function that will make a commit to our GitHub repository:

function commitToGitHub() {
    var token = "YOUR_PERSONAL_ACCESS_TOKEN";
    var repoUrl = "https://api.github.com/repos/YOUR_USERNAME/YOUR_REPO/contents/YOUR_FILE";
    var message = "YOUR_COMMIT_MESSAGE";
    var content = "YOUR_CONTENT";

    var contentEncoded = Utilities.base64Encode(content);

    var options = {
        method: "PUT",
        headers: {
            "Authorization": "token " + token,
            "Content-Type": "application/json"
        },
        payload: JSON.stringify({
            "message": message,
            "content": contentEncoded
        })
    };

    UrlFetchApp.fetch(repoUrl, options);
}

Let’s go through the code line by line:

  • First, we define our commitToGitHub function.
  • Next, we set our Personal Access Token to the token variable.
  • We set our GitHub repository URL to the repoUrl variable. Make sure to replace YOUR_USERNAME, YOUR_REPO, and YOUR_FILE with your GitHub username, repository name, and filename respectively.
  • We set our commit message to the message variable.
  • We set our content to the content variable – this is the content that we want to commit to our repository.
  • We encode our content using Utilities.base64Encode().
  • We set our options for our HTTP request. We set the HTTP method to PUT, set the Authorization header to include our Personal Access Token, set the Content-Type header to application/json, and set our payload to include our commit message and encoded content.
  • Finally, we make an HTTP request to our GitHub repository using UrlFetchApp.fetch().

Using Your Google Apps Script Function

Now that you’ve written your Google Apps Script function, let’s use it to make a commit to your GitHub repository. Simply call your function from your Google Apps Script editor by clicking on the Run button. You’ll see a pop-up asking you to authorize the script – click on Review permissions, then click on Allow.

Once you’ve authorized the script, your function will run and a commit will be made to your GitHub repository. You can check your repository to see that your commit was successful.