Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(manager/gradle-wrapper): refresh Gradle lockfile after wrapper update #23081

Merged
merged 1 commit into from
Jul 2, 2023
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
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 ||
rarkins marked this conversation as resolved.
Show resolved Hide resolved
isGcvPropsFile(packageFileName)
) {
cmd += ' --write-locks';
} else {
const updatedDepNames = updatedDeps
Expand Down