commitWorkflowToGitHub function
Implementation
Future<void> commitWorkflowToGitHub(
String repoUrl,
String pat,
String branch,
) async {
try {
final uriParts = repoUrl.replaceAll('https://github.com/', '').split('/');
if (uriParts.length != 2) {
print('❌ Invalid GitHub repository URL.');
return;
}
final owner = uriParts[0];
final repo = uriParts[1];
final filePath = '.github/workflows/flutter-ci.yml';
final file = File(filePath);
if (!file.existsSync()) {
print('❌ Workflow file not found at $filePath');
return;
}
final content = base64Encode(await file.readAsBytes());
final apiUrl =
'https://api.github.com/repos/$owner/$repo/contents/$filePath';
final response = await http.put(
Uri.parse(apiUrl),
headers: {
'Authorization': 'token $pat',
'Accept': 'application/vnd.github+json',
},
body: jsonEncode({
'message': 'Add Flutter CI/CD workflow',
'content': content,
'branch': branch,
}),
);
if (response.statusCode == 201 || response.statusCode == 200) {
print('✅ Workflow committed to GitHub successfully.');
} else {
print('❌ Failed to commit workflow: ${response.body}');
}
} catch (e) {
print('❌ Error committing workflow: $e');
}
}