-
Notifications
You must be signed in to change notification settings - Fork 92
/
feedback_gitlab.dart
98 lines (89 loc) · 2.73 KB
/
feedback_gitlab.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import 'dart:convert';
import 'package:feedback/feedback.dart';
import 'package:flutter/foundation.dart';
import 'package:http/http.dart' as http;
import 'package:http_parser/http_parser.dart';
/// This is an extension to make it easier to call
/// [showAndUploadToGitLab].
extension BetterFeedbackX on FeedbackController {
/// Example usage:
/// ```dart
/// import 'package:feedback_gitlab/feedback_gitlab.dart';
///
/// RaisedButton(
/// child: Text('Click me'),
/// onPressed: (){
/// BetterFeedback.of(context).showAndUploadToGitLab
/// projectId: 'gitlab-project-id',
/// apiToken: 'gitlab-api-token',
/// gitlabUrl: 'gitlab.org', // optional, defaults to 'gitlab.com'
/// );
/// }
/// )
/// ```
/// The API token needs access to:
/// - read_api
/// - write_repository
/// See https://docs.gitlab.com/ee/user/project/settings/project_access_tokens.html#limiting-scopes-of-a-project-access-token
void showAndUploadToGitLab({
required String projectId,
required String apiToken,
String? gitlabUrl,
http.Client? client,
}) {
show(uploadToGitLab(
projectId: projectId,
apiToken: apiToken,
gitlabUrl: gitlabUrl,
client: client,
));
}
}
/// See [BetterFeedbackX.showAndUploadToGitLab].
/// This is just [visibleForTesting].
@visibleForTesting
OnFeedbackCallback uploadToGitLab({
required String projectId,
required String apiToken,
String? gitlabUrl,
http.Client? client,
}) {
final httpClient = client ?? http.Client();
final baseUrl = gitlabUrl ?? 'gitlab.com';
return (UserFeedback feedback) async {
final uri = Uri.https(
baseUrl,
'/api/v4/projects/$projectId/uploads',
);
final uploadRequest = http.MultipartRequest('POST', uri)
..headers.putIfAbsent('PRIVATE-TOKEN', () => apiToken)
..fields['id'] = projectId
..files.add(http.MultipartFile.fromBytes(
'file',
feedback.screenshot,
filename: 'feedback.png',
contentType: MediaType('image', 'png'),
));
final uploadResponse = await httpClient.send(uploadRequest);
final dynamic uploadResponseMap = jsonDecode(
await uploadResponse.stream.bytesToString(),
);
final imageMarkdown = uploadResponseMap["markdown"] as String?;
final extras = feedback.extra?.toString() ?? '';
final description = '${feedback.text}\n'
'${imageMarkdown ?? 'Missing image!'}\n'
'$extras';
// Create issue
await httpClient.post(
Uri.https(
baseUrl,
'/api/v4/projects/$projectId/issues',
<String, String>{
'title': feedback.text,
'description': description,
},
),
headers: {'PRIVATE-TOKEN': apiToken},
);
};
}