Skip to content

Commit 699bb42

Browse files
committed
Add content param to listReactions for issues
1 parent 742ab63 commit 699bb42

File tree

5 files changed

+75
-11
lines changed

5 files changed

+75
-11
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: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -133,15 +133,18 @@ class IssuesService extends Service {
133133
/// This API is currently in preview. It may break.
134134
///
135135
/// 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-
);
136+
Stream<Reaction> listReactions(RepositorySlug slug, int issueNumber,
137+
{ReactionType content}) {
138+
var query = content != null ? '?content=${content.content}' : '';
139+
return PaginationHelper(github).objects(
140+
'GET',
141+
'/repos/${slug.owner}/${slug.name}/issues/$issueNumber/reactions$query',
142+
(i) => Reaction.fromJson(i),
143+
headers: {
144+
'Accept': 'application/vnd.github.squirrel-girl-preview+json',
145+
},
146+
);
147+
}
145148

146149
/// Edit an issue.
147150
///

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+
}

lib/src/util.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ String buildQueryString(Map<String, dynamic> params) {
1515
if (params[key] == null) {
1616
continue;
1717
}
18-
queryString.write('$key=${Uri.encodeComponent(params[key].toString())}');
18+
queryString
19+
.write('$key=${Uri.encodeQueryComponent(params[key].toString())}');
1920
if (i != params.keys.length) {
2021
queryString.write('&');
2122
}

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)