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

refactor: Uses standard org.sonar.check.Rule annotation #16

Closed
Closed
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
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,4 @@ In order to implement a check for the rule, create a Check class inherited from

Have a look at `swift-lang/src/main/java/io/ecocode/ios/swift/checks/idleness/IdleTimerDisabledCheck` to learn more about the implementation.

Don't forget to add the `@RegisterRule` annotation to the check in order to register it to the AST visitor.
Don't forget to add the `@org.sonar.check.Rule` annotation to the check in order to register it to the AST visitor.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ ecoCode iOS SonarQube plugin is an "eco-responsibility" static code analyzer for

Ready to use binaries are available [from GitHub](https://github.com/green-code-initiative/ecoCode-ios/releases).

🚀 Quickstart
NB: To work, `ecocode-ios` needs `Swift` language support in SonarQube. For *SonarQube Community Edition* (which does not support Swift language), you need to install an additional plugin like [sonar-apple](https://github.com/insideapp-oss/sonar-apple).

🚀 Development Quickstart
-------------

### Requirements
Expand Down
2 changes: 1 addition & 1 deletion commons-ios/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,4 @@

</dependencies>

</project>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package io.ecocode.ios.swift;
package io.ecocode.ios;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
public class Const {
public static final String SWIFT_REPOSITORY_KEY = "ecoCode-swift";

@Retention(RetentionPolicy.RUNTIME)
public @interface RegisterRule {
private Const() {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,11 @@ public class ReportIssue {

public ReportIssue(String ruleId, String message, @Nullable String filePath, @Nullable Integer lineNumber) {
this.ruleId = ruleId;
this.message = message;
this.message = Objects.requireNonNull(message);
this.filePath = filePath;
this.lineNumber = lineNumber;
}

public ReportIssue(String ruleId, String message) {
this.ruleId = ruleId;
this.message = message;
this.filePath = null;
this.lineNumber = null;
}

public String getRuleId() {
return ruleId;
}
Expand Down
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should keep the repository parameter in case we want to reuse the issue recorder to register rules for other iOS compliant languages (such as Objective-C)

Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,13 @@ public class ReportIssueRecorder {

private static final Logger LOGGER = Loggers.get(ReportIssueRecorder.class);
private final SensorContext sensorContext;
private static final String REPOSITORY_KEY = "ecoCode-swift";

public ReportIssueRecorder(SensorContext sensorContext) {
this.sensorContext = sensorContext;
}

public void recordIssues(List<ReportIssue> issues, String repository) {
public void recordIssues(List<ReportIssue> issues) {

final FileSystem fs = sensorContext.fileSystem();
final FilePredicates predicates = fs.predicates();
Expand All @@ -51,7 +52,7 @@ public void recordIssues(List<ReportIssue> issues, String repository) {
// The issue to be record
NewIssue sonarIssue = sensorContext
.newIssue()
.forRule(RuleKey.of(repository, issue.getRuleId()));
.forRule(RuleKey.of(REPOSITORY_KEY, issue.getRuleId()));
// The location of the issue to be record
NewIssueLocation sonarIssueLocation = sonarIssue.newLocation()
.message(issue.getMessage());
Expand Down Expand Up @@ -86,7 +87,6 @@ public void recordIssues(List<ReportIssue> issues, String repository) {
// Associating the location to the issue and saving it.
sonarIssue.at(sonarIssueLocation).save();
}

}
}
}
72 changes: 21 additions & 51 deletions commons-ios/src/main/java/io/ecocode/ios/checks/RuleCheck.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,68 +17,48 @@
*/
package io.ecocode.ios.checks;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import io.ecocode.ios.antlr.AntlrContext;
import io.ecocode.ios.antlr.ParseTreeItemVisitor;
import io.ecocode.ios.rules.RepositoryRule;
import io.ecocode.ios.rules.RepositoryRuleParser;
import org.sonar.api.batch.fs.InputFile;
import org.sonar.api.batch.sensor.SensorContext;
import org.sonar.api.utils.log.Logger;
import org.sonar.api.utils.log.Loggers;
import org.sonar.check.Rule;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import static java.lang.String.format;
import static java.util.Optional.ofNullable;

public abstract class RuleCheck implements ParseTreeItemVisitor {

private static final Logger LOGGER = Loggers.get(RuleCheck.class);

protected String ruleId;
protected String rulesPath;
protected String repositoryKey;

private class Issue {
protected Issue(String ruleId, int startIndex) {

protected Issue(String ruleId, int startIndex, String message) {
this.ruleId = ruleId;
this.startIndex = startIndex;
this.message = Objects.requireNonNull(message);
}
protected String ruleId;
protected int startIndex;
final String ruleId;
final int startIndex;
final String message;
}

private List<Issue> issues = new ArrayList<>();
private final List<Issue> issues = new ArrayList<>();

private static List<RepositoryRule> rules = new ArrayList<>();
public static RepositoryRule getRepositoryRule(String ruleId, String rulesPath) throws IOException {
if (rules.isEmpty()) {
RepositoryRuleParser repositoryRuleParser = new RepositoryRuleParser();
rules = repositoryRuleParser.parse(rulesPath);
}
Optional<RepositoryRule> rule = rules.stream().filter(r -> r.getKey().equals(ruleId)).findFirst();
if (rule.isPresent()) {
return rule.get();
} else {
return null;
}
protected RuleCheck() {
this.ruleId = ofNullable(this.getClass().getAnnotation(Rule.class))
.orElseThrow(() -> new IllegalArgumentException("Please add @org.sonar.check.Rule to: " + this.getClass()))
.key();
}

protected RuleCheck(String ruleId, String rulesPath, String repositoryKey) {
this.ruleId = ruleId;
this.rulesPath = rulesPath;
this.repositoryKey = repositoryKey;
}

protected void recordIssue(String ruleId, int startIndex) {
issues.add(new Issue(ruleId, startIndex));
protected void recordIssue(int startIndex, String message) {
issues.add(new Issue(this.ruleId, startIndex, message));
}

@Override
public void fillContext(SensorContext context, AntlrContext antlrContext) {

final InputFile file = antlrContext.getFile();
if (file == null) {
return;
Expand All @@ -90,22 +70,12 @@ public void fillContext(SensorContext context, AntlrContext antlrContext) {
// Compute location
int[] loc = antlrContext.getLineAndColumn(i.startIndex);
// Retrieve rule data
try {
RepositoryRule rule = RuleCheck.getRepositoryRule(i.ruleId, rulesPath);
if (rule != null) {
reportIssues.add(new ReportIssue(i.ruleId, rule.getDescription(), file.toString(), loc[0]));
} else {
LOGGER.warn(format("Failed to identify rule %s", i.ruleId));
}

} catch (IOException e) {
LOGGER.warn(format("Failed to identify rule %s", i.ruleId),e);
}
reportIssues.add(new ReportIssue(i.ruleId, i.message, file.toString(), loc[0]));
}

// Record
ReportIssueRecorder recorder = new ReportIssueRecorder(context);
recorder.recordIssues(reportIssues, repositoryKey);
recorder.recordIssues(reportIssues);

// Clear issues for next file
issues.clear();
Expand Down

This file was deleted.

68 changes: 0 additions & 68 deletions commons-ios/src/main/java/io/ecocode/ios/rules/RepositoryRule.java

This file was deleted.

Loading
Loading