This commit is contained in:
Edwin Sirko
2024-01-26 21:07:36 -05:00
parent 7b797db603
commit 1219afee65
2 changed files with 98 additions and 59 deletions
+61 -28
View File
@@ -10,7 +10,7 @@ describe('getConsolidatedDirectory', () => {
let sourceDir: string let sourceDir: string
beforeAll(() => { beforeAll(() => {
sourceDir = `.`// fsHelper.createTempDir() sourceDir = `.` // fsHelper.createTempDir()
fs.mkdirSync(`${sourceDir}/folder1`) fs.mkdirSync(`${sourceDir}/folder1`)
fs.mkdirSync(`${sourceDir}/folder2`) fs.mkdirSync(`${sourceDir}/folder2`)
fs.mkdirSync(`${sourceDir}/folder2/folder3`) fs.mkdirSync(`${sourceDir}/folder2/folder3`)
@@ -20,11 +20,9 @@ describe('getConsolidatedDirectory', () => {
fs.writeFileSync(`${sourceDir}/folder2/folder3/file3.txt`, fileContent) fs.writeFileSync(`${sourceDir}/folder2/folder3/file3.txt`, fileContent)
}) })
beforeEach(() => { beforeEach(() => {})
})
afterEach(() => { afterEach(() => {})
})
afterAll(() => { afterAll(() => {
fs.rmSync(`file0.txt`) fs.rmSync(`file0.txt`)
@@ -32,54 +30,89 @@ describe('getConsolidatedDirectory', () => {
fs.rmSync(`folder2`, { recursive: true }) fs.rmSync(`folder2`, { recursive: true })
}) })
it('returns the directory itself if it is a single directory, and instructed to not clean it up', () => {
it("returns the directory itself if it is a single directory, and instructed to not clean it up", () => {
// TODO: In these tests, we're not really distinguishing between the `publish-action-package` directory and the consumer repo directory, i.e., they share the same space. // TODO: In these tests, we're not really distinguishing between the `publish-action-package` directory and the consumer repo directory, i.e., they share the same space.
// In real life, when the consumer workflow runs, its own javascript is in ., but // In real life, when the consumer workflow runs, its own javascript is in ., but
// the publish-action-package's code is in ${{github.action_path}}. // the publish-action-package's code is in ${{github.action_path}}.
// So.... I guess to emulate this, we should create a temp directory (representing the consumer repo) // So.... I guess to emulate this, we should create a temp directory (representing the consumer repo)
// and cd there before the test starts? // and cd there before the test starts?
const { consolidatedPath, needToCleanUpDir } = fsHelper.getConsolidatedDirectory(".") const { consolidatedPath, needToCleanUpDir } =
fsHelper.getConsolidatedDirectory('.')
expect(needToCleanUpDir).toBe(false) expect(needToCleanUpDir).toBe(false)
expect(consolidatedPath).toBe(".") expect(consolidatedPath).toBe('.')
expect(fsHelper.readFileContents(`file0.txt`).toString()).toEqual(fileContent) expect(fsHelper.readFileContents(`file0.txt`).toString()).toEqual(
expect(fsHelper.readFileContents(`folder1/file1.txt`).toString()).toEqual(fileContent) fileContent
expect(fsHelper.readFileContents(`folder2/file2.txt`).toString()).toEqual(fileContent) )
expect(fsHelper.readFileContents(`folder2/folder3/file3.txt`).toString()).toEqual(fileContent) expect(fsHelper.readFileContents(`folder1/file1.txt`).toString()).toEqual(
fileContent
)
expect(fsHelper.readFileContents(`folder2/file2.txt`).toString()).toEqual(
fileContent
)
expect(
fsHelper.readFileContents(`folder2/folder3/file3.txt`).toString()
).toEqual(fileContent)
}) })
it('returns a new directory containing copies of the multiple paths if they are legally specified, and instruct to clean it up', () => { it('returns a new directory containing copies of the multiple paths if they are legally specified, and instruct to clean it up', () => {
const { consolidatedPath, needToCleanUpDir } = fsHelper.getConsolidatedDirectory("file0.txt folder1") const { consolidatedPath, needToCleanUpDir } =
fsHelper.getConsolidatedDirectory('file0.txt folder1')
expect(needToCleanUpDir).toBe(true) expect(needToCleanUpDir).toBe(true)
expect(consolidatedPath).not.toBe(".") expect(consolidatedPath).not.toBe('.')
expect(fsHelper.readFileContents(path.join(consolidatedPath, `file0.txt`)).toString()).toEqual(fileContent) expect(
expect(fsHelper.readFileContents(path.join(consolidatedPath, `folder1/file1.txt`)).toString()).toEqual(fileContent) fsHelper
expect(fs.existsSync(path.join(consolidatedPath, `folder2/file2.txt`))).toEqual(false) .readFileContents(path.join(consolidatedPath, `file0.txt`))
expect(fs.existsSync(path.join(consolidatedPath, `folder2/folder3/file3.txt`))).toEqual(false) .toString()
).toEqual(fileContent)
expect(
fsHelper
.readFileContents(path.join(consolidatedPath, `folder1/file1.txt`))
.toString()
).toEqual(fileContent)
expect(
fs.existsSync(path.join(consolidatedPath, `folder2/file2.txt`))
).toEqual(false)
expect(
fs.existsSync(path.join(consolidatedPath, `folder2/folder3/file3.txt`))
).toEqual(false)
}) })
it('what happens here?', () => { it('what happens here?', () => {
const { consolidatedPath, needToCleanUpDir } = fsHelper.getConsolidatedDirectory("folder1 folder2/folder3") const { consolidatedPath, needToCleanUpDir } =
fsHelper.getConsolidatedDirectory('folder1 folder2/folder3')
expect(needToCleanUpDir).toBe(true) expect(needToCleanUpDir).toBe(true)
expect(consolidatedPath).not.toBe(".") expect(consolidatedPath).not.toBe('.')
expect(fs.existsSync(path.join(consolidatedPath, `file0.txt`))).toEqual(false) expect(fs.existsSync(path.join(consolidatedPath, `file0.txt`))).toEqual(
expect(fsHelper.readFileContents(path.join(consolidatedPath, `folder1/file1.txt`)).toString()).toEqual(fileContent) false
expect(fs.existsSync(path.join(consolidatedPath, `folder2/file2.txt`))).toEqual(false) )
expect(fsHelper.readFileContents(path.join(consolidatedPath, `folder3/file3.txt`)).toString()).toEqual(fileContent) // <--- TODO: This is what I'm unsure of expect(
fsHelper
.readFileContents(path.join(consolidatedPath, `folder1/file1.txt`))
.toString()
).toEqual(fileContent)
expect(
fs.existsSync(path.join(consolidatedPath, `folder2/file2.txt`))
).toEqual(false)
expect(
fsHelper
.readFileContents(path.join(consolidatedPath, `folder3/file3.txt`))
.toString()
).toEqual(fileContent) // <--- TODO: This is what I'm unsure of
}) })
it('throws an error for illegal path spec - single', () => { it('throws an error for illegal path spec - single', () => {
expect(() => { expect(() => {
const { consolidatedPath, needToCleanUpDir } = fsHelper.getConsolidatedDirectory("folder4") const { consolidatedPath, needToCleanUpDir } =
fsHelper.getConsolidatedDirectory('folder4')
}).toThrow('filePath folder4 does not exist') }).toThrow('filePath folder4 does not exist')
}) })
it('throws an error for illegal path spec - multiple', () => { it('throws an error for illegal path spec - multiple', () => {
expect(() => { expect(() => {
const { consolidatedPath, needToCleanUpDir } = fsHelper.getConsolidatedDirectory("folder1 folder4") const { consolidatedPath, needToCleanUpDir } =
fsHelper.getConsolidatedDirectory('folder1 folder4')
}).toThrow('filePath folder4 does not exist') }).toThrow('filePath folder4 does not exist')
}) })
Generated Vendored
+35 -29
View File
@@ -74693,7 +74693,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod }; return (mod && mod.__esModule) ? mod : { "default": mod };
}; };
Object.defineProperty(exports, "__esModule", ({ value: true })); Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.bundleFilesintoDirectory = exports.readFileContents = exports.isActionRepo = exports.isDirectory = exports.createArchives = exports.removeDir = exports.createTempDir = void 0; exports.readFileContents = exports.isActionRepo = exports.isDirectory = exports.createArchives = exports.getConsolidatedDirectory = exports.removeDir = exports.createTempDir = void 0;
const fs = __importStar(__nccwpck_require__(57147)); const fs = __importStar(__nccwpck_require__(57147));
const fs_extra_1 = __importDefault(__nccwpck_require__(5630)); const fs_extra_1 = __importDefault(__nccwpck_require__(5630));
const path = __importStar(__nccwpck_require__(71017)); const path = __importStar(__nccwpck_require__(71017));
@@ -74716,6 +74716,24 @@ function removeDir(dir) {
} }
} }
exports.removeDir = removeDir; exports.removeDir = removeDir;
// TODO: rename this function, it is not state-preserving, so it shouldn't just be called "get'"
function getConsolidatedDirectory(filePathSpec) {
const paths = filePathSpec.split(' '); // TODO: handle files with spaces
// TODO: do check on paths to make sure they're valid and not reaching outside the space
let consolidatedPath = '';
let needToCleanUpDir = false;
if (paths.length === 1 && isDirectory(paths[0])) {
// If the path is a single directory, we can skip the bundling step
consolidatedPath = paths[0];
}
else {
// Otherwise, we need to bundle the files & folders into a temporary directory
consolidatedPath = bundleFilesintoDirectory(paths);
needToCleanUpDir = true;
}
return { consolidatedPath, needToCleanUpDir };
}
exports.getConsolidatedDirectory = getConsolidatedDirectory;
// Creates both a tar.gz and zip archive of the given directory and returns the paths to both archives (stored in the provided target directory) // Creates both a tar.gz and zip archive of the given directory and returns the paths to both archives (stored in the provided target directory)
// as well as the size/sha256 hash of each file. // as well as the size/sha256 hash of each file.
async function createArchives(distPath, archiveTargetPath = createTempDir()) { async function createArchives(distPath, archiveTargetPath = createTempDir()) {
@@ -74734,7 +74752,7 @@ async function createArchives(distPath, archiveTargetPath = createTempDir()) {
resolve(fileMetadata(zipPath)); resolve(fileMetadata(zipPath));
}); });
archive.pipe(output); archive.pipe(output);
archive.directory(distPath, false); archive.directory(distPath, false); // TODO: make sure this doesn't include dirs that start with ., same with below
archive.finalize(); archive.finalize();
}); });
const createTarPromise = new Promise((resolve, reject) => { const createTarPromise = new Promise((resolve, reject) => {
@@ -74773,23 +74791,23 @@ function readFileContents(filePath) {
return fs.readFileSync(filePath); return fs.readFileSync(filePath);
} }
exports.readFileContents = readFileContents; exports.readFileContents = readFileContents;
function bundleFilesintoDirectory(files, targetDir = createTempDir()) { function bundleFilesintoDirectory(filePaths) {
for (const file of files) { const targetDir = createTempDir();
if (!fs.existsSync(file)) { for (const filePath of filePaths) {
throw new Error(`File ${file} does not exist`); if (!fs.existsSync(filePath)) {
throw new Error(`filePath ${filePath} does not exist`);
} }
if (isDirectory(file)) { if (isDirectory(filePath)) {
const targetFolder = path.join(targetDir, path.basename(file)); const targetFolder = path.join(targetDir, path.basename(filePath)); // TODO: basename is probably not what we actually want here. Or is it? Maybe conflicts between dir1/dir2 and dir1/dir3/dir2 are just user error or ??
fs_extra_1.default.copySync(file, targetFolder); fs_extra_1.default.copySync(filePath, targetFolder); // TODO: ignore files preceded by .
} }
else { else {
const targetFile = path.join(targetDir, path.basename(file)); const targetFile = path.join(targetDir, path.basename(filePath));
fs.copyFileSync(file, targetFile); fs.copyFileSync(filePath, targetFile);
} }
} }
return targetDir; return targetDir;
} }
exports.bundleFilesintoDirectory = bundleFilesintoDirectory;
// Converts a file path to a filemetadata object by querying the fs for relevant metadata. // Converts a file path to a filemetadata object by querying the fs for relevant metadata.
async function fileMetadata(filePath) { async function fileMetadata(filePath) {
const stats = fs.statSync(filePath); const stats = fs.statSync(filePath);
@@ -74987,7 +75005,6 @@ Object.defineProperty(exports, "__esModule", ({ value: true }));
const main_1 = __nccwpck_require__(70399); const main_1 = __nccwpck_require__(70399);
const minimist_1 = __importDefault(__nccwpck_require__(35871)); const minimist_1 = __importDefault(__nccwpck_require__(35871));
const path = (0, minimist_1.default)(process.argv.slice(2)).path || '.'; const path = (0, minimist_1.default)(process.argv.slice(2)).path || '.';
console.log(path);
// eslint-disable-next-line @typescript-eslint/no-floating-promises // eslint-disable-next-line @typescript-eslint/no-floating-promises
(0, main_1.run)(path); (0, main_1.run)(path);
@@ -75061,29 +75078,18 @@ async function run(pathInput) {
return; return;
} }
const token = process.env.TOKEN; const token = process.env.TOKEN;
// Gather & validate user input const { consolidatedPath, needToCleanUpDir } = fsHelper.getConsolidatedDirectory(pathInput);
// Paths to be included in the OCI image if (needToCleanUpDir) {
// const paths: string[] = core.getInput('path').split(' ') tmpDirs.push(consolidatedPath);
const paths = pathInput.split(' ');
let path = '';
if (paths.length === 1 && fsHelper.isDirectory(paths[0])) {
// If the path is a single directory, we can skip the bundling step
path = paths[0];
} }
else { if (!fsHelper.isActionRepo(consolidatedPath)) {
// Otherwise, we need to bundle the files & folders into a temporary directory
const bundleDir = fsHelper.createTempDir();
tmpDirs.push(bundleDir);
path = fsHelper.bundleFilesintoDirectory(paths, bundleDir);
}
if (!fsHelper.isActionRepo(path)) {
core.setFailed('action.y(a)ml not found. Action packages can be created only for action repositories.'); core.setFailed('action.y(a)ml not found. Action packages can be created only for action repositories.');
return; return;
} }
// Create a temporary directory to store the archives // Create a temporary directory to store the archives
const archiveDir = fsHelper.createTempDir(); const archiveDir = fsHelper.createTempDir();
tmpDirs.push(archiveDir); tmpDirs.push(archiveDir);
const archives = await fsHelper.createArchives(path, archiveDir); const archives = await fsHelper.createArchives(consolidatedPath, archiveDir);
const manifest = ociContainer.createActionPackageManifest(archives.tarFile, archives.zipFile, repository, targetVersion.raw, new Date()); const manifest = ociContainer.createActionPackageManifest(archives.tarFile, archives.zipFile, repository, targetVersion.raw, new Date());
// Generate SHA-256 hash of the manifest // Generate SHA-256 hash of the manifest
const manifestSHA = crypto_1.default.createHash('sha256'); const manifestSHA = crypto_1.default.createHash('sha256');