How to check project structure and report issues at project level?


I’m creating custom rules in Java that check my company’s coding requirements. One of the specifications says:

Projects must follow the naming convention xxx_yyy

There are other specs that involve checking that files also follow naming conventions, that they are placed in their proper folders, etc.

Is it possible to run a single check for the project file system and report issues on it instead of individual files (that might or might not be java, as well)?

You can implement your own org.sonar.apai.bath.Sensor. Its method

void execute(SensorContext context)

will be invoked for each project module starting from “leaf” modules. Sensor context contains filesystem so you have access to project (module) structure.

Thank you, I’ve been able to implement my own Sensor and report arbitrary issues on files.

I’m having trouble targetting the base directory as a location to report the issue. How can I give the NewIssueLocation the file system’s base directory as a InputFile? I’ve tried this:

FileSystem fs = context.fileSystem();
NewIssue newIssue = context.newIssue()
				.forRule(RuleKey.of("repoKey", "ruleKey"));

NewIssueLocation primaryLocation = newIssue.newLocation()
		.message("test message on baseDir");;;

But fs.inputFile(fs.predicates().is(fs.baseDir())) returns null and throws java.lang.IllegalArgumentException: Component can't be null

I figured it out. The NewIssueLocation.on() method has InputComponent as parameter, so I can pass the context.project() itself and flag the project on its own as I intended. The end result looks like this:

NewIssue newIssue = context.newIssue()
		.forRule(RuleKey.of("repoKey", "ruleKey"));

NewIssueLocation primaryLocation = newIssue.newLocation()
		.message("issue message");;;

I’m off to buy a rubber duck.