|
| 1 | +const core = require("@actions/core"); |
| 2 | +const exec = require("@actions/exec"); |
| 3 | +const fs = require("fs"); |
| 4 | +const os = require("os"); |
| 5 | +const path = require("path"); |
| 6 | +const axios = require("axios"); |
| 7 | +const unzipper = require("unzipper"); |
| 8 | +const yaml = require("js-yaml"); |
| 9 | + |
| 10 | +async function run() { |
| 11 | + try { |
| 12 | + // Get inputs |
| 13 | + const actionRef = core.getInput("action-ref"); |
| 14 | + const extraArgs = core.getInput("extra-args") || ""; |
| 15 | + |
| 16 | + // Parse action-ref (expects format: owner/repo@ref) |
| 17 | + const [repo, ref] = parseActionRef(actionRef); |
| 18 | + core.info(`Parsed repo: ${repo}, ref: ${ref}`); |
| 19 | + |
| 20 | + // Construct URL for the repository zip archive |
| 21 | + const zipUrl = `https://github.com/${repo}/archive/${ref}.zip`; |
| 22 | + core.info(`Downloading action from: ${zipUrl}`); |
| 23 | + |
| 24 | + // Create a temporary directory for extraction |
| 25 | + const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), "nested-action-")); |
| 26 | + |
| 27 | + // Download and extract the zip archive |
| 28 | + const response = await axios({ |
| 29 | + url: zipUrl, |
| 30 | + method: "GET", |
| 31 | + responseType: "stream" |
| 32 | + }); |
| 33 | + await new Promise((resolve, reject) => { |
| 34 | + response.data |
| 35 | + .pipe(unzipper.Extract({ path: tempDir })) |
| 36 | + .on("close", resolve) |
| 37 | + .on("error", reject); |
| 38 | + }); |
| 39 | + core.info(`Downloaded and extracted to ${tempDir}`); |
| 40 | + |
| 41 | + // GitHub archives typically extract to a folder named "repo-ref" |
| 42 | + const repoName = repo.split("/")[1]; |
| 43 | + const extractedFolder = path.join(tempDir, `${repoName}-${ref}`); |
| 44 | + if (!fs.existsSync(extractedFolder)) { |
| 45 | + throw new Error(`Extracted folder ${extractedFolder} not found.`); |
| 46 | + } |
| 47 | + |
| 48 | + // Read action.yml from the downloaded action |
| 49 | + const actionYmlPath = path.join(extractedFolder, "action.yml"); |
| 50 | + if (!fs.existsSync(actionYmlPath)) { |
| 51 | + throw new Error(`action.yml not found in ${extractedFolder}`); |
| 52 | + } |
| 53 | + const actionConfig = yaml.load(fs.readFileSync(actionYmlPath, "utf8")); |
| 54 | + const entryPoint = actionConfig.runs && actionConfig.runs.main; |
| 55 | + if (!entryPoint) { |
| 56 | + throw new Error("Entry point (runs.main) not defined in action.yml"); |
| 57 | + } |
| 58 | + core.info(`Nested action entry point: ${entryPoint}`); |
| 59 | + |
| 60 | + // Construct full path to the nested action's entry file |
| 61 | + const entryFile = path.join(extractedFolder, entryPoint); |
| 62 | + if (!fs.existsSync(entryFile)) { |
| 63 | + throw new Error(`Entry file ${entryFile} does not exist.`); |
| 64 | + } |
| 65 | + |
| 66 | + // Optionally, install dependencies if package.json exists |
| 67 | + const pkgJsonPath = path.join(extractedFolder, "package.json"); |
| 68 | + if (fs.existsSync(pkgJsonPath)) { |
| 69 | + core.info("Installing dependencies for nested action..."); |
| 70 | + await exec.exec("npm", ["install"], { cwd: extractedFolder }); |
| 71 | + } |
| 72 | + |
| 73 | + // Execute the nested action using Node.js |
| 74 | + const args = extraArgs.split(/\s+/).filter((a) => a); // split and remove empty strings |
| 75 | + core.info(`Executing nested action: node ${entryFile} ${args.join(" ")}`); |
| 76 | + await exec.exec("node", [entryFile, ...args], { cwd: extractedFolder }); |
| 77 | + |
| 78 | + } catch (error) { |
| 79 | + core.setFailed(`Wrapper action failed: ${error.message}`); |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +function parseActionRef(refString) { |
| 84 | + const parts = refString.split("@"); |
| 85 | + if (parts.length !== 2) { |
| 86 | + throw new Error("Invalid action-ref format. Expected 'owner/repo@ref'"); |
| 87 | + } |
| 88 | + return parts; |
| 89 | +} |
| 90 | + |
| 91 | +run(); |
0 commit comments