Future storeTryChange()

in builder/lib/src/firestore.dart [351:432]


  Future<bool> storeTryChange(
      Map<String, dynamic> change, int review, int patchset) async {
    final name = change['name'] as String;
    final result = change['result'] as String;
    final expected = change['expected'] as String;
    final previousResult = change['previous_result'] as String;
    final configuration = change['configuration'] as String;

    // Find an existing result record for this test on this patchset.
    final responses = await query(
        from: 'try_results',
        where: compositeFilter([
          fieldEquals('review', review),
          fieldEquals('patchset', patchset),
          fieldEquals('name', name),
          fieldEquals('result', result),
          fieldEquals('previous_result', previousResult),
          fieldEquals('expected', expected)
        ]),
        limit: 1);
    // TODO(karlklose): We could run only this query, and then see if the
    //                  patchset is equal or not. We don't need the separate
    //                  query with equal patchset.
    //                  The test for previous.isNotEmpty below can be replaced
    //                  with responses.patchset != patchset.  We need to hit
    //                  the createDocument both if there was a previous response,
    //                  or there is no response at all.
    if (responses.isEmpty) {
      // Is the previous result for this test on this review approved?
      final previous = await query(
          from: 'try_results',
          where: compositeFilter([
            fieldEquals('review', review),
            fieldEquals('name', name),
            fieldEquals('result', result),
            fieldEquals('previous_result', previousResult),
            fieldEquals('expected', expected),
          ]),
          orderBy: orderBy('patchset', false),
          limit: 1);
      final approved =
          previous.isNotEmpty && previous.first.getBool('approved') == true;

      final document = Document()
        ..fields = taggedMap({
          'name': name,
          'result': result,
          'previous_result': previousResult,
          'expected': expected,
          'review': review,
          'patchset': patchset,
          'configurations': <String>[configuration],
          'approved': approved
        });
      await firestore.projects.databases.documents
          .createDocument(document, documents, 'try_results', mask_fieldPaths: [
        'name',
        'result',
        'previous_result',
        'expected',
        'review',
        'patchset',
        'configurations',
        'approved'
      ]);
      documentsWritten++;
      return approved;
    } else {
      final document = responses.first;
      // Update the TryResult for this test, adding this configuration.
      final values = ArrayValue()..values = [taggedValue(configuration)];
      final addConfiguration = FieldTransform()
        ..fieldPath = 'configurations'
        ..appendMissingElements = values;
      await _executeWrite([
        Write()
          ..update = document.toDocument()
          ..updateTransforms = [addConfiguration]
      ]);
      return document.getBool('approved') == true;
    }
  }