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(flags): label small ai flags prs as review/low-hanging-fruit (#109199)

Gustavo H. Strassburgerd67df4e7f8db
diff --git a/.github/scripts/label-pr-from-title.js b/.github/scripts/label-pr-from-title.js
index 6306ca03af9a..587d63263535 100644
--- a/.github/scripts/label-pr-from-title.js
+++ b/.github/scripts/label-pr-from-title.js
@@ -2,7 +2,8 @@
// Adds team/feature labels to a PR based on its conventional-commit scope
// (`type(scope): summary`), plus the `docs` label for docs-typed or Inkeep-bot
-// PRs. Additive only — it never removes labels, so manual labels and the
+// PRs, plus the feature flags team's `review/low-hanging-fruit` label (see the
+// feature flags team section below). Additive only — it never removes labels, so manual labels and the
// ownership-based labeler (assign-reviewers.js) are left intact.
//
// The scope -> labels mapping lives in .github/auto-assign-labels.json, OUTSIDE
@@ -11,6 +12,7 @@
// checkout, never the PR, so a fork PR can't inject its own mappings, and the
// PR title only ever selects from a fixed, pre-approved set of labels.
+const { execFileSync } = require('child_process')
const fs = require('fs')
const path = require('path')
@@ -118,6 +120,174 @@ async function addLabels(labels) {
}
}
+// ---------------------------------------------------------------------------
+// Feature flags team only: the `review/low-hanging-fruit` label.
+//
+// PRs that the PostHog AI app opens for the feature flags team get this label
+// when they are quick to review, so the team can pick them off the Feature
+// Flags board first. Other teams have not opted in, so everything in this section is scoped to the
+// feature flags team. main() calls only flagsLowHangingFruitLabel().
+// ---------------------------------------------------------------------------
+
+const FLAGS_LOW_HANGING_FRUIT_LABEL = 'review/low-hanging-fruit'
+
+// The PostHog AI app's login. Its PRs carry the `self-driving` label, but the
+// app adds that label a few seconds after it opens the PR, so the `opened`
+// payload does not have it yet. The author is set when the PR opens.
+const FLAGS_AI_BOT_LOGIN = 'posthog[bot]'
+
+// The label is only for cards on the Feature Flags board. That board auto-adds
+// every PR with the team label, so the team label stands in for the board. The
+// board is not checked directly for two reasons: this workflow's token cannot
+// read organization projects, and the card does not exist yet when the PR opens.
+const FLAGS_TEAM_LABEL = 'team/feature-flags'
+
+// Test code cannot break production, so test files do not count toward the
+// limits. A tests-only PR therefore qualifies at any size.
+const FLAGS_TEST_PATH_PATTERNS = [
+ /(^|\/)(tests?|__tests__|__snapshots__)\//,
+ /\.(test|spec)\.[cm]?[jt]sx?$/,
+ /(^|\/)(test_[^/]*|conftest)\.py$/,
+ /_test\.(py|go|rs)$/,
+ // Rust test modules that live in `src/`, next to the code they test.
+ /(^|\/)(test_[^/]*|tests)\.rs$/,
+]
+
+// Components and styles only. A kea logic or a plain `.ts` file holds state and
+// API calls, so it is not a UI tweak even when the diff is small.
+const FLAGS_UI_PATH_PATTERN = /^(frontend\/src|products\/[^/]+\/frontend)\/.+\.(tsx|scss|css)$/
+const FLAGS_KEA_LOGIC_PATTERN = /Logic\.tsx$/
+
+function isFlagsTestFile(filename) {
+ return FLAGS_TEST_PATH_PATTERNS.some((pattern) => pattern.test(filename))
+}
+
+function isFlagsUiFile(filename) {
+ return FLAGS_UI_PATH_PATTERN.test(filename) && !FLAGS_KEA_LOGIC_PATTERN.test(filename)
+}
+
+// A PR qualifies when its hand-written, non-test files fit at least one of
+// these shapes.
+const FLAGS_QUICK_REVIEW_SHAPES = [
+ { maxFiles: 2, maxLines: 50, fileMatches: () => true },
+ { maxFiles: 5, maxLines: 100, fileMatches: isFlagsUiFile },
+]
+
+// GitHub's maximum page size for the "list pull request files" endpoint.
+const FLAGS_FILES_PAGE_SIZE = 100
+
+// A small diff in these paths still needs a careful review: a migration changes
+// production data, and a workflow change runs with repository secrets.
+const FLAGS_RISKY_PATH_PATTERNS = [/(^|\/)migrations\//, /^\.github\//]
+
+function isFlagsLowHangingFruitCandidate(author, labels) {
+ return author === FLAGS_AI_BOT_LOGIN && labels.includes(FLAGS_TEAM_LABEL)
+}
+
+// Generated files do not count toward the size limits, because nobody reviews
+// them line by line. .gitattributes marks them as `linguist-generated`, and git
+// reads that file from the master checkout, so the list of generated paths has
+// one source of truth. If git fails, all files count, which can only withhold
+// the label.
+function flagsWithoutGeneratedFiles(files) {
+ if (files.length === 0) {
+ return files
+ }
+ try {
+ // `-z` separates paths with NUL in the input and output, so a PR file
+ // name that contains a newline cannot break the parse.
+ const output = execFileSync('git', ['check-attr', '-z', '--stdin', 'linguist-generated'], {
+ cwd: path.join(__dirname, '..', '..'),
+ input: files.map((file) => file.filename).join('\0'),
+ }).toString()
+ const fields = output.split('\0')
+ const generated = new Set()
+ for (let i = 0; i + 2 < fields.length; i += 3) {
+ // A bare `linguist-generated` reads as `set`, and the
+ // `linguist-generated=true` form reads as `true`.
+ if (fields[i + 2] === 'set' || fields[i + 2] === 'true') {
+ generated.add(fields[i])
+ }
+ }
+ return files.filter((file) => !generated.has(file.filename))
+ } catch (error) {
+ console.warn(`⚠️ Could not check for generated files: ${error?.message ?? error}`)
+ return files
+ }
+}
+
+// `files` is the first page of GitHub's "list pull request files" response.
+function isFlagsLowHangingFruit(files) {
+ // A full page means the PR has at least a page of files, and the files on
+ // later pages are not checked. So many files, even generated ones, is not a
+ // quick review, so a full page withholds the label.
+ if (files.length >= FLAGS_FILES_PAGE_SIZE) {
+ return false
+ }
+ // Check the risky paths before the generated files are removed, because
+ // .gitattributes also marks files under `.github/` as generated.
+ if (files.some((file) => FLAGS_RISKY_PATH_PATTERNS.some((pattern) => pattern.test(file.filename)))) {
+ return false
+ }
+ const reviewedFiles = flagsWithoutGeneratedFiles(files).filter((file) => !isFlagsTestFile(file.filename))
+ const changedLines = reviewedFiles.reduce((sum, file) => sum + file.additions + file.deletions, 0)
+ return FLAGS_QUICK_REVIEW_SHAPES.some(
+ (shape) =>
+ reviewedFiles.length <= shape.maxFiles &&
+ changedLines <= shape.maxLines &&
+ reviewedFiles.every((file) => shape.fileMatches(file.filename))
+ )
+}
+
+// The job applies every label after this request, and it has a five-minute
+// timeout. A stalled request must give up well before that, so the team and
+// docs labels still get applied.
+const FLAGS_FILES_REQUEST_TIMEOUT_MS = 10_000
+
+// Fetches only the first page, because isFlagsLowHangingFruit() withholds the
+// label when the page is full. Returns null on failure, because a missing size
+// must not fail the job or apply the label.
+async function fetchFlagsPrFiles() {
+ const { GITHUB_TOKEN, GITHUB_REPOSITORY, PR_NUMBER } = process.env
+
+ try {
+ const response = await fetch(
+ `https://api.github.com/repos/${GITHUB_REPOSITORY}/pulls/${PR_NUMBER}/files?per_page=${FLAGS_FILES_PAGE_SIZE}`,
+ {
+ headers: {
+ Authorization: `token ${GITHUB_TOKEN}`,
+ Accept: 'application/vnd.github.v3+json',
+ },
+ signal: AbortSignal.timeout(FLAGS_FILES_REQUEST_TIMEOUT_MS),
+ }
+ )
+
+ if (!response.ok) {
+ console.warn(`⚠️ Could not list PR files: ${response.status} ${response.statusText}`)
+ return null
+ }
+
+ return await response.json()
+ } catch (error) {
+ console.warn(`⚠️ Could not list PR files: ${error?.message ?? error}`)
+ return null
+ }
+}
+
+// Returns the label to add, or null. `labels` are the labels the title rules
+// computed for this PR, because the candidate check reads the team label.
+async function flagsLowHangingFruitLabel(author, labels) {
+ if (!isFlagsLowHangingFruitCandidate(author, labels)) {
+ return null
+ }
+ const files = await fetchFlagsPrFiles()
+ return files && isFlagsLowHangingFruit(files) ? FLAGS_LOW_HANGING_FRUIT_LABEL : null
+}
+
+// ---------------------------------------------------------------------------
+// End of the feature flags team section.
+// ---------------------------------------------------------------------------
+
async function main() {
const { GITHUB_TOKEN, GITHUB_REPOSITORY, PR_NUMBER, PR_TITLE, PR_AUTHOR } = process.env
const missing = Object.entries({ GITHUB_TOKEN, GITHUB_REPOSITORY, PR_NUMBER })
@@ -135,6 +305,10 @@ async function main() {
if (docsLabelApplies(PR_TITLE, PR_AUTHOR) && !labels.includes('docs')) {
labels.push('docs')
}
+ const flagsLabel = await flagsLowHangingFruitLabel(PR_AUTHOR, labels)
+ if (flagsLabel) {
+ labels.push(flagsLabel)
+ }
console.info(`PR title: ${PR_TITLE || '(empty)'}`)
await addLabels(labels)
} catch (error) {
@@ -147,4 +321,13 @@ if (require.main === module) {
main()
}
-module.exports = { parseScopes, parseType, docsLabelApplies, labelsForTitle, loadRules }
+module.exports = {
+ parseScopes,
+ parseType,
+ docsLabelApplies,
+ labelsForTitle,
+ loadRules,
+ isFlagsLowHangingFruitCandidate,
+ isFlagsLowHangingFruit,
+ flagsWithoutGeneratedFiles,
+}
diff --git a/.github/scripts/label-pr-from-title.test.js b/.github/scripts/label-pr-from-title.test.js
index d5ebb1322cea..84ba438166a8 100644
--- a/.github/scripts/label-pr-from-title.test.js
+++ b/.github/scripts/label-pr-from-title.test.js
@@ -7,7 +7,16 @@
const test = require('node:test')
const assert = require('node:assert/strict')
-const { parseScopes, parseType, docsLabelApplies, labelsForTitle, loadRules } = require('./label-pr-from-title')
+const {
+ parseScopes,
+ parseType,
+ docsLabelApplies,
+ labelsForTitle,
+ loadRules,
+ isFlagsLowHangingFruitCandidate,
+ isFlagsLowHangingFruit,
+ flagsWithoutGeneratedFiles,
+} = require('./label-pr-from-title')
// Mirrors the rule shape in .github/auto-assign-labels.json so the logic is
// exercised against the real structure without reading the file.
@@ -149,3 +158,180 @@ for (const scope of ['desktop', 'tasks', 'agent-proxy', 'canvas']) {
)
})
}
+
+// ---------------------------------------------------------------------------
+// Feature flags team only: the `review/low-hanging-fruit` label.
+// ---------------------------------------------------------------------------
+
+const FLAGS_LABELS = ['feature/feature-flags', 'team/feature-flags']
+
+const FLAGS_CANDIDATE_CASES = [
+ {
+ author: 'posthog[bot]',
+ labels: FLAGS_LABELS,
+ expected: true,
+ description: 'AI PR for the flags team is a candidate',
+ },
+ {
+ author: 'someuser',
+ labels: FLAGS_LABELS,
+ expected: false,
+ description: 'human PR for the flags team is not a candidate',
+ },
+ {
+ author: 'posthog[bot]',
+ labels: ['feature/desktop'],
+ expected: false,
+ description: 'AI PR for another team is not a candidate',
+ },
+]
+
+test('isFlagsLowHangingFruitCandidate', async (t) => {
+ for (const { author, labels, expected, description } of FLAGS_CANDIDATE_CASES) {
+ await t.test(description, () => {
+ assert.equal(isFlagsLowHangingFruitCandidate(author, labels), expected)
+ })
+ }
+})
+
+const file = (filename, additions, deletions = 0) => ({ filename, additions, deletions })
+const generatedFiles = (count) =>
+ Array.from({ length: count }, (_, i) => file(`frontend/src/generated/core/file${i}.ts`, 10))
+
+const FLAGS_LOW_HANGING_FRUIT_CASES = [
+ {
+ files: [file('frontend/src/scenes/feature-flags/FeatureFlag.tsx', 20, 5)],
+ expected: true,
+ description: 'small single-file change',
+ },
+ {
+ files: [file('a.py', 30, 5), file('b.py', 15)],
+ expected: true,
+ description: 'exactly at the line and file limits',
+ },
+ {
+ files: [file('a.py', 30, 5), file('b.py', 16)],
+ expected: false,
+ description: 'one line over the line limit',
+ },
+ {
+ files: [file('a.tsx', 1), file('b.tsx', 1), file('c.tsx', 1)],
+ expected: false,
+ description: 'one file over the file limit',
+ },
+ {
+ files: [file('posthog/migrations/1234_add_column.py', 20)],
+ expected: false,
+ description: 'small Django migration',
+ },
+ {
+ files: [file('rust/feature-flags/migrations/0001_init.sql', 10)],
+ expected: false,
+ description: 'small Rust migration',
+ },
+ {
+ files: [file('.github/workflows/ci-rust.yml', 2, 2)],
+ expected: false,
+ description: 'small workflow change',
+ },
+ {
+ files: [file('.github/actions/paths-filter/dist/index.js', 2, 2)],
+ expected: false,
+ description: 'small change to a generated file under .github/',
+ },
+ {
+ files: [
+ file('posthog/api/test/test_feature_flag.py', 400),
+ file('frontend/src/scenes/feature-flags/featureFlagLogic.test.ts', 300),
+ file('rust/feature-flags/src/flags/test_flag_matching.rs', 200),
+ file('rust/feature-flags/tests/test_flags.rs', 100),
+ ],
+ expected: true,
+ description: 'large tests-only change',
+ },
+ {
+ files: [
+ file('products/feature_flags/backend/local_evaluation.py', 30),
+ file('posthog/utils.py', 20),
+ file('products/feature_flags/backend/test/test_local_evaluation.py', 300),
+ file('frontend/src/scenes/feature-flags/featureFlagLogic.test.ts', 100),
+ ],
+ expected: true,
+ description: 'small source change with large tests',
+ },
+ {
+ files: [file('frontend/src/scenes/feature-flags/FeatureFlagTestingTab.tsx', 200)],
+ expected: false,
+ description: 'large change to a component with "Testing" in its name',
+ },
+ {
+ files: [
+ file('frontend/src/scenes/feature-flags/FeatureFlag.tsx', 30),
+ file('frontend/src/scenes/feature-flags/FeatureFlagSchedule.tsx', 30),
+ file('frontend/src/scenes/feature-flags/FeatureFlag.scss', 20),
+ file('products/feature_flags/frontend/FlagsTable.tsx', 10),
+ file('frontend/src/scenes/feature-flags/FlagCard.tsx', 10),
+ ],
+ expected: true,
+ description: 'UI tweak exactly at the line and file limits',
+ },
+ {
+ files: [file('frontend/src/scenes/feature-flags/FeatureFlag.tsx', 50), file('a.scss', 1), file('b.css', 40)],
+ expected: false,
+ description: 'UI tweak with a style file outside the frontend directories',
+ },
+ {
+ files: [
+ file('frontend/src/scenes/feature-flags/FeatureFlag.tsx', 50),
+ file('frontend/src/scenes/feature-flags/FeatureFlag.scss', 51),
+ ],
+ expected: false,
+ description: 'UI tweak one line over the line limit',
+ },
+ {
+ files: Array.from({ length: 6 }, (_, i) => file(`frontend/src/scenes/feature-flags/C${i}.tsx`, 1)),
+ expected: false,
+ description: 'UI tweak one file over the file limit',
+ },
+ {
+ files: [
+ file('frontend/src/scenes/feature-flags/FeatureFlag.tsx', 20),
+ file('frontend/src/scenes/feature-flags/featureFlagLogic.ts', 20),
+ file('frontend/src/scenes/feature-flags/flagsLogic.tsx', 20),
+ ],
+ expected: false,
+ description: 'UI tweak that also changes kea logics',
+ },
+ {
+ files: [...generatedFiles(98), file('a.tsx', 10)],
+ expected: true,
+ description: 'generated files below a full page do not count',
+ },
+ {
+ files: [...generatedFiles(99), file('a.tsx', 10)],
+ expected: false,
+ description: 'a full page withholds the label, because later pages go unchecked',
+ },
+]
+
+test('isFlagsLowHangingFruit', async (t) => {
+ for (const { files, expected, description } of FLAGS_LOW_HANGING_FRUIT_CASES) {
+ await t.test(description, () => {
+ assert.equal(isFlagsLowHangingFruit(files), expected)
+ })
+ }
+})
+
+// Runs against the shipped .gitattributes, so it also fails if the generated
+// API types stop being marked as `linguist-generated`.
+test('flagsWithoutGeneratedFiles drops generated files and keeps hand-written ones', () => {
+ const files = [
+ file('frontend/src/generated/core/api.schemas.ts', 300),
+ file('products/feature_flags/frontend/generated/api.ts', 120),
+ file('products/desktop/packages/ui/src/router/routeTree.gen.ts', 40),
+ file('frontend/src/scenes/feature-flags/FeatureFlag.tsx', 10),
+ ]
+ assert.deepEqual(flagsWithoutGeneratedFiles(files), [
+ file('frontend/src/scenes/feature-flags/FeatureFlag.tsx', 10),
+ ])
+})
diff --git a/.github/workflows/auto-assign-labels.yml b/.github/workflows/auto-assign-labels.yml
index aed2dca82a7f..db44e0740f25 100644
--- a/.github/workflows/auto-assign-labels.yml
+++ b/.github/workflows/auto-assign-labels.yml
@@ -1,7 +1,8 @@
name: Auto Assign Labels
# Adds team/feature labels to a PR based on its conventional-commit scope, e.g.
-# `feat(flags): …` -> `feature/feature-flags` + `team/feature-flags`. The
+# `feat(flags): …` -> `feature/feature-flags` + `team/feature-flags`. Small
+# feature flags PRs from the PostHog AI app also get `review/low-hanging-fruit`. The
# scope -> label mappings live in .github/auto-assign-labels.json so the owning
# team can change them without editing this workflow or the script.
#