-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathForm-To-PR-AppScript.js
136 lines (108 loc) · 4.55 KB
/
Form-To-PR-AppScript.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
// make sure to give permission by manual exec once
// make sure to add GH_TOKEN property, make sure your token is properly scoped
const USERNAME = "tpo-manit";
const REPO = "bluebook";
function onSubmit(e) {
var form = FormApp.getActiveForm();
var allResponses = form.getResponses();
var latestResponse = allResponses[allResponses.length - 1];
var response = latestResponse.getItemResponses();
var payload = {};
var allItems = form.getItems();
for (var i = 0; i < allItems.length; i++) {
var questionTitle = allItems[i].getTitle();
var answer = null;
var itemResponse = response.find(function(item) {
return item.getItem().getTitle() === questionTitle;
});
if (itemResponse) {
if (itemResponse.getItem().getType() === FormApp.ItemType.FILE_UPLOAD) {
var files = itemResponse.getResponse();
answer = files ? files.map(function(fileId) {
var fileUrl = `https://drive.google.com/file/d/${fileId}/view?usp=sharing`;
makeFilePublic(fileId);
return `[https://drive.google.com/file/d/${fileId}/view?usp=sharing](https://drive.google.com/file/d/${fileId}/view?usp=sharing)`;
}) : [];
} else {
answer = itemResponse.getResponse();
}
}
payload[questionTitle] = answer ? (Array.isArray(answer) ? answer : [answer]) : [];
}
var responseId = allResponses.length;
var date = new Date().toISOString().split('T')[0];
var fileName = `response_${responseId}_${date}.json`;
var responseData = {
"timestamp": new Date().toISOString(),
"email": latestResponse.getRespondentEmail(),
"data": payload
};
var fileContent = JSON.stringify(responseData, null, 2);
var GITHUB_TOKEN = PropertiesService.getScriptProperties().getProperty("GH_TOKEN");
createGitHubFile(fileName, fileContent, responseId, GITHUB_TOKEN);
}
function makeFilePublic(fileId) {
var driveService = DriveApp.getFileById(fileId); // Get the file by ID
driveService.setSharing(DriveApp.Access.ANYONE_WITH_LINK, DriveApp.Permission.VIEW); // Set the sharing to public with view access
}
function createGitHubFile(fileName, fileContent, responseId, GITHUB_TOKEN) {
var branchName = fileName;
var defaultBranch = 'main';
var url = `https://api.github.com/repos/${USERNAME}/${REPO}/git/refs/heads/${defaultBranch}`;
var options = {
"method": "get",
"headers": {
"Authorization": "token " + GITHUB_TOKEN
}
};
try {
var response = UrlFetchApp.fetch(url, options);
var defaultBranchData = JSON.parse(response.getContentText());
var sha = defaultBranchData.object.sha;
var createBranchUrl = `https://api.github.com/repos/${USERNAME}/${REPO}/git/refs`;
var createBranchPayload = {
"ref": `refs/heads/${branchName}`,
"sha": sha
};
var createBranchOptions = {
"method": "post",
"headers": {
"Authorization": "token " + GITHUB_TOKEN
},
"payload": JSON.stringify(createBranchPayload)
};
UrlFetchApp.fetch(createBranchUrl, createBranchOptions);
var createCommitUrl = `https://api.github.com/repos/${USERNAME}/${REPO}/contents/responses/${fileName}`;
var commitPayload = {
"message": `[Webhook] Response ${responseId}: ${new Date().toISOString()}`,
"content": Utilities.base64Encode(fileContent),
"branch": branchName
};
var commitOptions = {
"method": "put",
"headers": {
"Authorization": "token " + GITHUB_TOKEN
},
"payload": JSON.stringify(commitPayload)
};
UrlFetchApp.fetch(createCommitUrl, commitOptions);
var createPRUrl = `https://api.github.com/repos/${USERNAME}/${REPO}/pulls`;
var prPayload = {
"title": `Response ${responseId}`,
"head": branchName,
"base": defaultBranch,
"body": `This PR contains the response data for response ID ${responseId}`
};
var prOptions = {
"method": "post",
"headers": {
"Authorization": "token " + GITHUB_TOKEN
},
"payload": JSON.stringify(prPayload)
};
UrlFetchApp.fetch(createPRUrl, prOptions);
Logger.log("PR created successfully: " + fileName);
} catch (e) {
Logger.log("Error: " + e.toString());
}
}