⚡ Project management that doesn't become the project. Boards, Gantt charts, and GitHub integration. Self-host it or use Kaneo Cloud.
TypeScript honoissue-managementissue-trackerjira-alternativekanbanlinear-alternativemcpproject-managementreactself-hostedtypescript
fix: convert ineligible contributions to draft pull requests
Tin Sever291da4a413a5
diff --git a/.github/workflows/contribution-eligibility.yml b/.github/workflows/contribution-eligibility.ymlindex bffe15d27..5b5d3c414 100644--- a/.github/workflows/contribution-eligibility.yml+++ b/.github/workflows/contribution-eligibility.yml@@ -27,7 +27,7 @@ on:permissions:contents: read- pull-requests: read+ pull-requests: writeissues: readchecks: writediff --git a/CONTRIBUTING.md b/CONTRIBUTING.mdindex c33a65f05..df2a95726 100644--- a/CONTRIBUTING.md+++ b/CONTRIBUTING.md@@ -60,8 +60,12 @@ account, can't be handed to someone else, and only skips the issue step.Everyone still goes through code review, CI, and the [AI policy](AI_POLICY.md).Past contributions or org membership don't earn a voucher automatically.-The **Contribution eligibility** check on your PR tells you where you stand. If-it fails, link an approved issue or ask a maintainer to vouch for you.+The **Contribution eligibility** check on your PR tells you where you stand.+If you're ineligible, the automation converts your PR to a draft. Link an+approved issue or ask a maintainer to vouch for you, then mark the PR ready+for review once the check passes. Eligible drafts stay drafts until you mark+them ready. If eligibility cannot be checked because of an API or policy+error, the check fails without changing the PR's draft status.The check reruns when you edit the PR description or push commits, when anissue gains or loses the label, and when the voucher list changes. It can't@@ -70,8 +74,8 @@ those up (GitHub sometimes runs these late). Maintainers can also trigger theworkflow by hand. Dependabot PRs are checked after their CI run finishes,because bot-triggered workflows may only get read-only tokens.-Once the check is required, a failing result blocks merging. It won't stop-anyone from opening a PR, and it never closes one.+Once the check is required, a failing result blocks merging. Anyone can still+open a PR; ineligible PRs become drafts and are never automatically closed.#### Managing Vouchersdiff --git a/scripts/contribution-eligibility/check.mjs b/scripts/contribution-eligibility/check.mjsindex 8f8920116..9f700258c 100644--- a/scripts/contribution-eligibility/check.mjs+++ b/scripts/contribution-eligibility/check.mjs@@ -34,6 +34,9 @@ async function checkPullRequest(github, pull, policy, policyError) {description: "Pull request changed; waiting for a fresh check.",};}+ if (result.state === "failure" && !current.draft) {+ await github.convertToDraft(current.node_id);+ }return result;}diff --git a/scripts/contribution-eligibility/check.test.mjs b/scripts/contribution-eligibility/check.test.mjsindex 5ad2795e2..ab7b06021 100644--- a/scripts/contribution-eligibility/check.test.mjs+++ b/scripts/contribution-eligibility/check.test.mjs@@ -8,6 +8,8 @@ const sha = "a".repeat(40);const policy = { vouchedContributors: [], exemptBots: [] };const pull = {number: 1,+ node_id: "PR_test_1",+ draft: false,state: "open",user: { id: 123, login: "newcomer", type: "User" },head: { sha },@@ -33,6 +35,8 @@ function fixture(options = {}) {const requests = [];const statuses = [];const checks = new Map();+ const drafts = new Set();+ const conversions = [];const pulls = options.pulls ?? [pull];const fetcher = async (url, init) => {const parsed = new URL(url);@@ -71,11 +75,35 @@ function fixture(options = {}) {const current = pulls.find((item) => item.number === Number(path.split("/").at(-1)),);- data = { ...current, ...options.current };+ data = {+ ...current,+ draft: drafts.has(current.node_id) || current.draft,+ ...options.current,+ };} else if (path.includes("/collaborators/")) {status = options.permissionStatus ?? (options.permission ? 200 : 404);data = options.permission ?? {};} else if (path === "/graphql") {+ if (body.query.includes("convertPullRequestToDraft")) {+ if (options.conversionStatus) {+ return Response.json({}, { status: options.conversionStatus });+ }+ if (options.conversionErrors) {+ return Response.json({ errors: [{ message: "synthetic failure" }] });+ }+ conversions.push(body.variables.pullRequestId);+ drafts.add(body.variables.pullRequestId);+ return Response.json({+ data: {+ convertPullRequestToDraft: {+ pullRequest: {+ id: body.variables.pullRequestId,+ isDraft: options.conversionDraft ?? true,+ },+ },+ },+ });+ }if (body.query.includes("issue(number:")) {return Response.json({data: { repository: { issue: { labels: options.labelPage } } },@@ -119,9 +147,88 @@ function fixture(options = {}) {},fetcher,);- return { github, requests, statuses };+ return { github, requests, statuses, conversions };}+test("ineligible PRs become drafts once and are re-drafted if marked ready", async () => {+ const options = { links: [] };+ const { github, conversions, statuses } = fixture(options);+ await reconcile(github, async () => policy);+ assert.deepEqual(conversions, [pull.node_id]);+ assert.equal(statuses.at(-1).state, "failure");+ await reconcile(github, async () => policy);+ assert.deepEqual(conversions, [pull.node_id]);++ options.current = { draft: false };+ await reconcile(github, async () => policy);+ assert.deepEqual(conversions, [pull.node_id, pull.node_id]);+});++test("eligible PRs and existing drafts keep their draft status", async () => {+ for (const options of [+ {},+ { pulls: [{ ...pull, draft: true }] },+ { pulls: [{ ...pull, draft: true }], links: [] },+ { links: [], permission: { permission: "admin" } },+ ]) {+ const { github, conversions } = fixture(options);+ await reconcile(github, async () => policy);+ assert.deepEqual(conversions, []);+ }+});++test("approving an issue after conversion does not mark the draft ready", async () => {+ const options = { links: [] };+ const { github, conversions, statuses, requests } = fixture(options);+ await reconcile(github, async () => policy);+ options.links = [link];+ await reconcile(github, async () => policy);+ assert.equal(statuses.at(-1).state, "success");+ assert.deepEqual(conversions, [pull.node_id]);+ assert.equal(+ requests.some(({ body }) =>+ body?.query?.includes("markPullRequestReadyForReview"),+ ),+ false,+ );+});++test("stale or closed ineligible PRs are not converted", async () => {+ for (const current of [+ { head: { sha: "b".repeat(40) } },+ { body: "Fixes #99" },+ { base: { ref: "other" } },+ { state: "closed" },+ ]) {+ const { github, conversions } = fixture({ current, links: [] });+ await reconcile(github, async () => policy);+ assert.deepEqual(conversions, []);+ }+});++test("conversion failures fail the check and retry on the next run", async () => {+ for (const failure of [+ { conversionStatus: 403 },+ { conversionErrors: true },+ { conversionDraft: false },+ { current: { node_id: undefined } },+ ]) {+ const options = { links: [], ...failure };+ const { github, statuses } = fixture(options);+ await assert.rejects(+ reconcile(github, async () => policy),+ AggregateError,+ );+ assert.equal(statuses.at(-1).state, "failure");+ assert.match(statuses.at(-1).description, /could not be checked/);+ for (const key of Object.keys(failure)) delete options[key];+ options.current = { draft: false };+ await reconcile(github, async () => policy);+ assert.equal(statuses.at(-1).state, "failure");+ assert.match(statuses.at(-1).description, /ready-for-contribution/);+ }+});+test("approved issue publishes pending then success on the fork's exact head", async () => {const { github, statuses } = fixture();const results = await reconcile(github, async () => policy);@@ -147,10 +254,11 @@ test("mere mentions, foreign issues, deleted issues and revoked approval fail",{ issue: { ...issue, labels: [] } },{ issue: { ...issue, state: "closed" } },]) {- const { github, statuses } = fixture(options);+ const { github, statuses, conversions } = fixture(options);await reconcile(github, async () => policy);assert.equal(statuses.at(-1).state, "failure");assert.match(statuses.at(-1).description, /ready-for-contribution/);+ assert.deepEqual(conversions, [pull.node_id]);}});@@ -204,7 +312,7 @@ test("eligibility lookup and policy errors publish failing checks", async () =>{ permissionStatus: 500 },{ graphqlErrors: true },]) {- const { github, statuses } = fixture(options);+ const { github, statuses, conversions } = fixture(options);await assert.rejects(reconcile(github, async () => policy),AggregateError,@@ -213,6 +321,7 @@ test("eligibility lookup and policy errors publish failing checks", async () =>statuses.map((status) => status.state),["pending", "failure"],);+ assert.deepEqual(conversions, []);}for (const loader of [async () => ({}),@@ -220,9 +329,10 @@ test("eligibility lookup and policy errors publish failing checks", async () =>throw new SyntaxError("Invalid JSON");},]) {- const { github, statuses } = fixture();+ const { github, statuses, conversions } = fixture();await assert.rejects(reconcile(github, loader), AggregateError);assert.equal(statuses.at(-1).state, "failure");+ assert.deepEqual(conversions, []);}});@@ -280,8 +390,13 @@ test("a changed head, body or target cannot receive a stale success", async () =test("a vouched PR never overrides an ineligible PR on the same commit", async () => {for (const reverse of [false, true]) {- const newcomer = { ...pull, number: 2, user: { ...pull.user, id: 456 } };- const { github, statuses } = fixture({+ const newcomer = {+ ...pull,+ number: 2,+ node_id: "PR_test_2",+ user: { ...pull.user, id: 456 },+ };+ const { github, statuses, conversions } = fixture({pulls: reverse ? [newcomer, pull] : [pull, newcomer],links: [],});@@ -295,6 +410,7 @@ test("a vouched PR never overrides an ineligible PR on the same commit", async (false,);assert.match(statuses.at(-1).description, /PR #2/);+ assert.deepEqual(conversions, [newcomer.node_id]);}});diff --git a/scripts/contribution-eligibility/github.mjs b/scripts/contribution-eligibility/github.mjsindex 7c4b19b81..39dc732da 100644--- a/scripts/contribution-eligibility/github.mjs+++ b/scripts/contribution-eligibility/github.mjs@@ -49,6 +49,27 @@ export class GitHub {}}+ async convertToDraft(pullRequestId) {+ if (typeof pullRequestId !== "string" || !pullRequestId.trim()) {+ throw new Error("Missing pull request node ID.");+ }+ const result = await this.request("graphql", {+ method: "POST",+ body: {+ query: `mutation($pullRequestId: ID!) {+ convertPullRequestToDraft(input: { pullRequestId: $pullRequestId }) {+ pullRequest { id isDraft }+ }+ }`,+ variables: { pullRequestId },+ },+ });+ const converted = result.data?.convertPullRequestToDraft?.pullRequest;+ if (converted?.id !== pullRequestId || converted.isDraft !== true) {+ throw new Error("GitHub did not convert the pull request to draft.");+ }+ }+async linkedIssues(number) {const [owner, name] = this.repository.split("/");const issues = new Map();