3
3
import * as schema from "@octokit/graphql-schema/schema" ;
4
4
import * as yargs from "yargs" ;
5
5
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" ;
7
7
import { queryPRInfo , deriveStateForPR } from "./pr-info" ;
8
8
import { executePrActions } from "./execute-pr-actions" ;
9
9
import { getProjectBoardCards } from "./queries/projectboard-cards" ;
10
- import { runQueryToGetPRForCardId } from "./queries/card-id-to-pr-query" ;
11
10
import { createMutation , client } from "./graphql-client" ;
12
11
import { render } from "prettyjson" ;
13
12
import { inspect } from "util" ;
@@ -60,35 +59,39 @@ const show = (name: string, value: unknown) => {
60
59
console . log ( str ) ;
61
60
} ;
62
61
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
+
63
87
const start = async function ( ) {
64
88
console . log ( `Getting open PRs.` ) ;
65
- const { prNumbers : prs , cardIDs } = await getAllOpenPRsAndCardIDs ( ) ;
89
+ const prs = await getAllOpenPRs ( ) ;
66
90
//
67
91
for ( const pr of prs ) {
68
92
if ( ! shouldRunOn ( pr ) ) continue ;
69
93
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 ) ;
92
95
}
93
96
if ( args . dry || ! args . cleanup ) return ;
94
97
//
@@ -110,7 +113,7 @@ const start = async function () {
110
113
throw new Error ( `Could not find the 'Recently Merged' column in ${ columns . map ( n => n . name ) } ` ) ;
111
114
}
112
115
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 ) ;
114
117
if ( afterFirst50 . length > 0 ) {
115
118
console . log ( `Cutting "Recently Merged" projects to the last 50` ) ;
116
119
if ( cards . length < totalCount ) {
@@ -122,15 +125,12 @@ const start = async function () {
122
125
// Handle other columns
123
126
for ( const column of columns ) {
124
127
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 ;
127
131
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 ) ;
134
134
}
135
135
}
136
136
console . log ( "Done" ) ;
0 commit comments