fetchWorkflowContent function
Implementation
Future<String> fetchWorkflowContent({
required String owner,
required String repo,
required String workflowFileName,
required String token,
String? githubApiBaseUrl,
String? commitSha,
String? branch,
}) async {
final apiBase = githubApiBaseUrl != null && githubApiBaseUrl.isNotEmpty
? githubApiBaseUrl.replaceAll(RegExp(r'/+$'), '')
: 'https://api.github.com';
final ref = commitSha ?? branch;
final query = ref != null && ref.isNotEmpty
? '?ref=${Uri.encodeComponent(ref)}'
: '';
final url =
'$apiBase/repos/$owner/$repo/contents/.openci/$workflowFileName$query';
final response = await http.get(
Uri.parse(url),
headers: {
'Authorization': 'Bearer $token',
'Accept': 'application/vnd.github+json',
'X-GitHub-Api-Version': '2022-11-28',
'User-Agent': 'OpenCI-Worker',
},
);
if (response.statusCode != 200) {
throw HttpException(
'Failed to fetch workflow content: ${response.statusCode} ${response.body}',
);
}
final data = jsonDecode(response.body) as Map<String, dynamic>;
final content = data['content'] as String?;
if (content == null) {
throw StateError('No content in GitHub response');
}
final encoding = data['encoding'] as String? ?? 'base64';
if (encoding == 'base64') {
final cleaned = content.replaceAll(RegExp(r'\s+'), '');
return utf8.decode(base64.decode(cleaned));
}
return content;
}