Infer GitHub App ID where possible

This commit is contained in:
Imran Remtulla
2023-05-27 20:36:29 -04:00
parent 4c6303f783
commit 833ece1ef5
6 changed files with 62 additions and 17 deletions

View File

@ -6,6 +6,7 @@ import 'package:obtainium/app_sources/html.dart';
import 'package:obtainium/components/generated_form.dart';
import 'package:obtainium/custom_errors.dart';
import 'package:obtainium/providers/apps_provider.dart';
import 'package:obtainium/providers/logs_provider.dart';
import 'package:obtainium/providers/settings_provider.dart';
import 'package:obtainium/providers/source_provider.dart';
import 'package:url_launcher/url_launcher_string.dart';
@ -79,6 +80,44 @@ class GitHub extends AppSource {
canSearch = true;
}
@override
Future<String?> tryInferringAppId(String standardUrl,
{Map<String, dynamic> additionalSettings = const {}}) async {
const possibleBuildGradleLocations = [
'/app/build.gradle',
'android/app/build.gradle',
'src/app/build.gradle'
];
for (var path in possibleBuildGradleLocations) {
try {
var res = await sourceRequest(
'${await convertStandardUrlToAPIUrl(standardUrl)}/contents/$path');
if (res.statusCode == 200) {
try {
var body = jsonDecode(res.body);
var appId = utf8
.decode(base64
.decode(body['content'].toString().split('\n').join('')))
.split('\n')
.map((e) => e.trim())
.where((l) => l.startsWith('applicationId "'))
.first
.split('"')[1];
if (appId.isNotEmpty) {
return appId;
}
} catch (err) {
LogsProvider().add(
'Error parsing build.gradle from ${res.request!.url.toString()}: ${err.toString()}');
}
}
} catch (err) {
// Ignore - ID will be extracted from the APK
}
}
return null;
}
@override
String sourceSpecificStandardizeURL(String url) {
RegExp standardUrlRegEx = RegExp('^https?://$host/[^/]+/[^/]+');
@ -97,6 +136,12 @@ class GitHub extends AppSource {
return creds != null && creds.isNotEmpty ? '$creds@' : '';
}
Future<String> getAPIHost() async =>
'https://${await getCredentialPrefixIfAny()}api.$host';
Future<String> convertStandardUrlToAPIUrl(String standardUrl) async =>
'${await getAPIHost()}/repos${standardUrl.substring('https://$host'.length)}';
@override
String? changeLogPageFromStandardUrl(String standardUrl) =>
'$standardUrl/releases';
@ -239,7 +284,7 @@ class GitHub extends AppSource {
) async {
return await getLatestAPKDetailsCommon2(standardUrl, additionalSettings,
(bool useTagUrl) async {
return 'https://${await getCredentialPrefixIfAny()}api.$host/repos${standardUrl.substring('https://$host'.length)}/${useTagUrl ? 'tags' : 'releases'}?per_page=100';
return '${await convertStandardUrlToAPIUrl(standardUrl)}/${useTagUrl ? 'tags' : 'releases'}?per_page=100';
}, (Response res) {
rateLimitErrorCheck(res);
});
@ -281,7 +326,7 @@ class GitHub extends AppSource {
Future<Map<String, List<String>>> search(String query) async {
return searchCommon(
query,
'https://${await getCredentialPrefixIfAny()}api.$host/search/repositories?q=${Uri.encodeQueryComponent(query)}&per_page=100',
'${await getAPIHost()}/search/repositories?q=${Uri.encodeQueryComponent(query)}&per_page=100',
'items', onHttpErrorCode: (Response res) {
rateLimitErrorCheck(res);
});