-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathformDataToJson.js
More file actions
347 lines (286 loc) · 9.31 KB
/
formDataToJson.js
File metadata and controls
347 lines (286 loc) · 9.31 KB
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
// Retrieves file and returns as json object
async function retrieveFile(filePath) {
try {
const response = await fetch(filePath);
if (!response.ok) {
throw new Error("Network response was not ok");
}
// Returns file contents in a json format
return await response.json();
} catch (error) {
console.error("There was a problem with the fetch operation:", error);
return null;
}
}
function isMultiSelect(obj) {
if (typeof obj !== 'object' || obj === null || Array.isArray(obj)) return false;
for (const key in obj) {
if (typeof obj[key] !== 'boolean') {
return false;
}
}
return true; // Returns true if all values are booleans
}
// Convert from dictionary to array
function getSelectedOptions(options) {
let selectedOptions = [];
for (let key in options) {
if (options[key]) {
selectedOptions.push(key);
}
}
return selectedOptions;
}
// Populates fields with form data
function populateObject(data, fields) {
let reorderedObject = {}
for (const field of fields) {
let value = data[field];
// Does not assign optional properties with blank values
if (value == null || value === "") {
continue;
}
// Adjusts value accordingly if multi-select field
if ((typeof value === "object" && isMultiSelect(value))) {
value = getSelectedOptions(value);
}
// Recurses if multi-field object
else if (typeof value === 'object' && !Array.isArray(value) && value !== null && Object.keys(value).length > 1) {
value = populateObject(value, Object.keys(value));
}
reorderedObject[field] = value;
}
return reorderedObject;
}
async function populateCodeJson(data) {
// Fetching schema based on search params
const params = new URLSearchParams(window.location.search);
const page = params.get("page") || "gov";
const filePath = `schemas/${page}/schema.json`;
// Retrieves schema with fields in correct order
const schema = await retrieveFile(filePath);
let codeJson = {};
// Populates fields with form data
if (schema) {
codeJson = populateObject(data, Object.keys(schema.properties));
} else {
console.error("Failed to retrieve JSON data.");
}
return codeJson;
}
// Creates code.json object
async function createCodeJson(data) {
delete data.submit;
const codeJson = await populateCodeJson(data);
window.gh_api_key = data['gh_api_key']
console.log("TEST")
console.log(window.gh_api_key)
const jsonString = JSON.stringify(codeJson, null, 2);
document.getElementById("json-result").value = jsonString;
}
// Copies code.json to clipboard
async function copyToClipboard(event) {
event.preventDefault();
var textArea = document.getElementById("json-result");
textArea.select();
document.execCommand("copy")
}
const NEW_BRANCH = 'code-json-branch' + Math.random().toString(36).substring(2, 10);
function getOrgAndRepoArgsGitHub(url) {
const pattern = /https:\/\/github\.com\/([^\/]+)\/([^\/]+)/;
const match = url.match(pattern);
if (match) {
const owner = match[1];
const repo = match[2];
return { owner, repo };
}
else {
throw new Error('Invalid URL!');
}
}
async function createBranchOnProject(projectURL, token) {
const { owner, repo } = getOrgAndRepoArgsGitHub(projectURL);
const response = await fetch(`https://api.github.com/repos/${owner}/${repo}/git/refs/heads/main`,
{
method: 'GET',
headers: {
'Authorization': 'token '.concat(token),
},
}
);
const data = await response.json();
if (response.ok) {
const sha = data.object.sha;
const createBranchApiUrl = `https://api.github.com/repos/${owner}/${repo}/git/refs`;
// Create the new branch from the base branch
const newBranchResponse = await fetch(createBranchApiUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `token ${token}`,
},
body: JSON.stringify({
ref: `refs/heads/${NEW_BRANCH}`, // Name of the new branch
sha: sha, // SHA of the base branch (main)
}),
});
const newBranchData = await newBranchResponse.json();
if (newBranchResponse.ok) {
console.log('New branch created successfully: ', newBranchData);
return true;
}
else {
console.error('Error creating new branch: ', newBranchData);
alert("Failed to create branch on project! Error code: " + newBranchResponse.status + ". Please check API Key permissions and try again.")
return false;
}
}
else {
console.error('Error fetching base branch info:', data);
alert('Error fetching base branch info:', data);
return false;
}
}
async function addFileToBranch(projectURL, token, codeJSONObj) {
const { owner, repo } = getOrgAndRepoArgsGitHub(projectURL);
const FILE_PATH = 'code.json'
const createFileApiUrl = `https://api.github.com/repos/${owner}/${repo}/contents/${FILE_PATH}`;
const encodedContent = btoa(codeJSONObj);
console.log("Content: ", encodedContent);
console.log("Branch: ", NEW_BRANCH);
const response = await fetch(createFileApiUrl,
{
method: 'PUT',
headers: {
'Accept': 'application/vnd.github+json',
'Authorization': 'Bearer '.concat(token),
'X-GitHub-Api-Version': "2022-11-28"
},
body: JSON.stringify({
message: "Add codejson to project",
committer: {
name: "codejson-generator form site",
email: "opensource@cms.hhs.gov"
},
content: encodedContent,
branch: NEW_BRANCH,
}),
}
);
const data = await response.json()
if (response.ok) {
console.log('File added successfully: ', data);
return true;
}
else {
console.error('Error adding file: ', data);
alert("Failed to add file on project! Error code: " + response.status + ". Please check API Key permissions and try again.")
return false;
}
}
async function createPR(projectURL, token) {
const { owner, repo } = getOrgAndRepoArgsGitHub(projectURL);
const createPrApiUrl = `https://api.github.com/repos/${owner}/${repo}/pulls`;
const response = await fetch(createPrApiUrl,
{
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': 'token '.concat(token),
'X-GitHub-Api-Version': "2022-11-28"
},
body: JSON.stringify({
title: "Add code.json to Project",
body: "Add generated code.json file to project. Code.json was generated via codejson-generator form site.",
head: NEW_BRANCH,
base: 'main',
}),
}
);
const data = await response.json();
if (response.ok) {
console.log('Pull request created successfully: ', data);
return true;
}
else {
console.error("Error creating PR!: ", data);
alert("Failed to create PR on project! Error code: " + response.status + ". Please check API Key permissions and try again.")
return false;
}
}
// Creates PR on requested project
async function createProjectPR(event) {
event.preventDefault();
var textArea = document.getElementById("json-result");//Step 1
var codeJSONObj = JSON.parse(textArea.value)
if ('gh_api_key' in window) {
var apiKey = window.gh_api_key;
if ('repositoryURL' in codeJSONObj) {
var prCreated = false;
//Step 1
const branchCreated = await createBranchOnProject(codeJSONObj.repositoryURL, apiKey);
if (branchCreated) {
const fileAdded = await addFileToBranch(codeJSONObj.repositoryURL, apiKey, textArea.value);
if (fileAdded) {
prCreated = await createPR(codeJSONObj.repositoryURL, apiKey);
if (prCreated) {
console.log("PR successfully created!");
alert("PR has been created!");
}
}
}
else {
console.error("Could not create branch on requested repository with the requested API key!");
alert("Could not create branch on requested repository with the requested API key!");
}
}
else {
console.error("No URL found!");
alert("No URL given for project! Please provide project URL in repositoryURL text box");
}
}
else {
console.error("No API key found!");
alert("No API Key in submitted data! Please provide an API key");
}
//console.log(codeJSONObj)
}
// Triggers local file download
async function downloadFile(event) {
event.preventDefault();
const codeJson = document.getElementById("json-result").value
const jsonObject = JSON.parse(codeJson);
const jsonString = JSON.stringify(jsonObject, null, 2);
const blob = new Blob([jsonString], { type: "application/json" });
// Create anchor element and create download link
const link = document.createElement("a");
link.href = URL.createObjectURL(blob);
link.download = "code.json";
// Trigger the download
link.click();
}
// Triggers email(mailtolink)
async function emailFile(event) {
event.preventDefault();
const codeJson = document.getElementById("json-result").value
const jsonObject = JSON.parse(codeJson);
try {
const cleanData = { ...jsonObject };
delete cleanData.submit;
const jsonString = JSON.stringify(cleanData, null, 2);
const subject = "Code.json generator Results";
const body = `Hello,\n\nHere are the code.json results:\n\n${jsonString}\n\nThank you!`;
const recipients = ["opensource@cms.hhs.gov"];
const mailtoLink = `mailto:${recipients}?subject=${encodeURIComponent(subject)}&body=${encodeURIComponent(body)}`;
window.location.href = mailtoLink;
console.log("Email client opened");
} catch {
console.error("Error preparing email:", error);
showNotificationModal("Error preparing email. Please try again or copy the data manually.", 'error');
}
}
window.createCodeJson = createCodeJson;
window.copyToClipboard = copyToClipboard;
window.downloadFile = downloadFile;
window.createProjectPR = createProjectPR;
window.emailFile = emailFile;