Skip to content

Commit

Permalink
try more dynamic detection
Browse files Browse the repository at this point in the history
  • Loading branch information
jcbhmr committed Aug 17, 2023
1 parent bfc214b commit ca2db3f
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 43 deletions.
5 changes: 3 additions & 2 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ description: 📃 List all features in a monorepo in a GitHub Action
inputs:
path:
default: .
ref:
default: ${{ github.ref }}
ref: {}
base-ref: {}
outputs:
features:
Expand All @@ -19,4 +18,6 @@ runs:
shell: bash
env:
INPUT_PATH: ${{ inputs.path }}
INPUT_REF: ${{ inputs.ref }}
INPUT_BASE_REF: ${{ inputs.base-ref }}
GITHUB_EVENT: ${{ toJSON(github.event) }}
98 changes: 57 additions & 41 deletions main.ts
Original file line number Diff line number Diff line change
@@ -1,54 +1,70 @@
#!/usr/bin/env -S deno run -A
import * as core from "npm:@actions/core"
import { readFile, writeFile } from "node:fs/promises"
import process from "node:process"
import { glob } from "npm:glob"
import { $ } from "npm:zx"
import * as core from "npm:@actions/core";
import { readFile, writeFile } from "node:fs/promises";
import process from "node:process";
import { glob } from "npm:glob";
import { $ } from "npm:zx";

const path = core.getInput("path")
process.chdir(path)
$.cwd = process.cwd()
const path = core.getInput("path");
process.chdir(path);
$.cwd = process.cwd();

const features = await Promise.all(
(await glob("src/*/devcontainer-feature.json")).map(f =>
readFile(f, "utf8")
.then(x => JSON.parse(x))
)
)
core.setOutput("features", JSON.stringify(features))

const ref = core.getInput("ref")
const event = JSON.parse(process.env.GITHUB_EVENT)
console.log(event)

let baseRef: string | undefined
if (event.name === "pull_request") {
baseRef = event.pull_request.base.sha
} else if (event.name === "push") {
baseRef = event.before
(
await glob("src/*/devcontainer-feature.json")
).map((f) => readFile(f, "utf8").then((x) => JSON.parse(x)))
);
core.setOutput("features", JSON.stringify(features));

const eventName = process.env.GITHUB_EVENT_NAME;
const event = JSON.parse(process.env.GITHUB_EVENT);
console.log("event", event);

let baseRef = core.getInput("base_ref");
if (!baseRef) {
if (eventName === "push") {
baseRef = event.before;
}
}

let ref = core.getInput("ref");
if (!ref) {
if (eventName === "push") {
ref = event.after;
}
}
console.log(baseRef)

let changedFeatures: any[]
console.log("before", baseRef);
console.log("after", ref);

let changedFeatures: any[];
if (baseRef) {
const changedFiles = (await $`git diff --name-only ${baseRef} ${ref}`).toString().split(/\r?\n/g)
console.log(changedFiles)

const changedIds = changedFiles.map(x => /src\/(.*?)\//.match(x)?.[1]).filter(x => x)
changedFeatures = (await Promise.all(
changedIds.map(x => readFile(`src/${id}/devcontainer-feature.json`, "utf8")
.catch(() => {}))
))
.filter(x => x)
.map(x => {
const changedFiles = (await $`git diff --name-only ${baseRef} ${ref}`)
.toString()
.split(/\r?\n/g);
console.log(changedFiles);

const changedIds = changedFiles
.map((x) => /src\/(.*?)\//.match(x)?.[1])
.filter((x) => x);

changedFeatures = (
await Promise.all(
changedIds.map((x) =>
readFile(`src/${id}/devcontainer-feature.json`, "utf8").catch(() => {})
)
)
)
.filter((x) => x)
.map((x) => {
try {
return JSON.parse(x)
return JSON.parse(x);
} catch {}
})
.filter(x => x)
.filter((x) => x);
} else {
changedFeatures = []
changedFeatures = [];
}
console.log(changedFeatures)
console.log(changedFeatures);

core.setOutput("changed-features", JSON.stringify(changedFeatures))
core.setOutput("changed-features", JSON.stringify(changedFeatures));

0 comments on commit ca2db3f

Please sign in to comment.