Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

chore(ci): reuse lane context for targets and telemetry (#126794)

posthog[bot]4dad1abc06c4
diff --git a/.github/scripts/trunk-impacted-targets.js b/.github/scripts/trunk-impacted-targets.js
index a5e3dc42d4b1..67887b8fbbb9 100644
--- a/.github/scripts/trunk-impacted-targets.js
+++ b/.github/scripts/trunk-impacted-targets.js
@@ -3006,8 +3006,11 @@ module.exports = {
if (require.main === module) {
let result
+ let context = null
+ let changedFiles = []
+ let deletedFiles = null
try {
- const changedFiles = fs
+ changedFiles = fs
.readFileSync(0, 'utf8')
.split('\n')
.map((line) => line.trim())
@@ -3016,9 +3019,11 @@ if (require.main === module) {
console.error('No changed files on stdin; reporting ALL')
result = ALL
} else {
+ context = buildContext(REPO_ROOT)
+ deletedFiles = findDeletedFiles(changedFiles)
result = computeTargets(changedFiles, {
- ...buildContext(REPO_ROOT),
- deletedFiles: findDeletedFiles(changedFiles),
+ ...context,
+ deletedFiles,
})
}
} catch (error) {
@@ -3031,4 +3036,16 @@ if (require.main === module) {
console.error(`Computed ${result.length} target(s): ${JSON.stringify(result)}`)
}
process.stdout.write(JSON.stringify(result))
+ if (process.env.LANE_TELEMETRY_FILE) {
+ try {
+ const { summarizeLane } = require('./trunk-lane-telemetry')
+ context = context || buildContext(REPO_ROOT)
+ fs.writeFileSync(
+ process.env.LANE_TELEMETRY_FILE,
+ JSON.stringify(summarizeLane(changedFiles, result, context, deletedFiles))
+ )
+ } catch (error) {
+ console.error(`Could not write lane telemetry (${error.message}); keeping computed targets`)
+ }
+ }
}
diff --git a/.github/scripts/trunk-impacted-targets.test.js b/.github/scripts/trunk-impacted-targets.test.js
index 7a559c25ee88..8db12f6ad096 100644
--- a/.github/scripts/trunk-impacted-targets.test.js
+++ b/.github/scripts/trunk-impacted-targets.test.js
@@ -2334,3 +2334,29 @@ test('playwright changes overlap the frontend domain', () => {
true
)
})
+
+for (const writable of [true, false]) {
+ test(`the CLI preserves targets when the telemetry output is ${writable ? 'writable' : 'unwritable'}`, () => {
+ const root = fs.mkdtempSync(path.join(os.tmpdir(), 'lane-telemetry-'))
+ try {
+ const telemetryFile = path.join(root, writable ? 'properties.json' : 'missing/properties.json')
+ const output = execFileSync(process.execPath, [path.join(__dirname, 'trunk-impacted-targets.js')], {
+ input: 'README.md\n',
+ env: { ...process.env, PATH: '', LANE_TELEMETRY_FILE: telemetryFile },
+ encoding: 'utf8',
+ stdio: ['pipe', 'pipe', 'pipe'],
+ })
+ assert.deepEqual(JSON.parse(output), ['prose'])
+ if (writable) {
+ const properties = JSON.parse(fs.readFileSync(telemetryFile, 'utf8'))
+ assert.equal(properties.is_prose, true)
+ assert.equal(properties.changed_file_count, 1)
+ assert.equal(properties.cross_lane, false)
+ } else {
+ assert.equal(fs.existsSync(telemetryFile), false)
+ }
+ } finally {
+ fs.rmSync(root, { recursive: true, force: true })
+ }
+ })
+}
diff --git a/.github/scripts/trunk-lane-telemetry.js b/.github/scripts/trunk-lane-telemetry.js
index b3606684e558..116b9da60d36 100644
--- a/.github/scripts/trunk-lane-telemetry.js
+++ b/.github/scripts/trunk-lane-telemetry.js
@@ -48,7 +48,7 @@ function domainOf(target) {
// universe tells them apart. Without it the dashboard would read every widening
// as a PR that legitimately claimed every lane, which is the distinction this
// event exists to make.
-function buildProperties(changedFiles, impactedTargets, universe, crossLane = null) {
+function buildProperties(changedFiles, impactedTargets, universe, crossLane = null, semgrepDomains = null) {
const targets = Array.isArray(impactedTargets) ? impactedTargets : []
const isAll = impactedTargets === ALL || (Boolean(universe) && targets.length === universe.length)
const isProse = targets.length === 1 && targets[0] === 'prose'
@@ -74,7 +74,6 @@ function buildProperties(changedFiles, impactedTargets, universe, crossLane = nu
// Which blast radius each tripwire claimed, so a domain that turns out to
// widen more than its share is visible without re-deriving it from paths.
const tripwireDomains = {}
- let semgrepDomains = null
for (const file of tripwireFiles) {
let domain = tripwireDomain(file)
if (domain === 'semgrep') {
@@ -131,7 +130,30 @@ function buildProperties(changedFiles, impactedTargets, universe, crossLane = nu
}
}
-module.exports = { buildProperties }
+function summarizeLane(changedFiles, impactedTargets, context, deletedFiles = null) {
+ let universe = null
+ try {
+ universe = context ? allKnownTargets(context) : null
+ } catch (error) {
+ console.error(`Could not enumerate the target universe (${error.message}); is_all reports the sentinel only`)
+ }
+ let crossLane = null
+ if (context) {
+ try {
+ crossLane = crossLaneFiles(changedFiles, {
+ ...context,
+ deletedFiles: deletedFiles ?? findDeletedFiles(changedFiles),
+ })
+ } catch (error) {
+ console.error(
+ `Could not classify the change set by lane side (${error.message}); cross_lane reports unknown`
+ )
+ }
+ }
+ return buildProperties(changedFiles, impactedTargets, universe, crossLane, context?.semgrepDomains)
+}
+
+module.exports = { buildProperties, summarizeLane }
if (require.main === module) {
const changedFiles = fs
@@ -146,22 +168,10 @@ if (require.main === module) {
console.error(`Could not read IMPACTED_TARGETS (${error.message}); reporting the file side only`)
}
let context = null
- let universe = null
try {
context = buildContext(REPO_ROOT)
- universe = allKnownTargets(context)
} catch (error) {
- console.error(`Could not enumerate the target universe (${error.message}); is_all reports the sentinel only`)
- }
- let crossLane = null
- if (context) {
- try {
- crossLane = crossLaneFiles(changedFiles, { ...context, deletedFiles: findDeletedFiles(changedFiles) })
- } catch (error) {
- console.error(
- `Could not classify the change set by lane side (${error.message}); cross_lane reports unknown`
- )
- }
+ console.error(`Could not build the lane context (${error.message})`)
}
- process.stdout.write(JSON.stringify(buildProperties(changedFiles, impactedTargets, universe, crossLane)))
+ process.stdout.write(JSON.stringify(summarizeLane(changedFiles, impactedTargets, context)))
}
diff --git a/.github/scripts/trunk-lane-telemetry.test.js b/.github/scripts/trunk-lane-telemetry.test.js
index 310fd5b17a09..1b94c930350a 100644
--- a/.github/scripts/trunk-lane-telemetry.test.js
+++ b/.github/scripts/trunk-lane-telemetry.test.js
@@ -57,6 +57,9 @@ test('the ALL sentinel still reports no targets but is flagged', () => {
test('tripwire domains are counted alongside the files', () => {
const props = buildProperties(['tsconfig.json', 'mypy.ini', 'hogli.yaml'], UNIVERSE, UNIVERSE)
assert.deepEqual(props.tripwire_domains, { javascript: 1, python: 1, universal: 1 })
+ const rule = '.semgrep/rules/python/example.yml'
+ const shared = buildProperties([rule], UNIVERSE, UNIVERSE, null, new Map([[rule, 'python']]))
+ assert.deepEqual(shared.tripwire_domains, { python: 1 })
})
test('file paths are summarized rather than sent', () => {
diff --git a/.github/workflows/trunk-impacted-targets.yml b/.github/workflows/trunk-impacted-targets.yml
index 04e244d1d6da..fb2182d1f2a9 100644
--- a/.github/workflows/trunk-impacted-targets.yml
+++ b/.github/workflows/trunk-impacted-targets.yml
@@ -214,6 +214,7 @@ jobs:
id: targets
env:
RESOLVED: ${{ steps.changes.outputs.resolved }}
+ LANE_TELEMETRY_FILE: ${{ runner.temp }}/lane-telemetry.json
# Empty when the determinator was skipped or failed, which the
# script reads as every crate. Its own rebuild-all verdict
# needs no special case: it fills `crates` with the whole
@@ -248,7 +249,12 @@ jobs:
run: |
set -uo pipefail
touch "$RUNNER_TEMP/changed-files.txt"
- properties="$(node .github/scripts/trunk-lane-telemetry.js < "$RUNNER_TEMP/changed-files.txt")"
+ # Unrebased PRs can still have the target script without telemetry output.
+ if [ -f "$RUNNER_TEMP/lane-telemetry.json" ]; then
+ properties="$(cat "$RUNNER_TEMP/lane-telemetry.json")"
+ else
+ properties="$(node .github/scripts/trunk-lane-telemetry.js < "$RUNNER_TEMP/changed-files.txt")"
+ fi
printf 'properties=%s\n' "$properties" >> "$GITHUB_OUTPUT"
upload: