Skip to content

armgit / kaneo

Public Imported from GitHub
0

⚡ Project management that doesn't become the project. Boards, Gantt charts, and GitHub integration. Self-host it or use Kaneo Cloud.

fix(ci): batch linked issue eligibility lookups

Tin21f212377dce
diff --git a/scripts/contribution-eligibility/check.mjs b/scripts/contribution-eligibility/check.mjs
index 8349ae1b0..8f8920116 100644
--- a/scripts/contribution-eligibility/check.mjs
+++ b/scripts/contribution-eligibility/check.mjs
@@ -18,16 +18,7 @@ async function checkPullRequest(github, pull, policy, policyError) {
if (exemption) {
result = { state: "success", description: exemption };
} else {
- const numbers = await github.linkedIssueNumbers(pull.number);
- const issues = [];
- for (const issueNumber of numbers) {
- const issue = await github.request(
- `repos/${github.repository}/issues/${issueNumber}`,
- { allowNotFound: true },
- );
- if (issue) issues.push(issue);
- }
- result = assessIssues(issues);
+ result = assessIssues(await github.linkedIssues(pull.number));
}
const current = await github.request(
`repos/${github.repository}/pulls/${pull.number}`,
diff --git a/scripts/contribution-eligibility/check.test.mjs b/scripts/contribution-eligibility/check.test.mjs
index cddecfbb0..5ad2795e2 100644
--- a/scripts/contribution-eligibility/check.test.mjs
+++ b/scripts/contribution-eligibility/check.test.mjs
@@ -19,7 +19,15 @@ const issue = {
state: "open",
labels: [{ name: "ready-for-contribution" }],
};
-const link = { number: 12, repository: { nameWithOwner: "test/repo" } };
+const link = {
+ number: 12,
+ repository: { nameWithOwner: "test/repo" },
+ state: "OPEN",
+ labels: {
+ nodes: issue.labels,
+ pageInfo: { hasNextPage: false, endCursor: null },
+ },
+};
function fixture(options = {}) {
const requests = [];
@@ -68,8 +76,23 @@ function fixture(options = {}) {
status = options.permissionStatus ?? (options.permission ? 200 : 404);
data = options.permission ?? {};
} else if (path === "/graphql") {
- const page = options.linkPages?.[body.variables.after ? 1 : 0] ?? {
- nodes: options.links ?? [link],
+ if (body.query.includes("issue(number:")) {
+ return Response.json({
+ data: { repository: { issue: { labels: options.labelPage } } },
+ });
+ }
+ const pageIndex = body.variables.after ? Number(body.variables.after) : 0;
+ const page = options.linkPages?.[pageIndex] ?? {
+ nodes: options.links ?? [
+ {
+ ...link,
+ state: (options.issue ?? issue).state.toUpperCase(),
+ labels: {
+ nodes: (options.issue ?? issue).labels,
+ pageInfo: { hasNextPage: false, endCursor: null },
+ },
+ },
+ ],
pageInfo: { hasNextPage: false, endCursor: null },
};
data = options.graphqlErrors
@@ -82,9 +105,6 @@ function fixture(options = {}) {
repository: { pullRequest: { closingIssuesReferences: page } },
},
};
- } else if (path.startsWith("/repos/test/repo/issues/")) {
- data = options.issue ?? issue;
- status = options.issueStatus ?? 200;
} else {
throw new Error(`Unexpected test request: ${path}`);
}
@@ -123,7 +143,7 @@ test("mere mentions, foreign issues, deleted issues and revoked approval fail",
for (const options of [
{ links: [] },
{ links: [{ ...link, repository: { nameWithOwner: "other/repo" } }] },
- { issueStatus: 404 },
+ { links: [null] },
{ issue: { ...issue, labels: [] } },
{ issue: { ...issue, state: "closed" } },
]) {
@@ -183,7 +203,6 @@ test("eligibility lookup and policy errors publish failing checks", async () =>
{ permissionStatus: 403 },
{ permissionStatus: 500 },
{ graphqlErrors: true },
- { issueStatus: 500 },
]) {
const { github, statuses } = fixture(options);
await assert.rejects(
@@ -317,7 +336,7 @@ test("linked issues paginate and accept case-insensitive repository names", asyn
linkPages: [
{
nodes: [{ ...link, repository: { nameWithOwner: "foreign/repo" } }],
- pageInfo: { hasNextPage: true, endCursor: "next" },
+ pageInfo: { hasNextPage: true, endCursor: "1" },
},
{
nodes: [{ ...link, repository: { nameWithOwner: "TEST/REPO" } }, link],
@@ -325,13 +344,84 @@ test("linked issues paginate and accept case-insensitive repository names", asyn
},
],
});
- assert.deepEqual(await github.linkedIssueNumbers(1), [12]);
+ assert.deepEqual(await github.linkedIssues(1), [issue]);
assert.deepEqual(
requests.map((request) => request.body.variables.after),
- [null, "next"],
+ [null, "1"],
);
});
+test("a thousand closing references use ten batched issue requests", async () => {
+ const linkPages = Array.from({ length: 10 }, (_, page) => ({
+ nodes: Array.from({ length: 100 }, (_, index) => ({
+ ...link,
+ number: page * 100 + index + 1,
+ labels: {
+ ...link.labels,
+ nodes: page === 9 && index === 99 ? issue.labels : [],
+ },
+ })),
+ pageInfo: { hasNextPage: page < 9, endCursor: String(page + 1) },
+ }));
+ const { github, requests, statuses } = fixture({ linkPages });
+ await reconcile(github, async () => policy);
+ assert.equal(statuses.at(-1).state, "success");
+ assert.match(statuses.at(-1).description, /issue #1000/);
+ assert.equal(requests.filter(({ path }) => path === "/graphql").length, 10);
+ assert.equal(
+ requests.some(({ path }) => path.includes("/issues/")),
+ false,
+ );
+});
+
+test("approval labels beyond the first page are included", async () => {
+ const { github, requests, statuses } = fixture({
+ links: [
+ {
+ ...link,
+ labels: {
+ nodes: Array.from({ length: 100 }, (_, index) => ({
+ name: `label-${index}`,
+ })),
+ pageInfo: { hasNextPage: true, endCursor: "labels-next" },
+ },
+ },
+ ],
+ labelPage: {
+ nodes: issue.labels,
+ pageInfo: { hasNextPage: false, endCursor: null },
+ },
+ });
+ await reconcile(github, async () => policy);
+ assert.equal(statuses.at(-1).state, "success");
+ const labelRequest = requests.find(({ body }) =>
+ body?.query?.includes("issue(number:"),
+ );
+ assert.equal(labelRequest.body.variables.number, 12);
+ assert.equal(labelRequest.body.variables.after, "labels-next");
+});
+
+test("missing or invalid label pages fail the affected eligibility check", async () => {
+ for (const cursor of [null, "labels-next"]) {
+ const { github, statuses } = fixture({
+ links: [
+ {
+ ...link,
+ labels: {
+ nodes: [],
+ pageInfo: { hasNextPage: true, endCursor: cursor },
+ },
+ },
+ ],
+ });
+ await assert.rejects(
+ reconcile(github, async () => policy),
+ AggregateError,
+ );
+ assert.equal(statuses.at(-1).state, "failure");
+ }
+});
+
test("open PRs paginate beyond the first hundred", async () => {
const first = Array.from({ length: 100 }, (_, index) => ({
number: index + 1,
@@ -346,7 +436,7 @@ test("invalid pagination and missing configuration reject", async () => {
{ nodes: [], pageInfo: { hasNextPage: true, endCursor: null } },
],
});
- await assert.rejects(github.linkedIssueNumbers(1), /pagination cursor/);
+ await assert.rejects(github.linkedIssues(1), /pagination cursor/);
assert.throws(
() =>
new GitHub({ GITHUB_REPOSITORY: "../invalid", GH_TOKEN: "synthetic" }),
diff --git a/scripts/contribution-eligibility/github.mjs b/scripts/contribution-eligibility/github.mjs
index a0eff0637..7c4b19b81 100644
--- a/scripts/contribution-eligibility/github.mjs
+++ b/scripts/contribution-eligibility/github.mjs
@@ -49,9 +49,9 @@ export class GitHub {
}
}
- async linkedIssueNumbers(number) {
+ async linkedIssues(number) {
const [owner, name] = this.repository.split("/");
- const numbers = new Set();
+ const issues = new Map();
let after = null;
do {
const result = await this.request("graphql", {
@@ -61,7 +61,15 @@ export class GitHub {
repository(owner: $owner, name: $name) {
pullRequest(number: $number) {
closingIssuesReferences(first: 100, after: $after) {
- nodes { number repository { nameWithOwner } }
+ nodes {
+ number
+ state
+ repository { nameWithOwner }
+ labels(first: 100) {
+ nodes { name }
+ pageInfo { hasNextPage endCursor }
+ }
+ }
pageInfo { hasNextPage endCursor }
}
}
@@ -75,13 +83,18 @@ export class GitHub {
if (!connection) throw new Error("GitHub did not return linked issues.");
for (const issue of connection.nodes) {
if (
+ issue &&
issue.repository.nameWithOwner.toLowerCase() ===
- this.repository.toLowerCase()
+ this.repository.toLowerCase()
) {
- numbers.add(issue.number);
+ issues.set(issue.number, {
+ number: issue.number,
+ state: issue.state.toLowerCase(),
+ labels: await this.issueLabels(issue.number, issue.labels),
+ });
}
}
- if (!connection.pageInfo.hasNextPage) return [...numbers];
+ if (!connection.pageInfo.hasNextPage) return [...issues.values()];
if (
!connection.pageInfo.endCursor ||
connection.pageInfo.endCursor === after
@@ -91,4 +104,40 @@ export class GitHub {
after = connection.pageInfo.endCursor;
} while (after);
}
+
+ async issueLabels(number, firstPage) {
+ const [owner, name] = this.repository.split("/");
+ let connection = firstPage;
+ const labels = [...connection.nodes];
+ let after = null;
+ while (connection.pageInfo.hasNextPage) {
+ if (
+ !connection.pageInfo.endCursor ||
+ connection.pageInfo.endCursor === after
+ ) {
+ throw new Error("GitHub returned an invalid label pagination cursor.");
+ }
+ after = connection.pageInfo.endCursor;
+ const result = await this.request("graphql", {
+ method: "POST",
+ body: {
+ query: `query($owner: String!, $name: String!, $number: Int!, $after: String) {
+ repository(owner: $owner, name: $name) {
+ issue(number: $number) {
+ labels(first: 100, after: $after) {
+ nodes { name }
+ pageInfo { hasNextPage endCursor }
+ }
+ }
+ }
+ }`,
+ variables: { owner, name, number, after },
+ },
+ });
+ connection = result.data?.repository?.issue?.labels;
+ if (!connection) throw new Error("GitHub did not return issue labels.");
+ labels.push(...connection.nodes);
+ }
+ return labels;
+ }
}