Skip to content

Commit c5e3be9

Browse files
author
Maxim Zaytsev
authored
[GradleV3] Add getGradleVersion method (#18927)
1 parent a139d34 commit c5e3be9

File tree

12 files changed

+217
-28
lines changed

12 files changed

+217
-28
lines changed

Tasks/GradleV3/Modules/code-coverage.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export function enableCodeCoverageAsync(settings: ICodeCoverageSettings): Q.Prom
2323
buildProperties['reportdirectory'] = settings.reportDirectoryName;
2424
buildProperties['ismultimodule'] = String(settings.isMultiModule);
2525
buildProperties['gradle5xOrHigher'] = String(settings.gradle5xOrHigher);
26+
buildProperties['gradleVersion'] = settings.gradleVersion;
2627

2728
const codeCoverageEnabler: ICodeCoverageEnabler = new CodeCoverageEnablerFactory().getTool('gradle', settings.codeCoverageTool.toLowerCase());
2829
return codeCoverageEnabler.enableCodeCoverage(buildProperties);

Tasks/GradleV3/Modules/environment.ts

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as tl from 'azure-pipelines-task-lib/task';
22
import * as javaCommon from 'azure-pipelines-tasks-java-common/java-common';
3-
import { IExecOptions } from 'azure-pipelines-task-lib/toolrunner';
3+
import { IExecOptions, ToolRunner } from 'azure-pipelines-task-lib/toolrunner';
44

55
// Setting the access token env var to both VSTS and AZURE_ARTIFACTS for
66
// backwards compatibility with repos that already use the older env var.
@@ -88,3 +88,30 @@ export function setGradleOpts(gradleOptions: string): void {
8888
tl.debug(`GRADLE_OPTS is now set to ${gradleOptions}`);
8989
}
9090
}
91+
92+
/**
93+
* Determine Gradle version by running ./gradlew --version
94+
* @param {string} wrapperScript - Relative path from the repository root to the Gradle Wrapper script.
95+
* @returns {string} Gradle version
96+
*/
97+
export function getGradleVersion(wrapperScript: string): string {
98+
const gradleVersionRunner: ToolRunner = tl.tool(wrapperScript);
99+
gradleVersionRunner.arg('--version');
100+
101+
const gradleOutput: string = gradleVersionRunner.execSync().stdout;
102+
const gradleVersion: string = extractGradleVersion(gradleOutput);
103+
104+
if (gradleVersion === 'unknown'){
105+
tl.warning(tl.loc('UnableToExtractGradleVersion'));
106+
}
107+
108+
tl.debug(`Gradle version: ${gradleVersion}`);
109+
110+
return gradleVersion;
111+
}
112+
113+
export function extractGradleVersion(str: string): string {
114+
const regex = /^Gradle (?<version>\d+\.\d+(?:\.\d+)?.*$)/m;
115+
const match = str.match(regex);
116+
return match?.groups?.version || 'unknown';
117+
}

Tasks/GradleV3/Strings/resources.resjson/en-US/resources.resjson

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,5 +94,6 @@
9494
"loc.messages.FileNotFound": "File or folder doesn't exist: %s",
9595
"loc.messages.FailedToAppendCC": "Unable to append code coverage data: %s",
9696
"loc.messages.NoTestResults": "No test result files matching %s were found, so publishing JUnit test results is being skipped.",
97-
"loc.messages.chmodGradlew": "Used 'chmod' method for gradlew file to make it executable."
97+
"loc.messages.chmodGradlew": "Used 'chmod' method for gradlew file to make it executable.",
98+
"loc.messages.UnableToExtractGradleVersion": "Unable to extract Gradle version from gradle output."
9899
}

Tasks/GradleV3/Tests/L0.ts

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { PmdTool } from 'azure-pipelines-tasks-codeanalysis-common/Common/PmdToo
1111
import { CheckstyleTool } from 'azure-pipelines-tasks-codeanalysis-common/Common/CheckstyleTool';
1212
import { FindbugsTool } from 'azure-pipelines-tasks-codeanalysis-common/Common/FindbugsTool';
1313
import { AnalysisResult } from 'azure-pipelines-tasks-codeanalysis-common/Common/AnalysisResult';
14+
import { extractGradleVersion } from '../Modules/environment';
1415

1516
let isWindows: RegExpMatchArray = os.type().match(/^Win/);
1617
let gradleWrapper: string = isWindows ? 'gradlew.bat' : 'gradlew';
@@ -767,7 +768,7 @@ describe('Gradle L0 Suite', function () {
767768
tr.run();
768769

769770
assert(tr.succeeded, 'task should have succeeded');
770-
assert(tr.invokedToolCount === 2, 'should have only run gradle 2 times');
771+
assert(tr.invokedToolCount === 3, 'should have only run gradle 3 times');
771772
assert(tr.stderr.length === 0, 'should not have written to stderr');
772773
assert(tr.ran(gradleWrapper + ` properties`), 'should have run Gradle with properties');
773774
assert(tr.ran(gradleWrapper + ` clean build jacocoTestReport`), 'should have run Gradle with code coverage');
@@ -794,7 +795,7 @@ describe('Gradle L0 Suite', function () {
794795
tr.run();
795796

796797
assert(tr.failed, 'task should have failed');
797-
assert(tr.invokedToolCount === 2, 'should have only run gradle 2 times');
798+
assert(tr.invokedToolCount === 3, 'should have only run gradle 3 times');
798799
assert(tr.stderr.length === 0, 'should not have written to stderr');
799800
assert(tr.stdout.indexOf('loc_mock_NoCodeCoverage') > -1, 'should have given an error message');
800801
assert(tr.ran(gradleWrapper + ` properties`), 'should have run Gradle with properties');
@@ -820,7 +821,7 @@ describe('Gradle L0 Suite', function () {
820821
tr.run();
821822

822823
assert(tr.succeeded, 'task should have succeeded');
823-
assert(tr.invokedToolCount === 2, 'should have only run gradle 2 times');
824+
assert(tr.invokedToolCount === 3, 'should have only run gradle 3 times');
824825
assert(tr.stderr.length === 0, 'should not have written to stderr');
825826
assert(tr.ran(`${gradleWrapper} properties`), 'should have run Gradle with properties');
826827
assert(tr.ran(`${gradleWrapper} clean build jacocoTestReport`), 'should have run Gradle with code coverage');
@@ -846,7 +847,7 @@ describe('Gradle L0 Suite', function () {
846847
tr.run();
847848

848849
assert(tr.succeeded, 'task should have succeeded');
849-
assert(tr.invokedToolCount === 2, 'should have only run gradle 2 times');
850+
assert(tr.invokedToolCount === 3, 'should have only run gradle 3 times');
850851
assert(tr.stderr.length === 0, 'should not have written to stderr');
851852
assert(tr.ran(`${gradleWrapper} properties`), 'should have run Gradle with properties');
852853
assert(tr.ran(`${gradleWrapper} clean build jacocoTestReport`), 'should have run Gradle with code coverage');
@@ -924,4 +925,22 @@ describe('Gradle L0 Suite', function () {
924925
});
925926
// /* END Tools tests */
926927

928+
it('extractGradleVersion returns correct results', (done) => {
929+
const log1: string = 'Gradle 4.0.1';
930+
const log2: string = 'Gradle 4.0';
931+
const log3: string = 'Gradle 3.5-rc-2';
932+
const log4: string = 'Gradle 8.5-20230916222118+0000';
933+
const log5: string = 'Gradle 8.5-20230916222118-0000';
934+
const log6: string = 'Gradle 8.4-branch-ljacomet_kotlin_kotlin_1_9_10-20230901164331+0000'
935+
const log7: string = '';
936+
assert(extractGradleVersion(log1) === '4.0.1', 'extractGradleVersion should return 4.0.1');
937+
assert(extractGradleVersion(log2) === '4.0', 'extractGradleVersion should return 4.0');
938+
assert(extractGradleVersion(log3) === '3.5-rc-2', 'extractGradleVersion should return 3.5-rc-2');
939+
assert(extractGradleVersion(log4) === '8.5-20230916222118+0000', 'extractGradleVersion should return 8.5-20230916222118+0000');
940+
assert(extractGradleVersion(log5) === '8.5-20230916222118-0000', 'extractGradleVersion should return 8.5-20230916222118-0000');
941+
assert(extractGradleVersion(log6) === '8.4-branch-ljacomet_kotlin_kotlin_1_9_10-20230901164331+0000', 'extractGradleVersion should return 8.4-branch-ljacomet_kotlin_kotlin_1_9_10-20230901164331+0000');
942+
assert(extractGradleVersion(log7) === 'unknown', 'extractGradleVersion should return unknown');
943+
944+
done();
945+
});
927946
});

Tasks/GradleV3/Tests/L0JacocoGradle4x.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,14 @@ let a: ma.TaskLibAnswers = <ma.TaskLibAnswers>{
2525
'/home/repo/src': true
2626
},
2727
'exec': {
28+
'gradlew.bat --version': {
29+
'code': 0,
30+
'stdout': 'More sample gradle output'
31+
},
32+
'gradlew --version': {
33+
'code': 0,
34+
'stdout': 'More sample gradle output'
35+
},
2836
'gradlew.bat properties': {
2937
'code': 0,
3038
'stdout': 'More sample gradle output'

Tasks/GradleV3/Tests/L0JacocoGradle5x.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,14 @@ let a: ma.TaskLibAnswers = <ma.TaskLibAnswers>{
2525
'/home/repo/src': true
2626
},
2727
'exec': {
28+
'gradlew.bat --version': {
29+
'code': 0,
30+
'stdout': 'More sample gradle output'
31+
},
32+
'gradlew --version': {
33+
'code': 0,
34+
'stdout': 'More sample gradle output'
35+
},
2836
'gradlew.bat properties': {
2937
'code': 0,
3038
'stdout': 'More sample gradle output'

Tasks/GradleV3/gradletask.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ import { CheckstyleTool } from 'azure-pipelines-tasks-codeanalysis-common/Common
88
import { FindbugsTool } from 'azure-pipelines-tasks-codeanalysis-common/Common/FindbugsTool';
99
import { SpotbugsTool } from 'azure-pipelines-tasks-codeanalysis-common/Common/SpotbugsTool';
1010
import { IAnalysisTool } from 'azure-pipelines-tasks-codeanalysis-common/Common/IAnalysisTool';
11+
import { emitTelemetry } from 'azure-pipelines-tasks-utility-common/telemetry';
1112
import { ToolRunner } from 'azure-pipelines-task-lib/toolrunner';
12-
import { getExecOptions, setJavaHome, setGradleOpts } from './Modules/environment';
13+
import { getExecOptions, setJavaHome, setGradleOpts, getGradleVersion } from './Modules/environment';
1314
import { configureWrapperScript, isMultiModuleProject } from './Modules/project-configuration';
1415
import { enableCodeCoverageAsync, publishTestResults, publishCodeCoverageResultsAsync, resolveCodeCoveragePreset } from './Modules/code-coverage';
1516
import { ICodeAnalysisResult, ICodeCoveragePreset, ICodeCoverageSettings, IPublishCodeCoverageSettings, ITaskResult } from './interfaces';
@@ -75,6 +76,9 @@ async function run() {
7576
reportingTaskName = codeCoveragePreset.reportingTaskName;
7677
// END: determine isMultiModule
7778

79+
// Determine gradle version
80+
const gradleVersion: string = getGradleVersion(wrapperScript);
81+
7882
// Clean the report directory before enabling code coverage
7983
tl.rmRF(reportDirectory);
8084

@@ -88,9 +92,12 @@ async function run() {
8892
reportDirectoryName: reportDirectoryName,
8993
summaryFileName: codeCoveragePreset.summaryFileName,
9094
isMultiModule: isMultiModule,
91-
gradle5xOrHigher: gradle5xOrHigher
95+
gradle5xOrHigher: gradle5xOrHigher,
96+
gradleVersion: gradleVersion
9297
};
9398

99+
emitTelemetry('TaskHub', 'GradleV3', { codeCoverageSettings: codeCoverageSettings });
100+
94101
await enableCodeCoverageAsync(codeCoverageSettings);
95102
}
96103

Tasks/GradleV3/interfaces.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export interface ICodeCoverageSettings {
1616
summaryFileName: string;
1717
isMultiModule: boolean;
1818
gradle5xOrHigher: boolean;
19+
gradleVersion: string;
1920
}
2021

2122
export interface IPublishCodeCoverageSettings {

0 commit comments

Comments
 (0)