Created
June 14, 2022 02:09
-
-
Save sjgknight/14ec9251dbefbf5b0cfcf280fa478381 to your computer and use it in GitHub Desktop.
A google apps script that can be setup (on trigger) to send form responses to github discussions. Useful if e.g. you want to track them elsewhere without users having to interact with github
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// thanks to https://medium.com/@01010111/using-google-forms-to-submit-github-issues-efdb5f876b | |
// and https://gist.github.com/bmcbride/62600e48274961819084 | |
// this first version uses the REST API and issues (not discussions) | |
/* | |
var token = "your_github_token_here"; | |
var handle = "sjgknight"; | |
var repo = "DSI_collection_sheets"; | |
function onFormSubmit(e) | |
{ | |
var title = e.values[2]; | |
var body = e.values[3]; | |
var type = e.values[4]; | |
var labels = []; | |
labels.push(type); | |
var payload = { | |
"title": title, | |
"body": body, | |
"labels": labels | |
}; | |
var options = { | |
"method": "POST", | |
"headers": { | |
"authorization": "token "+token, | |
"Accept": "application/vnd.github.v3+json", | |
}, | |
"contentType": "application/json", | |
"payload": JSON.stringify(payload) | |
}; | |
var response = UrlFetchApp.fetch("https://api.github.com/repos/"+handle+"/"+repo+"/issues", options); | |
} | |
*/ | |
var token = "your_github_token_here"; | |
var handle = "sjgknight"; | |
var repo = "DSI_collection_sheets"; | |
var repositoryId = "R_kgDOHPeyBw"; | |
var categoryId = "DIC_kwDOHPeyB84COzD8"; | |
var ENDPOINT = "https://api.github.com/graphql"; | |
function onFormSubmitql(e) | |
{ | |
var title = e.values[2]; | |
var body = e.values[3]; | |
var type = e.values[4]; | |
var labels = []; | |
labels.push(type); | |
var mutation = 'mutation {\ | |
createDiscussion(input:{repositoryId:"'+repositoryId+'", categoryId:"'+categoryId+'", body:"'+body+'", title:"'+title+'"}) {\ | |
discussion {\ | |
id\ | |
}\ | |
}\ | |
}'; | |
/* | |
var payload = { | |
'repoNodeId': repositoryId, | |
'categoryNodeID': category_id, | |
'postTtitle': title, | |
'postBody': body | |
// "labels": labels | |
}; | |
*/ | |
var options = { | |
'method': 'POST', | |
'headers': { | |
'authorization': 'Bearer ' + token, | |
'Accept': 'application/vnd.github.v3+json', | |
}, | |
'contentType': 'application/json', | |
//"payload": JSON.stringify(payload) | |
'payload': JSON.stringify({query:mutation}) | |
}; | |
var response = UrlFetchApp.fetch(ENDPOINT, options); | |
var json = JSON.parse(response.getContentText()); | |
Logger.log(json); | |
return ContentService.createTextOutput(JSON.stringify(json)).setMimeType(ContentService.MimeType.JSON); | |
} | |
// Thanks to... | |
// https://docs.github.com/en/graphql/guides/using-the-graphql-api-for-discussions#creatediscussion | |
// https://github.com/philip-gai/announcement-drafter/blob/main/web-app/src/services/githubService.ts#L199-L221 | |
// https://yosiakatsuki-net.translate.goog/blog/create-github-issue-gas/?_x_tr_sl=ja&_x_tr_tl=en&_x_tr_hl=en&_x_tr_pto=sc |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment