Skip to content

Commit ca50dcb

Browse files
committed
Refactor the title of the curation PR to testConfig
This change establishes a central location for managing PR titles, streamlining control over both PR creation and deletion. Curation configurations have been added to testConfig. In addition to the PR title, repoOwner and repoName can now also be configured.
1 parent b7361f0 commit ca50dcb

File tree

5 files changed

+48
-22
lines changed

5 files changed

+48
-22
lines changed

tools/integration/lib/cleanupPR.js

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@ const TARGET_TITLE = 'test maven/mavencentral/org.apache.httpcomponents/httpcore
1616
/** @type {string} */
1717
const 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 }

tools/integration/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"test": "npm run mocha && npm run lint",
77
"e2e-test-harvest": "mocha test/integration/harvestTest.js",
88
"e2e-test-service": "mocha --exit \"test/integration/e2e-test-service/**/*.js\"",
9-
"e2e-test-service-cleanup": "node ./lib/cleanupPR.js",
9+
"e2e-test-service-cleanup": "node ./test/integration/cleanup.js",
1010
"e2e-test-definition": "mocha --exit \"test/integration/e2e-test-service/definitionTest.js\"",
1111
"mocha": "mocha --exit \"test/lib/**/*.js\"",
1212
"lint": "npm run prettier:check && npm run eslint",
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// (c) Copyright 2025, SAP SE and ClearlyDefined contributors. Licensed under the MIT license.
2+
// SPDX-License-Identifier: MIT
3+
const { cleanupPR } = require('../../lib/cleanupPR')
4+
const { curation } = require('./testConfig')
5+
6+
cleanupPR(curation)
7+
.then(() => console.log('Cleanup completed.'))
8+
.catch(error => console.error(`Error during cleanup: ${error.message}`))

tools/integration/test/integration/e2e-test-service/curationTest.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
const { deepStrictEqual, strictEqual, ok } = require('assert')
55
const { callFetchWithRetry: callFetch, buildPostOpts } = require('../../../lib/fetch')
6-
const { devApiBaseUrl, definition } = require('../testConfig')
6+
const { devApiBaseUrl, definition, curation } = require('../testConfig')
77

88
describe('Validate curation', function () {
99
this.timeout(definition.timeout)
@@ -12,6 +12,7 @@ describe('Validate curation', function () {
1212
afterEach(() => new Promise(resolve => setTimeout(resolve, definition.timeout / 2)))
1313

1414
const coordinates = 'maven/mavencentral/org.apache.httpcomponents/httpcore/4.4.16'
15+
const title = curation.title || `test ${coordinates}`
1516

1617
describe('Propose curation', function () {
1718
const [type, provider, namespace, name, revision] = coordinates.split('/')
@@ -25,7 +26,7 @@ describe('Validate curation', function () {
2526
before('curate', async function () {
2627
const curationResponse = await callFetch(
2728
`${devApiBaseUrl}/curations`,
28-
buildCurationOpts(coordinates, type, provider, namespace, name, revision, curation)
29+
buildCurationOpts(title, type, provider, namespace, name, revision, curation)
2930
).then(r => r.json())
3031
prNumber = curationResponse.prNumber
3132
})
@@ -85,10 +86,10 @@ describe('Validate curation', function () {
8586
})
8687
})
8788

88-
function buildCurationOpts(coordinates, type, provider, namespace, name, revision, curation) {
89+
function buildCurationOpts(title, type, provider, namespace, name, revision, curation) {
8990
const contributionInfo = {
9091
type: 'other',
91-
summary: `test ${coordinates}`
92+
summary: title
9293
}
9394
const patch = {
9495
coordinates: { type, provider, namespace, name },

tools/integration/test/integration/testConfig.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,9 @@ module.exports = {
8888
definition: {
8989
timeout: 1000 * 60 // for each component
9090
},
91+
curation: {
92+
title: 'test maven/mavencentral/org.apache.httpcomponents/httpcore/4.4.16'
93+
},
9194
origins: {
9295
timeout: 1000 * 60 * 2
9396
}

0 commit comments

Comments
 (0)