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

// Applies pending pre-approvals for any changelists that have landed according
// to the git history of HEAD.

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

import 'package:args/args.dart';

import 'results.dart';

main(List<String> args) async {
  final parser = new ArgParser();
  parser.addFlag("dry",
      abbr: "n",
      help: "Don't write out the updated approvals.",
      negatable: false);
  parser.addFlag("help", help: "Show the program usage.", negatable: false);
  parser.addOption("upload",
      abbr: "u",
      help: "Upload the updated results to this cloud storage location");

  final options = parser.parse(args);
  if (options["help"]) {
    print("""
Usage: apply_preapprovals.dart [OPTION]... APPROVALS
Applies pending pre-approvals for any changelists that have landed according to
the git history of HEAD.

The options are as follows:

${parser.usage}""");
    return;
  }

  final parameters = options.rest;
  if (parameters.length != 1) {
    print("error: Expected one parameter");
    exitCode = 2;
    return;
  }

  // Locate gsutil.py.
  gsutilPy = Platform.script
      .resolve("../../third_party/gsutil/gsutil.py")
      .toFilePath();

  final approvalsPath = parameters[0];
  final approvals = await loadResultsMap(approvalsPath);

  // Find the changelists with pre-approvals.
  final allChangelists = <String>{};
  for (final key in approvals.keys) {
    final record = approvals[key];
    final preapprovals =
        record.putIfAbsent("preapprovals", () => <String, dynamic>{});
    allChangelists.addAll(preapprovals.keys);
  }
  if (allChangelists.isEmpty) {
    print("No pre-approvals are pending");
  }

  // Find the order the pre-approved changelists landed in.
  final joinedChangelistsPattern = allChangelists.join("\\|");
  final pattern = "^Change-Id: \\($joinedChangelistsPattern\\)\$";
  final arguments = [
    "rev-list",
    "--pretty=medium",
    "--grep=$pattern",
    "--reverse",
    "HEAD"
  ];
  final processOutput = await Process.run("git", arguments, runInShell: true);
  if (processOutput.exitCode != 0) {
    throw new Exception("Failed to run git $arguments\n"
        "exitCode: ${processOutput.exitCode}\n"
        "stdout: ${processOutput.stdout}\n"
        "stderr: ${processOutput.stderr}");
  }
  final landedChangelists = <String>[];
  final commitOfChangelist = <String, String>{};
  String currentCommit;
  for (final line in LineSplitter.split(processOutput.stdout)) {
    if (line.startsWith("commit ")) {
      currentCommit = line.substring("commit ".length);
    } else if (line.startsWith("    Change-Id: ")) {
      final changeId = line.substring("    Change-Id: ".length);
      if (allChangelists.contains(changeId)) {
        landedChangelists.add(changeId);
        commitOfChangelist[changeId] = currentCommit;
      }
    }
  }
  if (processOutput.stdout != "") {
    print(processOutput.stdout);
  }

  // Report the status of each of the pre-approved changelists.
  final unlandedChangelists =
      allChangelists.difference(landedChangelists.toSet());
  for (final changelist in unlandedChangelists) {
    final changelistUrl = "https://dart-review.googlesource.com/q/$changelist";
    print("Pending: Changelist $changelistUrl hasn't landed yet");
  }
  if (allChangelists.isNotEmpty && landedChangelists.isEmpty) {
    print("No pre-approved changelists have landed.");
  }
  for (final changelist in landedChangelists) {
    final changelistUrl = "https://dart-review.googlesource.com/q/$changelist";
    final commit = commitOfChangelist[changelist];
    print("Landed: Changelist $changelistUrl landed in commit $commit");
  }
<commit_msg>[infra] Add --apply-changelist option to apply_preapprovals.<commit_after>#!/usr/bin/env dart
// Copyright (c) 2018, 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.

// Applies pending pre-approvals for any changelists that have landed according
// to the git history of HEAD.

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

import 'package:args/args.dart';

import 'results.dart';

main(List<String> args) async {
  final parser = new ArgParser();
  parser.addFlag("dry",
      abbr: "n",
      help: "Don't write out the updated approvals.",
      negatable: false);
  parser.addMultiOption("apply-changelist",
      abbr: "A",
      help: "Apply this changelist even if it hasn't landed",
      splitCommas: false);
  parser.addFlag("help", help: "Show the program usage.", negatable: false);
  parser.addOption("upload",
      abbr: "u",
      help: "Upload the updated results to this cloud storage location");

  final options = parser.parse(args);
  if (options["help"]) {
    print("""
Usage: apply_preapprovals.dart [OPTION]... APPROVALS
Applies pending pre-approvals for any changelists that have landed according to
the git history of HEAD.

The options are as follows:

${parser.usage}""");
    return;
  }

  final parameters = options.rest;
  if (parameters.length != 1) {
    print("error: Expected one parameter");
    exitCode = 2;
    return;
  }

  // Locate gsutil.py.
  gsutilPy = Platform.script
      .resolve("../../third_party/gsutil/gsutil.py")
      .toFilePath();

  final approvalsPath = parameters[0];
  final approvals = await loadResultsMap(approvalsPath);

  // Find the changelists with pre-approvals.
  final allChangelists = <String>{};
  for (final key in approvals.keys) {
    final record = approvals[key];
    final preapprovals =
        record.putIfAbsent("preapprovals", () => <String, dynamic>{});
    allChangelists.addAll(preapprovals.keys);
  }
  if (allChangelists.isEmpty) {
    print("No pre-approvals are pending");
  }

  // Find the order the pre-approved changelists landed in.
  final joinedChangelistsPattern = allChangelists.join("\\|");
  final pattern = "^Change-Id: \\($joinedChangelistsPattern\\)\$";
  final arguments = [
    "rev-list",
    "--pretty=medium",
    "--grep=$pattern",
    "--reverse",
    "HEAD"
  ];
  final processOutput = await Process.run("git", arguments, runInShell: true);
  if (processOutput.exitCode != 0) {
    throw new Exception("Failed to run git $arguments\n"
        "exitCode: ${processOutput.exitCode}\n"
        "stdout: ${processOutput.stdout}\n"
        "stderr: ${processOutput.stderr}");
  }
  final landedChangelists = <String>[];
  final commitOfChangelist = <String, String>{};
  String currentCommit;
  for (final line in LineSplitter.split(processOutput.stdout)) {
    if (line.startsWith("commit ")) {
      currentCommit = line.substring("commit ".length);
    } else if (line.startsWith("    Change-Id: ")) {
      final changeId = line.substring("    Change-Id: ".length);
      if (allChangelists.contains(changeId)) {
        landedChangelists.add(changeId);
        commitOfChangelist[changeId] = currentCommit;
      }
    }
  }
  if (processOutput.stdout != "") {
    print(processOutput.stdout);
  }

  // Report the status of each of the pre-approved changelists.
  final unlandedChangelists =
      allChangelists.difference(landedChangelists.toSet());
  for (final changelist in unlandedChangelists) {
    final changelistUrl = "https://dart-review.googlesource.com/q/$changelist";
    print("Pending: Changelist $changelistUrl hasn't landed yet");
  }
  if (allChangelists.isNotEmpty && landedChangelists.isEmpty) {
    print("No pre-approved changelists have landed.");
  }
  for (final changelist in landedChangelists) {
    final changelistUrl = "https://dart-review.googlesource.com/q/$changelist";
    final commit = commitOfChangelist[changelist];
    print("Landed: Changelist $changelistUrl landed in commit $commit");
  }
  for (final changelist in options["apply-changelist"]) {
    final changelistUrl = "https://dart-review.googlesource.com/q/$changelist";
    print("Force applying: Pretending $changelistUrl has landed");
    landedChangelists.add(changelist);
  }

{'fraction_non_alphanumeric': 0.1318187340541854, 'fraction_numerical': 0.0019438707325962824, 'mean_word_length': 3.6066032456631225, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 15, 'https://': 5, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5197609', 'n_tokens_mistral': 2593, 'n_tokens_neox': 2531, 'n_words': 678}