Skip to content

Commit

Permalink
VSTS-190 Changed the directory for generating report-task.txt files (…
Browse files Browse the repository at this point in the history
…and thus the directory to get report from for uploading summaries) + updated UT
  • Loading branch information
mickael-caro-sonarsource committed Aug 5, 2019
1 parent 8570554 commit 956ba21
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 5 deletions.
14 changes: 14 additions & 0 deletions common/ts/__tests__/prepare-task-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,17 @@ it('should display warning for dedicated extension for Sonarcloud', async () =>
'There is a dedicated extension for SonarCloud: https://marketplace.visualstudio.com/items?itemName=SonarSource.sonarcloud'
);
});

it('should build report task path from variables', () => {
const reportDirectory = 'C:\\temp\\dir';
const buildNumber = '20250909.1';

const reportFullPath = `${reportDirectory}\\${buildNumber}\\report-task.txt`;

jest.spyOn(tl, 'getVariable').mockImplementationOnce(() => reportDirectory);
jest.spyOn(tl, 'getVariable').mockImplementationOnce(() => buildNumber);

const actual = prept.reportPath();

expect(actual).toBe(reportFullPath);
});
9 changes: 9 additions & 0 deletions common/ts/prepare-task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import Endpoint, { EndpointType } from './sonarqube/Endpoint';
import Scanner, { ScannerMode } from './sonarqube/Scanner';
import { toCleanJSON } from './helpers/utils';
import { getServerVersion } from './helpers/request';
import { REPORT_TASK_NAME } from './sonarqube/TaskReport';

const REPO_NAME_VAR = 'Build.Repository.Name';

Expand Down Expand Up @@ -35,6 +36,8 @@ export default async function prepareTask(endpoint: Endpoint, rootPath: string)
.map(keyValue => keyValue.split(/=(.+)/))
.forEach(([k, v]) => (props[k] = v));

props['sonar.scanner.metadataFilePath'] = reportPath();

tl.setVariable('SONARQUBE_SCANNER_MODE', scannerMode);
tl.setVariable('SONARQUBE_ENDPOINT', endpoint.toJson(), true);
tl.setVariable(
Expand Down Expand Up @@ -114,6 +117,12 @@ function branchName(fullName: string) {
return fullName;
}

export function reportPath(): string {
return `${tl.getVariable('Agent.TempDirectory')}\\${tl.getVariable(
'Build.BuildNumber'
)}\\${REPORT_TASK_NAME}`;
}

/**
* Waiting for https://github.com/Microsoft/vsts-tasks/issues/7592
* query the repo to get the full name of the default branch.
Expand Down
4 changes: 2 additions & 2 deletions common/ts/sonarqube/TaskReport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ export default class TaskReport {
}

public static findTaskFileReport(): string[] {
const taskReportGlob = path.join('**', REPORT_TASK_NAME);
const taskReportGlob = path.join(tl.getVariable('Build.BuildNumber'), REPORT_TASK_NAME);
const taskReportGlobResult = tl.findMatch(
tl.getVariable('Agent.BuildDirectory'),
tl.getVariable('Agent.TempDirectory'),
taskReportGlob
);
tl.debug(`[SQ] Searching for ${taskReportGlob} - found ${taskReportGlobResult.length} file(s)`);
Expand Down
6 changes: 3 additions & 3 deletions common/ts/sonarqube/__tests__/TaskReport-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,12 @@ it('should find report files', async () => {
expect(reportFiles[0]).toBe('path1');
expect(reportFiles[1]).toBe('path2');

expect(tl.getVariable).toHaveBeenCalledTimes(1);
expect(tl.getVariable).toBeCalledWith('Agent.BuildDirectory');
expect(tl.getVariable).toHaveBeenCalledTimes(2);
expect(tl.getVariable).toBeCalledWith('Agent.TempDirectory');

// Calculate the expected path to take account of different
// path separators in Windows/non-Windows
const expectedSearchPath = path.join('**', 'report-task.txt');
const expectedSearchPath = path.join(tl.getVariable('Build.BuildNumber'), 'report-task.txt');
expect(tl.findMatch).toHaveBeenCalledTimes(1);
expect(tl.findMatch).toHaveBeenCalledWith('mock root search path', expectedSearchPath);
});

0 comments on commit 956ba21

Please sign in to comment.