Add GitHub Repository Validation to Leaderboard Submission Dialog (#5539)

This commit is contained in:
hunteraraujo
2023-10-04 11:32:45 -07:00
committed by GitHub
parent 1bd85cbc09
commit f97fc0dd3d
2 changed files with 40 additions and 8 deletions

View File

@@ -1,3 +1,6 @@
import 'package:http/http.dart' as http;
import 'dart:convert';
class UriUtility {
static bool isURL(String url) {
// Validate if the URL string is empty, or contains spaces or invalid characters
@@ -44,4 +47,29 @@ class UriUtility {
print('URL is valid.');
return true;
}
Future<bool> isValidGitHubRepo(String repoUrl) async {
var uri = Uri.parse(repoUrl);
if (uri.host != 'github.com') {
return false;
}
var segments = uri.pathSegments;
if (segments.length < 2) {
return false;
}
var user = segments[0];
var repo = segments[1];
var apiUri = Uri.https('api.github.com', '/repos/$user/$repo');
var response = await http.get(apiUri);
if (response.statusCode != 200) {
return false;
}
var data = json.decode(response.body);
return data is Map && data['full_name'] == '$user/$repo';
}
}