mirror of
https://github.com/aljazceru/Auto-GPT.git
synced 2025-12-17 14:04:27 +01:00
Add GitHub Repository Validation to Leaderboard Submission Dialog (#5539)
This commit is contained in:
@@ -1,3 +1,6 @@
|
|||||||
|
import 'package:http/http.dart' as http;
|
||||||
|
import 'dart:convert';
|
||||||
|
|
||||||
class UriUtility {
|
class UriUtility {
|
||||||
static bool isURL(String url) {
|
static bool isURL(String url) {
|
||||||
// Validate if the URL string is empty, or contains spaces or invalid characters
|
// Validate if the URL string is empty, or contains spaces or invalid characters
|
||||||
@@ -44,4 +47,29 @@ class UriUtility {
|
|||||||
print('URL is valid.');
|
print('URL is valid.');
|
||||||
return true;
|
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';
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -44,14 +44,18 @@ class _LeaderboardSubmissionDialogState
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
void _validateAndSubmit() {
|
void _validateAndSubmit() async {
|
||||||
|
setState(() {
|
||||||
|
_teamNameError = null;
|
||||||
|
_repoUrlError = null;
|
||||||
|
_commitShaError = null;
|
||||||
|
});
|
||||||
|
|
||||||
bool isValid = true;
|
bool isValid = true;
|
||||||
|
|
||||||
if (_teamNameController.text.isEmpty) {
|
if (_teamNameController.text.isEmpty) {
|
||||||
isValid = false;
|
isValid = false;
|
||||||
_teamNameError = 'Team Name is required';
|
_teamNameError = 'Team Name is required';
|
||||||
} else {
|
|
||||||
_teamNameError = null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (_repoUrlController.text.isEmpty) {
|
if (_repoUrlController.text.isEmpty) {
|
||||||
@@ -60,20 +64,20 @@ class _LeaderboardSubmissionDialogState
|
|||||||
} else if (!UriUtility.isURL(_repoUrlController.text)) {
|
} else if (!UriUtility.isURL(_repoUrlController.text)) {
|
||||||
isValid = false;
|
isValid = false;
|
||||||
_repoUrlError = 'Invalid URL format';
|
_repoUrlError = 'Invalid URL format';
|
||||||
} else {
|
} else if (!(await UriUtility()
|
||||||
_repoUrlError = null;
|
.isValidGitHubRepo(_repoUrlController.text))) {
|
||||||
|
isValid = false;
|
||||||
|
_repoUrlError = 'Not a valid GitHub repository';
|
||||||
}
|
}
|
||||||
|
|
||||||
if (_commitShaController.text.isEmpty) {
|
if (_commitShaController.text.isEmpty) {
|
||||||
isValid = false;
|
isValid = false;
|
||||||
_commitShaError = 'Commit SHA is required';
|
_commitShaError = 'Commit SHA is required';
|
||||||
} else {
|
|
||||||
_commitShaError = null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isValid) {
|
if (isValid) {
|
||||||
print('Valid leaderboard submission parameters!');
|
print('Valid leaderboard submission parameters!');
|
||||||
_saveToSharedPreferences();
|
await _saveToSharedPreferences();
|
||||||
widget.onSubmit?.call(_teamNameController.text, _repoUrlController.text,
|
widget.onSubmit?.call(_teamNameController.text, _repoUrlController.text,
|
||||||
_commitShaController.text);
|
_commitShaController.text);
|
||||||
Navigator.of(context).pop();
|
Navigator.of(context).pop();
|
||||||
|
|||||||
Reference in New Issue
Block a user