diff --git a/packages/globe_tasks/lib/server.dart b/packages/globe_tasks/lib/server.dart index 6257337..9d6fc39 100644 --- a/packages/globe_tasks/lib/server.dart +++ b/packages/globe_tasks/lib/server.dart @@ -12,14 +12,10 @@ final dotEnv = DotEnv(includePlatformEnvironment: true, quiet: true)..load(); String get _globeAPIToken => dotEnv['GLOBE_API_TOKEN']!; String get _githubToken => dotEnv['GITHUB_API_TOKEN']!; +typedef Task = Future Function(); + // Configure routes. final _router = Router() - ..get('/', (req) { - const headers = { - HttpHeaders.contentTypeHeader: 'text/plain; charset=utf-8' - }; - return Response.ok('Okay 🚀', headers: headers); - }) ..post('/tasks/check-dart-version', _checkDartVersion) ..post('/tasks/check-flutter-version', _checkFlutterVersion); @@ -38,7 +34,7 @@ Future _checkDartVersion(Request request) async { getLatestDartReleases(), ).wait; - final tasks = Function()>[]; + final tasks = []; if (globeImages.stable != dartReleases.stable) { tasks.add(() => _triggerDartWorkflow(dartReleases.stable)); @@ -53,13 +49,7 @@ Future _checkDartVersion(Request request) async { tasks.add(() => _triggerDartWorkflow(devRelease)); } - if (tasks.isEmpty) { - return Response.ok('Already has latest images.'); - } - - await tasks.map((task) => task.call()).wait; - - return Response.ok('Completed.'); + return await _execTasks(tasks); } Future _checkFlutterVersion(Request request) async { @@ -68,7 +58,7 @@ Future _checkFlutterVersion(Request request) async { getLatestFlutterRelease(), ).wait; - final tasks = Function()>[]; + final tasks = []; if (globeImages.stable != flutterReleases.stable) { print('Add Flutter ${flutterReleases.stable}'); @@ -80,16 +70,38 @@ Future _checkFlutterVersion(Request request) async { tasks.add(() => _triggerFlutterWorkflow(flutterReleases.beta)); } + return await _execTasks(tasks); +} + +Future _triggerDartWorkflow(SdkRelease release) => triggerWorkflow( + _githubToken, + Workflow.dart, + release, + ); +Future _triggerFlutterWorkflow(SdkRelease release) => triggerWorkflow( + _githubToken, + Workflow.flutter, + release, + ); + +Future _execTasks(List tasks) async { if (tasks.isEmpty) { return Response.ok('Already has latest images.'); } - await tasks.map((task) => task.call()).wait; - - return Response.ok('Completed.'); + try { + await tasks.map((task) => task.call()).wait; + + return Response.ok( + 'Completed.', + headers: {HttpHeaders.contentTypeHeader: 'text/plain'}, + ); + } catch (e, trace) { + print(e); + print(trace); + return Response.ok( + 'An error occurred while triggering tasks', + headers: {HttpHeaders.contentTypeHeader: 'text/plain'}, + ); + } } - -Future _triggerDartWorkflow(SdkRelease release) => - triggerWorkflow(_githubToken, Workflow.dart, release); -Future _triggerFlutterWorkflow(SdkRelease release) => - triggerWorkflow(_githubToken, Workflow.flutter, release); diff --git a/packages/globe_tasks/lib/utils.dart b/packages/globe_tasks/lib/utils.dart index c68ddf5..86f5e01 100644 --- a/packages/globe_tasks/lib/utils.dart +++ b/packages/globe_tasks/lib/utils.dart @@ -1,5 +1,4 @@ import 'dart:convert'; -import 'dart:io'; import 'package:http/http.dart' as http; import 'package:collection/collection.dart'; @@ -216,9 +215,7 @@ Future triggerWorkflow( body: json.encode({'ref': 'main', 'inputs': flow.input(release)}), ); - print(result.body); - - if (result.statusCode != 200) { - exit(1); + if (result.statusCode != 204) { + throw Exception(result.body); } }