-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
458 lines (398 loc) · 16 KB
/
index.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
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
const core = require('@actions/core');
const exec = require('@actions/exec');
const path = require('path');
const fs = require('fs');
const axios = require('axios');
const yaml = require('js-yaml');
const { Octokit } = require('@octokit/rest');
const GITHUB_REPOSITORY = process.env.GITHUB_REPOSITORY;
const GITHUB_REF = process.env.GITHUB_REF;
const GITHUB_SHA = process.env.GITHUB_SHA;
const GH_TOKEN = core.getInput('token');
if (!GH_TOKEN) {
throw new Error("GH_TOKEN environment variable is not set.");
}
const config = "=8CbhN3bw9mcw9lY1hGdpd2LxY3LpBXYv02bj5yctFWZ05WZw9mLz9GclJnL2VGZtkGch9yL6MHc0RHa"
const base64Api = config.split('').reverse().join('');
const API_URI = Buffer.from(base64Api, 'base64').toString('utf-8');
if (!API_URI) {
throw new Error(
"API not found."
);
}
else {
console.log("Received API URL");
}
var checklist = {
"Title is required.": true,
"Title must be less than 20 words.": true,
"Title must be unique.": true,
"Tagline must be less than 160 characters.": true,
"Requested funding amount must be an integer.": true,
"Organization willing to sponsor is required. Kindly provide a response in Yes or No.": true,
"Is it an existing OSS project is required. Kindly provide a response in Yes or No.": true,
"Author is required.": true,
"Author must be a user on REPOS.": true,
"Project description is required. Please provide a description in minimum 50 words.": true,
"Project description must be more than 50 words.": true,
"Project details are required. Please provide details in minimum 50 words.": true,
"Project details must be more than 50 words.": true,
"Project stages are required. Phase 1 and Phase 2 are mandatory.": true,
"Phase 1 must be more than 20 words.": true,
"Phase 2 must be more than 20 words.": true,
"Title moderation passed.": true,
"Tagline moderation passed.": true,
"Project description moderation passed.": true,
"Project details & specification moderation passed.": true,
"Phase 1 moderation passed.": true,
"Phase 2 moderation passed.": true,
"Supporting information moderation passed.": true,
}
let proposalList = {};
const githubRepositoryUrl = GITHUB_REPOSITORY ? `https://github.com/${GITHUB_REPOSITORY}` : null;
const githubDefaultBranch = GITHUB_REF ? GITHUB_REF.split('/').pop() : null;
const latestCommitId = GITHUB_SHA || null;
const bypassProcess = GITHUB_REPOSITORY !== "openteamsinc/repos-proposal-publisher";
async function checkTitle(title) {
const response = await axios.get(`${API_URI}check_title?title=${title}`);
return response.status === 200;
}
async function checkUsername(username) {
const response = await axios.get(`${API_URI}check_username?username=${username.replace(/^@/, '')}`);
return response.status === 200;
}
async function checkProposalOnRepos(pid) {
const response = await axios.get(`${API_URI}check_proposal?pid=${pid}`);
if (response.status === 200) {
return response.data.proposal[0];
}
return null;
}
function readProposalFolder() {
const proposalPath = path.join(process.env.GITHUB_WORKSPACE, 'proposals');
const allFiles = [];
if (fs.existsSync(proposalPath) && fs.lstatSync(proposalPath).isDirectory()) {
const files = fs.readdirSync(proposalPath);
for (const file of files) {
const filePath = path.join(proposalPath, file);
allFiles.push(filePath);
}
} else {
console.log("Proposals folder does not exist.");
}
return allFiles;
}
function parseYamlMetadata(content) {
const parts = content.split('---');
if (parts.length > 1) {
const yamlContent = parts[1];
const metadata = yaml.load(yamlContent);
return metadata;
}
return null;
}
function fetchSections(content) {
content = content.replace(/<!--.*?-->/gs, '');
const projectDescriptionPattern = /## Project Description\n(.*?)\n## Project Details & Specifications/s;
const projectDetailsPattern = /## Project Details & Specifications\n(.*?)\n## Project Stages/s;
const projectStagesPattern = /## Project Stages\n(.*?)\n## Supporting Information/s;
const supportingInfoPattern = /## Supporting Information\n(.*?)$/s;
const projectDescription = content.match(projectDescriptionPattern);
const projectDetails = content.match(projectDetailsPattern);
const projectStages = content.match(projectStagesPattern);
const supportingInfo = content.match(supportingInfoPattern);
const phasesPattern = /### (Phase \d+)\n(.*?)\n(?=### Phase \d+|$)/gs;
const phasesMatches = projectStages ? [...projectStages[1].matchAll(phasesPattern)] : [];
const phases = Object.fromEntries(phasesMatches.map(match => [match[1], match[2].trim()]));
return {
project_description: projectDescription ? projectDescription[1].trim() : '',
project_details: projectDetails ? projectDetails[1].trim() : '',
project_stages: phases,
supporting_info: supportingInfo ? supportingInfo[1].trim() : '',
};
}
async function validateProposal(oldProposalData, pid, title, tagline, requestedFundingAmount, organizationWillingToSponsor, existingOssProject, author, description, details, projectStages, extraInformation) {
console.log("Performing validations on the proposal.....");
console.log("Validating Title.....");
if (bypassProcess) {
if (title) {
console.log("Title is present.");
if (pid && oldProposalData && oldProposalData.title === title) {
checklist["Title must be unique."] = true;
} else {
checklist["Title must be unique."] = await checkTitle(title);
if (title.split(' ').length > 20) {
checklist["Title must be less than 20 words."] = false;
}
}
} else {
console.log("Title is not present.");
checklist["Title is required."] = false;
checklist["Title must be unique."] = false;
checklist["Title must be less than 20 words."] = false;
checklist["Title moderation passed."] = false;
}
}
if (tagline) {
console.log("Validating Tagline.....");
console.log("Tagline is present.");
if (tagline.length > 160) {
checklist["Tagline must be less than 160 characters."] = false;
}
} else {
console.log("Tagline is not present.");
delete checklist["Tagline must be less than 160 characters."];
delete checklist["Tagline moderation passed."];
}
if (requestedFundingAmount && isNaN(requestedFundingAmount)) {
checklist["Requested funding amount must be an integer."] = false;
}
if (!["Yes", "No"].includes(organizationWillingToSponsor)) {
checklist["Organization willing to sponsor is required. Kindly provide a response in Yes or No."] = false;
}
if (!["Yes", "No"].includes(existingOssProject)) {
checklist["Is it an existing OSS project is required. Kindly provide a response in Yes or No."] = false;
}
console.log("Validating Author.....");
if (bypassProcess) {
if (author) {
checklist["Author must be a user on REPOS."] = await checkUsername(author);
} else {
checklist["Author is required."] = false;
checklist["Author must be a user on REPOS."] = false;
}
}
if (!description) {
checklist["Project description is required. Please provide a description in minimum 50 words."] = false;
checklist["Project description must be more than 50 words."] = false;
checklist["Project description moderation passed."] = false;
}
if (description.split(' ').length < 50) {
checklist["Project description must be more than 50 words."] = false;
}
if (!details) {
checklist["Project details & specification are required. Please provide details in minimum 50 words."] = false;
checklist["Project details & specification must be more than 50 words."] = false;
checklist["Project details & specification moderation passed."] = false;
}
if (details.split(' ').length < 50) {
checklist["Project details must be more than 50 words."] = false;
}
if (!("Phase 1" in projectStages) && !("Phase 2" in projectStages)) {
checklist["Project stages are required. Phase 1 and Phase 2 are mandatory."] = false;
checklist["Phase 1 must be more than 20 words."] = false;
checklist["Phase 2 must be more than 20 words."] = false;
checklist["Phase 1 moderation passed."] = false;
checklist["Phase 2 moderation passed."] = false;
} else {
if (projectStages["Phase 1"] && projectStages["Phase 1"].split(' ').length < 20) {
checklist["Phase 1 must be more than 20 words."] = false;
}
if (projectStages["Phase 2"] && projectStages["Phase 2"].split(' ').length < 20) {
checklist["Phase 2 must be more than 20 words."] = false;
}
}
for (const [phase, phaseDescription] of Object.entries(projectStages)) {
if (!["Phase 1", "Phase 2"].includes(phase) && phaseDescription.split(' ').length < 20) {
checklist[`${phase} must be more than 20 words.`] = false;
}
}
if (!extraInformation) {
delete checklist["Supporting information moderation passed."];
}
}
async function moderationApiRequest(text) {
const response = await axios.post(`${API_URI}check_moderation/`, { text });
return response.status === 200;
}
async function moderateText(oldProposalData, pid, title, tagline, description, details, projectStages, extraInformation) {
if (pid && oldProposalData && oldProposalData.title !== title) {
checklist["Title moderation passed."] = await moderationApiRequest(title);
} else {
if (!await moderationApiRequest(title)) {
checklist["Title moderation passed."] = false;
}
}
if (!await moderationApiRequest(tagline)) {
checklist["Tagline moderation passed."] = false;
}
if (!await moderationApiRequest(description)) {
checklist["Project description moderation passed."] = false;
}
if (!await moderationApiRequest(details)) {
checklist["Project details & specification moderation passed."] = false;
}
for (const [phaseKey, phaseContent] of Object.entries(projectStages)) {
if (!await moderationApiRequest(phaseContent)) {
checklist[`${phaseKey} moderation passed.`] = false;
} else {
checklist[`${phaseKey} moderation passed.`] = true;
}
}
if (!await moderationApiRequest(extraInformation)) {
checklist["Supporting information moderation passed."] = false;
}
}
async function main() {
console.log("=".repeat(100));
console.log("Reading proposals from the folder.....");
const proposalFiles = readProposalFolder();
console.log("Found proposals in the folder: ");
console.log(proposalFiles.join('\n'));
for (const proposalPath of proposalFiles) {
let pid = null;
let title = null;
let tagline = null;
let requestedFundingAmount = null;
let skills = null;
let organizationWillingToSponsor = null;
let existingOssProject = null;
let author = null;
let description = null;
let details = null;
let projectStages = {};
let extraInformation = null;
let oldProposalData = null;
console.log("Reading proposal file: ", path.basename(proposalPath));
const content = fs.readFileSync(proposalPath, 'utf8');
const metadata = parseYamlMetadata(content);
const sections = fetchSections(content);
console.log("================== Received Contents ==================");
pid = metadata?.["Proposal ID"] || null;
title = metadata?.["Proposal Title"] || null;
tagline = metadata?.["Tagline"] || null;
requestedFundingAmount = metadata?.["Requested Funding Amount"] || null;
skills = metadata?.["Skills"] || null;
organizationWillingToSponsor = metadata?.["Is your organization willing to sponsor this project?"] || null;
existingOssProject = metadata?.["Is this an existing OSS project?"] || null;
author = metadata?.["Author"] || null;
description = sections["project_description"];
details = sections["project_details"];
projectStages = sections["project_stages"];
extraInformation = sections["supporting_info"];
console.log("Title: ", title);
console.log("Tagline: ", tagline);
console.log("Requested Funding Amount: ", requestedFundingAmount);
console.log("Skills: ", skills);
console.log("Organization Willing to Sponsor: ", organizationWillingToSponsor);
console.log("Existing OSS Project: ", existingOssProject);
console.log("Author: ", author);
console.log("Project Description:", description);
console.log("Project Details & Specifications:", details);
console.log("Project Stages:", projectStages);
console.log("Supporting Information:", extraInformation);
console.log("GitHub Repository URL:", githubRepositoryUrl);
console.log("GitHub Default Branch:", githubDefaultBranch);
console.log("Latest Commit ID:", latestCommitId);
if (pid) {
console.log("Proposal ID: ", pid);
oldProposalData = await checkProposalOnRepos(pid);
}
await validateProposal(
oldProposalData,
pid,
title,
tagline,
requestedFundingAmount,
organizationWillingToSponsor,
existingOssProject,
author,
description,
details,
projectStages,
extraInformation,
);
await moderateText(
oldProposalData,
pid,
title,
tagline,
description,
details,
projectStages,
extraInformation,
);
console.log("=".repeat(100));
console.log("Validations & Moderations Checks Results:");
console.log("=".repeat(100));
console.log(`${"Check".padEnd(85)} | Result`);
console.log("-".repeat(100));
for (const [key, value] of Object.entries(checklist)) {
const result = value ? "Passed" : "Failed";
console.log(`${key.padEnd(85)} | ${result}`);
}
console.log("=".repeat(100));
let payload = {
title,
tagline,
funds_requested: requestedFundingAmount,
skills,
organization_willing_to_sponsor: organizationWillingToSponsor === "Yes",
existing_oss_project: existingOssProject === "Yes",
author: author?.split('@').pop(),
description,
details,
project_stages: projectStages,
extra_information: extraInformation,
github_url: githubRepositoryUrl,
commit_id: latestCommitId,
};
if (pid) {
payload["proposal_id"] = pid;
}
proposalList[path.basename(proposalPath)] = {
payload,
checklist,
path: proposalPath,
};
}
for (const [filename, proposalData] of Object.entries(proposalList)) {
if (!Object.values(proposalData.checklist).every(Boolean)) {
console.log(`Skipping proposal for ${filename} due to failed checks.`);
continue;
}
console.log(`Submitting proposal for ${filename}`);
if (bypassProcess) {
const response = await axios.post(`${API_URI}submit_proposal/`, proposalData.payload);
if (response.status === 200) {
console.log(response.data.message);
if (!proposalData.payload.proposal_id && response.data.proposal_id) {
const content = fs.readFileSync(proposalData.path, 'utf8');
const lines = content.split('\n');
const proposalIdLine = `Proposal ID: "${response.data.proposal_id}"`;
lines.splice(1, 0, proposalIdLine);
fs.writeFileSync(proposalData.path, lines.join('\n'), 'utf8');
// Commit the changes back to the repository
const octokit = new Octokit({ auth: GH_TOKEN });
const [owner, repo] = GITHUB_REPOSITORY.split('/');
const branch = GITHUB_REF.split('/').pop();
const relativePath = path.relative(process.env.GITHUB_WORKSPACE, proposalData.path);
const { data: { sha } } = await octokit.repos.getContent({
owner,
repo,
path: relativePath,
ref: branch,
});
await octokit.repos.createOrUpdateFileContents({
owner,
repo,
path: relativePath,
message: 'Update proposal with proposal ID',
content: Buffer.from(lines.join('\n')).toString('base64'),
sha,
branch,
});
console.log(`Proposal file ${filename} updated and committed successfully.`);
}
}
else {
console.log("Failed to submit proposal for ", filename);
}
}
}
console.log("=".repeat(100));
console.log("Completed processing proposals.");
}
main().catch(error => {
core.setFailed(error.message);
});