import { Octokit } from "@octokit/rest" ; declare type refGraph = { node : { name : string ; id : string ; pulls : { edges : [ { node : { number : number ; } ; } ] ; } ; } ; } ; declare type repositoryGraph = { repository : { refs : { edges : refGraph [ ] ; } ; } ; } ; param hours param minutes param dryRun export type deletionOptions = { hours ? : number ; minutes ? : number ; dryRun ? : boolean ; } ; param octocat param owner param repo param options export async function deleteBranches ( octocat , owner , repo , options = { } ) { if ( ! owner || ! repo ) { throw new Error ( "Missing owner or repo name." ) ; } const expires = new Date ( ) ; if ( ! options . hours && ! options . minutes ) { options . hours = 48 ; } if ( options . hours ) { expires . setUTCHours ( 0 - options . hours , 0 , 0 ) ; } else if ( options . minutes ) { expires . setUTCMinutes ( expires . getUTCMinutes ( ) - options . minutes ) ; } else { throw new Error ( "Invalid options passed." ) ; } const expireDate = expires . toISOString ( ) . replace ( /[:.]/g , "_" ) ; const query = `query {
        repository(name: "${ repo }", owner: "${ owner }") {
            id,
            name,
            refs(refPrefix: "refs/heads/", first: 10) {
                edges {
                    node {
                        name,
                        id,
                        pulls:associatedPullRequests(first: 5, states:OPEN) {
                            edges {
                                node {
                                    id,
                                    number,
                                    title,
                                }
                            }
                        }
                    }
                }
            }
        }
    }` ; const result = await octocat . graphql ( query ) ; await Promise . all ( result . repository . refs . edges . map ( ref => { const br = ref . node ; const name = br . name ; const suffix = name . match ( /ggg[_\-]test-branch-\S+?[\-_](.*)/ ) ; if ( suffix && suffix [ 1 ] < expireDate ) { if ( br . pulls . edges . length ) { console . log ( `Closing PR ${ br . pulls . edges [ 0 ] . node . number }` ) ; } console . log ( `Deleting branch ${ br . name }` ) ; const mutate = `mutation DeleteBranch {
                    deleteRef(input:{refId: "${ br . id }"}) {
                    clientMutationId }}` ; if ( ! options . dryRun ) { return octocat . graphql ( mutate ) ; } else { console . log ( `Deletion of refId: "${ br . id }" skipped` ) ; } } } ) ) ; }