Skip to content

Commit

Permalink
fix(gradle-wrapper): support for wrapper updates in sub-directory (#1…
Browse files Browse the repository at this point in the history
…7164)

Co-authored-by: Michael Kriese <michael.kriese@visualon.de>
  • Loading branch information
Churro and viceice committed Aug 18, 2022
1 parent c82536e commit b8004fe
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 7 deletions.
49 changes: 49 additions & 0 deletions lib/modules/manager/gradle-wrapper/artifacts.spec.ts
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(
[
'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: {
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
@@ -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
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
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(
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

0 comments on commit b8004fe

Please sign in to comment.