Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save doub1ejack/43b3fe5724c9eeb1561ab33df7f72567 to your computer and use it in GitHub Desktop.
Save doub1ejack/43b3fe5724c9eeb1561ab33df7f72567 to your computer and use it in GitHub Desktop.
Create a new GitHub Issue from a Google Form submission

Wiring up a Google Form to GitHub is not that difficult with a little bit of Apps Script automation. All you need is a Google account, a GitHub account, and a web browser...

1) Set up your GitHub Personal Access Token

Personal access tokens provide an easy way to interact with the GitHub API without having to mess with OAuth. If you don't already have a personal access token with repo or public_repo access, visit your GitHub settings page and generate a new token.

Be sure to copy your token some place safe and keep it secure. Once generated, you will not be able to view or copy the token again.

2) Set up the Form & Spreadsheet

  1. Create a Google Form.
  2. From the Responses tab, click the More icon.
  3. Select Choose a response destination.
  4. Select New spreadsheet: Creates a new spreadsheet in Google Sheets for responses.
  5. Click Create to create and open the sheet.

3) Configure the App Script Logic

  1. You should have a newly created blank spreadsheet with headers automatically generated from your form.
  2. Click Tools > Script editor... to launch the App Script editor coding environment. This Script will be bound to your sheet, so you can listen for form submissions and fire off a new issue to your GitHub repo.
  3. Delete the boilerplate code in the Code.gs file and replace it with something similar to this:
// github access token for github user with access to the desired repo
var ghToken = "zzzzzzzzz";

function myFormSubmit(e) {
  
  // If all questions are required, getItemResponses returns responses in form-order
  // (see https://stackoverflow.com/a/26285341/263900) 
  var formResponse = e.response;
  var itemResponses = formResponse.getItemResponses();
  
  // grab responses by form position
  var title = itemResponses[0].getResponse();  // returns a string
  var description = itemResponses[1].getResponse();
  var userName = itemResponses[2].getResponse();
  var userOrg = itemResponses[3].getResponse();
  Logger.log(JSON.stringify([title,description,userName, userOrg]));
  
  var payload = {
    "title": title,
    "body": `${description}\n ~ ${userName} (${userOrg})`
  };
   
  var options = {
    "method": "POST",
    "contentType": "application/json",
    "payload": JSON.stringify(payload)
  };
  
  var response = UrlFetchApp.fetch("https://api.github.com/repos/codeforbtv/expunge-vt/issues?access_token="+ghToken, options);
}
  1. The onFormSubmit function includes an event object e but this object does not include the form/spreadsheet values. However, you can retrieve the responses as shown above. (Note that this will provide values in the order they appear on the form, and also only works when all the form questions are 'required'.)
  2. Reference the GitHub Issues API for a full list of options for programmatically creating a new issue (labels, users, etc).
  3. Once we've built the title and body of the issue, we can build the HTTP request using App Script's URL Fetch Service.
  4. Give your app script project a name and save it .

4) Set up the project Trigger

  1. From within the app script editor, click Edit > Current project's triggers.
  2. Add a trigger with these values
    • Choose which function to run: myFormSubmit
    • Which runs at deployment: Head
    • Select event source: From form
    • Select event type: On form submit
    • Failure notification settings: Notify me immedately
  3. Click Save and accept any authorizations to access your forms and access web services on your behalf.
  4. This trigger will listen to form submissions and pass the data to your function, which POSTs the new issue to your GitHub repo.

5) Give it a try

Go ahead and submit the form.

If it was successful, you should have a new issue in your github queue. If not..

Conclusion

This exercise demonstrates how to utilize Google Forms as a front-end for capturing information, which can then be passed on to other services, such as GitHub, CartoDB, Fulcrum, etc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment