Skip to content

Commit

Permalink
Added compare method for avoid duplicate report (#2)
Browse files Browse the repository at this point in the history
* correct description & readme example

* Added license & change error example

* Changed assert explaintion

* Added compare method

* Use compare method for avoid duplicate issues
  • Loading branch information
M97Chahboun committed Jan 11, 2023
1 parent 2643768 commit cd6d458
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 19 deletions.
22 changes: 21 additions & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1 +1,21 @@
TODO: Add your license here.
MIT License

Copyright (c) 2022 Mohammed CHAHBOUN

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ WidgetsFlutterBinding.ensureInitialized();
owner: owner,
token: const String.fromEnvironment('token'),
repo: const String.fromEnvironment("repo"));
if (!kReleaseMode) {
if (kReleaseMode) {
// For report exceptions & bugs Automaticlly
GhSnitch.listenToExceptions(assignees: [owner]);
}
Expand Down
6 changes: 3 additions & 3 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class _MyHomePageState extends State<MyHomePage> {
void _incrementCounter() {
Navigator.of(context)
.push(MaterialPageRoute(builder: (BuildContext context) {
return const Screen([]);
return const ScreenError(555);
}));
setState(() {
// This call to setState tells the Flutter framework that something has
Expand Down Expand Up @@ -139,8 +139,8 @@ class _MyHomePageState extends State<MyHomePage> {
}
}

class Screen extends StatelessWidget {
const Screen(this.wrong, {super.key});
class ScreenError extends StatelessWidget {
const ScreenError(this.wrong, {super.key});
final dynamic wrong;
@override
Widget build(BuildContext context) {
Expand Down
12 changes: 6 additions & 6 deletions lib/src/gh_snitch.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,14 @@ class GhSnitch {
static _handleNotInitialized() {
String solve =
"""GhSnitch not initialized, add this code before runApp method \nWidgetsFlutterBinding.ensureInitialized();
await dotenv.load(fileName: ".env");
const String owner = String.fromEnvironment('owner');
GhSnitch.initialize(
owner: dotenv.env['owner']!,
token: dotenv.env['token']!,
repo: dotenv.env['repo']!);
if (kReleaseMode) {
owner: owner,
token: const String.fromEnvironment('token'),
repo: const String.fromEnvironment("repo"));
if (!kReleaseMode) {
// For report exceptions & bugs Automaticlly
GhSnitch.listenToExceptions();
GhSnitch.listenToExceptions(assignees: [owner]);
}""";
assert(_instance.initialized, solve);
}
Expand Down
16 changes: 16 additions & 0 deletions lib/src/utils/compare.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
double compare(String txt1, String txt2) {
List splitTxt1 = txt1.split(" ");
List splitTxt2 = txt2.split(" ");
List result = [];
for (var e in splitTxt1) {
for (String i in splitTxt2) {
if (e == i) {
result.add(e);
}
}
}
double n = (splitTxt1.toSet().length + splitTxt2.toSet().length) / 2;
double percent = (result.toSet().length * 100) / n;
print(percent.toString());
return percent;
}
21 changes: 14 additions & 7 deletions lib/src/utils/github_snitch_instance.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import 'dart:io';

import 'package:client_information/client_information.dart';
import 'package:flutter/foundation.dart';
import 'package:github_snitch/github_snitch.dart';
import 'package:github_snitch/src/utils/compare.dart';

import '../models/comment.dart';
import '../models/issue.dart';
import 'constants.dart';
import 'gh_requests.dart';
import 'gh_response.dart';
Expand Down Expand Up @@ -34,7 +36,7 @@ class GhSnitchInstance {
await createLabel(fromGhRSnitchPackage,
"Errors caught by Github Snitch package", "970206");
String issueEndpoint = "$owner/$repo/issues";
bool notCreated = await issueNotCreated(title, issueEndpoint);
bool notCreated = await issueIsNew(body, issueEndpoint);
if (notCreated) {
Map<String, dynamic> issueBody = {
ownerBody: owner,
Expand Down Expand Up @@ -112,9 +114,13 @@ class GhSnitchInstance {
this.token = token;
this.owner = owner;
this.repo = repo;
ghRequest = GhRequest(token);
reportSavedIssues();
log("✅ GhSnitch initialized");
if (token.isEmpty || owner.isEmpty || repo.isEmpty) {
log("🔴 Echec to initialize GhSnitch");
} else {
ghRequest = GhRequest(token);
reportSavedIssues();
log("✅ GhSnitch initialized $repo");
}
}

void listenToExceptions({
Expand Down Expand Up @@ -185,14 +191,15 @@ class GhSnitchInstance {
}
}

Future<bool> issueNotCreated(String title, String endpoint) async {
Future<bool> issueIsNew(String body, String endpoint) async {
String params = "?state=all&labels=$fromGhRSnitchPackage";
GhResponse ghResponse =
await ghRequest.request("GET", endpoint + params, "");
if (ghResponse.statusCode == 200) {
bool notExist = true;
for (var e in (ghResponse.response as List)) {
if (e[bodyTitle] == title) {
double comparePercent = compare(e[bodyBody], body);
if (comparePercent <= 80.0) {
notExist = false;
}
}
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: github_snitch
description: Package for report app crashes & issues to it github repo
description: Package for capture & open github issue for crashes, issues & proposals
version: 0.0.1-beta
homepage: https://github.com/M97Chahboun/github_snitch

Expand Down

0 comments on commit cd6d458

Please sign in to comment.