Skip to content

Commit 8976e56

Browse files
authored
Merge pull request #204 from SpinlockLabs/add_to_list_issue_reactions
Add content param to listReactions for issues
2 parents 742ab63 + 8f66a39 commit 8976e56

File tree

4 files changed

+80
-10
lines changed

4 files changed

+80
-10
lines changed

lib/src/common/github.dart

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,13 @@ class GitHub {
352352
final json = jsonDecode(response.body);
353353
message = json['message'];
354354
if (json['errors'] != null) {
355-
errors = List<Map<String, String>>.from(json['errors']);
355+
try {
356+
errors = List<Map<String, String>>.from(json['errors']);
357+
} catch (_) {
358+
errors = [
359+
{'code': json['errors'].toString()}
360+
];
361+
}
356362
}
357363
}
358364
switch (response.statusCode) {

lib/src/common/issues_service.dart

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -130,18 +130,28 @@ class IssuesService extends Service {
130130
);
131131
}
132132

133+
/// Gets a stream of [Reaction]s for an issue.
134+
/// The optional content param let's you filter the request for only reactions
135+
/// of that type.
136+
/// WARNING: ReactionType.plusOne and ReactionType.minusOne currently do not
137+
/// work and will throw an exception is used. All others without + or - signs
138+
/// work fine to filter.
139+
///
133140
/// This API is currently in preview. It may break.
134141
///
135142
/// See https://developer.github.com/v3/reactions/
136-
Stream<Reaction> listReactions(RepositorySlug slug, int issueNumber) =>
137-
PaginationHelper(github).objects(
138-
'GET',
139-
'/repos/${slug.owner}/${slug.name}/issues/$issueNumber/reactions',
140-
(i) => Reaction.fromJson(i),
141-
headers: {
142-
'Accept': 'application/vnd.github.squirrel-girl-preview+json',
143-
},
144-
);
143+
Stream<Reaction> listReactions(RepositorySlug slug, int issueNumber,
144+
{ReactionType content}) {
145+
var query = content != null ? '?content=${content.content}' : '';
146+
return PaginationHelper(github).objects(
147+
'GET',
148+
'/repos/${slug.owner}/${slug.name}/issues/$issueNumber/reactions$query',
149+
(i) => Reaction.fromJson(i),
150+
headers: {
151+
'Accept': 'application/vnd.github.squirrel-girl-preview+json',
152+
},
153+
);
154+
}
145155

146156
/// Edit an issue.
147157
///

lib/src/common/model/reaction.dart

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import 'package:github/src/common.dart';
22
import 'package:github/src/common/model/users.dart';
33
import 'package:json_annotation/json_annotation.dart';
4+
import 'package:meta/meta.dart';
45

56
part 'reaction.g.dart';
67

@@ -23,8 +24,50 @@ class Reaction {
2324
this.createdAt,
2425
});
2526

27+
ReactionType get type => ReactionType.fromString(content);
28+
2629
factory Reaction.fromJson(Map<String, dynamic> json) =>
2730
_$ReactionFromJson(json);
2831

2932
Map<String, dynamic> toJson() => _$ReactionToJson(this);
3033
}
34+
35+
@immutable
36+
class ReactionType {
37+
final String content;
38+
final String emoji;
39+
const ReactionType._(this.content, this.emoji);
40+
41+
@override
42+
String toString() => content;
43+
44+
static const plusOne = ReactionType._('+1', '👍');
45+
static const minusOne = ReactionType._('-1', '👎');
46+
static const laugh = ReactionType._('laugh', '😄');
47+
static const confused = ReactionType._('confused', '😕');
48+
static const heart = ReactionType._('heart', '❤️');
49+
static const hooray = ReactionType._('hooray', '🎉');
50+
static const rocket = ReactionType._('rocket', '🚀');
51+
static const eyes = ReactionType._('eyes', '👀');
52+
53+
static final _types = {
54+
'+1': plusOne,
55+
'-1': minusOne,
56+
'laugh': laugh,
57+
'confused': confused,
58+
'heart': heart,
59+
'hooray': hooray,
60+
'rocket': rocket,
61+
'eyes': eyes,
62+
':+1:': plusOne,
63+
':-1:': minusOne,
64+
':laugh:': laugh,
65+
':confused:': confused,
66+
':heart:': heart,
67+
':hooray:': hooray,
68+
':rocket:': rocket,
69+
':eyes:': eyes,
70+
};
71+
72+
static ReactionType fromString(String content) => _types[content];
73+
}

test/experiment/reactions.dart

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import 'package:github/github.dart';
2+
3+
void main() {
4+
final github = GitHub(auth: findAuthenticationFromEnvironment());
5+
github.issues
6+
.listReactions(RepositorySlug('SpinlockLabs', 'github.dart'), 177,
7+
content: ReactionType.plusOne)
8+
.listen((Reaction r) {
9+
print(ReactionType.fromString(r.content).emoji);
10+
});
11+
}

0 commit comments

Comments
 (0)