-
Notifications
You must be signed in to change notification settings - Fork 14
ci: add label policy check #693
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| export const POLICY = { | ||
| priorities: ["P0", "P1", "P2", "P3"], | ||
| types: ["bug", "enhancement", "task", "documentation"], | ||
| routing: ["app", "ui", "platform", "harness", "ci"], | ||
| issueForbiddenLabels: ["dependencies", "github_actions", "javascript"], | ||
| } | ||
|
|
||
| function intersection(labels, allowed) { | ||
| return allowed.filter((label) => labels.has(label)) | ||
| } | ||
|
|
||
| function error(message, labels) { | ||
| return { message, labels } | ||
| } | ||
|
|
||
| function labelList(labels) { | ||
| if (labels.length <= 1) return labels.join("") | ||
| return `${labels.slice(0, -1).join(", ")}, or ${labels[labels.length - 1]}` | ||
| } | ||
|
|
||
| export function validateLabelPolicy({ itemType, labels = [] }) { | ||
| const labelSet = new Set(labels) | ||
| const errors = [] | ||
|
|
||
| const priorities = intersection(labelSet, POLICY.priorities) | ||
| if (priorities.length !== 1) { | ||
| errors.push(error(`${itemType} must have exactly one priority label: ${labelList(POLICY.priorities)}`, priorities)) | ||
| } | ||
|
|
||
| const types = intersection(labelSet, POLICY.types) | ||
| if (types.length !== 1) { | ||
| errors.push(error(`${itemType} must have exactly one type label: ${labelList(POLICY.types)}`, types)) | ||
| } | ||
|
|
||
| const routing = intersection(labelSet, POLICY.routing) | ||
| if (routing.length < 1) { | ||
| errors.push(error(`${itemType} must have at least one primary routing label: ${labelList(POLICY.routing)}`, routing)) | ||
| } | ||
|
|
||
| if (labelSet.has("tech-debt") && !labelSet.has("task")) { | ||
| errors.push(error("tech-debt is only allowed with the task type label", ["tech-debt"])) | ||
| } | ||
|
|
||
| const forbiddenIssueLabels = | ||
| itemType === "issue" ? intersection(labelSet, POLICY.issueForbiddenLabels) : [] | ||
| if (forbiddenIssueLabels.length > 0) { | ||
| errors.push( | ||
| error(`issue must not use PR automation labels: ${labelList(POLICY.issueForbiddenLabels)}`, forbiddenIssueLabels), | ||
| ) | ||
| } | ||
|
|
||
| return { | ||
| ok: errors.length === 0, | ||
| errors, | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,128 @@ | ||
| import test from "node:test" | ||
| import assert from "node:assert/strict" | ||
|
|
||
| import { validateLabelPolicy } from "./label-policy-check.js" | ||
|
|
||
| function messages(result) { | ||
| return result.errors.map((error) => error.message) | ||
| } | ||
|
|
||
| test("accepts a valid issue label set", () => { | ||
| const result = validateLabelPolicy({ | ||
| itemType: "issue", | ||
| labels: ["task", "P2", "app", "tech-debt"], | ||
| }) | ||
|
|
||
| assert.deepEqual(result.errors, []) | ||
| }) | ||
|
|
||
| test("accepts a valid pull request label set", () => { | ||
| const result = validateLabelPolicy({ | ||
| itemType: "pull_request", | ||
| labels: ["enhancement", "P2", "app", "ui"], | ||
| }) | ||
|
|
||
| assert.deepEqual(result.errors, []) | ||
| }) | ||
|
|
||
| test("accepts ci as a primary routing label", () => { | ||
| const result = validateLabelPolicy({ | ||
| itemType: "pull_request", | ||
| labels: ["task", "P2", "ci"], | ||
| }) | ||
|
|
||
| assert.deepEqual(result.errors, []) | ||
| }) | ||
|
|
||
| test("rejects missing priority labels", () => { | ||
| const result = validateLabelPolicy({ | ||
| itemType: "issue", | ||
| labels: ["bug", "app"], | ||
| }) | ||
|
|
||
| assert.deepEqual(messages(result), ["issue must have exactly one priority label: P0, P1, P2, or P3"]) | ||
| }) | ||
|
|
||
| test("treats missing labels input as an empty label set", () => { | ||
| const result = validateLabelPolicy({ | ||
| itemType: "issue", | ||
| }) | ||
|
|
||
| assert.deepEqual(messages(result), [ | ||
| "issue must have exactly one priority label: P0, P1, P2, or P3", | ||
| "issue must have exactly one type label: bug, enhancement, task, or documentation", | ||
| "issue must have at least one primary routing label: app, ui, platform, harness, or ci", | ||
| ]) | ||
| }) | ||
|
|
||
| test("rejects multiple priority labels", () => { | ||
| const result = validateLabelPolicy({ | ||
| itemType: "issue", | ||
| labels: ["bug", "P1", "P2", "app"], | ||
| }) | ||
|
|
||
| assert.deepEqual(messages(result), ["issue must have exactly one priority label: P0, P1, P2, or P3"]) | ||
| }) | ||
|
|
||
| test("rejects missing type labels", () => { | ||
| const result = validateLabelPolicy({ | ||
| itemType: "pull_request", | ||
| labels: ["P2", "app"], | ||
| }) | ||
|
|
||
| assert.deepEqual(messages(result), [ | ||
| "pull_request must have exactly one type label: bug, enhancement, task, or documentation", | ||
| ]) | ||
| }) | ||
|
|
||
| test("rejects multiple type labels", () => { | ||
| const result = validateLabelPolicy({ | ||
| itemType: "issue", | ||
| labels: ["bug", "task", "P2", "app"], | ||
| }) | ||
|
|
||
| assert.deepEqual(messages(result), ["issue must have exactly one type label: bug, enhancement, task, or documentation"]) | ||
| }) | ||
|
|
||
| test("rejects missing primary routing labels", () => { | ||
| const result = validateLabelPolicy({ | ||
| itemType: "issue", | ||
| labels: ["task", "P2"], | ||
| }) | ||
|
|
||
| assert.deepEqual(messages(result), ["issue must have at least one primary routing label: app, ui, platform, harness, or ci"]) | ||
| }) | ||
|
|
||
| test("rejects tech-debt outside task issues", () => { | ||
| const result = validateLabelPolicy({ | ||
| itemType: "issue", | ||
| labels: ["bug", "P2", "app", "tech-debt"], | ||
| }) | ||
|
|
||
| assert.deepEqual(messages(result), ["tech-debt is only allowed with the task type label"]) | ||
| }) | ||
|
|
||
| test("rejects dependency automation labels on issues", () => { | ||
| const result = validateLabelPolicy({ | ||
| itemType: "issue", | ||
| labels: ["task", "P3", "app", "dependencies"], | ||
| }) | ||
|
|
||
| assert.deepEqual(messages(result), [ | ||
| "issue must not use PR automation labels: dependencies, github_actions, or javascript", | ||
| ]) | ||
| }) | ||
|
|
||
| test("reports all independent label policy failures", () => { | ||
| const result = validateLabelPolicy({ | ||
| itemType: "issue", | ||
| labels: ["bug", "enhancement", "P1", "P2", "dependencies"], | ||
| }) | ||
|
|
||
| assert.deepEqual(messages(result), [ | ||
| "issue must have exactly one priority label: P0, P1, P2, or P3", | ||
| "issue must have exactly one type label: bug, enhancement, task, or documentation", | ||
| "issue must have at least one primary routing label: app, ui, platform, harness, or ci", | ||
| "issue must not use PR automation labels: dependencies, github_actions, or javascript", | ||
| ]) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| name: label-policy | ||
|
|
||
| on: | ||
| issues: | ||
| types: [opened, edited, labeled, unlabeled, reopened] | ||
| pull_request_target: | ||
| types: [opened, edited, labeled, unlabeled, synchronize, reopened] | ||
| branches: [dev] | ||
|
|
||
| permissions: | ||
| contents: read | ||
| issues: read | ||
| pull-requests: read | ||
|
|
||
| jobs: | ||
| label-policy: | ||
| runs-on: ubuntu-latest | ||
| timeout-minutes: 5 | ||
| steps: | ||
| - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # actions/checkout@v6 | ||
| if: github.event_name == 'pull_request_target' | ||
| with: | ||
| persist-credentials: false | ||
| ref: ${{ github.event.pull_request.base.sha }} | ||
|
|
||
| - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # actions/checkout@v6 | ||
| if: github.event_name == 'issues' | ||
| with: | ||
| persist-credentials: false | ||
|
|
||
| - uses: actions/github-script@f28e40c7f34bde8b3046d885e986cb6290c5673b # actions/github-script@v7 | ||
| with: | ||
| script: | | ||
| const path = require("node:path") | ||
| const { pathToFileURL } = require("node:url") | ||
| const policy = await import( | ||
| pathToFileURL(path.join(process.env.GITHUB_WORKSPACE, ".github/scripts/label-policy-check.js")).href, | ||
| ) | ||
|
|
||
| const item = | ||
| context.eventName === "issues" ? context.payload.issue : context.payload.pull_request | ||
| const itemType = context.eventName === "issues" ? "issue" : "pull_request" | ||
| const labels = item.labels.map((label) => label.name) | ||
| const result = policy.validateLabelPolicy({ itemType, labels }) | ||
|
|
||
| if (!result.ok) { | ||
| const labelText = labels.length > 0 ? labels.join(", ") : "(none)" | ||
| const errorText = result.errors.map((error) => `- ${error.message}`).join("\n") | ||
| core.setFailed(`Label policy failed for ${itemType} #${item.number}.\n\nLabels: ${labelText}\n\n${errorText}`) | ||
| return | ||
| } | ||
|
|
||
| core.info(`Label policy passed for ${itemType} #${item.number}: ${labels.join(", ")}`) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.