Skip to content

Commit

Permalink
Merge branch 'main' into migration-prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabriel-Ladzaretti committed Oct 15, 2022
2 parents 2fc0144 + 5559b53 commit 2a3348a
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 15 deletions.
60 changes: 55 additions & 5 deletions lib/modules/manager/gradle/artifacts.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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 () => {
Expand Down Expand Up @@ -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' +
'"',
Expand All @@ -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' +
'"',
Expand Down Expand Up @@ -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' },
Expand Down Expand Up @@ -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' },
},
]);
});
});
17 changes: 15 additions & 2 deletions lib/modules/manager/gradle/artifacts.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -15,6 +15,7 @@ import { getFileList, getRepoStatus } from '../../../util/git';
import { regEx } from '../../../util/regex';
import {
extraEnv,
extractGradleVersion,
getJavaConstraint,
gradleWrapperFileName,
} from '../gradle-wrapper/utils';
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -108,7 +120,8 @@ export async function updateArtifacts({
{
toolName: 'java',
constraint:
config.constraints?.java ?? getJavaConstraint(config.currentValue),
config.constraints?.java ??
getJavaConstraint(await getGradleVersion(gradlewFile)),
},
],
};
Expand Down
15 changes: 11 additions & 4 deletions lib/util/git/private-key.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os from 'os';
import is from '@sindresorhus/is';
import fs from 'fs-extra';
import upath from 'upath';
import { PLATFORM_GPG_FAILED } from '../../constants/error-messages';
Expand All @@ -10,7 +11,13 @@ let gitPrivateKey: string | undefined;
let keyId: string | undefined;

export function setPrivateKey(key: string | undefined): void {
gitPrivateKey = key?.trim();
if (!is.nonEmptyStringAndNotWhitespace(key)) {
return;
}
logger.debug(
'gitPrivateKey: successfully set (but not yet written/configured)'
);
gitPrivateKey = key.trim();
}

async function importKey(): Promise<void> {
Expand All @@ -34,11 +41,11 @@ export async function writePrivateKey(): Promise<void> {
if (!gitPrivateKey) {
return;
}
logger.debug('Setting git private key');
try {
await importKey();
logger.debug('gitPrivateKey: imported');
} catch (err) {
logger.warn({ err }, 'Error writing git private key');
logger.warn({ err }, 'gitPrivateKey: error importing');
throw new Error(PLATFORM_GPG_FAILED);
}
}
Expand All @@ -47,7 +54,7 @@ export async function configSigningKey(cwd: string): Promise<void> {
if (!gitPrivateKey) {
return;
}
logger.debug('Configuring commits signing');
logger.debug('gitPrivateKey: configuring commit signing');
// TODO: types (#7154)
await exec(`git config user.signingkey ${keyId!}`, { cwd });
await exec(`git config commit.gpgsign true`, { cwd });
Expand Down
14 changes: 10 additions & 4 deletions lib/util/http/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ function applyDefaultHeaders(options: Options): void {
// `Buffer` in the latter case.
// We don't declare overload signatures because it's immediately wrapped by
// `request`.
async function gotRoutine<T>(
async function gotTask<T>(
url: string,
options: GotOptions,
requestStats: Omit<RequestStats, 'duration' | 'statusCode'>
Expand Down Expand Up @@ -162,16 +162,22 @@ export class Http<Opts extends HttpOptions = HttpOptions> {
// istanbul ignore else: no cache tests
if (!resPromise) {
const startTime = Date.now();
const queueTask = (): Promise<Response<T>> => {
const httpTask = (): Promise<Response<T>> => {
const queueDuration = Date.now() - startTime;
return gotRoutine(url, options, {
return gotTask(url, options, {
method: options.method ?? 'get',
url,
queueDuration,
});
};

const queue = getQueue(url);
resPromise = queue?.add(queueTask) ?? queueTask();
const queuedTask = queue
? () => queue.add<Response<T>>(httpTask)
: httpTask;

resPromise = queuedTask();

if (options.method === 'get' || options.method === 'head') {
memCache.set(cacheKey, resPromise); // always set if it's a get or a head
}
Expand Down

0 comments on commit 2a3348a

Please sign in to comment.