-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
91 lines (81 loc) · 2.92 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
const core = require('@actions/core');
const fs = require('fs');
const path = require('path');
const { DateTime } = require('luxon');
function parseDate(rawDate) {
// ISO timestamp
console.log('Checking for ISO timestamp...');
var date = DateTime.fromISO(rawDate);
if (date.isValid) return date;
// Date object
console.log('Checking for date dictionary...');
const dateDict = parseDateObject(rawDate);
date = DateTime.fromObject(dateDict[0], dateDict[1]);
if (!date.isValid) core.setFailed(`No valid date format provided. Exiting...`);
return date;
}
function parseDateObject(dateDict) {
var splitDict = dateDict.replace(/\s/g, '').split(',');
var d = {};
var opts = {};
for (let i = 0; i < splitDict.length; i++) {
const pair = splitDict[i].split(':');
if (pair.length != 2) {
core.setFailed(`Failed to parse ${splitDict[i]}. Exiting...`);
return;
}
isNaN(parseInt(pair[1])) ? opts[pair[0].toLowerCase()] = pair[1] : d[pair[0].toLowerCase()] = Number(pair[1]);
}
return [d, opts];
}
function checkDate(rawDueDate) {
const currDate = DateTime.now();
console.log(`Current time: ${currDate.toString()}`)
var dueDate = parseDate(rawDueDate);
console.log(`Due date: ${dueDate.toString()}`)
if (currDate > dueDate) {
core.setFailed(`Workflow run at time ${currDate.toString()} is after specified due date ${dueDate.toString()}`);
}
}
function checkRequiredFiles(rawFileList) {
const fileList = rawFileList.split(',');
for (f in fileList) {
if (!fs.existsSync(fileList[f].trim())) {
core.setFailed(`Required file ${fileList[f]} does not exist in this repository.`);
}
}
}
function checkReferenceFiles(rawRefList) {
const refList = rawRefList.split(',');
var refPairs = [];
for (r in refList) {
refPairs.push(refList[r].split(':'));
}
for (p in refPairs) {
referenceRecursive(refPairs[p][0].trim(), refPairs[p][1].trim());
}
}
function referenceRecursive(src, ref) {
if (!fs.existsSync(ref)) {
core.setFailed(`File ${ref} does not exist locally and cannot be referenced. Exiting...`)
}
var isDirectory = fs.statSync(src).isDirectory();
if (isDirectory) {
fs.readdirSync(src).forEach((child) => {
referenceRecursive(path.join(src, child),
path.join(ref, child));
});
} else {
let srcFile = fs.readFileSync(src, 'utf8');
let refFile = fs.readFileSync(ref, 'utf8');
if (srcFile != refFile) {
core.setFailed(`Contents of files ${src} and ${ref} are not equal. Exiting...`);
}
}
}
const dueDate = core.getInput('due');
if (dueDate) checkDate(dueDate);
const requiredFiles = core.getInput('required_files');
if (requiredFiles) checkRequiredFiles(requiredFiles);
const referenceFiles = core.getInput('reference_files');
if (referenceFiles) checkReferenceFiles(referenceFiles);