Skip to content

Commit e81762b

Browse files
committed
Updated console reporter to report to stderr if violation severity is greater than info.
1 parent 381e631 commit e81762b

9 files changed

+76
-43
lines changed

CHANGELOG.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,10 @@
3939

4040
- Added new reporter for json
4141

42-
- Cleanup code and documentation
42+
- Cleanup code and documentation
43+
44+
## 2.0.5
45+
46+
- Updated console reporter to report to stderr if violation severity is greater than info.
47+
48+
- Minor code improvements.

bin/dbstyleguidechecker.dart

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,9 @@ Future<void> main(List<String> arguments) async {
7070
CodeStyleViolationsChecker checker;
7171

7272
try {
73-
final CodeStyleViolationsReporter reporter =
74-
_createReporter(scriptArgument);
73+
final CodeStyleViolationsReporter reporter = _createReporter(
74+
scriptArgument,
75+
);
7576

7677
checker = _createChecker(scriptArgument, reporter);
7778
} on UnrecoverableException catch (exception) {
@@ -114,7 +115,7 @@ CodeStyleViolationsChecker _createChecker(
114115
default:
115116
throw UnrecoverableException(
116117
'Invalid project type specified, '
117-
"supported are ${supportedProjectType.join(", ")}",
118+
"supported are ${supportedProjectType.join(", ")}",
118119
exitInvalidArgument,
119120
);
120121
}
@@ -132,7 +133,7 @@ CodeStyleViolationsReporter _createReporter(
132133
if (reporterOutputFile == null) {
133134
throw const UnrecoverableException(
134135
"Reporter of type 'file' specified "
135-
'but reporter output file not specified.',
136+
'but reporter output file not specified.',
136137
exitMissingRequiredArgument,
137138
);
138139
}

example/analysis_options.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
include: package:flutter_code_style/analysis_options.yaml

example/pubspec.yaml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,5 @@ author: darelbitsy <bitsydarel@gmail.com>
66
environment:
77
sdk: '>=2.4.0 <3.0.0'
88

9-
#dependencies:
10-
# path: ^1.6.0
11-
129
dev_dependencies:
1310
flutter_code_style: ^1.6.2
14-
test: ^1.8.0

lib/dbstyleguidechecker.dart

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,12 @@ abstract class CodeStyleViolationsChecker {
7474
/// [parser] to parse founded style guide violations.
7575
///
7676
/// [reporter] to report founded style guide violations.
77-
const CodeStyleViolationsChecker(this.styleGuide,
78-
this.projectDir,
79-
this.parser,
80-
this.reporter,);
77+
const CodeStyleViolationsChecker(
78+
this.styleGuide,
79+
this.projectDir,
80+
this.parser,
81+
this.reporter,
82+
);
8183

8284
/// Run the checker.
8385
Future<void> check() async {
@@ -117,6 +119,8 @@ abstract class CodeStyleViolationsParser {
117119
/// Parse violations contained in the [violations].
118120
///
119121
/// [projectDir] the violations are coming from.
120-
Future<List<CodeStyleViolation>> parse(final String violations,
121-
final String projectDir,);
122+
Future<List<CodeStyleViolation>> parse(
123+
final String violations,
124+
final String projectDir,
125+
);
122126
}

lib/src/checkers/dart_project_style_guide_checker.dart

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ class DartProjectStyleGuideChecker extends CodeStyleViolationsChecker {
5151
@override
5252
Future<String> getCodeStyleViolations() {
5353
return runPubGet().then(
54-
(_) => Process.run(
54+
(_) => Process.run(
5555
'dartanalyzer',
5656
<String>[
5757
'--format',
@@ -62,6 +62,7 @@ class DartProjectStyleGuideChecker extends CodeStyleViolationsChecker {
6262
],
6363
runInShell: true,
6464
stdoutEncoding: utf8,
65+
stderrEncoding: utf8,
6566
).then((ProcessResult processResult) {
6667
final String output = processResult.stdout.toString();
6768

@@ -78,6 +79,7 @@ class DartProjectStyleGuideChecker extends CodeStyleViolationsChecker {
7879
<String>['get'],
7980
runInShell: true,
8081
stdoutEncoding: utf8,
82+
stderrEncoding: utf8,
8183
).then<void>((ProcessResult result) {
8284
final String errorOutput = result.stderr.toString();
8385

lib/src/reporters/console_code_style_violations_reporter.dart

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,23 @@ class ConsoleCodeStyleViolationsReporter extends CodeStyleViolationsReporter {
4545
@override
4646
Future<void> report(List<CodeStyleViolation> violations) async {
4747
for (final CodeStyleViolation violation in violations) {
48-
stdout.writeln(
49-
yellow.wrap(
50-
'${violation.severity.id}, ${violation.ruleDescription} in file '
51-
'${violation.file} at line ${violation.line}, line column '
52-
'${violation.lineColumn}',
53-
),
54-
);
48+
if (violation.severity.level > ViolationSeverity.info.level) {
49+
stderr.writeln(
50+
red.wrap(
51+
'${violation.severity.id}, ${violation.ruleDescription} in file '
52+
'${violation.file} at line ${violation.line}, line column '
53+
'${violation.lineColumn}',
54+
),
55+
);
56+
} else {
57+
stdout.writeln(
58+
yellow.wrap(
59+
'${violation.severity.id}, ${violation.ruleDescription} in file '
60+
'${violation.file} at line ${violation.line}, line column '
61+
'${violation.lineColumn}',
62+
),
63+
);
64+
}
5565
}
5666
}
5767
}

lib/src/reporters/github/github_api_service.dart

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,15 @@ class GithubApiService {
5858
final Map<String, String> headers;
5959

6060
/// Create a new [GithubApiService].
61-
GithubApiService(this.repoOwner,
62-
this.repoName,
63-
this.apiToken, [
64-
this.baseUrl = 'https://api.github.com',
65-
]) : headers = <String, String>{
66-
'Accept': 'application/vnd.github.v3+json',
67-
'Authorization': 'token $apiToken'
68-
};
61+
GithubApiService(
62+
this.repoOwner,
63+
this.repoName,
64+
this.apiToken, [
65+
this.baseUrl = 'https://api.github.com',
66+
]) : headers = <String, String>{
67+
'Accept': 'application/vnd.github.v3+json',
68+
'Authorization': 'token $apiToken'
69+
};
6970

7071
/// Verify if pull request is open.
7172
Future<bool> isPullRequestOpen(final String pullRequestId) async {
@@ -93,10 +94,12 @@ class GithubApiService {
9394
}
9495

9596
/// Add a review comment to the github pull request.
96-
Future<void> addReviewComment(final String pullRequestId,
97-
final CodeStyleViolation violation,
98-
final GithubFileDiff fileDiff,
99-
final String commitId,) async {
97+
Future<void> addReviewComment(
98+
final String pullRequestId,
99+
final CodeStyleViolation violation,
100+
final GithubFileDiff fileDiff,
101+
final String commitId,
102+
) async {
100103
final int violationLineInDiff = await findViolationLineInFileDiff(
101104
fileDiff?.patch,
102105
violation.line,
@@ -125,8 +128,10 @@ class GithubApiService {
125128
}
126129

127130
/// Require code changes.
128-
Future<void> requestChanges(final String commitId,
129-
final String pullRequestId,) async {
131+
Future<void> requestChanges(
132+
final String commitId,
133+
final String pullRequestId,
134+
) async {
130135
final Map<String, dynamic> reviewStatus = <String, dynamic>{
131136
'commit_id': commitId,
132137
'event': 'REQUEST_CHANGES',
@@ -149,12 +154,14 @@ class GithubApiService {
149154
}
150155

151156
/// Notify github that the pull request meet the project code style.
152-
Future<void> onCodeStyleViolationNotFound(final String commitId,
153-
final String pullRequestId,) async {
157+
Future<void> onCodeStyleViolationNotFound(
158+
final String commitId,
159+
final String pullRequestId,
160+
) async {
154161
final Map<String, dynamic> reviewStatus = <String, dynamic>{
155162
'commit_id': commitId,
156163
'body':
157-
'This is close to perfect! Waiting for someone to review and merge',
164+
'This is close to perfect! Waiting for someone to review and merge',
158165
};
159166

160167
final http.Response response = await http.post(
@@ -172,7 +179,9 @@ class GithubApiService {
172179
}
173180

174181
/// Get the files included in a pull request.
175-
Future<List<GithubFileDiff>> getPullRequestFiles(final String pullRequestId,) async {
182+
Future<List<GithubFileDiff>> getPullRequestFiles(
183+
final String pullRequestId,
184+
) async {
176185
final http.Response response = await http.get(
177186
'$baseUrl/repos/$repoOwner/$repoName/pulls/$pullRequestId/files',
178187
headers: headers,
@@ -244,7 +253,11 @@ class GithubApiService {
244253
@visibleForTesting
245254
String formatViolationMessage(CodeStyleViolation violation) {
246255
final StringBuffer template = StringBuffer()
247-
..writeln(violation.ruleDescription)..writeln('**SEVERITY**: ${violation.severity.id}')..writeln('**RULE**: ${violation.rule}')..writeln('**FILE**: ${violation.file}')..writeln('**LINE**: ${violation.line}');
256+
..writeln(violation.ruleDescription)
257+
..writeln('**SEVERITY**: ${violation.severity.id}')
258+
..writeln('**RULE**: ${violation.rule}')
259+
..writeln('**FILE**: ${violation.file}')
260+
..writeln('**LINE**: ${violation.line}');
248261

249262
return template.toString();
250263
}

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: dbstyleguidechecker
22
description: A tool that help you verify a project agains code style guide and automatize pull request review.
3-
version: 2.0.4
3+
version: 2.0.5
44
homepage: https://github.com/bitsydarel/dbstyleguidechecker
55
repository: https://github.com/bitsydarel/dbstyleguidechecker
66
issue_tracker: https://github.com/bitsydarel/dbstyleguidechecker/issues

0 commit comments

Comments
 (0)