From 2833be4b6c514989a74b6b4592c51d8a0a561c48 Mon Sep 17 00:00:00 2001 From: HelloCore Date: Tue, 3 Oct 2023 21:35:52 +0700 Subject: [PATCH 01/11] feat: add git diff in DartUtils --- dangerfile.dart | 5 +- packages/danger_core/lib/danger_core.dart | 1 + .../danger_core/lib/src/danger_utils.dart | 30 +- .../danger_core/lib/src/models/git_diff.dart | 28 ++ .../lib/src/utils/git_diff_parser.dart | 58 +++ .../test/fixtures/diff/add_only.diff | 27 ++ .../danger_core/test/fixtures/diff/mix.diff | 27 ++ .../test/fixtures/diff/multiple_chunk.diff | 26 ++ .../test/fixtures/diff/multiple_files.diff | 326 ++++++++++++++ .../test/fixtures/diff/remove_only.diff | 23 + .../test/fixtures/diff/rename_only.diff | 4 + .../test/parser/git_diff_test.dart | 119 +++++ tools/diff_parser.dart | 418 ++++++++++++++++++ 13 files changed, 1090 insertions(+), 2 deletions(-) create mode 100644 packages/danger_core/lib/src/models/git_diff.dart create mode 100644 packages/danger_core/lib/src/utils/git_diff_parser.dart create mode 100644 packages/danger_core/test/fixtures/diff/add_only.diff create mode 100644 packages/danger_core/test/fixtures/diff/mix.diff create mode 100644 packages/danger_core/test/fixtures/diff/multiple_chunk.diff create mode 100644 packages/danger_core/test/fixtures/diff/multiple_files.diff create mode 100644 packages/danger_core/test/fixtures/diff/remove_only.diff create mode 100644 packages/danger_core/test/fixtures/diff/rename_only.diff create mode 100644 packages/danger_core/test/parser/git_diff_test.dart create mode 100644 tools/diff_parser.dart diff --git a/dangerfile.dart b/dangerfile.dart index 55896e7..a474d84 100644 --- a/dangerfile.dart +++ b/dangerfile.dart @@ -4,7 +4,10 @@ import 'package:path/path.dart' show join, current; import 'package:danger_core/danger_core.dart'; import 'package:danger_plugin_dart_test/danger_plugin_dart_test.dart'; -void main() { +void main() async { + final fullDiff = await DangerUtils.getFullDiff(); + message('There are ${fullDiff.length} changed files'); + if (danger.isGitHub) { if (danger.github.pr.title.contains('WIP') == true) { warn('PR is considered WIP'); diff --git a/packages/danger_core/lib/danger_core.dart b/packages/danger_core/lib/danger_core.dart index fc57547..5f5973c 100644 --- a/packages/danger_core/lib/danger_core.dart +++ b/packages/danger_core/lib/danger_core.dart @@ -4,6 +4,7 @@ library danger_core; export 'src/danger_executor.dart'; +export 'src/danger_utils.dart'; export 'src/models/danger_dsl.dart'; export 'src/models/bitbucket_cloud.dart'; export 'src/models/git_dsl.dart'; diff --git a/packages/danger_core/lib/src/danger_utils.dart b/packages/danger_core/lib/src/danger_utils.dart index 62a19b0..b249f4a 100644 --- a/packages/danger_core/lib/src/danger_utils.dart +++ b/packages/danger_core/lib/src/danger_utils.dart @@ -1,10 +1,38 @@ import 'dart:io'; +import 'package:danger_core/danger_core.dart'; +import 'package:danger_core/src/models/git_diff.dart'; +import 'package:danger_core/src/utils/git_diff_parser.dart'; + class DangerUtils { - Future spawn(String command, + DangerUtils._(); + + static Future spawn(String command, {List arguments = const []}) async { final result = await Process.run(command, arguments); return result.stdout.toString().trim(); } + + static Future> getFullDiff() async { + var base = ''; + + if (danger.isGitHub) { + base = danger.github.pr.base.ref; + } else if (danger.isBitbucketCloud) { + base = danger.bitbucketCloud.pr.destination.branch.name; + } else if (danger.isGitLab) { + base = danger.gitLab.mergeRequest.targetBranch; + } else { + base = danger.settings.cliArgs?['base'] ?? ''; + } + + if (base.isEmpty) { + throw 'Cannot find base branch'; + } + + final data = + await DangerUtils.spawn('git', arguments: ['diff', 'HEAD', base]); + return GitDiffParser.parse(data); + } } diff --git a/packages/danger_core/lib/src/models/git_diff.dart b/packages/danger_core/lib/src/models/git_diff.dart new file mode 100644 index 0000000..4784098 --- /dev/null +++ b/packages/danger_core/lib/src/models/git_diff.dart @@ -0,0 +1,28 @@ +class GitDiff { + final String fromFile; + final String toFile; + final List diffBlocks; + + GitDiff( + {required this.fromFile, required this.toFile, required this.diffBlocks}); +} + +class DiffBlock { + final int fromStart; + final int fromEnd; + final int toStart; + final int toEnd; + + final List addedLines; + final List removedLines; + final List unchangedLines; + + DiffBlock( + {required this.fromStart, + required this.fromEnd, + required this.toStart, + required this.toEnd, + required this.addedLines, + required this.removedLines, + required this.unchangedLines}); +} diff --git a/packages/danger_core/lib/src/utils/git_diff_parser.dart b/packages/danger_core/lib/src/utils/git_diff_parser.dart new file mode 100644 index 0000000..95ce7fe --- /dev/null +++ b/packages/danger_core/lib/src/utils/git_diff_parser.dart @@ -0,0 +1,58 @@ +import 'package:danger_core/src/models/git_diff.dart'; + +class GitDiffParser { + GitDiffParser._(); + + static List parse(String diffData) { + List diffs = []; + List lines = diffData.split('\n'); + GitDiff? currentDiff; + DiffBlock? currentBlock; + + for (var line in lines) { + if (line.startsWith('diff --git')) { + final fromFile = line.split(' ')[2].substring(2); + final toFile = line.split(' ')[3].substring(2); + currentDiff = + GitDiff(fromFile: fromFile, toFile: toFile, diffBlocks: []); + diffs.add(currentDiff); + continue; + } + + if (line.startsWith('@@')) { + String header = line.substring(3); + List parts = header.split(' '); + List fromRange = parts[0].split(','); + List toRange = parts[1].split(','); + + final fromStart = int.parse(fromRange[0].substring(1)); + final fromEnd = fromStart + + (fromRange.length > 1 ? int.parse(fromRange[1]) : 1) - + 1; + final toStart = int.parse(toRange[0].substring(1)); + final toEnd = + toStart + (toRange.length > 1 ? int.parse(toRange[1]) : 1) - 1; + + currentBlock = DiffBlock( + fromStart: fromStart, + fromEnd: fromEnd, + toStart: toStart, + toEnd: toEnd, + addedLines: [], + removedLines: [], + unchangedLines: []); + currentDiff?.diffBlocks.add(currentBlock); + } + + if (line.startsWith('+')) { + currentBlock?.addedLines.add(line.substring(1)); + } else if (line.startsWith('-')) { + currentBlock?.removedLines.add(line.substring(1)); + } else if (line.startsWith(' ')) { + currentBlock?.unchangedLines.add(line.substring(1)); + } + } + + return diffs; + } +} diff --git a/packages/danger_core/test/fixtures/diff/add_only.diff b/packages/danger_core/test/fixtures/diff/add_only.diff new file mode 100644 index 0000000..9c608f6 --- /dev/null +++ b/packages/danger_core/test/fixtures/diff/add_only.diff @@ -0,0 +1,27 @@ +diff --git a/.github/workflows/verify_compatibility.yaml b/.github/workflows/verify_compatibility.yaml +index 507cacb..701dd94 100644 +--- a/.github/workflows/verify_compatibility.yaml ++++ b/.github/workflows/verify_compatibility.yaml +@@ -38,6 +38,22 @@ jobs: + run: dart pub get + working-directory: packages/danger_dart/ + ++ - name: Install app (non nullsafety) dependencies ++ run: dart pub get ++ working-directory: example/pre_nullsafety/ ++ ++ - name: Run danger local (non nullsafety) ++ run: danger_dart local ++ working-directory: example/pre_nullsafety/ ++ ++ - name: Install app dependencies (nullsafety) ++ run: dart pub get ++ working-directory: example/target_nullsafety/ ++ ++ - name: Run danger local (nullsafety) ++ run: danger_dart local ++ working-directory: example/target_nullsafety/ ++ + - name: Install plugin dependencies + run: dart pub get + working-directory: example/with_plugin/danger_plugin_example/ \ No newline at end of file diff --git a/packages/danger_core/test/fixtures/diff/mix.diff b/packages/danger_core/test/fixtures/diff/mix.diff new file mode 100644 index 0000000..732fddb --- /dev/null +++ b/packages/danger_core/test/fixtures/diff/mix.diff @@ -0,0 +1,27 @@ +diff --git a/example/dart3/pubspec.yaml b/example/pre_nullsafety/pubspec.yaml +similarity index 64% +rename from example/dart3/pubspec.yaml +rename to example/pre_nullsafety/pubspec.yaml +index 8aca558..9c93b1b 100644 +--- a/example/dart3/pubspec.yaml ++++ b/example/pre_nullsafety/pubspec.yaml +@@ -1,13 +1,16 @@ +-name: danger_test_target_null_safety ++name: danger_test_pre_nullsafety + description: A simple command-line application. + # version: 1.0.0 + # homepage: https://www.example.com + + environment: +- sdk: ">=3.0.0 <4.0.0" ++ sdk: ">=2.7.0 <3.0.0" ++ ++dependencies: ++ json_annotation: ^3.0.0 + + dev_dependencies: + lints: ^1.0.1 +- path: ^1.8.3 ++ path: ^1.8.0 + danger_core: + path: ../../packages/danger_core \ No newline at end of file diff --git a/packages/danger_core/test/fixtures/diff/multiple_chunk.diff b/packages/danger_core/test/fixtures/diff/multiple_chunk.diff new file mode 100644 index 0000000..c50c119 --- /dev/null +++ b/packages/danger_core/test/fixtures/diff/multiple_chunk.diff @@ -0,0 +1,26 @@ +diff --git a/packages/danger_plugin_golden_reporter/pubspec.yaml b/packages/danger_plugin_golden_reporter/pubspec.yaml +index eca2e35..a0d38bd 100644 +--- a/packages/danger_plugin_golden_reporter/pubspec.yaml ++++ b/packages/danger_plugin_golden_reporter/pubspec.yaml +@@ -1,10 +1,10 @@ + name: danger_plugin_golden_reporter + description: A Danger Dart plugin to display golden images on pull requests. +-version: 2.0.0 ++version: 1.0.0 + homepage: https://github.com/danger/dart + + environment: +- sdk: ">=3.0.0 <4.0.0" ++ sdk: ">=2.12.0 <3.0.0" + + dependencies: + # danger_core: ">= 1.0.0 < 2.0.0" +@@ -12,7 +12,7 @@ dependencies: + path: ../danger_core + + dev_dependencies: +- lints: ^2.1.1 ++ lints: ^1.0.1 + test: ^1.20.2 + mockito: ^5.1.0 + build_runner: ^2.1.8 \ No newline at end of file diff --git a/packages/danger_core/test/fixtures/diff/multiple_files.diff b/packages/danger_core/test/fixtures/diff/multiple_files.diff new file mode 100644 index 0000000..537d35b --- /dev/null +++ b/packages/danger_core/test/fixtures/diff/multiple_files.diff @@ -0,0 +1,326 @@ +diff --git a/.github/workflows/verify_compatibility.yaml b/.github/workflows/verify_compatibility.yaml +index 507cacb..701dd94 100644 +--- a/.github/workflows/verify_compatibility.yaml ++++ b/.github/workflows/verify_compatibility.yaml +@@ -38,6 +38,22 @@ jobs: + run: dart pub get + working-directory: packages/danger_dart/ + ++ - name: Install app (non nullsafety) dependencies ++ run: dart pub get ++ working-directory: example/pre_nullsafety/ ++ ++ - name: Run danger local (non nullsafety) ++ run: danger_dart local ++ working-directory: example/pre_nullsafety/ ++ ++ - name: Install app dependencies (nullsafety) ++ run: dart pub get ++ working-directory: example/target_nullsafety/ ++ ++ - name: Run danger local (nullsafety) ++ run: danger_dart local ++ working-directory: example/target_nullsafety/ ++ + - name: Install plugin dependencies + run: dart pub get + working-directory: example/with_plugin/danger_plugin_example/ +@@ -50,18 +66,3 @@ jobs: + run: danger_dart local + working-directory: example/with_plugin/ + +- - name: Install app dependencies (Dart 2) +- run: dart pub get +- working-directory: example/dart2/ +- +- - name: Run danger local (Dart 2) +- run: danger_dart local +- working-directory: example/dart2/ +- +- - name: Install app dependencies (Dart 3) +- run: dart pub get +- working-directory: example/dart3/ +- +- - name: Run danger local (Dart 3) +- run: danger_dart local +- working-directory: example/dart3/ +diff --git a/example/dart3/dangerfile.dart b/example/pre_nullsafety/dangerfile.dart +similarity index 61% +rename from example/dart3/dangerfile.dart +rename to example/pre_nullsafety/dangerfile.dart +index fb4ec51..5cf13f1 100644 +--- a/example/dart3/dangerfile.dart ++++ b/example/pre_nullsafety/dangerfile.dart +@@ -1,5 +1,5 @@ + import 'package:danger_core/danger_core.dart'; + + void main() { +- message('hello from Dart 3'); ++ message('hello from pre-nullsafety'); + } +diff --git a/example/dart3/pubspec.yaml b/example/pre_nullsafety/pubspec.yaml +similarity index 64% +rename from example/dart3/pubspec.yaml +rename to example/pre_nullsafety/pubspec.yaml +index 8aca558..9c93b1b 100644 +--- a/example/dart3/pubspec.yaml ++++ b/example/pre_nullsafety/pubspec.yaml +@@ -1,13 +1,16 @@ +-name: danger_test_target_null_safety ++name: danger_test_pre_nullsafety + description: A simple command-line application. + # version: 1.0.0 + # homepage: https://www.example.com + + environment: +- sdk: ">=3.0.0 <4.0.0" ++ sdk: ">=2.7.0 <3.0.0" ++ ++dependencies: ++ json_annotation: ^3.0.0 + + dev_dependencies: + lints: ^1.0.1 +- path: ^1.8.3 ++ path: ^1.8.0 + danger_core: + path: ../../packages/danger_core +diff --git a/example/dart2/dangerfile.dart b/example/target_nullsafety/dangerfile.dart +similarity index 100% +rename from example/dart2/dangerfile.dart +rename to example/target_nullsafety/dangerfile.dart +diff --git a/example/dart2/pubspec.yaml b/example/target_nullsafety/pubspec.yaml +similarity index 75% +rename from example/dart2/pubspec.yaml +rename to example/target_nullsafety/pubspec.yaml +index 58e68a9..2971b33 100644 +--- a/example/dart2/pubspec.yaml ++++ b/example/target_nullsafety/pubspec.yaml +@@ -1,4 +1,4 @@ +-name: dart2 ++name: danger_test_target_null_safety + description: A simple command-line application. + # version: 1.0.0 + # homepage: https://www.example.com +@@ -6,6 +6,9 @@ description: A simple command-line application. + environment: + sdk: ">=2.12.0 <3.0.0" + ++dependencies: ++ json_annotation: ^3.0.0 ++ + dev_dependencies: + lints: ^1.0.1 + path: ^1.8.0 +diff --git a/example/with_plugin/pubspec.yaml b/example/with_plugin/pubspec.yaml +index 7980d4b..8fb2b3d 100644 +--- a/example/with_plugin/pubspec.yaml ++++ b/example/with_plugin/pubspec.yaml +@@ -6,6 +6,9 @@ description: A simple command-line application. + environment: + sdk: ">=2.12.0 <3.0.0" + ++dependencies: ++ json_annotation: ^3.0.0 ++ + dev_dependencies: + lints: ^1.0.1 + path: ^1.8.0 +diff --git a/packages/danger_core/CHANGELOG.md b/packages/danger_core/CHANGELOG.md +index 2470454..6a86da3 100644 +--- a/packages/danger_core/CHANGELOG.md ++++ b/packages/danger_core/CHANGELOG.md +@@ -1,7 +1,3 @@ +-## 2.0.0 +- +-- Migrate to Dart 3 +- + ## 1.0.1 + + - Fixed GitLabDSL +diff --git a/packages/danger_core/pubspec.yaml b/packages/danger_core/pubspec.yaml +index 4ba9480..c487bd1 100644 +--- a/packages/danger_core/pubspec.yaml ++++ b/packages/danger_core/pubspec.yaml +@@ -1,17 +1,17 @@ + name: danger_core + description: Core of Danger Dart, tool to help you reviewing the code. +-version: 2.0.0 ++version: 1.0.1 + homepage: https://github.com/danger/dart + + environment: +- sdk: ">=3.0.0 <4.0.0" ++ sdk: ">=2.12.0 <3.0.0" + + dev_dependencies: +- lints: ^2.1.1 +- json_annotation: ^4.8.1 +- test: ^1.24.7 +- json_serializable: ^6.7.1 +- build_runner: ^2.4.6 +- mockito: ^5.4.2 ++ lints: ^1.0.1 ++ json_annotation: ^4.4.0 ++ test: ^1.20.2 ++ json_serializable: ^6.1.5 ++ build_runner: ^2.1.8 ++ mockito: ^5.1.0 + isolate: ^2.1.1 +- path: ^1.8.3 ++ path: ^1.8.0 +diff --git a/packages/danger_dart/CHANGELOG.md b/packages/danger_dart/CHANGELOG.md +index 10911ab..33d4244 100644 +--- a/packages/danger_dart/CHANGELOG.md ++++ b/packages/danger_dart/CHANGELOG.md +@@ -1,7 +1,3 @@ +-## 2.0.0 +- +-- Migrate to Dart 3 +- + ## 1.0.1 + + - Add argument `--failOnErrors` on every command +diff --git a/packages/danger_dart/lib/danger_util.dart b/packages/danger_dart/lib/danger_util.dart +index a6f1981..6332c40 100644 +--- a/packages/danger_dart/lib/danger_util.dart ++++ b/packages/danger_dart/lib/danger_util.dart +@@ -126,6 +126,7 @@ class DangerUtil { + } + tempFile.createSync(); + tempFile.writeAsStringSync(''' ++// @dart=2.7 + import 'dart:developer'; + + import 'package:danger_core/danger_core.dart'; +diff --git a/packages/danger_dart/pubspec.yaml b/packages/danger_dart/pubspec.yaml +index d1f5e3f..254c34d 100644 +--- a/packages/danger_dart/pubspec.yaml ++++ b/packages/danger_dart/pubspec.yaml +@@ -7,20 +7,20 @@ executables: + danger_dart: + + environment: +- sdk: ">=3.0.0 <4.0.0" ++ sdk: ">=2.12.0 <3.0.0" + + dependencies: +- meta: ^1.10.0 +- args: ^2.4.2 +- process_run: ^0.13.1 +- fimber: ^0.7.0 +- path: ^1.8.3 ++ meta: ^1.7.0 ++ args: ^2.3.0 ++ process_run: ^0.12.3+2 ++ fimber: ^0.6.5 ++ path: ^1.8.1 + # danger_core: ">= 1.0.0 < 2.0.0" + danger_core: + path: ../danger_core + + dev_dependencies: +- lints: ^2.1.1 +- test: ^1.24.7 +- mockito: ^5.4.2 +- build_runner: ^2.4.6 ++ lints: ^1.0.1 ++ test: ^1.20.2 ++ mockito: ^5.1.0 ++ build_runner: ^2.1.8 +diff --git a/packages/danger_plugin_dart_test/CHANGELOG.md b/packages/danger_plugin_dart_test/CHANGELOG.md +index 40d5f85..e2f6109 100644 +--- a/packages/danger_plugin_dart_test/CHANGELOG.md ++++ b/packages/danger_plugin_dart_test/CHANGELOG.md +@@ -1,7 +1,3 @@ +-## 2.0.0 +- +-- Migrate to Dart 3 +- + ## 1.0.0 + + - Supports Null Safety +diff --git a/packages/danger_plugin_dart_test/pubspec.yaml b/packages/danger_plugin_dart_test/pubspec.yaml +index 4721fff..9239b79 100644 +--- a/packages/danger_plugin_dart_test/pubspec.yaml ++++ b/packages/danger_plugin_dart_test/pubspec.yaml +@@ -1,17 +1,17 @@ + name: danger_plugin_dart_test + description: A Danger Dart plugin to process test result. +-version: 2.0.0 ++version: 1.0.0 + homepage: https://github.com/danger/dart + + environment: +- sdk: ">=3.0.0 <4.0.0" ++ sdk: ">=2.12.0 <3.0.0" + + dependencies: +- path: ^1.8.3 ++ path: ">= 1.8.0 < 2.0.0" + # danger_core: ">= 1.0.0 < 2.0.0" + danger_core: + path: ../danger_core + + dev_dependencies: +- lints: ^2.1.1 +- test: ^1.24.7 ++ lints: ^1.0.1 ++ test: ^1.20.2 +diff --git a/packages/danger_plugin_golden_reporter/CHANGELOG.md b/packages/danger_plugin_golden_reporter/CHANGELOG.md +index fd53903..6fdf70e 100644 +--- a/packages/danger_plugin_golden_reporter/CHANGELOG.md ++++ b/packages/danger_plugin_golden_reporter/CHANGELOG.md +@@ -1,7 +1,3 @@ +-## 2.0.0 +- +-- Migrate to Dart 3 +- + ## 1.0.0 + + - Supports Null Safety +diff --git a/packages/danger_plugin_golden_reporter/pubspec.yaml b/packages/danger_plugin_golden_reporter/pubspec.yaml +index eca2e35..a0d38bd 100644 +--- a/packages/danger_plugin_golden_reporter/pubspec.yaml ++++ b/packages/danger_plugin_golden_reporter/pubspec.yaml +@@ -1,10 +1,10 @@ + name: danger_plugin_golden_reporter + description: A Danger Dart plugin to display golden images on pull requests. +-version: 2.0.0 ++version: 1.0.0 + homepage: https://github.com/danger/dart + + environment: +- sdk: ">=3.0.0 <4.0.0" ++ sdk: ">=2.12.0 <3.0.0" + + dependencies: + # danger_core: ">= 1.0.0 < 2.0.0" +@@ -12,7 +12,7 @@ dependencies: + path: ../danger_core + + dev_dependencies: +- lints: ^2.1.1 ++ lints: ^1.0.1 + test: ^1.20.2 + mockito: ^5.1.0 + build_runner: ^2.1.8 +diff --git a/pubspec.yaml b/pubspec.yaml +index c7730a5..6a91c44 100644 +--- a/pubspec.yaml ++++ b/pubspec.yaml +@@ -4,11 +4,11 @@ description: A simple command-line application. + # homepage: https://www.example.com + + environment: +- sdk: ">=3.0.0 <4.0.0" ++ sdk: ">=2.12.0 <3.0.0" + + dev_dependencies: +- lints: ^2.1.1 +- path: ^1.8.3 ++ lints: ^1.0.1 ++ path: ^1.8.0 + danger_core: + path: packages/danger_core + danger_plugin_dart_test: \ No newline at end of file diff --git a/packages/danger_core/test/fixtures/diff/remove_only.diff b/packages/danger_core/test/fixtures/diff/remove_only.diff new file mode 100644 index 0000000..bf7b19e --- /dev/null +++ b/packages/danger_core/test/fixtures/diff/remove_only.diff @@ -0,0 +1,23 @@ +diff --git a/.github/workflows/verify_compatibility.yaml b/.github/workflows/verify_compatibility.yaml +index 507cacb..701dd94 100644 +--- a/.github/workflows/verify_compatibility.yaml ++++ b/.github/workflows/verify_compatibility.yaml +@@ -50,18 +66,3 @@ jobs: + run: danger_dart local + working-directory: example/with_plugin/ + +- - name: Install app dependencies (Dart 2) +- run: dart pub get +- working-directory: example/dart2/ +- +- - name: Run danger local (Dart 2) +- run: danger_dart local +- working-directory: example/dart2/ +- +- - name: Install app dependencies (Dart 3) +- run: dart pub get +- working-directory: example/dart3/ +- +- - name: Run danger local (Dart 3) +- run: danger_dart local +- working-directory: example/dart3/ \ No newline at end of file diff --git a/packages/danger_core/test/fixtures/diff/rename_only.diff b/packages/danger_core/test/fixtures/diff/rename_only.diff new file mode 100644 index 0000000..fcb72db --- /dev/null +++ b/packages/danger_core/test/fixtures/diff/rename_only.diff @@ -0,0 +1,4 @@ +diff --git a/example/dart2/dangerfile.dart b/example/target_nullsafety/dangerfile.dart +similarity index 100% +rename from example/dart2/dangerfile.dart +rename to example/target_nullsafety/dangerfile.dart \ No newline at end of file diff --git a/packages/danger_core/test/parser/git_diff_test.dart b/packages/danger_core/test/parser/git_diff_test.dart new file mode 100644 index 0000000..f31d3a3 --- /dev/null +++ b/packages/danger_core/test/parser/git_diff_test.dart @@ -0,0 +1,119 @@ +import 'dart:io'; + +import 'package:danger_core/src/utils/git_diff_parser.dart'; +import 'package:test/test.dart'; +import 'package:path/path.dart' show join, current; + +void main() { + group('git_diff_parser', () { + test('should be able to parse add_only correctly', () { + final fixtureFile = + File(join(current, 'test', 'fixtures', 'diff', 'add_only.diff')); + final str = fixtureFile.readAsStringSync(); + final data = GitDiffParser.parse(str); + expect(data.length, 1); + expect( + data.first.fromFile, '.github/workflows/verify_compatibility.yaml'); + expect(data.first.toFile, '.github/workflows/verify_compatibility.yaml'); + expect(data.first.diffBlocks.length, 1); + expect(data.first.diffBlocks.first.addedLines.length, 16); + expect(data.first.diffBlocks.first.removedLines, isEmpty); + expect(data.first.diffBlocks.first.unchangedLines.length, 6); + expect(data.first.diffBlocks.first.fromStart, 38); + expect(data.first.diffBlocks.first.fromEnd, 43); + expect(data.first.diffBlocks.first.toStart, 38); + expect(data.first.diffBlocks.first.toEnd, 59); + }); + + test('should be able to parse remove_only correctly', () { + final fixtureFile = + File(join(current, 'test', 'fixtures', 'diff', 'remove_only.diff')); + final str = fixtureFile.readAsStringSync(); + final data = GitDiffParser.parse(str); + expect(data.length, 1); + expect( + data.first.fromFile, '.github/workflows/verify_compatibility.yaml'); + expect(data.first.toFile, '.github/workflows/verify_compatibility.yaml'); + expect(data.first.diffBlocks.length, 1); + expect(data.first.diffBlocks.first.addedLines, isEmpty); + expect(data.first.diffBlocks.first.removedLines.length, 15); + expect(data.first.diffBlocks.first.unchangedLines.length, 3); + expect(data.first.diffBlocks.first.fromStart, 50); + expect(data.first.diffBlocks.first.fromEnd, 67); + expect(data.first.diffBlocks.first.toStart, 66); + expect(data.first.diffBlocks.first.toEnd, 68); + }); + + test('should be able to parse rename_only correctly', () { + final fixtureFile = + File(join(current, 'test', 'fixtures', 'diff', 'rename_only.diff')); + final str = fixtureFile.readAsStringSync(); + final data = GitDiffParser.parse(str); + expect(data.length, 1); + expect(data.first.fromFile, 'example/dart2/dangerfile.dart'); + expect(data.first.toFile, 'example/target_nullsafety/dangerfile.dart'); + expect(data.first.diffBlocks.length, 0); + }); + + test('should be able to parse mix correctly', () { + final fixtureFile = + File(join(current, 'test', 'fixtures', 'diff', 'mix.diff')); + final str = fixtureFile.readAsStringSync(); + final data = GitDiffParser.parse(str); + expect(data.length, 1); + expect(data.first.fromFile, 'example/dart3/pubspec.yaml'); + expect(data.first.toFile, 'example/pre_nullsafety/pubspec.yaml'); + expect(data.first.diffBlocks.length, 1); + expect(data.first.diffBlocks.first.addedLines.length, 6); + expect(data.first.diffBlocks.first.removedLines.length, 3); + expect(data.first.diffBlocks.first.unchangedLines.length, 10); + expect(data.first.diffBlocks.first.fromStart, 1); + expect(data.first.diffBlocks.first.fromEnd, 13); + expect(data.first.diffBlocks.first.toStart, 1); + expect(data.first.diffBlocks.first.toEnd, 16); + }); + + test('should be able to parse multiple_chunk correctly', () { + final fixtureFile = File( + join(current, 'test', 'fixtures', 'diff', 'multiple_chunk.diff')); + final str = fixtureFile.readAsStringSync(); + final data = GitDiffParser.parse(str); + expect(data.length, 1); + expect(data.first.fromFile, + 'packages/danger_plugin_golden_reporter/pubspec.yaml'); + expect(data.first.toFile, + 'packages/danger_plugin_golden_reporter/pubspec.yaml'); + expect(data.first.diffBlocks.length, 2); + + expect(data.first.diffBlocks.first.addedLines.length, 2); + expect(data.first.diffBlocks.first.removedLines.length, 2); + expect(data.first.diffBlocks.first.unchangedLines.length, 8); + expect(data.first.diffBlocks.first.fromStart, 1); + expect(data.first.diffBlocks.first.fromEnd, 10); + expect(data.first.diffBlocks.first.toStart, 1); + expect(data.first.diffBlocks.first.toEnd, 10); + + expect(data.first.diffBlocks[1].addedLines.length, 1); + expect(data.first.diffBlocks[1].removedLines.length, 1); + expect(data.first.diffBlocks[1].unchangedLines.length, 6); + expect(data.first.diffBlocks[1].fromStart, 12); + expect(data.first.diffBlocks[1].fromEnd, 18); + expect(data.first.diffBlocks[1].toStart, 12); + expect(data.first.diffBlocks[1].toEnd, 18); + }); + + test('should be able to parse multiple_files', () { + final fixtureFile = File( + join(current, 'test', 'fixtures', 'diff', 'multiple_files.diff')); + final str = fixtureFile.readAsStringSync(); + final data = GitDiffParser.parse(str); + expect(data.length, 16); + expect( + data.first.fromFile, '.github/workflows/verify_compatibility.yaml'); + expect(data.first.toFile, '.github/workflows/verify_compatibility.yaml'); + + expect(data.last.fromFile, 'pubspec.yaml'); + expect(data.last.toFile, 'pubspec.yaml'); + }); + }); +} diff --git a/tools/diff_parser.dart b/tools/diff_parser.dart new file mode 100644 index 0000000..a27179a --- /dev/null +++ b/tools/diff_parser.dart @@ -0,0 +1,418 @@ +class GitDiff { + late String fromFile; + late String toFile; + List diffBlocks = []; + + @override + String toString() { + String diffString = 'Diff between $fromFile and $toFile:\n'; + for (var block in diffBlocks) { + diffString += '$block\n'; + } + return diffString; + } +} + +class DiffBlock { + late int fromStart; + late int fromEnd; + late int toStart; + late int toEnd; + List addedLines = []; + List removedLines = []; + List unchangedLines = []; + + @override + String toString() { + return 'DiffBlock: Lines $fromStart-$fromEnd in fromFile, Lines $toStart-$toEnd in toFile\n' + 'Added Lines: $addedLines\n' + 'Removed Lines: $removedLines\n' + 'Unchanged Lines: $unchangedLines\n'; + } +} + +class GitDiffParser { + List parse(String diffData) { + List diffs = []; + List lines = diffData.split('\n'); + GitDiff? currentDiff; + DiffBlock? currentBlock; + + for (var line in lines) { + if (line.startsWith('diff --git')) { + currentDiff = GitDiff(); + currentDiff.fromFile = line.split(' ')[2]; + currentDiff.toFile = line.split(' ')[3]; + diffs.add(currentDiff); + continue; + } + + if (line.startsWith('@@')) { + currentBlock = DiffBlock(); + currentDiff?.diffBlocks.add(currentBlock); + + String header = line.substring(3); + List parts = header.split(' '); + List fromRange = parts[0].split(','); + List toRange = parts[1].split(','); + + currentBlock.fromStart = int.parse(fromRange[0].substring(1)); + currentBlock.fromEnd = currentBlock.fromStart + + (fromRange.length > 1 ? int.parse(fromRange[1]) : 1) - + 1; + currentBlock.toStart = int.parse(toRange[0].substring(1)); + currentBlock.toEnd = currentBlock.toStart + + (toRange.length > 1 ? int.parse(toRange[1]) : 1) - + 1; + } + + if (line.startsWith('+')) { + currentBlock?.addedLines.add(line.substring(1)); + } else if (line.startsWith('-')) { + currentBlock?.removedLines.add(line.substring(1)); + } else if (line.startsWith(' ')) { + currentBlock?.unchangedLines.add(line.substring(1)); + } + } + + return diffs; + } +} + +void main() { + String diffData = """ +diff --git a/.github/workflows/verify_compatibility.yaml b/.github/workflows/verify_compatibility.yaml +index 507cacb..701dd94 100644 +--- a/.github/workflows/verify_compatibility.yaml ++++ b/.github/workflows/verify_compatibility.yaml +@@ -38,6 +38,22 @@ jobs: + run: dart pub get + working-directory: packages/danger_dart/ + ++ - name: Install app (non nullsafety) dependencies ++ run: dart pub get ++ working-directory: example/pre_nullsafety/ ++ ++ - name: Run danger local (non nullsafety) ++ run: danger_dart local ++ working-directory: example/pre_nullsafety/ ++ ++ - name: Install app dependencies (nullsafety) ++ run: dart pub get ++ working-directory: example/target_nullsafety/ ++ ++ - name: Run danger local (nullsafety) ++ run: danger_dart local ++ working-directory: example/target_nullsafety/ ++ + - name: Install plugin dependencies + run: dart pub get + working-directory: example/with_plugin/danger_plugin_example/ +@@ -50,18 +66,3 @@ jobs: + run: danger_dart local + working-directory: example/with_plugin/ + +- - name: Install app dependencies (Dart 2) +- run: dart pub get +- working-directory: example/dart2/ +- +- - name: Run danger local (Dart 2) +- run: danger_dart local +- working-directory: example/dart2/ +- +- - name: Install app dependencies (Dart 3) +- run: dart pub get +- working-directory: example/dart3/ +- +- - name: Run danger local (Dart 3) +- run: danger_dart local +- working-directory: example/dart3/ +diff --git a/example/dart3/dangerfile.dart b/example/pre_nullsafety/dangerfile.dart +similarity index 61% +rename from example/dart3/dangerfile.dart +rename to example/pre_nullsafety/dangerfile.dart +index fb4ec51..5cf13f1 100644 +--- a/example/dart3/dangerfile.dart ++++ b/example/pre_nullsafety/dangerfile.dart +@@ -1,5 +1,5 @@ + import 'package:danger_core/danger_core.dart'; + + void main() { +- message('hello from Dart 3'); ++ message('hello from pre-nullsafety'); + } +diff --git a/example/dart3/pubspec.yaml b/example/pre_nullsafety/pubspec.yaml +similarity index 64% +rename from example/dart3/pubspec.yaml +rename to example/pre_nullsafety/pubspec.yaml +index 8aca558..9c93b1b 100644 +--- a/example/dart3/pubspec.yaml ++++ b/example/pre_nullsafety/pubspec.yaml +@@ -1,13 +1,16 @@ +-name: danger_test_target_null_safety ++name: danger_test_pre_nullsafety + description: A simple command-line application. + # version: 1.0.0 + # homepage: https://www.example.com + + environment: +- sdk: ">=3.0.0 <4.0.0" ++ sdk: ">=2.7.0 <3.0.0" ++ ++dependencies: ++ json_annotation: ^3.0.0 + + dev_dependencies: + lints: ^1.0.1 +- path: ^1.8.3 ++ path: ^1.8.0 + danger_core: + path: ../../packages/danger_core +diff --git a/example/dart2/dangerfile.dart b/example/target_nullsafety/dangerfile.dart +similarity index 100% +rename from example/dart2/dangerfile.dart +rename to example/target_nullsafety/dangerfile.dart +diff --git a/example/dart2/pubspec.yaml b/example/target_nullsafety/pubspec.yaml +similarity index 75% +rename from example/dart2/pubspec.yaml +rename to example/target_nullsafety/pubspec.yaml +index 58e68a9..2971b33 100644 +--- a/example/dart2/pubspec.yaml ++++ b/example/target_nullsafety/pubspec.yaml +@@ -1,4 +1,4 @@ +-name: dart2 ++name: danger_test_target_null_safety + description: A simple command-line application. + # version: 1.0.0 + # homepage: https://www.example.com +@@ -6,6 +6,9 @@ description: A simple command-line application. + environment: + sdk: ">=2.12.0 <3.0.0" + ++dependencies: ++ json_annotation: ^3.0.0 ++ + dev_dependencies: + lints: ^1.0.1 + path: ^1.8.0 +diff --git a/example/with_plugin/pubspec.yaml b/example/with_plugin/pubspec.yaml +index 7980d4b..8fb2b3d 100644 +--- a/example/with_plugin/pubspec.yaml ++++ b/example/with_plugin/pubspec.yaml +@@ -6,6 +6,9 @@ description: A simple command-line application. + environment: + sdk: ">=2.12.0 <3.0.0" + ++dependencies: ++ json_annotation: ^3.0.0 ++ + dev_dependencies: + lints: ^1.0.1 + path: ^1.8.0 +diff --git a/packages/danger_core/CHANGELOG.md b/packages/danger_core/CHANGELOG.md +index 2470454..6a86da3 100644 +--- a/packages/danger_core/CHANGELOG.md ++++ b/packages/danger_core/CHANGELOG.md +@@ -1,7 +1,3 @@ +-## 2.0.0 +- +-- Migrate to Dart 3 +- + ## 1.0.1 + + - Fixed GitLabDSL +diff --git a/packages/danger_core/pubspec.yaml b/packages/danger_core/pubspec.yaml +index 4ba9480..c487bd1 100644 +--- a/packages/danger_core/pubspec.yaml ++++ b/packages/danger_core/pubspec.yaml +@@ -1,17 +1,17 @@ + name: danger_core + description: Core of Danger Dart, tool to help you reviewing the code. +-version: 2.0.0 ++version: 1.0.1 + homepage: https://github.com/danger/dart + + environment: +- sdk: ">=3.0.0 <4.0.0" ++ sdk: ">=2.12.0 <3.0.0" + + dev_dependencies: +- lints: ^2.1.1 +- json_annotation: ^4.8.1 +- test: ^1.24.7 +- json_serializable: ^6.7.1 +- build_runner: ^2.4.6 +- mockito: ^5.4.2 ++ lints: ^1.0.1 ++ json_annotation: ^4.4.0 ++ test: ^1.20.2 ++ json_serializable: ^6.1.5 ++ build_runner: ^2.1.8 ++ mockito: ^5.1.0 + isolate: ^2.1.1 +- path: ^1.8.3 ++ path: ^1.8.0 +diff --git a/packages/danger_dart/CHANGELOG.md b/packages/danger_dart/CHANGELOG.md +index 10911ab..33d4244 100644 +--- a/packages/danger_dart/CHANGELOG.md ++++ b/packages/danger_dart/CHANGELOG.md +@@ -1,7 +1,3 @@ +-## 2.0.0 +- +-- Migrate to Dart 3 +- + ## 1.0.1 + + - Add argument `--failOnErrors` on every command +diff --git a/packages/danger_dart/lib/danger_util.dart b/packages/danger_dart/lib/danger_util.dart +index a6f1981..6332c40 100644 +--- a/packages/danger_dart/lib/danger_util.dart ++++ b/packages/danger_dart/lib/danger_util.dart +@@ -126,6 +126,7 @@ class DangerUtil { + } + tempFile.createSync(); + tempFile.writeAsStringSync(''' ++// @dart=2.7 + import 'dart:developer'; + + import 'package:danger_core/danger_core.dart'; +diff --git a/packages/danger_dart/pubspec.yaml b/packages/danger_dart/pubspec.yaml +index d1f5e3f..254c34d 100644 +--- a/packages/danger_dart/pubspec.yaml ++++ b/packages/danger_dart/pubspec.yaml +@@ -7,20 +7,20 @@ executables: + danger_dart: + + environment: +- sdk: ">=3.0.0 <4.0.0" ++ sdk: ">=2.12.0 <3.0.0" + + dependencies: +- meta: ^1.10.0 +- args: ^2.4.2 +- process_run: ^0.13.1 +- fimber: ^0.7.0 +- path: ^1.8.3 ++ meta: ^1.7.0 ++ args: ^2.3.0 ++ process_run: ^0.12.3+2 ++ fimber: ^0.6.5 ++ path: ^1.8.1 + # danger_core: ">= 1.0.0 < 2.0.0" + danger_core: + path: ../danger_core + + dev_dependencies: +- lints: ^2.1.1 +- test: ^1.24.7 +- mockito: ^5.4.2 +- build_runner: ^2.4.6 ++ lints: ^1.0.1 ++ test: ^1.20.2 ++ mockito: ^5.1.0 ++ build_runner: ^2.1.8 +diff --git a/packages/danger_plugin_dart_test/CHANGELOG.md b/packages/danger_plugin_dart_test/CHANGELOG.md +index 40d5f85..e2f6109 100644 +--- a/packages/danger_plugin_dart_test/CHANGELOG.md ++++ b/packages/danger_plugin_dart_test/CHANGELOG.md +@@ -1,7 +1,3 @@ +-## 2.0.0 +- +-- Migrate to Dart 3 +- + ## 1.0.0 + + - Supports Null Safety +diff --git a/packages/danger_plugin_dart_test/pubspec.yaml b/packages/danger_plugin_dart_test/pubspec.yaml +index 4721fff..9239b79 100644 +--- a/packages/danger_plugin_dart_test/pubspec.yaml ++++ b/packages/danger_plugin_dart_test/pubspec.yaml +@@ -1,17 +1,17 @@ + name: danger_plugin_dart_test + description: A Danger Dart plugin to process test result. +-version: 2.0.0 ++version: 1.0.0 + homepage: https://github.com/danger/dart + + environment: +- sdk: ">=3.0.0 <4.0.0" ++ sdk: ">=2.12.0 <3.0.0" + + dependencies: +- path: ^1.8.3 ++ path: ">= 1.8.0 < 2.0.0" + # danger_core: ">= 1.0.0 < 2.0.0" + danger_core: + path: ../danger_core + + dev_dependencies: +- lints: ^2.1.1 +- test: ^1.24.7 ++ lints: ^1.0.1 ++ test: ^1.20.2 +diff --git a/packages/danger_plugin_golden_reporter/CHANGELOG.md b/packages/danger_plugin_golden_reporter/CHANGELOG.md +index fd53903..6fdf70e 100644 +--- a/packages/danger_plugin_golden_reporter/CHANGELOG.md ++++ b/packages/danger_plugin_golden_reporter/CHANGELOG.md +@@ -1,7 +1,3 @@ +-## 2.0.0 +- +-- Migrate to Dart 3 +- + ## 1.0.0 + + - Supports Null Safety +diff --git a/packages/danger_plugin_golden_reporter/pubspec.yaml b/packages/danger_plugin_golden_reporter/pubspec.yaml +index eca2e35..a0d38bd 100644 +--- a/packages/danger_plugin_golden_reporter/pubspec.yaml ++++ b/packages/danger_plugin_golden_reporter/pubspec.yaml +@@ -1,10 +1,10 @@ + name: danger_plugin_golden_reporter + description: A Danger Dart plugin to display golden images on pull requests. +-version: 2.0.0 ++version: 1.0.0 + homepage: https://github.com/danger/dart + + environment: +- sdk: ">=3.0.0 <4.0.0" ++ sdk: ">=2.12.0 <3.0.0" + + dependencies: + # danger_core: ">= 1.0.0 < 2.0.0" +@@ -12,7 +12,7 @@ dependencies: + path: ../danger_core + + dev_dependencies: +- lints: ^2.1.1 ++ lints: ^1.0.1 + test: ^1.20.2 + mockito: ^5.1.0 + build_runner: ^2.1.8 +diff --git a/pubspec.yaml b/pubspec.yaml +index c7730a5..6a91c44 100644 +--- a/pubspec.yaml ++++ b/pubspec.yaml +@@ -4,11 +4,11 @@ description: A simple command-line application. + # homepage: https://www.example.com + + environment: +- sdk: ">=3.0.0 <4.0.0" ++ sdk: ">=2.12.0 <3.0.0" + + dev_dependencies: +- lints: ^2.1.1 +- path: ^1.8.3 ++ lints: ^1.0.1 ++ path: ^1.8.0 + danger_core: + path: packages/danger_core + danger_plugin_dart_test: +"""; + + GitDiffParser parser = GitDiffParser(); + List diffs = parser.parse(diffData); + + for (var diff in diffs) { + print(diff); + } +} From 7021041ae4dbe4a400f9479556572ecd4453e533 Mon Sep 17 00:00:00 2001 From: HelloCore Date: Tue, 3 Oct 2023 21:36:57 +0700 Subject: [PATCH 02/11] chore: update CHANGELOG --- packages/danger_core/CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/danger_core/CHANGELOG.md b/packages/danger_core/CHANGELOG.md index 2470454..09c8d6d 100644 --- a/packages/danger_core/CHANGELOG.md +++ b/packages/danger_core/CHANGELOG.md @@ -1,5 +1,6 @@ ## 2.0.0 +- Provides API to get GitDiff (DangerUtils.getFullDiff) - Migrate to Dart 3 ## 1.0.1 From bebbaa1607e8f6f93adc09477d0a18e3eb57e673 Mon Sep 17 00:00:00 2001 From: HelloCore Date: Tue, 3 Oct 2023 21:48:22 +0700 Subject: [PATCH 03/11] chore: try print log --- .vscode/launch.json | 8 +++++++- packages/danger_core/lib/src/danger_utils.dart | 5 +++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index e63f471..25dc282 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -14,6 +14,12 @@ "name": "Dart: Attach to Process", "type": "dart", "request": "attach" + }, + { + "name": "Danger Attach", + "type": "dart", + "request": "attach", + "program": "tool/dangerfile.dart" } ] -} +} \ No newline at end of file diff --git a/packages/danger_core/lib/src/danger_utils.dart b/packages/danger_core/lib/src/danger_utils.dart index b249f4a..1ccdace 100644 --- a/packages/danger_core/lib/src/danger_utils.dart +++ b/packages/danger_core/lib/src/danger_utils.dart @@ -31,8 +31,13 @@ class DangerUtils { throw 'Cannot find base branch'; } + print('BASE is [$base]'); + final data = await DangerUtils.spawn('git', arguments: ['diff', 'HEAD', base]); + print('DATA ====='); + print(data); + print('DATA ====='); return GitDiffParser.parse(data); } } From 24851cccf4d2267486420606c62e3c7a4754dfe3 Mon Sep 17 00:00:00 2001 From: HelloCore Date: Tue, 3 Oct 2023 21:54:59 +0700 Subject: [PATCH 04/11] fix: add fetch-depth to get GIT history --- .github/workflows/pr_flow.yaml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/pr_flow.yaml b/.github/workflows/pr_flow.yaml index 2f24fff..f36a976 100644 --- a/.github/workflows/pr_flow.yaml +++ b/.github/workflows/pr_flow.yaml @@ -13,6 +13,8 @@ jobs: steps: - uses: actions/checkout@v2 + with: + fetch-depth: 2 - uses: actions/setup-node@v1 From 148846a54bd7d54c727296cc331f024962170c40 Mon Sep 17 00:00:00 2001 From: HelloCore Date: Tue, 3 Oct 2023 22:02:17 +0700 Subject: [PATCH 05/11] fix: try origin/ target branch --- .github/workflows/pr_flow.yaml | 2 -- dangerfile.dart | 3 ++- .../danger_core/lib/src/danger_utils.dart | 27 +++++++++++-------- 3 files changed, 18 insertions(+), 14 deletions(-) diff --git a/.github/workflows/pr_flow.yaml b/.github/workflows/pr_flow.yaml index f36a976..2f24fff 100644 --- a/.github/workflows/pr_flow.yaml +++ b/.github/workflows/pr_flow.yaml @@ -13,8 +13,6 @@ jobs: steps: - uses: actions/checkout@v2 - with: - fetch-depth: 2 - uses: actions/setup-node@v1 diff --git a/dangerfile.dart b/dangerfile.dart index a474d84..3fb5334 100644 --- a/dangerfile.dart +++ b/dangerfile.dart @@ -5,7 +5,8 @@ import 'package:danger_core/danger_core.dart'; import 'package:danger_plugin_dart_test/danger_plugin_dart_test.dart'; void main() async { - final fullDiff = await DangerUtils.getFullDiff(); + final fullDiff = await DangerUtils.getFullDiff( + target: 'origin/${DangerUtils.getTargetBranch()}'); message('There are ${fullDiff.length} changed files'); if (danger.isGitHub) { diff --git a/packages/danger_core/lib/src/danger_utils.dart b/packages/danger_core/lib/src/danger_utils.dart index 1ccdace..36a7ab6 100644 --- a/packages/danger_core/lib/src/danger_utils.dart +++ b/packages/danger_core/lib/src/danger_utils.dart @@ -14,30 +14,35 @@ class DangerUtils { return result.stdout.toString().trim(); } - static Future> getFullDiff() async { - var base = ''; + static String getTargetBranch() { + var target = ''; if (danger.isGitHub) { - base = danger.github.pr.base.ref; + target = danger.github.pr.base.ref; } else if (danger.isBitbucketCloud) { - base = danger.bitbucketCloud.pr.destination.branch.name; + target = danger.bitbucketCloud.pr.destination.branch.name; } else if (danger.isGitLab) { - base = danger.gitLab.mergeRequest.targetBranch; + target = danger.gitLab.mergeRequest.targetBranch; } else { - base = danger.settings.cliArgs?['base'] ?? ''; + target = danger.settings.cliArgs?['base'] ?? ''; } - if (base.isEmpty) { + if (target.isEmpty) { throw 'Cannot find base branch'; } - print('BASE is [$base]'); + return target; + } + + static Future> getFullDiff( + {String source = "HEAD", String? target}) async { + var base = target ?? ''; + if (base.isEmpty) { + base = getTargetBranch(); + } final data = await DangerUtils.spawn('git', arguments: ['diff', 'HEAD', base]); - print('DATA ====='); - print(data); - print('DATA ====='); return GitDiffParser.parse(data); } } From be431cac5291b786cc118396c6b28822a3db8eb8 Mon Sep 17 00:00:00 2001 From: HelloCore Date: Tue, 3 Oct 2023 22:15:57 +0700 Subject: [PATCH 06/11] feat: add more doc --- .github/workflows/pr_flow.yaml | 4 +++- dangerfile.dart | 3 +-- packages/danger_core/lib/src/danger_utils.dart | 16 ++++++++++++++-- 3 files changed, 18 insertions(+), 5 deletions(-) diff --git a/.github/workflows/pr_flow.yaml b/.github/workflows/pr_flow.yaml index 2f24fff..047225d 100644 --- a/.github/workflows/pr_flow.yaml +++ b/.github/workflows/pr_flow.yaml @@ -12,7 +12,9 @@ jobs: runs-on: macos-latest steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v4 + with: + fetch-depth: 0 - uses: actions/setup-node@v1 diff --git a/dangerfile.dart b/dangerfile.dart index 3fb5334..a474d84 100644 --- a/dangerfile.dart +++ b/dangerfile.dart @@ -5,8 +5,7 @@ import 'package:danger_core/danger_core.dart'; import 'package:danger_plugin_dart_test/danger_plugin_dart_test.dart'; void main() async { - final fullDiff = await DangerUtils.getFullDiff( - target: 'origin/${DangerUtils.getTargetBranch()}'); + final fullDiff = await DangerUtils.getFullDiff(); message('There are ${fullDiff.length} changed files'); if (danger.isGitHub) { diff --git a/packages/danger_core/lib/src/danger_utils.dart b/packages/danger_core/lib/src/danger_utils.dart index 36a7ab6..9760395 100644 --- a/packages/danger_core/lib/src/danger_utils.dart +++ b/packages/danger_core/lib/src/danger_utils.dart @@ -14,6 +14,7 @@ class DangerUtils { return result.stdout.toString().trim(); } + /// Get PR target branch based on git provider static String getTargetBranch() { var target = ''; @@ -34,9 +35,20 @@ class DangerUtils { return target; } + /// Get Git full diff. + /// The default targetBranch will be selected based on current environment. + /// + /// This function needs Git history on the machine. + /// + /// For GitHub Action: + /// ```yaml + /// - uses: actions/checkout@v4 + /// with: + /// fetch-depth: 0 + /// ``` static Future> getFullDiff( - {String source = "HEAD", String? target}) async { - var base = target ?? ''; + {String sourceBranch = "HEAD", String? targetBranch}) async { + var base = targetBranch ?? ''; if (base.isEmpty) { base = getTargetBranch(); } From 1f6cd3902f7fc49fcb4f0389d6d13616d19f35ed Mon Sep 17 00:00:00 2001 From: HelloCore Date: Tue, 3 Oct 2023 22:22:52 +0700 Subject: [PATCH 07/11] chore: try run in shell --- packages/danger_core/lib/src/danger_utils.dart | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/danger_core/lib/src/danger_utils.dart b/packages/danger_core/lib/src/danger_utils.dart index 9760395..4a88c27 100644 --- a/packages/danger_core/lib/src/danger_utils.dart +++ b/packages/danger_core/lib/src/danger_utils.dart @@ -9,7 +9,7 @@ class DangerUtils { static Future spawn(String command, {List arguments = const []}) async { - final result = await Process.run(command, arguments); + final result = await Process.run(command, arguments, runInShell: true); return result.stdout.toString().trim(); } @@ -55,6 +55,9 @@ class DangerUtils { final data = await DangerUtils.spawn('git', arguments: ['diff', 'HEAD', base]); + print("======="); + print(data); + print("======="); return GitDiffParser.parse(data); } } From d6c30c05cc9cb12322a4e5d4d1fe1de222efafd5 Mon Sep 17 00:00:00 2001 From: HelloCore Date: Tue, 3 Oct 2023 22:28:41 +0700 Subject: [PATCH 08/11] chore: try git fetch --- .github/workflows/pr_flow.yaml | 2 -- dangerfile.dart | 2 ++ .../danger_core/lib/src/danger_utils.dart | 20 ++++++++++++------- 3 files changed, 15 insertions(+), 9 deletions(-) diff --git a/.github/workflows/pr_flow.yaml b/.github/workflows/pr_flow.yaml index 047225d..9468771 100644 --- a/.github/workflows/pr_flow.yaml +++ b/.github/workflows/pr_flow.yaml @@ -13,8 +13,6 @@ jobs: steps: - uses: actions/checkout@v4 - with: - fetch-depth: 0 - uses: actions/setup-node@v1 diff --git a/dangerfile.dart b/dangerfile.dart index a474d84..c128420 100644 --- a/dangerfile.dart +++ b/dangerfile.dart @@ -5,7 +5,9 @@ import 'package:danger_core/danger_core.dart'; import 'package:danger_plugin_dart_test/danger_plugin_dart_test.dart'; void main() async { + await DangerUtils.gitFetchBranch(); final fullDiff = await DangerUtils.getFullDiff(); + message('There are ${fullDiff.length} changed files'); if (danger.isGitHub) { diff --git a/packages/danger_core/lib/src/danger_utils.dart b/packages/danger_core/lib/src/danger_utils.dart index 4a88c27..0ec4a6e 100644 --- a/packages/danger_core/lib/src/danger_utils.dart +++ b/packages/danger_core/lib/src/danger_utils.dart @@ -14,6 +14,19 @@ class DangerUtils { return result.stdout.toString().trim(); } + static Future gitFetchBranch({String? targetBranch}) async { + var base = targetBranch ?? ''; + if (base.isEmpty) { + base = getTargetBranch(); + } + + final data = + await DangerUtils.spawn('git', arguments: ['fetch', 'origin', base]); + print("======="); + print(data); + print("======="); + } + /// Get PR target branch based on git provider static String getTargetBranch() { var target = ''; @@ -39,13 +52,6 @@ class DangerUtils { /// The default targetBranch will be selected based on current environment. /// /// This function needs Git history on the machine. - /// - /// For GitHub Action: - /// ```yaml - /// - uses: actions/checkout@v4 - /// with: - /// fetch-depth: 0 - /// ``` static Future> getFullDiff( {String sourceBranch = "HEAD", String? targetBranch}) async { var base = targetBranch ?? ''; From 7f5bc1487783e5880a54071cdcd11d02df8e1fd3 Mon Sep 17 00:00:00 2001 From: HelloCore Date: Tue, 3 Oct 2023 22:35:22 +0700 Subject: [PATCH 09/11] chore: log process on GitHub Action --- packages/danger_core/lib/src/danger_utils.dart | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/packages/danger_core/lib/src/danger_utils.dart b/packages/danger_core/lib/src/danger_utils.dart index 0ec4a6e..0235817 100644 --- a/packages/danger_core/lib/src/danger_utils.dart +++ b/packages/danger_core/lib/src/danger_utils.dart @@ -10,7 +10,13 @@ class DangerUtils { static Future spawn(String command, {List arguments = const []}) async { final result = await Process.run(command, arguments, runInShell: true); - + print("======="); + print('Exit code ${result.exitCode}'); + print('stderr:'); + print(result.stderr); + print('stdout:'); + print(result.stdout); + print("======="); return result.stdout.toString().trim(); } @@ -22,9 +28,6 @@ class DangerUtils { final data = await DangerUtils.spawn('git', arguments: ['fetch', 'origin', base]); - print("======="); - print(data); - print("======="); } /// Get PR target branch based on git provider @@ -60,10 +63,7 @@ class DangerUtils { } final data = - await DangerUtils.spawn('git', arguments: ['diff', 'HEAD', base]); - print("======="); - print(data); - print("======="); + await DangerUtils.spawn('git', arguments: ['diff', base, 'HEAD']); return GitDiffParser.parse(data); } } From ca273d757d8713749f48341061ee26467c7aec04 Mon Sep 17 00:00:00 2001 From: HelloCore Date: Tue, 3 Oct 2023 22:41:46 +0700 Subject: [PATCH 10/11] chore: log process on GitHub Action --- dangerfile.dart | 3 ++- packages/danger_core/lib/src/danger_utils.dart | 3 +-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/dangerfile.dart b/dangerfile.dart index c128420..6f5ef37 100644 --- a/dangerfile.dart +++ b/dangerfile.dart @@ -6,7 +6,8 @@ import 'package:danger_plugin_dart_test/danger_plugin_dart_test.dart'; void main() async { await DangerUtils.gitFetchBranch(); - final fullDiff = await DangerUtils.getFullDiff(); + final fullDiff = await DangerUtils.getFullDiff( + targetBranch: 'origin/${DangerUtils.getTargetBranch()}'); message('There are ${fullDiff.length} changed files'); diff --git a/packages/danger_core/lib/src/danger_utils.dart b/packages/danger_core/lib/src/danger_utils.dart index 0235817..3f3e417 100644 --- a/packages/danger_core/lib/src/danger_utils.dart +++ b/packages/danger_core/lib/src/danger_utils.dart @@ -62,8 +62,7 @@ class DangerUtils { base = getTargetBranch(); } - final data = - await DangerUtils.spawn('git', arguments: ['diff', base, 'HEAD']); + final data = await DangerUtils.spawn('git', arguments: ['diff', base]); return GitDiffParser.parse(data); } } From 8df4084fac5a8e2062b835ba62f4d57625322af2 Mon Sep 17 00:00:00 2001 From: HelloCore Date: Tue, 3 Oct 2023 22:47:21 +0700 Subject: [PATCH 11/11] chore: remove log --- packages/danger_core/lib/src/danger_utils.dart | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/packages/danger_core/lib/src/danger_utils.dart b/packages/danger_core/lib/src/danger_utils.dart index 3f3e417..fa582d9 100644 --- a/packages/danger_core/lib/src/danger_utils.dart +++ b/packages/danger_core/lib/src/danger_utils.dart @@ -10,13 +10,6 @@ class DangerUtils { static Future spawn(String command, {List arguments = const []}) async { final result = await Process.run(command, arguments, runInShell: true); - print("======="); - print('Exit code ${result.exitCode}'); - print('stderr:'); - print(result.stderr); - print('stdout:'); - print(result.stdout); - print("======="); return result.stdout.toString().trim(); } @@ -26,8 +19,7 @@ class DangerUtils { base = getTargetBranch(); } - final data = - await DangerUtils.spawn('git', arguments: ['fetch', 'origin', base]); + await DangerUtils.spawn('git', arguments: ['fetch', 'origin', base]); } /// Get PR target branch based on git provider