Skip to content

Commit a3b0006

Browse files
Add the 'PushEvent' webhook and associated PushCommit object (#386)
* Add the 'PushEvent' webhook and associated PushCommit object * Add a test, handle integer times that come in from the Repository object in PushEvents only * Remove unnecessary decorator + update changelog * Add a comment about the time weirdness
1 parent 5ef10ff commit a3b0006

File tree

8 files changed

+274
-15
lines changed

8 files changed

+274
-15
lines changed

CHANGELOG.md

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 9.18.0
2+
3+
* Adds the initial `PushEvent` object in https://github.com/SpinlockLabs/github.dart/pull/386
4+
* Update the `Repository` values `created_at` and `pushed_at` to handle integer times
5+
16
## 9.17.0
27

38
* Add bearerToken constructor to Authentication class by @kevmoo in https://github.com/SpinlockLabs/github.dart/pull/381
@@ -307,7 +312,7 @@ Map<String, GistFile>? files;
307312
- Clean up lints https://github.com/SpinlockLabs/github.dart/pull/202
308313

309314
## 6.0.5
310-
- Fix null errors issue https://github.com/SpinlockLabs/github.dart/issues/199
315+
- Fix null errors issue https://github.com/SpinlockLabs/github.dart/issues/199
311316

312317
## 6.0.4
313318
- This fixes #196 (https://github.com/SpinlockLabs/github.dart/issues/196)
@@ -357,10 +362,10 @@ Map<String, GistFile>? files;
357362

358363
Deprecations:
359364

360-
- The `draft` and `prerelease` properties in the CreateRelease and Release
365+
- The `draft` and `prerelease` properties in the CreateRelease and Release
361366
- classes have been renamed to `isDraft` and `isPrerelease` for clarity.
362-
- Release.targetCommitsh has been renamed to Release.targetCommitish.
363-
- The `release` parameter in RepositoriesService.createRelease
367+
- Release.targetCommitsh has been renamed to Release.targetCommitish.
368+
- The `release` parameter in RepositoriesService.createRelease
364369
has been renamed to `createRelease`.
365370
- `RepositoriesService.getRelease` has been renamed to `RepositoriesService.getReleaseById`
366371

@@ -369,7 +374,7 @@ has been renamed to `createRelease`.
369374
- Add access to labels on Pull Requests https://github.com/SpinlockLabs/github.dart/pull/163
370375
- Adding draft property to PR model https://github.com/SpinlockLabs/github.dart/pull/162
371376
- updateFile request must be a PUT https://github.com/SpinlockLabs/github.dart/pull/160
372-
377+
373378
## v5.1.0
374379

375380
- `Repository`: added `updatedAt` and `license` fields.
@@ -386,7 +391,7 @@ has been renamed to `createRelease`.
386391

387392
## v5.0.0
388393

389-
- **BREAKING** `RepositoriesService.listCollaborators` now returns
394+
- **BREAKING** `RepositoriesService.listCollaborators` now returns
390395
`Stream<Collaborator>` instead of `Stream<User>`.
391396
- `Collaborator` is a new type that includes collaborator-specific
392397
information.
@@ -403,7 +408,7 @@ has been renamed to `createRelease`.
403408
- Removed unsupported `limit` parameter.
404409
- Removed flaky retry logic. Instead, `NotReady` is thrown, which can be used
405410
to decide to retry at the call site.
406-
- Made associated classes `ContributorStatistics` and
411+
- Made associated classes `ContributorStatistics` and
407412
`ContributorWeekStatistics` immutable. Since these classes are only meant as
408413
return values, we're not treating this as a breaking change.
409414
- Added `Stream<CodeSearchResults> github.search.code(...)` search API
@@ -433,7 +438,7 @@ has been renamed to `createRelease`.
433438

434439
## v2.3.2
435440

436-
- Automatically attempt to find GitHub user information in the process environment when running on the standalone VM.
441+
- Automatically attempt to find GitHub user information in the process environment when running on the standalone VM.
437442
- Add `ref` parameter to `getReadme` method for the repository service.
438443

439444
## v2.3.1
@@ -447,7 +452,7 @@ has been renamed to `createRelease`.
447452
- Moved `CHANGELOG` content back to repo.
448453
- Added `rateLimitLimit`, `rateLimitRemaining` and `rateLimitReset` to `GitHub`.
449454
- Added `id` to `Issue`
450-
- Added `direction`, `sort` and `since` optional arguments to
455+
- Added `direction`, `sort` and `since` optional arguments to
451456
`IssueService.listByRepo`.
452457

453458
## v2.1.0

lib/src/common/model/git.dart

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,28 @@ class CreateGitCommit {
9898
Map<String, dynamic> toJson() => _$CreateGitCommitToJson(this);
9999
}
100100

101+
/// Model class for a pushed commit.
102+
@JsonSerializable()
103+
class PushGitCommit {
104+
PushGitCommit(this.id, this.message, this.timestamp, this.url);
105+
106+
/// The commit hash.
107+
String? id;
108+
109+
/// The commit message.
110+
String? message;
111+
112+
/// The timestamp of the commit.
113+
DateTime? timestamp;
114+
115+
/// The direct url to the commit.
116+
String? url;
117+
118+
factory PushGitCommit.fromJson(Map<String, dynamic> input) =>
119+
_$PushGitCommitFromJson(input);
120+
Map<String, dynamic> toJson() => _$PushGitCommitToJson(this);
121+
}
122+
101123
/// Model class for an author or committer of a commit. The [GitCommitUser] may
102124
/// not correspond to a GitHub [User].
103125
@JsonSerializable(includeIfNull: false)

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

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

lib/src/common/model/repos.dart

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,9 +225,11 @@ class Repository {
225225
int networkCount;
226226

227227
/// The time the repository was created at
228+
@JsonKey(fromJson: Repository.dynamicToDateTime)
228229
DateTime? createdAt;
229230

230231
/// The last time the repository was pushed at
232+
@JsonKey(fromJson: Repository.dynamicToDateTime)
231233
DateTime? pushedAt;
232234

233235
DateTime? updatedAt;
@@ -459,6 +461,18 @@ class Repository {
459461

460462
@override
461463
String toString() => 'Repository: $owner/$name';
464+
465+
/// In some cases, github webhooks send time values as an integer. This method
466+
/// is added to handle those cases, but otherwise parse like normal.
467+
static DateTime? dynamicToDateTime(dynamic time) {
468+
if (time == null) {
469+
return null;
470+
}
471+
if (time.runtimeType == int) {
472+
return DateTime.fromMillisecondsSinceEpoch(time * 1000);
473+
}
474+
return DateTime.parse(time as String);
475+
}
462476
}
463477

464478
/// Model class for repository permissions.

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

Lines changed: 2 additions & 6 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: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,3 +235,40 @@ class CreateEvent extends HookEvent {
235235

236236
Map<String, dynamic> toJson() => _$CreateEventToJson(this);
237237
}
238+
239+
@JsonSerializable(fieldRename: FieldRename.snake)
240+
class PushEvent extends HookEvent {
241+
PushEvent({
242+
this.ref,
243+
this.before,
244+
this.after,
245+
this.repository,
246+
this.headCommit,
247+
this.commits,
248+
this.sender,
249+
});
250+
251+
factory PushEvent.fromJson(Map<String, dynamic> input) =>
252+
_$PushEventFromJson(input);
253+
String? ref;
254+
String? before;
255+
String? after;
256+
@JsonKey(toJson: handleIntegerTimes)
257+
Repository? repository;
258+
PushGitCommit? headCommit;
259+
List<PushGitCommit>? commits;
260+
User? sender;
261+
262+
Map<String, dynamic> toJson() => _$PushEventToJson(this);
263+
264+
static Map<String, dynamic>? handleIntegerTimes(Repository? repository) {
265+
var repositoryMap = repository?.toJson();
266+
for (final parameter in ['created_at', 'pushed_at']) {
267+
if (repositoryMap?[parameter] != null) {
268+
final createdAt = DateTime.parse(repositoryMap?[parameter]);
269+
repositoryMap?[parameter] = createdAt.millisecondsSinceEpoch ~/ 1000;
270+
}
271+
}
272+
return repositoryMap;
273+
}
274+
}

lib/src/server/hooks.g.dart

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

0 commit comments

Comments
 (0)