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

Pull Request Code Analysis resolves module file paths when appropriate. #3039

Merged
merged 1 commit into from
Nov 7, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 36 additions & 2 deletions Tasks/PullRequestCodeAnalysis/PRCA/SonarQubeReportProcessor.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as fs from 'fs';
import * as path from 'path';
import * as util from 'util';

import { PRInjectorError } from './PRInjectorError';
Expand Down Expand Up @@ -61,8 +62,18 @@ export class SonarQubeReportProcessor implements ISonarQubeReportProcessor {
throw new PRInjectorError('Invalid SonarQube report - some components do not have keys');
}

if (component.path) {
map.set(component.key, component.path);
if (component.path != null) {
var fullPath = component.path;

if (component.moduleKey != null) { // if the component belongs to a module, we need to prepend the module path
// #TODO: Support nested modules once the SonarQube report correctly lists moduleKey in nested modules
var module:any = this.GetObjectWithKey(sonarQubeReport.components, component.moduleKey);
if (module.path != null) { // some modules do not list a path
fullPath = path.join('/', module.path, component.path); // paths must start with a path seperator
}
}

map.set(component.key, path.normalize(fullPath));
}
}

Expand Down Expand Up @@ -180,6 +191,29 @@ export class SonarQubeReportProcessor implements ISonarQubeReportProcessor {
return 6;
}
}

/**
* Finds and returns the first object with the given key from a given section of the SonarQube report.
* @param sonarQubeReportSection
* @param searchKey
* @returns {any} Null if object not found, otherwise the first object with a "key" field matching searchKey.
*/
private GetObjectWithKey(sonarQubeReportSection: any, searchKey: string): any {

if (!sonarQubeReportSection) {
return null;
}

for (var component of sonarQubeReportSection) {
if (!component.key) {
throw new PRInjectorError('Invalid SonarQube report - some components do not have keys');
}

if (component.key == searchKey) {
return component;
}
}
}
}


12 changes: 6 additions & 6 deletions Tasks/PullRequestCodeAnalysis/Tests/L0.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ function VerifyMessage(
expectedPriority: number) {

chai.expect(actualMessage.content).to.equal(expectedContent, 'Content mismatch');
chai.expect(actualMessage.file).to.equal(expectedFile, 'File mismatch');
chai.expect(path.normalize(actualMessage.file)).to.equal(path.normalize(expectedFile), 'File mismatch');
chai.expect(actualMessage.line).to.equal(expectedLine, 'Line mismatch');
chai.expect(actualMessage.priority).to.equal(expectedPriority, 'Priority mismatch');
}
Expand Down Expand Up @@ -193,7 +193,7 @@ describe('The PRCA', function () {

it('has multiple comments to post', () => {
// Arrange
server.setModifiedFilesInPr(['src/main/java/com/mycompany/app/App.java']);
server.setModifiedFilesInPr(['my-app/src/main/java/com/mycompany/app/App.java']);
var sqReportPath: string = path.join(__dirname, 'data', 'sonar-report.json');

// Act
Expand Down Expand Up @@ -381,15 +381,15 @@ describe('The PRCA', function () {
// Assert
chai.expect(messages).to.have.length(3, 'There are 3 new issues in the report');

// valid issue
// valid issue in a module with a path
VerifyMessage(
messages[0],
'Remove this unused "x" local variable. (squid:S1481)',
'src/main/java/com/mycompany/app/App.java',
'my-app/src/main/java/com/mycompany/app/App.java',
12,
3);

// another valid issue in a different file
// another valid issue in a different file, but in a module without a path
VerifyMessage(
messages[1],
'Replace this usage of System.out or System.err by a logger. (squid:S106)',
Expand All @@ -401,7 +401,7 @@ describe('The PRCA', function () {
VerifyMessage(
messages[2],
'Bad code right here... (squid:S106)',
'src/main/java/com/mycompany/app/App.java',
'my-app/src/main/java/com/mycompany/app/App.java',
15,
6);

Expand Down
8 changes: 6 additions & 2 deletions Tasks/PullRequestCodeAnalysis/Tests/data/sonar-report.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,11 @@
],
"components": [
{
"key": "com.mycompany.app:my-app"
"key": "com.mycompany.app:my-app",
"path":"my-app"
},
{
"key": "com.mycompany.test:test"
},
{
"key": "com.mycompany.app:my-app:src/main/java/com/mycompany/app/App.java",
Expand All @@ -89,7 +93,7 @@
{
"key": "com.mycompany.app:my-app:src/test/java/com/mycompany/app/AppTest.java",
"path": "src/test/java/com/mycompany/app/AppTest.java",
"moduleKey": "com.mycompany.app:my-app",
"moduleKey": "com.mycompany.test:test",
"status": "SAME"
},
{
Expand Down