Skip to content

Commit

Permalink
feat: handling of artifacts:untracked
Browse files Browse the repository at this point in the history
  • Loading branch information
maxime.dubourg committed Mar 2, 2021
1 parent cab8495 commit 53623fd
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 6 deletions.
4 changes: 2 additions & 2 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ pre:
- touch foo.txt
- mkdir -p bar && touch bar/bar.txt
artifacts:
untracked: true
paths:
- foo.txt
- bar
- package.json

pre-test:
stage: prepare
Expand Down
26 changes: 23 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@ const osExec = promisify(require("child_process").exec);
const merge = require("lodash.merge");

const define = require("./src/pre-defined");
const { getValidUrl, mkdirpRecSync, drawPipeline } = require("./src/utils");
const {
getValidUrl,
mkdirpRecSync,
readdirRecSync,
drawPipeline,
} = require("./src/utils");
const {
ARGV,
ONLY_JOBS,
Expand Down Expand Up @@ -486,8 +491,8 @@ async function main() {
if ("artifacts" in job) {
ARTIFACTS[name] = {};

if (job.artifacts.paths) {
for (const file of job.artifacts.paths) {
const copyFiles = (files) => {
for (const file of files) {
const fileAbs = path.join(PROJECT_FILES_TEMP_DIR, file);
const targetAbs = path.join(
GLCI_ARTIFACTS_DIR,
Expand All @@ -501,6 +506,21 @@ async function main() {

ARTIFACTS[name][file] = targetAbs;
}
};

if (job.artifacts.untracked === true) {
const untracked = readdirRecSync(
PROJECT_FILES_TEMP_DIR,
[],
"",
projectFiles
).filter((file) => !projectFiles.includes(file));

copyFiles(untracked);
}

if (job.artifacts.paths) {
copyFiles(job.artifacts.paths);
}
}

Expand Down
39 changes: 38 additions & 1 deletion src/utils.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
const fs = require("fs-extra");
const path = require("path");
const chalk = require("chalk");

const { RESERVED_JOB_NAMES } = require("./constants");
const { RESERVED_JOB_NAMES, DEFAULT_STAGES } = require("./constants");

/**
* given an url, returns the cleaned url with protocol
*/
function getValidUrl(url) {
const newUrl = decodeURIComponent(url).trim().replace(/\s/g, "");

Expand All @@ -17,12 +21,44 @@ function getValidUrl(url) {
return newUrl;
}

/**
* recursive mkdir -p
*/
function mkdirpRecSync(dir) {
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, { recursive: true });
}
}

/**
* recursively list a directory files
* if projectFiles is given, not going rec on a directory that isn't in it
*/
function readdirRecSync(dir, files, parent, projectFiles = []) {
const currentDirFiles = fs.readdirSync(dir, {
withFileTypes: true,
});

for (const file of currentDirFiles) {
const fileRel = path.join(parent, file.name);

if (
!file.isDirectory() ||
(!projectFiles.includes(fileRel) &&
!projectFiles.some((name) => path.dirname(name) === fileRel))
) {
files.push(fileRel);
} else {
readdirRecSync(fileRel, files, fileRel, projectFiles);
}
}

return files;
}

/**
* logs a representation of a pipeline jobs
*/
function drawPipeline(ci, onlyJobs) {
// [[ "test", [ "test:unit", "test:e2e" ] ], [ "build", "build:staging" ]]
const rows = [];
Expand Down Expand Up @@ -112,5 +148,6 @@ function drawPipeline(ci, onlyJobs) {
module.exports = {
getValidUrl,
mkdirpRecSync,
readdirRecSync,
drawPipeline,
};

0 comments on commit 53623fd

Please sign in to comment.