Skip to content

Instantly share code, notes, and snippets.

@SimonGoring
Created August 16, 2016 05:29
Show Gist options
  • Select an option

  • Save SimonGoring/5377ecbc5d30c0a1ad12885b52471ed7 to your computer and use it in GitHub Desktop.

Select an option

Save SimonGoring/5377ecbc5d30c0a1ad12885b52471ed7 to your computer and use it in GitHub Desktop.
Making responsive webforms for Neotoma

Bryan McBride had a really useful gist posted that helped link a Google Form to a GitHub repository. For Neotoma we have a number of associated tools and products and we'd like to be able to track feature requests and bug reports more centrally. To do this I decided to link a Google Form to our related GitHub repositories.

The form itself is found here.

To actually link the form to the repositories requires a bit of work. The use of the Google Apps Script Editor was pretty new to me, but it's fairly straightforward, especially given the fairly clear instructions provided on Bryan's gist.

I got a GitHub personal access token at the repo level and plugged that into the top of the script.

Some of this is a bit messy. I think a switch statement would work better than the long if/else statements I have here, but I don't want to spend much more time on this :)

var ghToken = REPLACE_THIS_WITH_YOUR_TOKEN;

function onFormSubmit(e) {

  // Pulls responses from the Form:
  var itemResponses = e.response.getItemResponses();
  
  // Decide what to do with the responses:
 
  var item1 = itemResponses[0].getResponse();
  // One of three choices:
  if(item1 == "Bug report") {
    var labels = "bug";
    }
  else if(item1 == "New feature for an existing tool"){
    var labels = "enhancement";
  }
  else {
    // I'm providing an empty tag here, but we could add something else.
    labels = "";
  }
  
  // Which repo does it go to?
  var repo_ans = itemResponses[1].getResponse();
  if (repo_ans == "Explorer - http://apps.neotomadb.org") {
      repo = "NeotomaDB/Explorer";
    }
    else if (repo_ans == "Neotoma API - http://api.neotomadb.org") {
      repo = "NeotomaDB/Neotoma-API";
    }
    else if (repo_ans == "Tilia - http://tiliait.com") {
      repo = "SimonGoring/Tilia";
    }
    else if (repo_ans == "neotoma R package - http://github.com/ropensci/neotoma") {
      repo = "ropensci/neotoma";
    }
  else if (repo_ans == "Neotoma Website - http://neotomadb.org") {
      repo = "NeotomaDB/neotomadb.github.io";
    }
  else if (repo_ans == "Documentation") {
      repo = "SimonGoring/neotomadb-manual";
    }
  else if (repo_ans == "earthlife R package - https://github.com/EarthLifeConsortium/earthlife") {
      repo = "EarthLifeConsortium/earthlife";
    }
  else {
      repo = "NeotomaDB/Neotoma-Database";
    }
  
  // Issue title
  var title = itemResponses[2].getResponse();
  
  // Issue body:
  var body = itemResponses[3].getResponse();
  
  var payload = {
    "title": "From form: " + title,
    "body": body + "\n\n*This issue was autogenerated by the Google Form for [Features](https://docs.google.com/forms/d/e/1FAIpQLSdRNat6L9grRF0xU5gibkr26xq9jD9wyHgw_AWxhrgn0lWv7w/viewform)*"
  };

  var options = {
    "method": "POST",
    "contentType": "application/json",
    "payload": JSON.stringify(payload)
  };

  var response = UrlFetchApp.fetch("https://api.github.com/repos/"+repo+"/issues?access_token="+ghToken, options);
}

Although the form does ask for contact information, I've decided to keep that private, excluding it from the Issue created by the form. We do track that information if provided on the associated Sheet produced by the form. I'd rather not make someone's private information unexpectedly public.

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