Future _getNewCommits()

in builder/lib/src/commits_cache.dart [114:171]


  Future<void> _getNewCommits() async {
    const prefix = ")]}'\n";
    final lastCommit = await firestore.getLastCommit();
    final lastHash = lastCommit.hash;
    final lastIndex = lastCommit.index;

    final branch = 'master';
    final logUrl = 'https://dart.googlesource.com/sdk/+log/';
    final range = '$lastHash..$branch';
    final parameters = ['format=JSON', 'topo-order', 'first-parent', 'n=1000'];
    final url = Uri.parse('$logUrl$range?${parameters.join('&')}');
    final response = await httpClient.get(url);
    final protectedJson = response.body;
    if (!protectedJson.startsWith(prefix)) {
      throw Exception('Gerrit response missing prefix $prefix: $protectedJson.'
          'Requested URL: $url');
    }
    final commits = List.castFrom<dynamic, Map<String, dynamic>>(
        jsonDecode(protectedJson.substring(prefix.length))['log']);
    if (commits.isEmpty) {
      print('Found no new commits between $lastHash and $branch');
    }
    print('Fetched new commits from Gerrit (gitiles): $commits');
    final first = commits.last;
    if (first['parents'].first != lastHash) {
      throw 'First new commit ${first['commit']} is not'
          ' a child of last known commit $lastHash when fetching new commits';
    }
    var index = lastIndex + 1;
    for (final commit in commits.reversed) {
      final review = _review(commit);
      var reverted = _revert(commit);
      var relanded = _reland(commit);
      if (relanded != null) {
        reverted = null;
      }
      if (reverted != null) {
        final revertedCommit = await firestore.getCommit(reverted);
        if (revertedCommit != null && revertedCommit.isRevert) {
          reverted = null;
          relanded = revertedCommit.revertOf;
        }
      }
      await firestore.addCommit(commit['commit'], {
        fAuthor: commit['author']['email'],
        fCreated: parseGitilesDateTime(commit['committer']['time']),
        fIndex: index,
        fTitle: commit['message'].split('\n').first,
        if (review != null) fReview: review,
        if (reverted != null) fRevertOf: reverted,
        if (relanded != null) fRelandOf: relanded,
      });
      if (review != null) {
        await landReview(commit, index);
      }
      ++index;
    }
  }