Skip to content
This repository was archived by the owner on Jul 1, 2024. It is now read-only.
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 0cb46ed

Browse files
committedJan 31, 2021
Trigger bot to cleanup columns
1 parent 3d87cbc commit 0cb46ed

8 files changed

+133
-224
lines changed
 

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

+6-11
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/card-id-to-pr-query.ts

-26
This file was deleted.

‎src/queries/projectboard-cards.ts

+15-23
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/CardIdToPr.ts

-53
This file was deleted.

‎src/queries/schema/GetAllOpenPRs.ts

+60
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

-77
This file was deleted.

‎src/queries/schema/GetProjectBoardCards.ts

+18
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

+34-34
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,10 @@
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";
10-
import { runQueryToGetPRForCardId } from "./queries/card-id-to-pr-query";
1110
import { createMutation, client } from "./graphql-client";
1211
import { render } from "prettyjson";
1312
import { inspect } from "util";
@@ -60,35 +59,39 @@ const show = (name: string, value: unknown) => {
6059
console.log(str);
6160
};
6261

62+
async function processSingle(pr: number) {
63+
// Generate the info for the PR from scratch
64+
const info = await queryPRInfo(pr);
65+
if (args["show-raw"]) show("Raw Query Result", info);
66+
const prInfo = info.data.repository?.pullRequest;
67+
// If it didn't work, bail early
68+
if (!prInfo) {
69+
console.error(` No PR with this number exists, (${JSON.stringify(info)})`);
70+
return;
71+
}
72+
const state = await deriveStateForPR(prInfo);
73+
if (args["show-basic"]) show("Basic PR Info", state);
74+
// Show errors in log but keep processing to show in a comment too
75+
if (state.type === "error") console.error(` Error: ${state.message}`);
76+
// Show other messages too
77+
if ("message" in state) console.log(` ... ${state.message}`);
78+
// Convert the info to a set of actions for the bot
79+
const actions = computeActions(state,
80+
args["show-extended"] ? i => show("Extended Info", i) : undefined);
81+
if (args["show-actions"]) show("Actions", actions);
82+
// Act on the actions
83+
const mutations = await executePrActions(actions, prInfo, args.dry);
84+
if (args["show-mutations"] ?? args.dry) show("Mutations", mutations);
85+
}
86+
6387
const start = async function () {
6488
console.log(`Getting open PRs.`);
65-
const { prNumbers: prs, cardIDs } = await getAllOpenPRsAndCardIDs();
89+
const prs = await getAllOpenPRs();
6690
//
6791
for (const pr of prs) {
6892
if (!shouldRunOn(pr)) continue;
6993
console.log(`Processing #${pr} (${prs.indexOf(pr) + 1} of ${prs.length})...`);
70-
// Generate the info for the PR from scratch
71-
const info = await queryPRInfo(pr);
72-
if (args["show-raw"]) show("Raw Query Result", info);
73-
const prInfo = info.data.repository?.pullRequest;
74-
// If it didn't work, bail early
75-
if (!prInfo) {
76-
console.error(` No PR with this number exists, (${JSON.stringify(info)})`);
77-
continue;
78-
}
79-
const state = await deriveStateForPR(prInfo);
80-
if (args["show-basic"]) show("Basic PR Info", state);
81-
// Show errors in log but keep processing to show in a comment too
82-
if (state.type === "error") console.error(` Error: ${state.message}`);
83-
// Show other messages too
84-
if ("message" in state) console.log(` ... ${state.message}`);
85-
// Convert the info to a set of actions for the bot
86-
const actions = computeActions(state,
87-
args["show-extended"] ? i => show("Extended Info", i) : undefined);
88-
if (args["show-actions"]) show("Actions", actions);
89-
// Act on the actions
90-
const mutations = await executePrActions(actions, prInfo, args.dry);
91-
if (args["show-mutations"] ?? args.dry) show("Mutations", mutations);
94+
await processSingle(pr);
9295
}
9396
if (args.dry || !args.cleanup) return;
9497
//
@@ -110,7 +113,7 @@ const start = async function () {
110113
throw new Error(`Could not find the 'Recently Merged' column in ${columns.map(n => n.name)}`);
111114
}
112115
const { cards, totalCount } = recentlyMerged;
113-
const afterFirst50 = cards.sort((l, r) => l.updatedAt.localeCompare(r.updatedAt)).slice(50);
116+
const afterFirst50 = cards.sort((l, r) => Date.parse(l.updatedAt) - Date.parse(r.updatedAt)).slice(50);
114117
if (afterFirst50.length > 0) {
115118
console.log(`Cutting "Recently Merged" projects to the last 50`);
116119
if (cards.length < totalCount) {
@@ -122,15 +125,12 @@ const start = async function () {
122125
// Handle other columns
123126
for (const column of columns) {
124127
if (column.name === "Recently Merged") continue;
125-
const ids = column.cards.map(c => c.id).filter(c => !cardIDs.includes(c));
126-
if (ids.length === 0) continue;
128+
const cleanup = column.cards.map(card => card.number).filter((number): number is NonNullable<typeof number> =>
129+
!!number && !prs.includes(number));
130+
if (cleanup.length === 0) continue;
127131
console.log(`Cleaning up closed PRs in "${column.name}"`);
128-
// don't actually do the deletions, until I follow this and make sure that it's working fine
129-
for (const id of ids) {
130-
const info = await runQueryToGetPRForCardId(id);
131-
await deleteObject(id, info === undefined ? "???"
132-
: info.state === "CLOSED" ? undefined
133-
: "#" + info.number);
132+
for (const number of cleanup) {
133+
await processSingle(number);
134134
}
135135
}
136136
console.log("Done");

0 commit comments

Comments
 (0)
This repository has been archived.