ueman / feedback

A simple widget for getting better feedback.
https://pub.dev/packages/feedback
378 stars 92 forks source link

A better Template and Styling with Markdown for github , A new Package Required #269

Closed mg3994 closed 4 months ago

mg3994 commented 5 months ago

Is your feature request related to a problem? Please describe. This is a alternative of feedback_gitlab => feedback_github where we create issues using PAT

Describe the solution you'd like

buggy code but you will get what i am trying to say


import 'dart:io';

import 'package:feedback/feedback.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;

extension BetterFeedbackXT on FeedbackController {
  /// Example usage:
  /// ```dart
  /// import 'package:feedback_github/feedback_github.dart';
  ///
  /// RaisedButton(
  ///   child: Text('Click me'),
  ///   onPressed: (){
  ///     BetterFeedback.of(context).showAndUploadToGithub
  ///      tag: "BUG"
  ///    username:"githubusername"
  /// repository:"<REPO NAME>"
  ///       authToken: '<PAT TOKEN>',
  ///     );
  ///   }
  /// )
  void showAndUploadToGithub({
    required BuildContext context,
    required String username,
    required String repository,
    required String authToken,
    required String tag,
  }) {
    show(uploadToGithub(
      context: context,
      username: username,
      repository: repository,
      authToken: authToken,
      tag: tag,
    ));
  }
}

@visibleForTesting
OnFeedbackCallback uploadToGithub({
  required BuildContext context,
  required String username,
  required String repository,
  required String authToken,
  required String tag,
}) {
  final baseUrl = "https://api.github.com";

  return (UserFeedback feedback) async {
    final uri = Uri.parse('$baseUrl/repos/$username/$repository/issues');

    final request = http.MultipartRequest('POST', uri)
      ..fields['title'] = tag
      ..fields['body'] = feedback.text
      ..fields['extra'] = feedback.extra.toString();

    final screenshotBytes = Uint8List.fromList(feedback.screenshot);
    final tempFile = await _saveImageToFile(screenshotBytes);

    request.files.add(
      await http.MultipartFile.fromPath(
        'image',
        tempFile.path,
      ),
    );

    request.headers['Authorization'] = 'token $authToken';

    final streamedResponse = await request.send();
    final response = await http.Response.fromStream(streamedResponse);

    if (response.statusCode == 201) {
      ScaffoldMessenger.of(context).showSnackBar( //TODO: Avoid context at async gap
        const SnackBar(
          content: Text('Issue created successfully ✔'),
          backgroundColor: Colors.green,
        ),
      );
    } else {
      ScaffoldMessenger.of(context).showSnackBar( //TODO: Avoid context at async gap
        SnackBar(
          content: Text('Failed to create issue ❌ : ${response.body}'),
          backgroundColor: Colors.red,
        ),
      );
    }

    // Delete the temporary file
    await tempFile.delete();
  };
}

Future<File> _saveImageToFile(Uint8List imageBytes) async {
  final tempDir = await Directory.systemTemp.create();
  final tempFile = File('${tempDir.path}/screenshot.png');
  await tempFile.writeAsBytes(imageBytes);
  return tempFile;
}

Describe alternatives you've considered code provided

Additional context Please create a new package with that name where we can use custom Markdown and can create issues

mg3994 commented 4 months ago

Thanks for considering my Request

defuncart commented 4 months ago

This can currently be achieved by using feedback_github. Please see the README for more information.

ueman commented 4 months ago

As of now, this can't be done securely for GitHub.