#!/usr/bin/env dart
// Copyright (c) 2019, 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.

// Find the success/failure status for a builder that is written to
// Firestore by the cloud functions that process results.json.
// These cloud functions write a success/failure result to the
// builder table based on the approvals in Firestore.

import 'dart:async';
import 'dart:convert';
import 'dart:io';

import 'package:http/http.dart';

const firebaseUrl =
    'https://firestore.googleapis.com/v1/projects/dart-ci/databases/(default)/documents';

void main(List<String> args) async {
  final builder = args[0];
  final buildNumber = args[1];
  final client = Client();
  return; // Temporarily make script return immediately, while we
  // land recipe change that calls it.
  for (int count = 0; count < 30; ++count) {
    // TODO(whesse): Add code to fetch from "try_builds" for builders that
    // end with "-try".
    final response =
        await client.get('$firebaseUrl/build_status/$builder:$buildNumber');
    if (response.statusCode == HttpStatus.ok) {
      final document = jsonDecode(response.body);
      final status = (document['fields']['status'] ?? const {})['stringValue'];
      if (status == 'success') exit(0);
      if (status == 'failed') exit(1);
    }
    await Future.delayed(Duration(seconds: 10));
  }
  print(
      'No status received for $builder:$buildNumber after 30 attempts, with 10 second waits.');
  exit(2);
}

Add script to read builder status from Firestore
[infra] Add script to read builder status from Firestore

This will be used by the new approval workflow to mark a builder
as succeeded or failed (green or red) on the build console, depending
on approvals.

Change-Id: I47bddb1f964d5f5b7c06923660f86f6eb7a5e247
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/125667
Reviewed-by: Jonas Termansen <092ec58085ae5e368bc39d18752740ff81a2d09c@google.com>
