Skip to content

Commit

Permalink
chore: simplify code by removing parallelism (#240)
Browse files Browse the repository at this point in the history
This should make debugging any issue easier because the logs are less delayed.

Changes:
- simplify logger to log everything immediately
- handle each target repo sequentially instead of parallel
  • Loading branch information
adrianjost committed Aug 2, 2023
1 parent 840c1ac commit d796e58
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 122 deletions.
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions src/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const fs = require("fs");

const core = require("@actions/core");

const logger = require("./log")();
const log = require("./log");

const parseMultilineInput = (multilineInput) => {
return multilineInput.split("\n").map((e) => e.trim());
Expand Down Expand Up @@ -54,10 +54,10 @@ const context = {

while (fs.existsSync(context.TMPDIR)) {
context.TMPDIR = `tmp-${Date.now().toString()}`;
logger.info(`TEMP_DIR already exists. Using "${context.TMPDIR}" now.`);
log.info(`TEMP_DIR already exists. Using "${context.TMPDIR}" now.`);
}

logger.info("Context:", {
log.info("Context:", {
...context,
GITHUB_TOKEN: "<secret>",
});
Expand Down
24 changes: 11 additions & 13 deletions src/git.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const {
} = require("./context");

const utils = require("./utils");
const getLogger = require("./log");
const log = require("./log");

const interpolateCommitMessage = (message, data) => {
let newMessage = message;
Expand All @@ -30,18 +30,17 @@ module.exports = {
const { getRepoPath, getRepoSlug, getRepoBranch } =
utils.init(repoSlugAndBranch);

const logger = getLogger(repoSlugAndBranch);

function execCmd(command, workingDir) {
logger.info(`EXEC: "${command}" IN "${workingDir || "./"}"`);
log.info(`EXEC: "${command}" IN "${workingDir || "./"}"`);
return new Promise((resolve, reject) => {
exec(
command,
{
cwd: workingDir,
},
function (error, stdout) {
logger.info(`OUTPUT: "${error}${stdout}"`);
log.info(`OUTPUT ERROR: ${error ? `"${error}"` : ""}`);
log.info(`OUTPUT STDOUT: ${stdout ? `"${stdout}"` : ""}}`);
error ? reject(error) : resolve(stdout.trim());
}
);
Expand Down Expand Up @@ -70,11 +69,11 @@ module.exports = {

const commitAll = async () => {
if (!(await hasChanges())) {
logger.info("NO CHANGES DETECTED");
log.info("NO CHANGES DETECTED");
return;
}
logger.info("CHANGES DETECTED");
logger.info("COMMIT CHANGES...");
log.info("CHANGES DETECTED");
log.info("COMMIT CHANGES...");
const commitMessage = interpolateCommitMessage(COMMIT_MESSAGE, {
SRC_REPO: SRC_REPO,
TARGET_REPO: repoSlugAndBranch,
Expand All @@ -84,8 +83,7 @@ module.exports = {
const commands = [
`git config --local user.name "${GIT_USERNAME}"`,
`git config --local user.email "${GIT_EMAIL}"`,
// `git update-index --really-refresh`,
`git add -A`,
`git add --all`,
`git status`,
`git commit --message "${commitMessage}"`,
`git push`,
Expand All @@ -95,15 +93,15 @@ module.exports = {
output += await execCmd(cmd, getRepoPath());
}
} catch (error) {
logger.error(error);
log.error(error);
} finally {
if (!output.includes("Update file(s) from")) {
logger.error(output);
log.error(output);
throw new Error("failed to commit changes");
}
}
}
logger.info("CHANGES COMMITED");
log.info("CHANGES COMMITED");
};

return {
Expand Down
62 changes: 25 additions & 37 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ const { SRC_REPO, TARGET_REPOS, TMPDIR, SKIP_CLEANUP } = require("./context");
const git = require("./git");
const { removeDir } = require("./utils");

const getLogger = require("./log");
const showLogs = getLogger().print;
const utils = require("./utils");

const main = async () => {
Expand All @@ -20,51 +18,41 @@ const main = async () => {
const relativeSrcFiles = srcFiles.map((file) =>
utilsSrc.getRepoRelativeFilePath(file)
);
showLogs(SRC_REPO);

await new Promise((resolve) => setTimeout(resolve, 5000));

// EXEC IN TARGET REPOS
await Promise.all(
TARGET_REPOS.map(async (repo) => {
const utilsRepo = utils.init(repo);
const gitRepo = git.init(repo);
for (const repo of TARGET_REPOS) {
const utilsRepo = utils.init(repo);
const gitRepo = git.init(repo);

// PREPARE TARGET
await gitRepo.clone();
const targetFiles = await utilsRepo.getFiles();
const removedFiles = targetFiles.filter(
(file) =>
!relativeSrcFiles.includes(utilsRepo.getRepoRelativeFilePath(file))
);
// PREPARE TARGET
await gitRepo.clone();
const targetFiles = await utilsRepo.getFiles();
const removedFiles = targetFiles.filter(
(file) =>
!relativeSrcFiles.includes(utilsRepo.getRepoRelativeFilePath(file))
);

// UPDATE FILES
await Promise.all([
utilsRepo.removeFiles(removedFiles),
...srcFiles.map(async (srcFile) =>
utilsRepo.copyFile(
srcFile,
path.join(
utilsRepo.getRepoFilePath(),
utilsSrc.getRepoRelativeFilePath(srcFile)
)
// UPDATE FILES
await Promise.all([
utilsRepo.removeFiles(removedFiles),
...srcFiles.map(async (srcFile) =>
utilsRepo.copyFile(
srcFile,
path.join(
utilsRepo.getRepoFilePath(),
utilsSrc.getRepoRelativeFilePath(srcFile)
)
),
]);

// COMMIT UPDATES
await gitRepo.commitAll();
)
),
]);

showLogs(repo);
})
);
// COMMIT UPDATES
await gitRepo.commitAll();
}
} catch (err) {
error = err;
}

// Output all Logs
showLogs();

if (!SKIP_CLEANUP) {
await removeDir(TMPDIR);
}
Expand Down
72 changes: 17 additions & 55 deletions src/log.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,62 +10,24 @@ const joinAttributes = (...attrs) =>
)
.join(" ");

const logs = {};

module.exports = (id) => {
logs[id] = [];

const debug = (...attrs) => {
logs[id].push(["debug", joinAttributes(...attrs)]);
if (!id) {
print();
}
};

const error = (...attrs) => {
logs[id].push(["error", joinAttributes(...attrs)]);
core.setFailed(`Action failed with error ${joinAttributes(...attrs)}`);
if (!id) {
print();
}
};

const info = (...attrs) => {
logs[id].push(["info", joinAttributes(...attrs)]);
if (!id) {
print();
}
};

const warn = (...attrs) => {
logs[id].push(["warning", joinAttributes(...attrs)]);
if (!id) {
print();
}
};
const debug = (...attrs) => {
core.debug(joinAttributes(...attrs));
};

const _print = (id) => {
logs[id].forEach(([type, content]) => {
core[type](id !== "undefined" ? `${id}: ${content}` : content);
});
logs[id] = [];
};
const error = (...attrs) => {
core.error(joinAttributes(...attrs));
};

const print = (_id = id) => {
if (!_id) {
Object.keys(logs).forEach((key) => {
_print(key);
});
} else {
_print(_id);
}
};
const info = (...attrs) => {
core.info(joinAttributes(...attrs));
};

return {
debug,
error,
info,
print,
warn,
};
const warn = (...attrs) => {
core.warning(joinAttributes(...attrs));
};
module.exports = {
debug,
error,
info,
warn,
};
21 changes: 8 additions & 13 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,9 @@ const {
SRC_ROOT,
TARGET_ROOT,
} = require("./context");
const getLogger = require("./log");
const log = require("./log");

const init = (repoFullname) => {
const logger = getLogger(repoFullname);

const getRepoSlug = () => {
return repoFullname.split(":")[0];
};
Expand Down Expand Up @@ -51,7 +49,7 @@ const init = (repoFullname) => {
.replace(new RegExp(`^${TMPDIR}/${repoFullname}${getRepoRoot()}`), "");

const getMatchingFiles = (files) => {
logger.info(
log.info(
"FILE_PATTERNS",
FILE_PATTERNS.map((a) => a.toString())
);
Expand All @@ -64,12 +62,9 @@ const init = (repoFullname) => {

const getFiles = async () => {
const files = await listDir(getRepoPath(), [".git"]);
logger.debug(
"FILES:",
JSON.stringify(files.map(getPrettyPath), undefined, 2)
);
log.debug("FILES:", JSON.stringify(files.map(getPrettyPath), undefined, 2));
const matchingFiles = getMatchingFiles(files);
logger.info(
log.info(
"MATCHING FILES:",
JSON.stringify(matchingFiles.map(getPrettyPath), undefined, 2)
);
Expand All @@ -82,7 +77,7 @@ const init = (repoFullname) => {
.then(() => true)
.catch(() => false);
if (SKIP_REPLACE && targetExists) {
logger.info(
log.info(
"skip copying",
from.replace(/\\/g, "/").replace(/^\//, ""),
"to",
Expand All @@ -91,7 +86,7 @@ const init = (repoFullname) => {
);
return;
}
logger.info(
log.info(
"copy",
from.replace(/\\/g, "/").replace(/^\//, ""),
"to",
Expand All @@ -106,13 +101,13 @@ const init = (repoFullname) => {

const removeFiles = async (filePaths) => {
if (SKIP_DELETE) {
logger.info(
log.info(
"SKIP REMOVING FILES because `SKIP_DELETE` is set to `true`",
filePaths.map((f) => `"${f}"`).join(", ")
);
return;
}
logger.info("REMOVE FILES", filePaths);
log.info("REMOVE FILES", filePaths);
if (DRY_RUN) {
return;
}
Expand Down

0 comments on commit d796e58

Please sign in to comment.