Skip to content

Commit

Permalink
try git-based
Browse files Browse the repository at this point in the history
  • Loading branch information
jcbhmr committed Aug 17, 2023
1 parent ee97833 commit b096daf
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 35 deletions.
20 changes: 4 additions & 16 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ description: 📃 List all features in a monorepo in a GitHub Action
inputs:
path:
default: .
ref:
default: ${{ github.ref }}
base-ref: {}
outputs:
features:
value: ${{ steps.main.outputs.features }}
Expand All @@ -11,24 +14,9 @@ outputs:
runs:
using: composite
steps:
- id: paths-filter
uses: dorny/paths-filter@v2
with:
list-files: json
filters: |
src:
- ${{ inputs.path }}/src/*/**
test:
- ${{ inputs.path }}/test/*/**
- run: echo "$PATHS_FILTER_OUTPUTS" | jq
shell: bash
env:
PATHS_FILTER_OUTPUTS: ${{ toJSON(steps.paths-filter.outputs) }}
- id: main
run: '"$GITHUB_ACTION_PATH/mainw"'
shell: bash
env:
INPUT_PATH: ${{ inputs.path }}
# These are ALREADY JSON-encoded to JSON strings, so DON'T double-encode them.
PATHS_FILTER_OUTPUTS_SRC_FILES: ${{ steps.paths-filter.outputs.src_files }}
PATHS_FILTER_OUTPUTS_TEST_FILES: ${{ steps.paths-filter.outputs.test_files }}
GITHUB_EVENT: ${{ toJSON(github.event) }}
42 changes: 23 additions & 19 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ 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 features = await Promise.all(
(await glob("src/*/devcontainer-feature.json")).map(f =>
Expand All @@ -15,23 +17,25 @@ const features = await Promise.all(
)
core.setOutput("features", JSON.stringify(features))

// These are the CHANGED files.
// https://github.com/dorny/paths-filter
const srcFiles = JSON.parse(process.env.PATHS_FILTER_OUTPUTS_SRC_FILES)
const testFiles = JSON.parse(process.env.PATHS_FILTER_OUTPUTS_TEST_FILES)
console.dir(srcFiles)
console.dir(testFiles)

const changedFeatureIds = [
// These paths are from BEFORE the 'process.chdir()'
...srcFiles.map(x => x.match(/src\/(.*?)\//)[1]).filter(x => x),
...testFiles.map(x => x.match(/test\/(.*?)\//)[1]).filter(x => x),
]
const changedFeatures = (await Promise.all(
changedFeatureIds.map(id =>
readFile(`src/${id}/devcontainer-feature.json`, "utf8")
.then(x => JSON.parse(x))
.catch(() => {})
)
)).filter(x => x)
const ref = core.getInput("ref")
const event = JSON.parse(process.env.GITHUB_EVENT)
let changedFeatures;
if (event.pull_request) {
const baseRef = event.pull_request.base.ref
const changedFiles = (await $`git diff --name-only ${baseRef} ${ref}`).toString().split(/\r?\n/g)
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)
} catch {}
})
.filter(x => x)
} else {
changedFeatures = []
}
core.setOutput("changed-features", JSON.stringify(changedFeatures))

0 comments on commit b096daf

Please sign in to comment.