uploadToGitLab function

  1. @visibleForTesting
OnFeedbackCallback uploadToGitLab(
  1. {required String projectId,
  2. required String apiToken,
  3. String? gitlabUrl,
  4. Client? client}
)

Implementation

@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},
    );
  };
}