Skip to content

Commit

Permalink
fix(manager/gradle-wrapper): refresh Gradle lockfile after wrapper up…
Browse files Browse the repository at this point in the history
…date (#23081)
  • Loading branch information
Churro authored Jul 2, 2023
1 parent 7cd6f7c commit 5336723
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 4 deletions.
52 changes: 50 additions & 2 deletions lib/modules/manager/gradle-wrapper/artifacts.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,16 @@ import type { RepoGlobalConfig } from '../../../config/types';
import { resetPrefetchedImages } from '../../../util/exec/docker';
import type { StatusResult } from '../../../util/git/types';
import { getPkgReleases } from '../../datasource';
import type { UpdateArtifactsConfig } from '../types';
import { updateBuildFile } from './artifacts';
import { updateArtifacts as gradleUpdateArtifacts } from '../gradle';
import type { UpdateArtifactsConfig, UpdateArtifactsResult } from '../types';
import { updateBuildFile, updateLockFiles } from './artifacts';
import { updateArtifacts } from '.';

jest.mock('../../../util/fs');
jest.mock('../../../util/git');
jest.mock('../../../util/exec/env');
jest.mock('../../datasource');
jest.mock('../gradle');

process.env.CONTAINERBASE = 'true';

Expand Down Expand Up @@ -411,4 +413,50 @@ describe('modules/manager/gradle-wrapper/artifacts', () => {
expect(res).toBe('build.gradle.kts');
});
});

describe('updateLockFiles()', () => {
it('returns early if build script file not found', async () => {
fs.readLocalFile.mockResolvedValueOnce(null);

const res = await updateLockFiles('', {});
expect(logger.logger.debug).toHaveBeenCalledWith(
'build.gradle or build.gradle.kts not found'
);
expect(res).toBeNull();
});

it('includes gradle lockfile in result', async () => {
const execSnapshots = mockExecAll();
const updatedArtifacts: UpdateArtifactsResult[] = [
{
file: {
type: 'addition',
path: 'gradle.lockfile',
contents: 'test',
},
},
];
mockedFunction(gradleUpdateArtifacts).mockResolvedValue(updatedArtifacts);

git.getRepoStatus.mockResolvedValue(
partial<StatusResult>({
modified: ['gradle.lockfile'],
})
);

const res = await updateArtifacts({
packageFileName: 'gradle/wrapper/gradle-wrapper.properties',
updatedDeps: [],
newPackageFileContent: '',
config: { ...config, newValue: '8.2' },
});

expect(res).toStrictEqual(updatedArtifacts);
expect(execSnapshots).toMatchObject([
{
cmd: './gradlew wrapper --gradle-version 8.2',
},
]);
});
});
});
30 changes: 29 additions & 1 deletion lib/modules/manager/gradle-wrapper/artifacts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@ import type { StatusResult } from '../../../util/git/types';
import { Http } from '../../../util/http';
import { newlineRegex } from '../../../util/regex';
import { replaceAt } from '../../../util/string';
import type { UpdateArtifact, UpdateArtifactsResult } from '../types';
import { updateArtifacts as gradleUpdateArtifacts } from '../gradle';
import type {
UpdateArtifact,
UpdateArtifactsConfig,
UpdateArtifactsResult,
} from '../types';
import {
extraEnv,
getJavaConstraint,
Expand Down Expand Up @@ -107,6 +112,24 @@ export async function updateBuildFile(
return buildFileName;
}

export async function updateLockFiles(
buildFileName: string,
config: UpdateArtifactsConfig
): Promise<UpdateArtifactsResult[] | null> {
const buildFileContent = await readLocalFile(buildFileName, 'utf8');
if (!buildFileContent) {
logger.debug('build.gradle or build.gradle.kts not found');
return null;
}

return await gradleUpdateArtifacts({
packageFileName: buildFileName,
updatedDeps: [],
newPackageFileContent: buildFileContent,
config,
});
}

export async function updateArtifacts({
packageFileName,
newPackageFileContent,
Expand Down Expand Up @@ -175,6 +198,7 @@ export async function updateArtifacts({
distributionSha256Sum: checksum,
distributionUrl,
});
const lockFiles = await updateLockFiles(buildFileName, config);

const status = await getRepoStatus();
const artifactFileNames = [
Expand All @@ -191,6 +215,10 @@ export async function updateArtifacts({
)
)
).filter(is.truthy);
if (lockFiles) {
updateArtifactsResult.push(...lockFiles);
}

logger.debug(
{ files: updateArtifactsResult.map((r) => r.file?.path) },
`Returning updated gradle-wrapper files`
Expand Down
6 changes: 5 additions & 1 deletion lib/modules/manager/gradle/artifacts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,11 @@ export async function updateArtifacts({
.map(quote)
.join(' ')}`;

if (config.isLockFileMaintenance || isGcvPropsFile(packageFileName)) {
if (
config.isLockFileMaintenance ||
!updatedDeps.length ||
isGcvPropsFile(packageFileName)
) {
cmd += ' --write-locks';
} else {
const updatedDepNames = updatedDeps
Expand Down

0 comments on commit 5336723

Please sign in to comment.