diff --git a/utils/renovate/dependencyDashboard.js b/utils/renovate/dependencyDashboard.js index bcb2071..f6cfeba 100644 --- a/utils/renovate/dependencyDashboard.js +++ b/utils/renovate/dependencyDashboard.js @@ -8,6 +8,12 @@ export class Dependency { // Matches CR LF or CR or LF. const LINE_SEPARATOR_REGEX = /\r?\n|\r|\n/g; +// Matches the part of the Dependency Dashboard body text between the header and the horizontal rule, +// without capturing the header or the horizontal rule itself. +// +// This will prevent false positives from being detected in the preamble. +const DETECTED_DEPENDENCIES_SECTION_REGEX = /(?<=## Detected dependencies)(.*)(?=---)/s; + // Matches text in backticks separated by a space. // Group 1 is interpreted as the name, and group 2 the version. // Examples: @@ -28,11 +34,22 @@ const DEPENDENCY_NAME_AND_VERSION_REGEX = /`(\S+?) (.*)`/; const issueIsRenovateDependencyDashboard = (issue) => issue.user.login === "renovate[bot]" && issue.pull_request === undefined; +const getDetectedDependencies = (issue) => { + const match = issue.body.match(DETECTED_DEPENDENCIES_SECTION_REGEX); + + if (match === null) { + return null; + } + + return match[0]; +} + const parseDependenciesFromDashboard = (issue) => - issue.body - .split(LINE_SEPARATOR_REGEX) - .map(parseDependencyFromLine) - .filter((dependency) => dependency !== null); + getDetectedDependencies(issue) + ?.split(LINE_SEPARATOR_REGEX) + ?.map(parseDependencyFromLine) + ?.filter((dependency) => dependency !== null) + ?? []; const parseDependencyFromLine = (line) => { const match = line.match(DEPENDENCY_NAME_AND_VERSION_REGEX); diff --git a/utils/renovate/dependencyDashboard.test.js b/utils/renovate/dependencyDashboard.test.js index a99d45a..956cabe 100644 --- a/utils/renovate/dependencyDashboard.test.js +++ b/utils/renovate/dependencyDashboard.test.js @@ -24,7 +24,17 @@ describe("handleIssuesApiResponse", () => { user: { login: "renovate[bot]", }, - body: '# Dependency Dashboard\nList of dependencies:\n- `libquux v4.1.1.rc4`\n- `@xyzzy/utils "~> 22.04 Questing Quokka"`\n\nHere\'s some more:\n- `baz-framework ^0.1`', + body: '# Dependency Dashboard\n' + + 'Here\'s some things in the preamble that should not be picked up:\n' + + '`fake-dependency`, `another-fake-dependency`\n' + + '\n' + + '## Detected dependencies\n' + + '- `libquux v4.1.1.rc4`\n' + + '- `@xyzzy/utils "~> 22.04 Questing Quokka"`\n' + + '\n' + + 'Here\'s some more:\n' + + '- `baz-framework ^0.1`\n' + + '---', }, ], };