Skip to content
This repository has been archived by the owner on Jan 25, 2024. It is now read-only.

Gracefully handle unspecified dependencies #172

Merged
merged 1 commit into from
Nov 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions __tests__/expected_dependency_results.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4689,6 +4689,57 @@ export const GRADLE_EXAMPLE_DEPENDENCY_OUTPUT_UNRESOLVED = [
],
]


export const GRADLE_EXAMPLE_DEPENDENCY_OUTPUT_UNSPECIFIED = [
[
{
type: "maven",
name: "kotlin-stdlib-jdk8",
namespace: "org.jetbrains.kotlin",
version: "1.6.10",
qualifiers: null,
subpath: null,
},
{
type: "maven",
name: "kotlin-stdlib",
namespace: "org.jetbrains.kotlin",
version: "1.6.10",
qualifiers: null,
subpath: null,
},
],
[
{
type: "maven",
name: "kotlin-stdlib",
namespace: "org.jetbrains.kotlin",
version: "1.6.10",
qualifiers: null,
subpath: null,
},
{
type: "maven",
name: "annotations",
namespace: "org.jetbrains",
version: "13.0",
qualifiers: null,
subpath: null,
},
],
[
{
type: "maven",
name: "kotlin-stdlib-jdk8",
namespace: "org.jetbrains.kotlin",
version: "1.6.10",
qualifiers: null,
subpath: null,
},
undefined,
],
]

export const GRADLE_EXAMPLE_DEPENDENCY_OUTPUT_SPRING = [
[
{
Expand Down
35 changes: 34 additions & 1 deletion __tests__/parse.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import {
GRADLE_EXAMPLE_DEPENDENCY_OUTPUT,
GRADLE_EXAMPLE_DEPENDENCY_OUTPUT_UNRESOLVED,
GRADLE_EXAMPLE_DEPENDENCY_OUTPUT_SPRING,
GRADLE_EXAMPLE_DEPENDENCY_WITH_SUB_PROJECTS_OUTPUT
GRADLE_EXAMPLE_DEPENDENCY_WITH_SUB_PROJECTS_OUTPUT,
GRADLE_EXAMPLE_DEPENDENCY_OUTPUT_UNSPECIFIED
} from './expected_dependency_results'

const GRADLE_DEPENDENCY_OUTPUT = `
Expand Down Expand Up @@ -60,6 +61,31 @@ A web-based, searchable dependency report is available by adding the --scan opti
BUILD SUCCESSFUL in 555ms
1 actionable task: 1 executed`


const GRADLE_DEPENDENCY_OUTPUT_UNSPECIFIED = `
> Task :app:dependencies

------------------------------------------------------------
Project ':app'
------------------------------------------------------------

debugCompileClasspath - Compile classpath for compilation 'debug' (target (androidJvm)).
+--- org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.6.10
| +--- org.jetbrains.kotlin:kotlin-stdlib:1.6.10
| | +--- org.jetbrains:annotations:13.0
| | \\--- unspecified (n)
+--- org.jetbrains:annotations:{strictly 13.0} -> 13.0 (c)
\\--- org.jetbrains.kotlin:kotlin-stdlib-common:{strictly 1.6.10} -> 1.6.10 (c)

(c) - dependency constraint
(*) - dependencies omitted (listed previously)

A web-based, searchable dependency report is available by adding the --scan option.

BUILD SUCCESSFUL in 555ms
1 actionable task: 1 executed`


const GRADLE_DEPENDENCY_OUTPUT_SPRING = `
Type-safe dependency accessors is an incubating feature.

Expand Down Expand Up @@ -109,6 +135,13 @@ describe('parseGradleDependencyOutput', () => {
expect(dependencies).toEqual(GRADLE_EXAMPLE_DEPENDENCY_OUTPUT_UNRESOLVED)
})

test('parses output of gradle dependency command with unspecified dependencies', () => {
const dependencies = parseGradleGraph('test', GRADLE_DEPENDENCY_OUTPUT_UNSPECIFIED).packages

expect(Object.values(dependencies).length).toEqual(GRADLE_EXAMPLE_DEPENDENCY_OUTPUT_UNSPECIFIED.length)
expect(dependencies).toEqual(GRADLE_EXAMPLE_DEPENDENCY_OUTPUT_UNSPECIFIED)
})

test('parses output of gradle dependency command with bom into dependencies', () => {
const dependencies = parseGradleGraph('test', GRADLE_DEPENDENCY_OUTPUT_SPRING).packages

Expand Down
28 changes: 17 additions & 11 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

29 changes: 17 additions & 12 deletions src/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export function parseProjectSpecification(projectString: string, level = 0): Pro
*
* Identifies variant of specification (full maven spec or without version (if bom file is used)).
*/
export function parseGradlePackage(pkg: string, level = 0): PackageURL {
export function parseGradlePackage(pkg: string, level = 0): PackageURL | undefined {
const stripped = pkg.substring((level + 1) * DEPENDENCY_LEVEL_INLINE).trimEnd()
const split = stripped.split(':')
let packageName = ''
Expand All @@ -38,16 +38,19 @@ export function parseGradlePackage(pkg: string, level = 0): PackageURL {
;[libraryName, lineEnd] = secondaryParts
} else {
if (split[1].trim().endsWith(DEPENDENCY_NOT_RESOLVED)) {
core.warning(`Discovered unresolved dependency: ${pkg}`)
core.warning(`Discovered unresolved dependency: ${stripped}`)
libraryName = split[1].trim().replace(DEPENDENCY_NOT_RESOLVED, '')
} else {
core.error(`Could not parse package: '${pkg}' (1)`)
throw Error(`The given '${pkg} can't be parsed as a gradle package.`)
core.error(`Could not parse package: '${stripped}' (1)`)
throw Error(`The given '${stripped} can't be parsed as a gradle package.`)
}
}
} else if (split.length === 1 && stripped.trim().endsWith(DEPENDENCY_NOT_RESOLVED)) {
core.warning(`Could not parse unresolved package: '${stripped}' (3)`)
return undefined
} else if (split.length < 3) {
core.error(`Could not parse package: '${pkg}' (2)`)
throw Error(`The given '${pkg} can't be parsed as a gradle package.`)
core.error(`Could not parse package: '${stripped}' (2)`)
throw Error(`The given '${stripped} can't be parsed as a gradle package.`)
} else {
;[packageName, libraryName, lineEnd] = split
}
Expand Down Expand Up @@ -159,12 +162,14 @@ function parseGradleDependency(
}

const parent = parseGradlePackage(line, level)
if (parentParent) {
project.packages.push([parentParent, parent])
}
parseGradleDependency(rootProject, project, iterator, parent, level + 1, subModuleMode)
if (level === 0 || !parentParent) {
project.packages.push([parent, undefined])
if(parent) {
if (parentParent) {
project.packages.push([parentParent, parent])
}
parseGradleDependency(rootProject, project, iterator, parent, level + 1, subModuleMode)
if (level === 0 || !parentParent) {
project.packages.push([parent, undefined])
}
}
} else if (
strippedLine.startsWith(DEPENDENCY_CHILD_INSET[0]) ||
Expand Down