Skip to content

Commit

Permalink
fix: Fix status code check for github workflow dispatch (#112)
Browse files Browse the repository at this point in the history
* fix status code check

* _

* throw Exception instead of exiting
  • Loading branch information
codekeyz authored Oct 23, 2024
1 parent ecb82b2 commit 88e976e
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 28 deletions.
58 changes: 35 additions & 23 deletions packages/globe_tasks/lib/server.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> 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);

Expand All @@ -38,7 +34,7 @@ Future<Response> _checkDartVersion(Request request) async {
getLatestDartReleases(),
).wait;

final tasks = <Future<void> Function()>[];
final tasks = <Task>[];

if (globeImages.stable != dartReleases.stable) {
tasks.add(() => _triggerDartWorkflow(dartReleases.stable));
Expand All @@ -53,13 +49,7 @@ Future<Response> _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<Response> _checkFlutterVersion(Request request) async {
Expand All @@ -68,7 +58,7 @@ Future<Response> _checkFlutterVersion(Request request) async {
getLatestFlutterRelease(),
).wait;

final tasks = <Future<void> Function()>[];
final tasks = <Task>[];

if (globeImages.stable != flutterReleases.stable) {
print('Add Flutter ${flutterReleases.stable}');
Expand All @@ -80,16 +70,38 @@ Future<Response> _checkFlutterVersion(Request request) async {
tasks.add(() => _triggerFlutterWorkflow(flutterReleases.beta));
}

return await _execTasks(tasks);
}

Future<void> _triggerDartWorkflow(SdkRelease release) => triggerWorkflow(
_githubToken,
Workflow.dart,
release,
);
Future<void> _triggerFlutterWorkflow(SdkRelease release) => triggerWorkflow(
_githubToken,
Workflow.flutter,
release,
);

Future<Response> _execTasks(List<Task> 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<void> _triggerDartWorkflow(SdkRelease release) =>
triggerWorkflow(_githubToken, Workflow.dart, release);
Future<void> _triggerFlutterWorkflow(SdkRelease release) =>
triggerWorkflow(_githubToken, Workflow.flutter, release);
7 changes: 2 additions & 5 deletions packages/globe_tasks/lib/utils.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import 'dart:convert';
import 'dart:io';

import 'package:http/http.dart' as http;
import 'package:collection/collection.dart';
Expand Down Expand Up @@ -216,9 +215,7 @@ Future<void> 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);
}
}

0 comments on commit 88e976e

Please sign in to comment.