Skip to content

Commit

Permalink
De-async health checks (#208)
Browse files Browse the repository at this point in the history
* De-async health checks

* add debug

* Delay client

* Use non-delayed client

* Shorter duration

* Make client private

* Add changelog
  • Loading branch information
mosuem authored Jan 2, 2024
1 parent 2587855 commit 55251b1
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 9 deletions.
24 changes: 24 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "health",
"program": "pkgs/firehose/bin/health.dart",
"request": "launch",
"type": "dart",
"env": {
"GITHUB_REPOSITORY": "dart-lang/ecosystem",
"ISSUE_NUMBER": "173",
"GITHUB_SHA": "null",
"GITHUB_STEP_SUMMARY": "/tmp/github_step_summary_mock.md",
},
"args": [
"--checks",
"version,changelog,license,coverage,do-not-submit"
]
}
]
}
3 changes: 2 additions & 1 deletion pkgs/firehose/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
## 0.4.1-wip
## 0.4.1

- Ensure that packages are listed in lexical order.
- Require the latest `package:http`.
- Delay Github requests by a small delay to avoid http errors.

## 0.4.0

Expand Down
75 changes: 75 additions & 0 deletions pkgs/firehose/lib/src/delayed_client.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'dart:convert';

import 'dart:typed_data';

import 'package:http/http.dart' as http;

class DelayedClient implements http.Client {
final http.Client _client;
final Duration duration;

factory DelayedClient(Duration duration) => DelayedClient._(
client: http.Client(),
duration: duration,
);

DelayedClient._({required this.duration, required http.Client client})
: _client = client;

@override
void close() => _client.close();

@override
Future<http.Response> delete(Uri url,
{Map<String, String>? headers, Object? body, Encoding? encoding}) =>
Future.delayed(
duration,
() => _client.delete(url,
body: body, encoding: encoding, headers: headers));

@override
Future<http.Response> get(Uri url, {Map<String, String>? headers}) =>
Future.delayed(duration, () => _client.get(url, headers: headers));

@override
Future<http.Response> head(Uri url, {Map<String, String>? headers}) =>
Future.delayed(duration, () => _client.head(url, headers: headers));

@override
Future<http.Response> patch(Uri url,
{Map<String, String>? headers, Object? body, Encoding? encoding}) =>
Future.delayed(
duration,
() => _client.patch(url,
headers: headers, body: body, encoding: encoding));

@override
Future<http.Response> post(Uri url,
{Map<String, String>? headers, Object? body, Encoding? encoding}) =>
Future.delayed(
duration,
() => _client.post(url,
headers: headers, body: body, encoding: encoding));
@override
Future<http.Response> put(Uri url,
{Map<String, String>? headers, Object? body, Encoding? encoding}) =>
Future.delayed(
duration,
() => _client.put(url,
headers: headers, body: body, encoding: encoding));
@override
Future<String> read(Uri url, {Map<String, String>? headers}) =>
Future.delayed(duration, () => _client.read(url, headers: headers));

@override
Future<Uint8List> readBytes(Uri url, {Map<String, String>? headers}) =>
Future.delayed(duration, () => _client.readBytes(url, headers: headers));

@override
Future<http.StreamedResponse> send(http.BaseRequest request) =>
Future.delayed(duration, () => _client.send(request));
}
11 changes: 9 additions & 2 deletions pkgs/firehose/lib/src/github.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
import 'dart:io';

import 'package:github/github.dart';
import 'package:http/http.dart' as http;
import 'package:path/path.dart' as path;

import 'delayed_client.dart';
import 'repo.dart';

class GithubApi {
Expand All @@ -24,9 +26,14 @@ class GithubApi {
: _repoSlug = repoSlug,
_issueNumber = issueNumber;

final http.Client _client = DelayedClient(const Duration(milliseconds: 50));

late GitHub github = githubAuthToken != null
? GitHub(auth: Authentication.withToken(githubAuthToken))
: GitHub();
? GitHub(
auth: Authentication.withToken(githubAuthToken),
client: _client,
)
: GitHub(client: _client);

String? get githubAuthToken => _env['GITHUB_TOKEN'];

Expand Down
10 changes: 5 additions & 5 deletions pkgs/firehose/lib/src/health/health.dart
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ class Health {
var github = GithubApi();

// Do basic validation of our expected env var.
if (!expectEnv(github.githubAuthToken, 'GITHUB_TOKEN')) return;
if (!expectEnv(github.repoSlug?.fullName, 'GITHUB_REPOSITORY')) return;
if (!expectEnv(github.issueNumber?.toString(), 'ISSUE_NUMBER')) return;
if (!expectEnv(github.sha, 'GITHUB_SHA')) return;
Expand Down Expand Up @@ -78,10 +77,11 @@ class Health {
!github.prLabels.contains('skip-do-not-submit-check'))
doNotSubmitCheck,
];

var checked =
await Future.wait(checks.map((check) => check(github)).toList());
await writeInComment(github, checked);
final results = <HealthCheckResult>[];
for (var check in checks) {
results.add(await check(github));
}
await writeInComment(github, results);
}

Future<HealthCheckResult> validateCheck(GithubApi github) async {
Expand Down
2 changes: 1 addition & 1 deletion pkgs/firehose/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: firehose
description: A tool to automate publishing of Pub packages from GitHub actions.
version: 0.4.1-wip
version: 0.4.1
repository: https://github.com/dart-lang/ecosystem/tree/main/pkgs/firehose

environment:
Expand Down

0 comments on commit 55251b1

Please sign in to comment.