Skip to content

Commit 01a87bf

Browse files
Add a Changes object to the PullRequestEvent object so we can see what changed in edited PR events (#390)
* Add changes object so we can see what changed in edited PR. * Remove unused import * Formatting. * Make objects const
1 parent c86afa7 commit 01a87bf

File tree

6 files changed

+1316
-0
lines changed

6 files changed

+1316
-0
lines changed

lib/src/common/model/changes.dart

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import 'package:json_annotation/json_annotation.dart';
2+
import 'package:meta/meta.dart';
3+
4+
part 'changes.g.dart';
5+
6+
@immutable
7+
@JsonSerializable()
8+
class Ref {
9+
const Ref(this.from);
10+
final String? from;
11+
12+
factory Ref.fromJson(Map<String, dynamic> input) => _$RefFromJson(input);
13+
Map<String, dynamic> toJson() => _$RefToJson(this);
14+
}
15+
16+
@immutable
17+
@JsonSerializable()
18+
class Sha {
19+
const Sha(this.from);
20+
final String? from;
21+
22+
factory Sha.fromJson(Map<String, dynamic> input) => _$ShaFromJson(input);
23+
Map<String, dynamic> toJson() => _$ShaToJson(this);
24+
}
25+
26+
@immutable
27+
@JsonSerializable()
28+
class Base {
29+
const Base(this.ref, this.sha);
30+
final Ref? ref;
31+
final Sha? sha;
32+
33+
factory Base.fromJson(Map<String, dynamic> input) => _$BaseFromJson(input);
34+
Map<String, dynamic> toJson() => _$BaseToJson(this);
35+
}
36+
37+
@immutable
38+
@JsonSerializable()
39+
class Body {
40+
const Body(this.from);
41+
final String? from;
42+
43+
factory Body.fromJson(Map<String, dynamic> input) => _$BodyFromJson(input);
44+
Map<String, dynamic> toJson() => _$BodyToJson(this);
45+
}
46+
47+
@immutable
48+
@JsonSerializable()
49+
class Title {
50+
const Title({this.from});
51+
final String? from;
52+
53+
factory Title.fromJson(Map<String, dynamic> input) => _$TitleFromJson(input);
54+
Map<String, dynamic> toJson() => _$TitleToJson(this);
55+
}
56+
57+
@immutable
58+
@JsonSerializable()
59+
class Changes {
60+
const Changes(this.base, this.body, this.title);
61+
final Base? base;
62+
final Body? body;
63+
final Title? title;
64+
65+
factory Changes.fromJson(Map<String, dynamic> input) =>
66+
_$ChangesFromJson(input);
67+
Map<String, dynamic> toJson() => _$ChangesToJson(this);
68+
}

lib/src/common/model/changes.g.dart

Lines changed: 71 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/src/server/hooks.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
import 'dart:async';
22
import 'dart:convert';
33
import 'dart:io';
4+
45
import 'package:json_annotation/json_annotation.dart';
6+
57
import '../common.dart';
8+
import '../common/model/changes.dart';
69

710
part 'hooks.g.dart';
811

@@ -203,12 +206,14 @@ class PullRequestEvent extends HookEvent {
203206
this.pullRequest,
204207
this.sender,
205208
this.repository,
209+
this.changes,
206210
});
207211
String? action;
208212
int? number;
209213
PullRequest? pullRequest;
210214
User? sender;
211215
Repository? repository;
216+
Changes? changes;
212217

213218
factory PullRequestEvent.fromJson(Map<String, dynamic> input) =>
214219
_$PullRequestEventFromJson(input);

lib/src/server/hooks.g.dart

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/server/hooks_test.dart

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,29 @@ void main() {
5757
expect(sender.htmlUrl, "https://github.com/Codertocat");
5858
});
5959
});
60+
61+
group('EditedPullRequest', () {
62+
test('deserialize with body edit', () {
63+
final pullRequestEditedEvent = PullRequestEvent.fromJson(
64+
jsonDecode(prBodyEditedEvent) as Map<String, dynamic>);
65+
final changes = pullRequestEditedEvent.changes;
66+
expect(changes, isNotNull);
67+
expect(changes!.body!.from, isNotNull);
68+
assert(changes.body!.from ==
69+
'**This should not land until https://github.com/flutter/buildroot/pull/790');
70+
});
71+
72+
test('deserialize with base edit', () {
73+
final pullRequestEditedEvent = PullRequestEvent.fromJson(
74+
jsonDecode(prBaseEditedEvent) as Map<String, dynamic>);
75+
final changes = pullRequestEditedEvent.changes;
76+
expect(changes, isNotNull);
77+
expect(changes!.body, isNull);
78+
expect(changes.base, isNotNull);
79+
expect(changes.base!.ref, isNotNull);
80+
assert(changes.base!.ref!.from == 'main');
81+
assert(changes.base!.sha!.from ==
82+
'b3af5d64d3e6e2110b07d71909fc432537339659');
83+
});
84+
});
6085
}

0 commit comments

Comments
 (0)