Skip to content
This repository was archived by the owner on Jul 1, 2024. It is now read-only.

Commit 494d9a1

Browse files
committed
Specify closed PRs to run.ts
1 parent 3d87cbc commit 494d9a1

File tree

7 files changed

+112
-126
lines changed

7 files changed

+112
-126
lines changed

src/queries/all-open-prs-query.ts

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
import { gql, TypedDocumentNode } from "@apollo/client/core";
22
import { client } from "../graphql-client";
3-
import { GetAllOpenPRsAndCardIDs, GetAllOpenPRsAndCardIDsVariables } from "./schema/GetAllOpenPRsAndCardIDs";
3+
import { GetAllOpenPRs, GetAllOpenPRsVariables } from "./schema/GetAllOpenPRs";
44
import { noNullish } from "../util/util";
55

6-
export const getAllOpenPRsAndCardIDsQuery: TypedDocumentNode<GetAllOpenPRsAndCardIDs, GetAllOpenPRsAndCardIDsVariables> = gql`
7-
query GetAllOpenPRsAndCardIDs($endCursor: String) {
6+
export const getAllOpenPRsQuery: TypedDocumentNode<GetAllOpenPRs, GetAllOpenPRsVariables> = gql`
7+
query GetAllOpenPRs($endCursor: String) {
88
repository(owner: "DefinitelyTyped", name: "DefinitelyTyped") {
99
id
1010
pullRequests(states: OPEN, orderBy: { field: UPDATED_AT, direction: DESC }, first: 100, after: $endCursor) {
1111
nodes {
1212
number
13-
projectCards(first: 100) { nodes { id } }
1413
}
1514
pageInfo {
1615
hasNextPage
@@ -20,22 +19,18 @@ query GetAllOpenPRsAndCardIDs($endCursor: String) {
2019
}
2120
}`;
2221

23-
export async function getAllOpenPRsAndCardIDs() {
22+
export async function getAllOpenPRs() {
2423
const prNumbers: number[] = [];
25-
const cardIDs: string[] = [];
2624
let endCursor: string | undefined | null;
2725
while (true) {
2826
const result = await client.query({
29-
query: getAllOpenPRsAndCardIDsQuery,
27+
query: getAllOpenPRsQuery,
3028
fetchPolicy: "no-cache",
3129
variables: { endCursor },
3230
});
3331
prNumbers.push(...noNullish(result.data.repository?.pullRequests.nodes).map(pr => pr.number));
34-
for (const pr of noNullish(result.data.repository?.pullRequests.nodes)) {
35-
cardIDs.push(...noNullish(pr.projectCards.nodes).map(card => card.id));
36-
}
3732
if (!result.data.repository?.pullRequests.pageInfo.hasNextPage) {
38-
return { prNumbers, cardIDs };
33+
return prNumbers;
3934
}
4035
endCursor = result.data.repository.pullRequests.pageInfo.endCursor;
4136
}

src/queries/projectboard-cards.ts

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { gql, TypedDocumentNode } from "@apollo/client/core";
22
import { client } from "../graphql-client";
33
import { GetProjectBoardCards } from "./schema/GetProjectBoardCards";
4+
import { noNullish } from "../util/util";
45

56
const GetProjectBoardCardsQuery: TypedDocumentNode<GetProjectBoardCards, never> = gql`
67
query GetProjectBoardCards {
@@ -17,6 +18,11 @@ const GetProjectBoardCardsQuery: TypedDocumentNode<GetProjectBoardCards, never>
1718
nodes {
1819
id
1920
updatedAt
21+
content {
22+
... on PullRequest {
23+
number
24+
}
25+
}
2026
}
2127
}
2228
}
@@ -25,16 +31,6 @@ const GetProjectBoardCardsQuery: TypedDocumentNode<GetProjectBoardCards, never>
2531
}
2632
}`;
2733

28-
interface CardInfo {
29-
id: string;
30-
updatedAt: string;
31-
}
32-
interface ColumnInfo {
33-
name: string;
34-
totalCount: number;
35-
cards: CardInfo[];
36-
}
37-
3834
export async function getProjectBoardCards() {
3935
const results = await client.query({
4036
query: GetProjectBoardCardsQuery,
@@ -47,17 +43,13 @@ export async function getProjectBoardCards() {
4743
throw new Error("No project found");
4844
}
4945

50-
const columns: ColumnInfo[] = [];
51-
project.columns.nodes?.forEach(col => {
52-
if (!col) return;
53-
const cards: CardInfo[] = [];
54-
col.cards.nodes?.forEach(card => card && cards.push({ id: card.id, updatedAt: card.updatedAt }));
55-
columns.push({
56-
name: col.name,
57-
totalCount: col.cards.totalCount,
58-
cards,
59-
});
60-
});
61-
62-
return columns;
46+
return noNullish(project.columns.nodes).map(column => ({
47+
name: column.name,
48+
totalCount: column.cards.totalCount,
49+
cards: noNullish(column.cards.nodes).map(card => ({
50+
id: card.id,
51+
updatedAt: card.updatedAt,
52+
number: card.content && "number" in card.content ? card.content.number : undefined,
53+
})),
54+
}));
6355
}

src/queries/schema/GetAllOpenPRs.ts

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/* tslint:disable */
2+
/* eslint-disable */
3+
// @generated
4+
// This file was automatically generated and should not be edited.
5+
6+
// ====================================================
7+
// GraphQL query operation: GetAllOpenPRs
8+
// ====================================================
9+
10+
export interface GetAllOpenPRs_repository_pullRequests_nodes {
11+
__typename: "PullRequest";
12+
/**
13+
* Identifies the pull request number.
14+
*/
15+
number: number;
16+
}
17+
18+
export interface GetAllOpenPRs_repository_pullRequests_pageInfo {
19+
__typename: "PageInfo";
20+
/**
21+
* When paginating forwards, are there more items?
22+
*/
23+
hasNextPage: boolean;
24+
/**
25+
* When paginating forwards, the cursor to continue.
26+
*/
27+
endCursor: string | null;
28+
}
29+
30+
export interface GetAllOpenPRs_repository_pullRequests {
31+
__typename: "PullRequestConnection";
32+
/**
33+
* A list of nodes.
34+
*/
35+
nodes: (GetAllOpenPRs_repository_pullRequests_nodes | null)[] | null;
36+
/**
37+
* Information to aid in pagination.
38+
*/
39+
pageInfo: GetAllOpenPRs_repository_pullRequests_pageInfo;
40+
}
41+
42+
export interface GetAllOpenPRs_repository {
43+
__typename: "Repository";
44+
id: string;
45+
/**
46+
* A list of pull requests that have been opened in the repository.
47+
*/
48+
pullRequests: GetAllOpenPRs_repository_pullRequests;
49+
}
50+
51+
export interface GetAllOpenPRs {
52+
/**
53+
* Lookup a given repository by the owner and repository name.
54+
*/
55+
repository: GetAllOpenPRs_repository | null;
56+
}
57+
58+
export interface GetAllOpenPRsVariables {
59+
endCursor?: string | null;
60+
}

src/queries/schema/GetAllOpenPRsAndCardIDs.ts

Lines changed: 0 additions & 77 deletions
This file was deleted.

src/queries/schema/GetProjectBoardCards.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,31 @@
77
// GraphQL query operation: GetProjectBoardCards
88
// ====================================================
99

10+
export interface GetProjectBoardCards_repository_project_columns_nodes_cards_nodes_content_Issue {
11+
__typename: "Issue";
12+
}
13+
14+
export interface GetProjectBoardCards_repository_project_columns_nodes_cards_nodes_content_PullRequest {
15+
__typename: "PullRequest";
16+
/**
17+
* Identifies the pull request number.
18+
*/
19+
number: number;
20+
}
21+
22+
export type GetProjectBoardCards_repository_project_columns_nodes_cards_nodes_content = GetProjectBoardCards_repository_project_columns_nodes_cards_nodes_content_Issue | GetProjectBoardCards_repository_project_columns_nodes_cards_nodes_content_PullRequest;
23+
1024
export interface GetProjectBoardCards_repository_project_columns_nodes_cards_nodes {
1125
__typename: "ProjectCard";
1226
id: string;
1327
/**
1428
* Identifies the date and time when the object was last updated.
1529
*/
1630
updatedAt: any;
31+
/**
32+
* The card content item
33+
*/
34+
content: GetProjectBoardCards_repository_project_columns_nodes_cards_nodes_content | null;
1735
}
1836

1937
export interface GetProjectBoardCards_repository_project_columns_nodes_cards {

src/run.ts

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@
33
import * as schema from "@octokit/graphql-schema/schema";
44
import * as yargs from "yargs";
55
import { process as computeActions } from "./compute-pr-actions";
6-
import { getAllOpenPRsAndCardIDs } from "./queries/all-open-prs-query";
6+
import { getAllOpenPRs } from "./queries/all-open-prs-query";
77
import { queryPRInfo, deriveStateForPR } from "./pr-info";
88
import { executePrActions } from "./execute-pr-actions";
99
import { getProjectBoardCards } from "./queries/projectboard-cards";
1010
import { runQueryToGetPRForCardId } from "./queries/card-id-to-pr-query";
1111
import { createMutation, client } from "./graphql-client";
12+
import { flatten } from "./util/util";
1213
import { render } from "prettyjson";
1314
import { inspect } from "util";
1415

@@ -26,21 +27,18 @@ const args = yargs(process.argv.slice(2))
2627
"show-actions": { alias: ["s4"], type: "boolean", desc: "display actions" },
2728
"show-mutations": { alias: ["s5"], type: "boolean", desc: "display mutations" },
2829
})
29-
.coerce("_", (prs: (number|string)[]) => prs.map(pr => {
30-
if (typeof pr === "number") return (n: number) => n === pr;
31-
if (pr.match(/^\d+$/)) return (n: number) => n === +pr;
30+
.coerce("_", (prs: (number|string)[]) => flatten(prs.map(pr => {
31+
if (typeof pr === "number") return pr;
32+
if (pr.match(/^\d+$/)) return +pr;
3233
const m = pr.match(/^(\d+)-(\d+)$/);
3334
if (!m) throw new Error(`bad PR or PR range argument: "${pr}"`);
3435
const lo = +m[1]!, hi = +m[2]!;
35-
return (n: number) => lo <= n && n <= hi;
36-
}))
36+
return new Array(hi - lo + 1).map((empty, i) => lo + i);
37+
})))
3738
.help("h").alias("h", "help")
3839
.strict()
3940
.argv;
4041

41-
const shouldRunOn: (n: number) => boolean =
42-
args._.length === 0 ? _n => true : n => args._.some(p => p(n));
43-
4442
const xform = (x: unknown, xlate: (s: string) => string): unknown => {
4543
if (typeof x === "string") return xlate(x);
4644
if (typeof x !== "object" || x === null) return x;
@@ -62,10 +60,9 @@ const show = (name: string, value: unknown) => {
6260

6361
const start = async function () {
6462
console.log(`Getting open PRs.`);
65-
const { prNumbers: prs, cardIDs } = await getAllOpenPRsAndCardIDs();
63+
const prs = args._.length ? args._ : await getAllOpenPRs();
6664
//
6765
for (const pr of prs) {
68-
if (!shouldRunOn(pr)) continue;
6966
console.log(`Processing #${pr} (${prs.indexOf(pr) + 1} of ${prs.length})...`);
7067
// Generate the info for the PR from scratch
7168
const info = await queryPRInfo(pr);
@@ -90,7 +87,7 @@ const start = async function () {
9087
const mutations = await executePrActions(actions, prInfo, args.dry);
9188
if (args["show-mutations"] ?? args.dry) show("Mutations", mutations);
9289
}
93-
if (args.dry || !args.cleanup) return;
90+
if (args.dry || !args.cleanup || args._.length) return;
9491
//
9592
console.log("Cleaning up cards");
9693
const columns = await getProjectBoardCards();
@@ -122,7 +119,7 @@ const start = async function () {
122119
// Handle other columns
123120
for (const column of columns) {
124121
if (column.name === "Recently Merged") continue;
125-
const ids = column.cards.map(c => c.id).filter(c => !cardIDs.includes(c));
122+
const ids = column.cards.filter(c => c.number && !prs.includes(c.number)).map(c => c.id);
126123
if (ids.length === 0) continue;
127124
console.log(`Cleaning up closed PRs in "${column.name}"`);
128125
// don't actually do the deletions, until I follow this and make sure that it's working fine

src/util/util.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ export function noNullish<T>(arr: ReadonlyArray<T | null | undefined> | null | u
55
return arr.filter(arr => arr != null) as T[];
66
}
77

8-
export function flatten<T extends any[]>(xs: T[]) {
9-
return ([] as unknown as T).concat(...xs) as T;
8+
export function flatten<T>(xs: T[]) {
9+
type U = T extends (infer U)[] ? U : T;
10+
return ([] as U[]).concat(...xs as U[]);
1011
}
1112

1213
export function unique<T>(xs: T[]) {

0 commit comments

Comments
 (0)