-
Notifications
You must be signed in to change notification settings - Fork 12
/
github_api.dart
143 lines (127 loc) · 5.07 KB
/
github_api.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// Dart imports:
import 'dart:convert';
import 'dart:io';
// Flutter imports:
import 'package:flutter/foundation.dart';
// Package imports:
import 'package:flutter_config/flutter_config.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:github/github.dart';
import 'package:logger/logger.dart';
import 'package:package_info_plus/package_info_plus.dart';
import 'package:path_provider/path_provider.dart';
// Project imports:
import 'package:notredame/features/app/analytics/analytics_service.dart';
import 'package:notredame/features/app/error/internal_info_service.dart';
import 'package:notredame/features/more/feedback/models/feedback_issue.dart';
import 'package:notredame/utils/locator.dart';
class GithubApi {
static const String tag = "GithubApi";
static const String tagError = "$tag - Error";
static const String _envVariableGithubAPIKey = "GH_API_TOKEN";
static const String _repositorySlug = "ApplETS/Notre-Dame";
static const String _repositoryReportSlug = "ApplETS/Notre-Dame-Bug-report";
late GitHub _github;
final Logger _logger = locator<Logger>();
final AnalyticsService _analyticsService = locator<AnalyticsService>();
final InternalInfoService _internalInfoService =
locator<InternalInfoService>();
GithubApi() {
String githubApiToken;
if (kDebugMode &&
FlutterConfig.variables.containsKey(_envVariableGithubAPIKey)) {
githubApiToken = FlutterConfig.get(_envVariableGithubAPIKey).toString();
} else {
githubApiToken = const String.fromEnvironment(_envVariableGithubAPIKey);
}
_github = GitHub(auth: Authentication.withToken(githubApiToken));
}
/// Upload a file to the ApplETS/Notre-Dame-Bug-report repository
void uploadFileToGithub({required String filePath, required File file}) {
_github.repositories
.createFile(
RepositorySlug.full(_repositoryReportSlug),
CreateFile(
path: filePath,
content: base64Encode(file.readAsBytesSync()),
message: DateTime.now().toString(),
committer:
CommitUser('clubapplets-server', 'clubapplets@gmail.com'),
branch: 'main'))
.catchError((error) {
// ignore: avoid_dynamic_calls
_logger.e("uploadFileToGithub error: ${error.message}");
_analyticsService.logError(
tag,
// ignore: avoid_dynamic_calls
"uploadFileToGithub: ${error.message}",
error as GitHubError);
throw Error();
});
}
/// Create Github issue into the Notre-Dame repository with the labels bugs and the platform used.
/// The bug report will contain a file, a description [feedbackText] and also some information about the
/// application/device.
Future<Issue> createGithubIssue(
{required String feedbackText,
required String fileName,
required String feedbackType,
String? email}) async {
final PackageInfo packageInfo = await PackageInfo.fromPlatform();
return _github.issues
.create(
RepositorySlug.full(_repositorySlug),
IssueRequest(
title: 'Issue from ${packageInfo.appName} ',
body: " **Describe the issue** \n"
"```$feedbackText```\n\n"
"**Screenshot** \n"
"![screenshot](https://github.com/$_repositoryReportSlug/blob/main/$fileName?raw=true)\n\n"
"${await _internalInfoService.getDeviceInfoForErrorReporting()}"
"- **Email:** ${email ?? 'Not provided'} \n",
labels: [
feedbackType,
'platform: ${Platform.operatingSystem}'
]))
.catchError((error) {
// ignore: avoid_dynamic_calls
_logger.e("createGithubIssue error: ${error.message}");
_analyticsService.logError(
tag,
// ignore: avoid_dynamic_calls
"createGithubIssue: ${error.message}",
error as GitHubError);
throw Error();
});
}
Future<List<FeedbackIssue>> fetchIssuesByNumbers(
List<int> numbers, AppIntl appIntl) async {
final List<FeedbackIssue> issues = [];
for (int i = 0; i < numbers.length; i++) {
issues.add(FeedbackIssue(await _github.issues
.get(RepositorySlug.full(_repositorySlug), numbers[i])
.catchError((error) {
// ignore: avoid_dynamic_calls
_logger.e("fetchIssuesByNumbers error: ${error.message}");
_analyticsService.logError(
tag,
// ignore: avoid_dynamic_calls
"fetchIssuesByNumbers: ${error.message}",
error as GitHubError);
throw Error();
})));
}
return issues;
}
/// Create an empty bug picture in the local storage
Future<File> get localFile async {
final path = await _localPath;
final now = DateTime.now();
return File('$path/bugPicture-${now.hashCode}.png');
}
/// Get local storage path
Future<String> get _localPath async {
final directory = await getTemporaryDirectory();
return directory.path.replaceFirst('/', '');
}
}