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.

fix(ci): remove stale auto labels from PRs (#124572)

Charles Vien157f6dd0a670
diff --git a/.github/scripts/label-pr-from-title.js b/.github/scripts/label-pr-from-title.js
index 0d4e0531dafc..d14f1a4c3f48 100644
--- a/.github/scripts/label-pr-from-title.js
+++ b/.github/scripts/label-pr-from-title.js
@@ -3,8 +3,7 @@
// Adds team/feature labels to a PR based on its conventional-commit scope
// (`type(scope): summary`) and the paths it changes, plus the `docs` label for docs-typed or Inkeep-bot
// 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.
+// feature flags team section below).
//
// The scope and path -> labels mapping lives in .github/auto-assign-labels.json, OUTSIDE
// this script, so the owning team can adjust mappings without changing (and
@@ -106,45 +105,64 @@ function labelsForFiles(files, rules) {
return Array.from(labels)
}
-const PR_FILES_PAGE_SIZE = 100
+const GITHUB_TOKEN_LOGIN = 'github-actions[bot]'
+
+function staleLabels(eventsOldestFirst, wantedLabels, rules) {
+ const configLabels = new Set(rules.flatMap((rule) => rule.labels || []))
+ const lastEventByLabel = new Map()
+ for (const event of eventsOldestFirst) {
+ if ((event.event === 'labeled' || event.event === 'unlabeled') && event.label) {
+ lastEventByLabel.set(event.label.name, event)
+ }
+ }
+
+ return Array.from(lastEventByLabel.values())
+ .filter((event) => event.event === 'labeled' && event.actor?.login === GITHUB_TOKEN_LOGIN)
+ .map((event) => event.label.name)
+ .filter((label) => configLabels.has(label) && !wantedLabels.includes(label))
+}
+
+const PAGE_SIZE = 100
+
+const PR_FILES_API_LIMIT = 3000
// 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 PR_FILES_REQUEST_TIMEOUT_MS = 10_000
+const REQUEST_TIMEOUT_MS = 10_000
// Returns null on failure, because a missing file list must not fail the job or
// apply the low-hanging-fruit label.
-async function fetchPrFiles() {
- const { GITHUB_TOKEN, GITHUB_REPOSITORY, PR_NUMBER } = process.env
- const files = []
+async function fetchAllPages(resource) {
+ const { GITHUB_TOKEN, GITHUB_REPOSITORY } = process.env
+ const items = []
try {
for (let page = 1; ; page++) {
const response = await fetch(
- `https://api.github.com/repos/${GITHUB_REPOSITORY}/pulls/${PR_NUMBER}/files?per_page=${PR_FILES_PAGE_SIZE}&page=${page}`,
+ `https://api.github.com/repos/${GITHUB_REPOSITORY}/${resource}?per_page=${PAGE_SIZE}&page=${page}`,
{
headers: {
Authorization: `token ${GITHUB_TOKEN}`,
Accept: 'application/vnd.github.v3+json',
},
- signal: AbortSignal.timeout(PR_FILES_REQUEST_TIMEOUT_MS),
+ signal: AbortSignal.timeout(REQUEST_TIMEOUT_MS),
}
)
if (!response.ok) {
- console.warn(`⚠️ Could not list PR files: ${response.status} ${response.statusText}`)
+ console.warn(`⚠️ Could not list ${resource}: ${response.status} ${response.statusText}`)
return null
}
const batch = await response.json()
- files.push(...batch)
- if (batch.length < PR_FILES_PAGE_SIZE) {
- return files
+ items.push(...batch)
+ if (batch.length < PAGE_SIZE) {
+ return items
}
}
} catch (error) {
- console.warn(`⚠️ Could not list PR files: ${error?.message ?? error}`)
+ console.warn(`⚠️ Could not list ${resource}: ${error?.message ?? error}`)
return null
}
}
@@ -165,7 +183,7 @@ async function addLabels(labels) {
try {
// Idempotent: GitHub keeps labels already present, so re-runs on title
- // edits are safe. Additive — we never strip labels that no longer match.
+ // edits are safe.
const response = await fetch(`https://api.github.com/repos/${GITHUB_REPOSITORY}/issues/${PR_NUMBER}/labels`, {
method: 'POST',
headers: {
@@ -191,6 +209,32 @@ async function addLabels(labels) {
}
}
+async function removeLabels(labels) {
+ const { GITHUB_TOKEN, GITHUB_REPOSITORY, PR_NUMBER } = process.env
+
+ for (const label of labels) {
+ console.info(`Removing stale label: ${label}`)
+ try {
+ const response = await fetch(
+ `https://api.github.com/repos/${GITHUB_REPOSITORY}/issues/${PR_NUMBER}/labels/${encodeURIComponent(label)}`,
+ {
+ method: 'DELETE',
+ headers: {
+ Authorization: `token ${GITHUB_TOKEN}`,
+ Accept: 'application/vnd.github.v3+json',
+ },
+ signal: AbortSignal.timeout(REQUEST_TIMEOUT_MS),
+ }
+ )
+ if (!response.ok) {
+ console.warn(`⚠️ Could not remove ${label}: ${response.status} ${response.statusText}`)
+ }
+ } catch (error) {
+ console.warn(`⚠️ Could not remove ${label}: ${error?.message ?? error}`)
+ }
+ }
+}
+
// ---------------------------------------------------------------------------
// Feature flags team only: the `review/low-hanging-fruit` label.
//
@@ -333,7 +377,7 @@ async function main() {
try {
const rules = loadRules()
- const files = await fetchPrFiles()
+ const files = await fetchAllPages(`pulls/${PR_NUMBER}/files`)
const labels = Array.from(new Set([...labelsForTitle(PR_TITLE, rules), ...labelsForFiles(files ?? [], rules)]))
if (docsLabelApplies(PR_TITLE, PR_AUTHOR) && !labels.includes('docs')) {
labels.push('docs')
@@ -344,6 +388,10 @@ async function main() {
}
console.info(`PR title: ${PR_TITLE || '(empty)'}`)
await addLabels(labels)
+ if (files && files.length < PR_FILES_API_LIMIT) {
+ const events = await fetchAllPages(`issues/${PR_NUMBER}/events`)
+ await removeLabels(staleLabels(events ?? [], labels, rules))
+ }
} catch (error) {
console.error('Error:', error?.message ?? error)
process.exit(1)
@@ -360,6 +408,7 @@ module.exports = {
docsLabelApplies,
labelsForTitle,
labelsForFiles,
+ staleLabels,
loadRules,
isFlagsLowHangingFruitCandidate,
isFlagsLowHangingFruit,
diff --git a/.github/scripts/label-pr-from-title.test.js b/.github/scripts/label-pr-from-title.test.js
index 79505c7680b3..8cfea8546784 100644
--- a/.github/scripts/label-pr-from-title.test.js
+++ b/.github/scripts/label-pr-from-title.test.js
@@ -13,6 +13,7 @@ const {
docsLabelApplies,
labelsForTitle,
labelsForFiles,
+ staleLabels,
loadRules,
isFlagsLowHangingFruitCandidate,
isFlagsLowHangingFruit,
@@ -153,6 +154,53 @@ test('labelsForFiles', async (t) => {
}
})
+const labelEvent = (event, name, login) => ({ event, label: { name }, actor: { login } })
+
+const STALE_LABELS_CASES = [
+ {
+ events: [labelEvent('labeled', 'feature/agent-infra', 'github-actions[bot]')],
+ wanted: [],
+ expected: ['feature/agent-infra'],
+ description: 'a label this workflow added that no longer matches',
+ },
+ {
+ events: [labelEvent('labeled', 'feature/agent-infra', 'github-actions[bot]')],
+ wanted: ['feature/agent-infra'],
+ expected: [],
+ description: 'a label this workflow added that still matches',
+ },
+ {
+ events: [labelEvent('labeled', 'feature/agent-infra', 'someuser')],
+ wanted: [],
+ expected: [],
+ description: 'a label a person added',
+ },
+ {
+ events: [
+ labelEvent('labeled', 'feature/agent-infra', 'github-actions[bot]'),
+ labelEvent('unlabeled', 'feature/agent-infra', 'someuser'),
+ labelEvent('labeled', 'feature/agent-infra', 'someuser'),
+ ],
+ wanted: [],
+ expected: [],
+ description: 'a label a person added back after this workflow added it',
+ },
+ {
+ events: [labelEvent('labeled', 'docs', 'github-actions[bot]')],
+ wanted: [],
+ expected: [],
+ description: 'a label this workflow added that no rule maps to',
+ },
+]
+
+test('staleLabels', async (t) => {
+ for (const { events, wanted, expected, description } of STALE_LABELS_CASES) {
+ await t.test(description, () => {
+ assert.deepEqual(staleLabels(events, wanted, PATH_RULES), expected)
+ })
+ }
+})
+
const PARSE_TYPE_CASES = [
{ title: 'docs: add guide', expected: 'docs', description: 'type with no scope' },
{ title: 'docs(internal): x', expected: 'docs', description: 'type with scope' },