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

Implement Ballerina Scan Tool for Static Code Analysis #1

Closed
wants to merge 6 commits into from

Conversation

Xenowa
Copy link
Contributor

@Xenowa Xenowa commented Mar 4, 2024

Purpose

The purpose of this PR is to develop a Ballerina tool that performs static code analysis for Ballerina projects for identifying potential bugs, security vulnerabilities, and style violations and report analysis issues to static code analysis platforms like SonarQube.

Fixes #42256

Goals

  • Creating a Ballerina scan tool for performing core static code analysis based on language specific rules
  • Extending the Ballerina scan tool for reporting analysis issues to static code analysis platforms
  • Extending the Ballerina scan tool for introducing additional library specific rules and external rules

Approach

The initial release introduces the bal scan command. Current implementation uses the project API to retrieve syntax trees of all Ballerina files in a project for performing core analysis and generating an analysis report. However the generated report will have no issues as core rules are not implemented yet. As core analysis rules are reviewed and implemented, the scan tool will progressively gain the ability to identify and report potential issues and further features will be added iteratively as outlined in task #42260

Check List

  • Read the Contributing Guide
  • Updated Change Log
  • Checked Tooling Support
  • Added necessary tests
    • Unit Tests
    • Spec Conformance Tests
    • Integration Tests
    • Ballerina By Example Tests
  • Increased Test Coverage
  • Added necessary documentation
    • API documentation
    • Module documentation in Module.md files
    • Ballerina By Examples

@CLAassistant
Copy link

CLAassistant commented Mar 4, 2024

CLA assistant check
All committers have signed the CLA.

@@ -0,0 +1,2 @@
# Ensure all Java files use LF.
*.java eol=lf
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
*.java eol=lf
*.java eol=lf

packageUser: ${{ github.actor }}
packagePAT: ${{ secrets.GITHUB_TOKEN }}
run: |
./gradlew build
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
./gradlew build
./gradlew build

.vscode/

### Mac OS ###
.DS_Store
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
.DS_Store
.DS_Store

4. Run analysis and generate an analysis report.
```bash
bal scan --scan-report
```
Copy link
Contributor

Choose a reason for hiding this comment

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

We can remove this extra line

}

public int getStartLine() {

Choose a reason for hiding this comment

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

Suggested change

message,
issueType,
type,
moduleName + "/" + documentName,
Copy link
Contributor

Choose a reason for hiding this comment

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

File separator is depend on the OS. So it is better to not hardcode it.

throw new RuntimeException(e);
}

return target;
Copy link
Contributor

Choose a reason for hiding this comment

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

You can remove the target variable and return in each scenario separately.

@@ -0,0 +1,13 @@
# Scan tool properties
scanToolVersion=1.0.0
Copy link
Contributor

Choose a reason for hiding this comment

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

Fix these ^M

return allIssues;
}

public void analyzeDocument(Project currentProject,
Copy link

@poorna2152 poorna2152 Mar 4, 2024

Choose a reason for hiding this comment

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

Not sure whether this is the expected formatting for parameters in the Java function definitions. Can you check

@@ -0,0 +1,2 @@
rootProject.name = 'static-code-analysis-tool'
include 'ScanCommand'
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
include 'ScanCommand'
include 'ScanCommand'

import ballerina/io;
public function main() {
io:println("Ballerina Scan tool");
}
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
}
}

@Xenowa
Copy link
Contributor Author

Xenowa commented Mar 4, 2024

Since this PR is too large, will be breaking it down to smaller parts and send them

@Xenowa Xenowa closed this Mar 4, 2024
Xenowa referenced this pull request in Xenowa/static-code-analysis-tool Mar 4, 2024
Xenowa referenced this pull request in Xenowa/static-code-analysis-tool Mar 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[New Feature]: Support for Static Code Analysis in Ballerina
5 participants