Skip to content

Instantly share code, notes, and snippets.

@jamesray
Created April 14, 2023 17:26
Show Gist options
  • Save jamesray/7edf4dce4967b1e47f78fd9f90a25435 to your computer and use it in GitHub Desktop.
Save jamesray/7edf4dce4967b1e47f78fd9f90a25435 to your computer and use it in GitHub Desktop.
write a google form that connects to a github issue template?
You can't directly create a Google Form that connects to a GitHub issue template. However, you can achieve this by using Google Apps Script to create a custom form submission handler that sends the form data to the GitHub API, which in turn creates a new issue using a specific template.
First, create your Google Form and collect the necessary information for the issue.
Next, follow these steps to create a Google Apps Script to handle form submissions and create a GitHub issue:
Open the Google Form.
Click the three-dot menu in the top right corner and select "Script editor."
In the script editor, replace the default code with the following code:
javascript
Copy code
function onSubmit(e) {
var formResponse = e.response;
var itemResponses = formResponse.getItemResponses();
var issueTitle = '';
var issueBody = '';
for (var i = 0; i < itemResponses.length; i++) {
var itemResponse = itemResponses[i];
var question = itemResponse.getItem().getTitle();
var answer = itemResponse.getResponse();
if (question === 'Issue Title') {
issueTitle = answer;
} else {
issueBody += `**${question}**\n\n${answer}\n\n`;
}
}
createGitHubIssue(issueTitle, issueBody);
}
function createGitHubIssue(title, body) {
var githubToken = 'YOUR_GITHUB_PERSONAL_ACCESS_TOKEN';
var repoOwner = 'REPO_OWNER';
var repoName = 'REPO_NAME';
var issueTemplatePath = '.github/ISSUE_TEMPLATE/your_template.md';
var apiUrl = `https://api.github.com/repos/${repoOwner}/${repoName}/issues`;
var headers = {
'Authorization': 'token ' + githubToken,
'Accept': 'application/vnd.github+json'
};
var payload = {
'title': title,
'body': body,
'template': issueTemplatePath
};
var options = {
'method': 'post',
'headers': headers,
'payload': JSON.stringify(payload)
};
UrlFetchApp.fetch(apiUrl, options);
}
Replace YOUR_GITHUB_PERSONAL_ACCESS_TOKEN with your GitHub personal access token. To create one, follow the official GitHub guide.
Replace REPO_OWNER and REPO_NAME with the appropriate values for your GitHub repository.
Replace your_template.md in the issueTemplatePath variable with the name of your issue template file.
Save the script by clicking the floppy disk icon or pressing CTRL + S (or CMD + S on macOS).
In the script editor, click on "Triggers" (the clock icon in the left sidebar).
Click "Add Trigger" (the "+" button in the bottom right corner).
Set up the trigger as follows:
Choose which function to run: onSubmit
Choose which deployment should run: Head
Event source: From form
Event type: On form submit
Click "Save" to create the trigger.
Now, every time someone submits the Google Form, the onSubmit function will be triggered, which will then call createGitHubIssue to create a new issue in your GitHub repository using the specified issue template.
Please note that this approach doesn't directly use the issue template's YAML frontmatter. The issue's title and body are generated based on the form responses. The issue template file is only used for API reference purposes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment