diff --git a/lib/modules/manager/gradle/artifacts.spec.ts b/lib/modules/manager/gradle/artifacts.spec.ts index cfaa1e7f85147a6b1a06d03b271ba50e56f47e69..9bf95bc6928f9f41e1643839024887eb2a1d7194 100644 --- a/lib/modules/manager/gradle/artifacts.spec.ts +++ b/lib/modules/manager/gradle/artifacts.spec.ts @@ -66,6 +66,7 @@ describe('modules/manager/gradle/artifacts', () => { 'gradlew', 'build.gradle', 'gradle.lockfile', + 'gradle/wrapper/gradle-wrapper.properties', ]); fs.getFileContentMap.mockResolvedValue({ 'gradle.lockfile': 'Current gradle.lockfile', @@ -75,7 +76,19 @@ describe('modules/manager/gradle/artifacts', () => { modified: ['build.gradle', 'gradle.lockfile'], }) ); - fs.readLocalFile.mockResolvedValue('New gradle.lockfile'); + + // TODO: fix types, jest is using wrong overload (#7154) + fs.readLocalFile.mockImplementation((fileName: string): Promise<any> => { + let content = ''; + if (fileName === 'gradle.lockfile') { + content = 'New gradle.lockfile'; + } else if (fileName === 'gradle/wrapper/gradle-wrapper.properties') { + content = + 'distributionUrl=https\\://services.gradle.org/distributions/gradle-7.4-bin.zip'; + } + + return Promise.resolve(content); + }); }); it('aborts if no lockfile is found', async () => { @@ -265,7 +278,7 @@ describe('modules/manager/gradle/artifacts', () => { '-w "/tmp/github/some/repo" ' + 'renovate/sidecar' + ' bash -l -c "' + - 'install-tool java 11.0.1' + + 'install-tool java 16.0.1' + ' && ' + './gradlew --console=plain -q properties' + '"', @@ -283,7 +296,7 @@ describe('modules/manager/gradle/artifacts', () => { '-w "/tmp/github/some/repo" ' + 'renovate/sidecar' + ' bash -l -c "' + - 'install-tool java 11.0.1' + + 'install-tool java 16.0.1' + ' && ' + './gradlew --console=plain -q :dependencies --write-locks' + '"', @@ -313,12 +326,12 @@ describe('modules/manager/gradle/artifacts', () => { }, ]); expect(execSnapshots).toMatchObject([ - { cmd: 'install-tool java 11.0.1' }, + { cmd: 'install-tool java 16.0.1' }, { cmd: './gradlew --console=plain -q properties', options: { cwd: '/tmp/github/some/repo' }, }, - { cmd: 'install-tool java 11.0.1' }, + { cmd: 'install-tool java 16.0.1' }, { cmd: './gradlew --console=plain -q :dependencies --write-locks', options: { cwd: '/tmp/github/some/repo' }, @@ -425,4 +438,41 @@ describe('modules/manager/gradle/artifacts', () => { }) ).rejects.toThrow(TEMPORARY_ERROR); }); + + it('fallback to default Java version if Gradle version not extractable', async () => { + const execSnapshots = mockExecAll(); + GlobalConfig.set({ ...adminConfig, binarySource: 'install' }); + fs.readLocalFile + .mockResolvedValueOnce(null) + .mockResolvedValueOnce('New gradle.lockfile'); + + const res = await updateArtifacts({ + packageFileName: 'build.gradle', + updatedDeps: [], + newPackageFileContent: '', + config: { isLockFileMaintenance: true }, + }); + + expect(res).toEqual([ + { + file: { + type: 'addition', + path: 'gradle.lockfile', + contents: 'New gradle.lockfile', + }, + }, + ]); + expect(execSnapshots).toMatchObject([ + { cmd: 'install-tool java 11.0.1' }, + { + cmd: './gradlew --console=plain -q properties', + options: { cwd: '/tmp/github/some/repo' }, + }, + { cmd: 'install-tool java 11.0.1' }, + { + cmd: './gradlew --console=plain -q :dependencies --write-locks', + options: { cwd: '/tmp/github/some/repo' }, + }, + ]); + }); }); diff --git a/lib/modules/manager/gradle/artifacts.ts b/lib/modules/manager/gradle/artifacts.ts index eb95ef027c0276a42e1496745f0313a936a9ad77..f1bc77b70a991aa9daa85264a6fc1aedeac3074a 100644 --- a/lib/modules/manager/gradle/artifacts.ts +++ b/lib/modules/manager/gradle/artifacts.ts @@ -1,6 +1,6 @@ import is from '@sindresorhus/is'; import { quote } from 'shlex'; -import { dirname } from 'upath'; +import { dirname, join } from 'upath'; import { TEMPORARY_ERROR } from '../../../constants/error-messages'; import { logger } from '../../../logger'; import { exec } from '../../../util/exec'; @@ -15,6 +15,7 @@ import { getFileList, getRepoStatus } from '../../../util/git'; import { regEx } from '../../../util/regex'; import { extraEnv, + extractGradleVersion, getJavaConstraint, gradleWrapperFileName, } from '../gradle-wrapper/utils'; @@ -67,6 +68,17 @@ async function getSubProjectList( return subprojects; } +async function getGradleVersion(gradlewFile: string): Promise<string | null> { + const propertiesFile = join( + dirname(gradlewFile), + 'gradle/wrapper/gradle-wrapper.properties' + ); + const properties = await readLocalFile(propertiesFile, 'utf8'); + const extractResult = extractGradleVersion(properties ?? ''); + + return extractResult ? extractResult.version : null; +} + export async function updateArtifacts({ packageFileName, updatedDeps, @@ -108,7 +120,8 @@ export async function updateArtifacts({ { toolName: 'java', constraint: - config.constraints?.java ?? getJavaConstraint(config.currentValue), + config.constraints?.java ?? + getJavaConstraint(await getGradleVersion(gradlewFile)), }, ], };