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

feat: git diff #34

Merged
merged 16 commits into from
Oct 3, 2023
Merged
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 .github/workflows/pr_flow.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
runs-on: macos-latest

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4

- uses: actions/setup-node@v1

Expand Down
31 changes: 15 additions & 16 deletions .github/workflows/verify_compatibility.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,22 +38,6 @@ 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/
Expand All @@ -66,3 +50,18 @@ 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/
8 changes: 7 additions & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@
"name": "Dart: Attach to Process",
"type": "dart",
"request": "attach"
},
{
"name": "Danger Attach",
"type": "dart",
"request": "attach",
"program": "tool/dangerfile.dart"
}
]
}
}
8 changes: 7 additions & 1 deletion dangerfile.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@ 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 {
await DangerUtils.gitFetchBranch();
final fullDiff = await DangerUtils.getFullDiff(
targetBranch: 'origin/${DangerUtils.getTargetBranch()}');

message('There are ${fullDiff.length} changed files');

if (danger.isGitHub) {
if (danger.github.pr.title.contains('WIP') == true) {
warn('PR is considered WIP');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
name: danger_test_target_null_safety
name: dart2
description: A simple command-line application.
# version: 1.0.0
# homepage: https://www.example.com

environment:
sdk: ">=2.12.0 <3.0.0"

dependencies:
json_annotation: ^3.0.0

dev_dependencies:
lints: ^1.0.1
path: ^1.8.0
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import 'package:danger_core/danger_core.dart';

void main() {
message('hello from pre-nullsafety');
message('hello from Dart 3');
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
name: danger_test_pre_nullsafety
name: danger_test_target_null_safety
description: A simple command-line application.
# version: 1.0.0
# homepage: https://www.example.com

environment:
sdk: ">=2.7.0 <3.0.0"

dependencies:
json_annotation: ^3.0.0
sdk: ">=3.0.0 <4.0.0"

dev_dependencies:
lints: ^1.0.1
path: ^1.8.0
path: ^1.8.3
danger_core:
path: ../../packages/danger_core
3 changes: 0 additions & 3 deletions example/with_plugin/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@ 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
Expand Down
5 changes: 5 additions & 0 deletions packages/danger_core/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 2.0.0

- Provides API to get GitDiff (DangerUtils.getFullDiff)
- Migrate to Dart 3

## 1.0.1

- Fixed GitLabDSL
Expand Down
1 change: 1 addition & 0 deletions packages/danger_core/lib/danger_core.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
56 changes: 53 additions & 3 deletions packages/danger_core/lib/src/danger_utils.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,60 @@
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<String> spawn(String command,
{List<String> arguments = const []}) async {
final result = await Process.run(command, arguments);
DangerUtils._();

static Future<String> spawn(String command,
{List<String> arguments = const []}) async {
final result = await Process.run(command, arguments, runInShell: true);
return result.stdout.toString().trim();
}

static Future<void> gitFetchBranch({String? targetBranch}) async {
var base = targetBranch ?? '';
if (base.isEmpty) {
base = getTargetBranch();
}

await DangerUtils.spawn('git', arguments: ['fetch', 'origin', base]);
}

/// Get PR target branch based on git provider
static String getTargetBranch() {
var target = '';

if (danger.isGitHub) {
target = danger.github.pr.base.ref;
} else if (danger.isBitbucketCloud) {
target = danger.bitbucketCloud.pr.destination.branch.name;
} else if (danger.isGitLab) {
target = danger.gitLab.mergeRequest.targetBranch;
} else {
target = danger.settings.cliArgs?['base'] ?? '';
}

if (target.isEmpty) {
throw 'Cannot find base branch';
}

return target;
}

/// Get Git full diff.
/// The default targetBranch will be selected based on current environment.
///
/// This function needs Git history on the machine.
static Future<List<GitDiff>> getFullDiff(
{String sourceBranch = "HEAD", String? targetBranch}) async {
var base = targetBranch ?? '';
if (base.isEmpty) {
base = getTargetBranch();
}

final data = await DangerUtils.spawn('git', arguments: ['diff', base]);
return GitDiffParser.parse(data);
}
}
28 changes: 28 additions & 0 deletions packages/danger_core/lib/src/models/git_diff.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
class GitDiff {
final String fromFile;
final String toFile;
final List<DiffBlock> 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<String> addedLines;
final List<String> removedLines;
final List<String> unchangedLines;

DiffBlock(
{required this.fromStart,
required this.fromEnd,
required this.toStart,
required this.toEnd,
required this.addedLines,
required this.removedLines,
required this.unchangedLines});
}
58 changes: 58 additions & 0 deletions packages/danger_core/lib/src/utils/git_diff_parser.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import 'package:danger_core/src/models/git_diff.dart';

class GitDiffParser {
GitDiffParser._();

static List<GitDiff> parse(String diffData) {
List<GitDiff> diffs = [];
List<String> 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<String> parts = header.split(' ');
List<String> fromRange = parts[0].split(',');
List<String> 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;
}
}
18 changes: 9 additions & 9 deletions packages/danger_core/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
name: danger_core
description: Core of Danger Dart, tool to help you reviewing the code.
version: 1.0.1
version: 2.0.0
homepage: https://github.com/danger/dart

environment:
sdk: ">=2.12.0 <3.0.0"
sdk: ">=3.0.0 <4.0.0"

dev_dependencies:
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
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
isolate: ^2.1.1
path: ^1.8.0
path: ^1.8.3
27 changes: 27 additions & 0 deletions packages/danger_core/test/fixtures/diff/add_only.diff
Original file line number Diff line number Diff line change
@@ -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/
27 changes: 27 additions & 0 deletions packages/danger_core/test/fixtures/diff/mix.diff
Original file line number Diff line number Diff line change
@@ -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
Loading
Loading