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(gradle-wrapper): support for wrapper updates in sub-directory #17164

Merged
merged 10 commits into from
Aug 18, 2022
49 changes: 49 additions & 0 deletions lib/modules/manager/gradle-wrapper/artifacts.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -275,4 +275,53 @@ describe('modules/manager/gradle-wrapper/artifacts', () => {
]);
expect(execSnapshots).toBeEmptyArray();
});

it('handles gradle-wrapper in subdirectory', async () => {
const execSnapshots = mockExecAll();
git.getRepoStatus.mockResolvedValue(
partial<StatusResult>({
modified: [
'sub/gradle/wrapper/gradle-wrapper.properties',
'sub/gradlew',
'sub/gradlew.bat',
],
})
);

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

expect(res).toEqual(
viceice marked this conversation as resolved.
Show resolved Hide resolved
[
'sub/gradle/wrapper/gradle-wrapper.properties',
'sub/gradlew',
'sub/gradlew.bat',
].map((fileProjectPath) => ({
file: {
type: 'addition',
path: fileProjectPath,
contents: 'test',
},
}))
);
expect(execSnapshots).toMatchObject([
{
cmd: './gradlew wrapper --gradle-distribution-url https://services.gradle.org/distributions/gradle-6.3-bin.zip',
options: {
viceice marked this conversation as resolved.
Show resolved Hide resolved
cwd: '/tmp/github/some/repo/sub',
encoding: 'utf-8',
env: {
GRADLE_OPTS:
'-Dorg.gradle.parallel=true -Dorg.gradle.configureondemand=true -Dorg.gradle.daemon=false -Dorg.gradle.caching=false',
},
},
},
]);
});
});
14 changes: 12 additions & 2 deletions lib/modules/manager/gradle-wrapper/artifacts.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import is from '@sindresorhus/is';
import { quote } from 'shlex';
import { dirname, join } from 'upath';
import { TEMPORARY_ERROR } from '../../../constants/error-messages';
import { logger } from '../../../logger';
import { exec } from '../../../util/exec';
Expand All @@ -10,7 +11,12 @@ import type { StatusResult } from '../../../util/git/types';
import { Http } from '../../../util/http';
import { newlineRegex } from '../../../util/regex';
import type { UpdateArtifact, UpdateArtifactsResult } from '../types';
import { extraEnv, getJavaConstraint, prepareGradleCommand } from './utils';
import {
extraEnv,
getJavaConstraint,
gradleWrapperFileName,
prepareGradleCommand,
} from './utils';

const http = new Http('gradle-wrapper');

Expand Down Expand Up @@ -55,7 +61,10 @@ export async function updateArtifacts({
}: UpdateArtifact): Promise<UpdateArtifactsResult[] | null> {
try {
logger.debug({ updatedDeps }, 'gradle-wrapper.updateArtifacts()');
let cmd = await prepareGradleCommand();
const localGradleDir = join(dirname(packageFileName), '../../');
const gradlewFile = join(localGradleDir, gradleWrapperFileName());

let cmd = await prepareGradleCommand(gradlewFile);
if (!cmd) {
logger.info('No gradlew found - skipping Artifacts update');
return null;
Expand All @@ -81,6 +90,7 @@ export async function updateArtifacts({
}
logger.debug(`Updating gradle wrapper: "${cmd}"`);
const execOptions: ExecOptions = {
cwdFile: gradlewFile,
docker: {
image: 'sidecar',
},
Expand Down
4 changes: 2 additions & 2 deletions lib/modules/manager/gradle-wrapper/util.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ describe('modules/manager/gradle-wrapper/util', () => {
mode: 0o550,
})
);
expect(await prepareGradleCommand()).toBe('./gradlew');
expect(await prepareGradleCommand('./gradlew')).toBe('./gradlew');
});

it('returns null', async () => {
Expand All @@ -78,7 +78,7 @@ describe('modules/manager/gradle-wrapper/util', () => {
isFile: () => false,
})
);
expect(await prepareGradleCommand()).toBeNull();
expect(await prepareGradleCommand('./gradlew')).toBeNull();
});
});
});
7 changes: 4 additions & 3 deletions lib/modules/manager/gradle-wrapper/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,17 @@ export function gradleWrapperFileName(): string {
return './gradlew';
}

export async function prepareGradleCommand(): Promise<string | null> {
const gradlewFile = gradleWrapperFileName();
export async function prepareGradleCommand(
viceice marked this conversation as resolved.
Show resolved Hide resolved
gradlewFile: string
): Promise<string | null> {
const gradlewStat = await statLocalFile(gradlewFile);
if (gradlewStat?.isFile() === true) {
// if the file is not executable by others
if ((gradlewStat.mode & 0o1) === 0) {
// add the execution permission to the owner, group and others
await chmodLocalFile(gradlewFile, gradlewStat.mode | 0o111);
}
return gradlewFile;
return gradleWrapperFileName();
}
/* eslint-enable no-bitwise */
return null;
Expand Down