11// (c) Copyright 2025, SAP SE and ClearlyDefined contributors. Licensed under the MIT license.
22// SPDX-License-Identifier: MIT
33
4+ /** @type {string | undefined } */
45const GITHUB_TOKEN = process . env . GITHUB_TOKEN
6+
7+ /** @type {string } */
58const REPO_OWNER = 'clearlydefined'
9+
10+ /** @type {string } */
611const REPO_NAME = 'curated-data-dev'
712
13+ /** @type {string } */
814const TARGET_TITLE = 'test maven/mavencentral/org.apache.httpcomponents/httpcore/4.4.16'
15+
16+ /** @type {string } */
917const oneDayAgo = new Date ( Date . now ( ) - 24 * 60 * 60 * 1000 ) . toISOString ( )
1018
19+ /**
20+ * Creates an authenticated Octokit instance.
21+ * @returns {Promise<import('@octokit/rest').Octokit> }
22+ * @throws {Error } If GITHUB_TOKEN is not set.
23+ */
1124const createOctokit = ( ) =>
1225 import ( '@octokit/rest' ) . then ( ( { Octokit } ) => {
1326 if ( ! GITHUB_TOKEN ) {
@@ -18,7 +31,15 @@ const createOctokit = () =>
1831 } )
1932 } )
2033
34+ /**
35+ * Finds pull requests matching the given title and created before the specified date.
36+ * @param {import('@octokit/rest').Octokit } octokit - The Octokit instance.
37+ * @param {string } givenTitle - The title to search for.
38+ * @param {string } dateSince - The ISO date string to filter PRs created before this date.
39+ * @returns {Promise<{prNumber: number, prTitle: string}[]> } The list of matching pull requests.
40+ */
2141const findPullRequests = async ( octokit , givenTitle , dateSince ) => {
42+ /** @type {{prNumber: number, prTitle: string}[] } */
2243 const result = [ ]
2344 try {
2445 const iterator = octokit . paginate . iterator ( octokit . rest . pulls . list , {
@@ -36,11 +57,18 @@ const findPullRequests = async (octokit, givenTitle, dateSince) => {
3657 }
3758 return result
3859 } catch ( error ) {
39- console . error ( `Failed to fetch pull requests for repo ${ REPO_OWNER } /${ REPO_NAME } : ${ error . message } ` )
60+ const errorMessage = error instanceof Error ? error . message : String ( error )
61+ console . error ( `Failed to fetch pull requests for repo ${ REPO_OWNER } /${ REPO_NAME } : ${ errorMessage } ` )
4062 throw error
4163 }
4264}
4365
66+ /**
67+ * Filters pull requests by title.
68+ * @param {{number: number, title: string}[] } openPrs - The list of open pull requests.
69+ * @param {string } givenTitle - The title to search for.
70+ * @returns {{prNumber: number, prTitle: string}[] } The list of matching pull requests.
71+ */
4472const findInBatch = ( openPrs , givenTitle ) => {
4573 const found = ( openPrs || [ ] )
4674 . map ( ( { number, title } ) => {
@@ -53,14 +81,26 @@ const findInBatch = (openPrs, givenTitle) => {
5381 return found
5482}
5583
84+ /**
85+ * Checks if the iteration is done based on the date of the earliest PR.
86+ * @param {{created_at: string}[] } prsByDateDesc - The list of PRs sorted by date in descending order.
87+ * @param {string } dateSince - The ISO date string to compare against.
88+ * @returns {boolean } True if the iteration is done, false otherwise.
89+ */
5690const checkIsDone = ( prsByDateDesc , dateSince ) => {
5791 if ( ! prsByDateDesc . length ) return true
5892 const earliestPr = prsByDateDesc [ prsByDateDesc . length - 1 ]
5993 console . debug ( `${ earliestPr . created_at } < ${ dateSince } ?` )
6094 return earliestPr . created_at < dateSince
6195}
6296
63- // Function to close a pull request
97+ /**
98+ * Closes a pull request.
99+ * @param {import('@octokit/rest').Octokit } octokit - The Octokit instance.
100+ * @param {number } prNumber - The pull request number.
101+ * @returns {Promise<void> }
102+ * @throws {Error } If the pull request cannot be closed.
103+ */
64104const closePullRequest = async ( octokit , prNumber ) => {
65105 try {
66106 await octokit . pulls . update ( {
@@ -71,11 +111,17 @@ const closePullRequest = async (octokit, prNumber) => {
71111 } )
72112 console . info ( `PR #${ prNumber } closed successfully.` )
73113 } catch ( error ) {
74- console . error ( `Failed to close PR #${ prNumber } : ${ error . message } ` )
114+ const errorMessage = error instanceof Error ? error . message : String ( error )
115+ console . error ( `Failed to close PR #${ prNumber } : ${ errorMessage } ` )
75116 throw error
76117 }
77118}
78119
120+ /**
121+ * Cleans up pull requests with the specified title created before the given date.
122+ * @param {string } [dateSince=oneDayAgo] - The ISO date string to filter PRs created before this date.
123+ * @returns {Promise<void> }
124+ */
79125const cleanup = async ( dateSince = oneDayAgo ) => {
80126 console . info ( `Owner: ${ REPO_OWNER } , Repo: ${ REPO_NAME } ` )
81127 console . info ( `Searching for PRs with title: ${ TARGET_TITLE } ` )
0 commit comments