@@ -16,6 +16,13 @@ const TARGET_TITLE = 'test maven/mavencentral/org.apache.httpcomponents/httpcore
1616/** @type {string } */
1717const oneDayAgo = new Date ( Date . now ( ) - 24 * 60 * 60 * 1000 ) . toISOString ( )
1818
19+ /**
20+ * @typedef {Object } Options
21+ * @property {string } title - The title to search for.
22+ * @property {string } repoOwner - The owner of the repository.
23+ * @property {string } repoName - The name of the repository.
24+ */
25+
1926/**
2027 * Creates an authenticated Octokit instance.
2128 * @returns {Promise<import('@octokit/rest').Octokit> }
@@ -34,17 +41,18 @@ const createOctokit = () =>
3441/**
3542 * Finds pull requests matching the given title and created before the specified date.
3643 * @param {import('@octokit/rest').Octokit } octokit - The Octokit instance.
37- * @param {string } givenTitle - The title to search for.
44+ * @param {Options } options - The options for cleanup .
3845 * @param {string } dateSince - The ISO date string to filter PRs created before this date.
3946 * @returns {Promise<{prNumber: number, prTitle: string}[]> } The list of matching pull requests.
4047 */
41- const findPullRequests = async ( octokit , givenTitle , dateSince ) => {
48+ const findPullRequests = async ( octokit , options , dateSince ) => {
49+ const { title : givenTitle , repoOwner, repoName } = options
4250 /** @type {{prNumber: number, prTitle: string}[] } */
4351 const result = [ ]
4452 try {
4553 const iterator = octokit . paginate . iterator ( octokit . rest . pulls . list , {
46- owner : REPO_OWNER ,
47- repo : REPO_NAME ,
54+ owner : repoOwner ,
55+ repo : repoName ,
4856 state : 'open' ,
4957 sort : 'created' ,
5058 direction : 'desc'
@@ -58,7 +66,7 @@ const findPullRequests = async (octokit, givenTitle, dateSince) => {
5866 return result
5967 } catch ( error ) {
6068 const errorMessage = error instanceof Error ? error . message : String ( error )
61- console . error ( `Failed to fetch pull requests for repo ${ REPO_OWNER } /${ REPO_NAME } : ${ errorMessage } ` )
69+ console . error ( `Failed to fetch pull requests for repo ${ repoOwner } /${ repoName } : ${ errorMessage } ` )
6270 throw error
6371 }
6472}
@@ -97,15 +105,17 @@ const checkIsDone = (prsByDateDesc, dateSince) => {
97105/**
98106 * Closes a pull request.
99107 * @param {import('@octokit/rest').Octokit } octokit - The Octokit instance.
108+ * @param {Options } options - The options for cleanup.
100109 * @param {number } prNumber - The pull request number.
101110 * @returns {Promise<void> }
102111 * @throws {Error } If the pull request cannot be closed.
103112 */
104- const closePullRequest = async ( octokit , prNumber ) => {
113+ const closePullRequest = async ( octokit , options , prNumber ) => {
114+ const { repoOwner, repoName } = options
105115 try {
106116 await octokit . pulls . update ( {
107- owner : REPO_OWNER ,
108- repo : REPO_NAME ,
117+ owner : repoOwner ,
118+ repo : repoName ,
109119 pull_number : prNumber ,
110120 state : 'closed'
111121 } )
@@ -119,23 +129,27 @@ const closePullRequest = async (octokit, prNumber) => {
119129
120130/**
121131 * Cleans up pull requests with the specified title created before the given date.
132+ * @param {Object } opts - The options for cleanup.
133+ * @param {string } [opts.title=TARGET_TITLE] - The title to search for.
134+ * @param {string } [opts.repoOwner=REPO_OWNER] - The owner of the repository.
135+ * @param {string } [opts.repoName=REPO_NAME] - The name of the repository.
122136 * @param {string } [dateSince=oneDayAgo] - The ISO date string to filter PRs created before this date.
123137 * @returns {Promise<void> }
124138 */
125- const cleanup = async ( dateSince = oneDayAgo ) => {
126- console . info ( `Owner: ${ REPO_OWNER } , Repo: ${ REPO_NAME } ` )
127- console . info ( `Searching for PRs with title: ${ TARGET_TITLE } ` )
139+ const cleanupPR = async ( opts = { } , dateSince = oneDayAgo ) => {
140+ const { title = TARGET_TITLE , repoOwner = REPO_OWNER , repoName = REPO_NAME } = opts
141+ const options = { title, repoOwner, repoName }
142+ console . info ( `Cleanup options: ${ JSON . stringify ( options ) } ` )
143+ console . info ( `Searching for PRs with title: ${ title } ` )
128144
129145 const octokit = await createOctokit ( )
130- const found = await findPullRequests ( octokit , TARGET_TITLE , dateSince )
131- console . info ( `Found ${ found . length } PRs with title: ${ TARGET_TITLE } before ${ dateSince } ` )
146+ const found = await findPullRequests ( octokit , options , dateSince )
147+ console . info ( `Found ${ found . length } PRs with title: ${ title } before ${ dateSince } ` )
132148
133149 for ( const { prTitle, prNumber } of found ) {
134150 console . debug ( `Found PR #${ prNumber } with title: ${ prTitle } ` )
135- await closePullRequest ( octokit , prNumber )
151+ await closePullRequest ( octokit , options , prNumber )
136152 }
137153}
138154
139- cleanup ( )
140- . then ( ( ) => console . log ( 'Cleanup completed.' ) )
141- . catch ( error => console . error ( `Error during cleanup: ${ error . message } ` ) )
155+ module . exports = { cleanupPR }
0 commit comments