mirror of
https://github.com/tiennm99/is-a-dev.git
synced 2026-10-18 20:14:50 +00:00
optimise?
This commit is contained in:
1 parent
2434b8b429
commit
69eba1e0ad
1 file changed
+52
-29
+52
-29
@@ -3,51 +3,74 @@ const fs = require("fs-extra");
|
|||||||
const path = require("path");
|
const path = require("path");
|
||||||
|
|
||||||
const PR_AUTHOR = process.env.PR_AUTHOR;
|
const PR_AUTHOR = process.env.PR_AUTHOR;
|
||||||
const MODIFIED_FILES = process.env.MODIFIED_FILES.split(" ");
|
const MODIFIED_FILES = (process.env.MODIFIED_FILES || "").split(" ");
|
||||||
|
const EVENT = process.env.EVENT;
|
||||||
|
const RUN_ID = process.env.RUN_ID;
|
||||||
|
|
||||||
const domainsPath = path.resolve("domains");
|
const domainsPath = path.resolve("domains");
|
||||||
const headDomainsPath = path.resolve(`register-${process.env.RUN_ID}/domains`);
|
const headDomainsPath = path.resolve(`register-${RUN_ID}/domains`);
|
||||||
|
|
||||||
const admins = require("../util/administrators.json");
|
const admins = require("../util/administrators.json");
|
||||||
|
|
||||||
t("Modified JSON files must be owned by the PR author", (t) => {
|
async function getFileContent(basePath, fileName) {
|
||||||
if(process.env.EVENT !== "pull_request") {
|
try {
|
||||||
|
return await fs.readJson(path.join(basePath, fileName));
|
||||||
|
} catch (err) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
t("Modified JSON files must be owned by the PR author", async (t) => {
|
||||||
|
if (EVENT !== "pull_request") {
|
||||||
t.pass();
|
t.pass();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
MODIFIED_FILES.forEach((file) => {
|
await Promise.all(
|
||||||
const modifiedDomain = fs.readJsonSync(path.join(domainsPath, file.substring(file.lastIndexOf("/") + 1)));
|
MODIFIED_FILES.map(async (file) => {
|
||||||
let currentDomain = null;
|
const fileName = file.substring(file.lastIndexOf("/") + 1);
|
||||||
|
const modifiedDomain = await getFileContent(domainsPath, fileName);
|
||||||
|
const currentDomain = (await getFileContent(headDomainsPath, fileName)) || modifiedDomain;
|
||||||
|
|
||||||
try {
|
if (!modifiedDomain || !currentDomain) {
|
||||||
currentDomain = fs.readJsonSync(path.join(headDomainsPath, file.substring(file.lastIndexOf("/") + 1)));
|
t.fail(`${file}: Unable to read domain data`);
|
||||||
} catch {
|
return;
|
||||||
currentDomain = modifiedDomain;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
t.true(
|
t.true(
|
||||||
currentDomain.owner.username === PR_AUTHOR || admins.includes(PR_AUTHOR),
|
currentDomain.owner.username === PR_AUTHOR || admins.includes(PR_AUTHOR),
|
||||||
`${file}: Domain owner is ${domain.owner.username} but ${PR_AUTHOR} is the PR author`
|
`${file}: Domain owner is ${currentDomain.owner.username} but ${PR_AUTHOR} is the PR author`
|
||||||
);
|
);
|
||||||
});
|
})
|
||||||
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
t("New JSON files must be owned by the PR author", async (t) => {
|
||||||
t("New JSON files must be owned by the PR author", (t) => {
|
if (EVENT !== "pull_request") {
|
||||||
if(process.env.EVENT !== "pull_request") {
|
|
||||||
t.pass();
|
t.pass();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const newFiles = fs.readdirSync(domainsPath).filter((file) => !fs.readdirSync(headDomainsPath).includes(file.substring(file.lastIndexOf("/") + 1)));
|
const domainsFiles = await fs.readdir(domainsPath);
|
||||||
|
const headDomainsFiles = await fs.readdir(headDomainsPath).catch(() => []);
|
||||||
|
|
||||||
newFiles.forEach((file) => {
|
const newFiles = domainsFiles.filter(
|
||||||
const domain = fs.readJsonSync(path.join(domainsPath, file.substring(file.lastIndexOf("/") + 1)));
|
(file) => !headDomainsFiles.includes(file.substring(file.lastIndexOf("/") + 1))
|
||||||
|
);
|
||||||
|
|
||||||
t.true(
|
await Promise.all(
|
||||||
domain.owner.username === PR_AUTHOR || admins.includes(PR_AUTHOR),
|
newFiles.map(async (file) => {
|
||||||
`${file}: Domain owner is ${domain.owner.username} but ${PR_AUTHOR} is the PR author`
|
const domain = await getFileContent(domainsPath, file);
|
||||||
);
|
|
||||||
});
|
if (!domain) {
|
||||||
})
|
t.fail(`${file}: Unable to read domain data`);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
t.true(
|
||||||
|
domain.owner.username === PR_AUTHOR || admins.includes(PR_AUTHOR),
|
||||||
|
`${file}: Domain owner is ${domain.owner.username} but ${PR_AUTHOR} is the PR author`
|
||||||
|
);
|
||||||
|
})
|
||||||
|
);
|
||||||
|
});
|
||||||
Reference in new issue
Block a user