<commit_before>library api.github_event_handler;

import 'dart:async';

import 'package:appengine/appengine.dart' as ae;
import 'package:firebase/firebase_io.dart';
import 'package:github_hook/github_hook.dart';

import 'gmail.dart';
import 'server_utils.dart';

GitHubRequestHandler createEventHandler() {
  return _echoRequest;
}

Future<Null> _echoRequest(HookRequest request) async {
  if (request is IssuesHookRequest) {
    if (request.action == 'labeled') {
      await _handleLabeledEvent(request);
    }
  }
}

Future<Null> _handleLabeledEvent(IssuesHookRequest request) async {
  assert(request.action == 'labeled');

  assert(request.label != null);

  var client = new FirebaseClient(firebaseSecret);

  var labeledRepository = request.repository.fullName;

  var labelName = request.label.name;

  var items = await client.get(getUsersForRepoUri(labeledRepository)) as Map;

  if (items == null) {
    return;
  }

  var subscribedEmails = new Set<String>();

  items.forEach((String encodedEmail, Map labelMap) {
    if (labelMap.containsKey(labelName)) {
      var email = decodeKey(encodedEmail);

      // decode the email!
      subscribedEmails.add(email);
    }
  });

  ae.loggingService.info('Sending email to ${subscribedEmails.toString()}.');

  var subject =
      '${request.repository.fullName}#${request.issue.number} labeled ${request.label.name} by ${request.sender.user}';

  var body = '''
 User: ${request.sender.user} - ${request.sender.githubUrl}
Label: ${request.label.name}

${request.issue.githubUrl}
${request.issue.title}

${request.issue.body}
''';

  await sendEmail(subject, body, bccEmails: subscribedEmails);
}<commit_msg>Don't send email when no one is subscribed to a label<commit_after>library api.github_event_handler;

import 'dart:async';

import 'package:appengine/appengine.dart' as ae;
import 'package:firebase/firebase_io.dart';
import 'package:github_hook/github_hook.dart';

import 'gmail.dart';
import 'server_utils.dart';

GitHubRequestHandler createEventHandler() {
  return _echoRequest;
}

Future<Null> _echoRequest(HookRequest request) async {
  if (request is IssuesHookRequest) {
    if (request.action == 'labeled') {
      await _handleLabeledEvent(request);
    }
  }
}

Future<Null> _handleLabeledEvent(IssuesHookRequest request) async {
  assert(request.action == 'labeled');

  assert(request.label != null);

  var client = new FirebaseClient(firebaseSecret);

  var labeledRepository = request.repository.fullName;

  var labelName = request.label.name;

  var items = await client.get(getUsersForRepoUri(labeledRepository)) as Map;

  if (items == null) {
    return;
  }

  var subscribedEmails = new Set<String>();

  items.forEach((String encodedEmail, Map labelMap) {
    if (labelMap.containsKey(labelName)) {
      var email = decodeKey(encodedEmail);

      // decode the email!
      subscribedEmails.add(email);
    }
  });

  if (subscribedEmails.isEmpty) {
    ae.loggingService.info('No subscriptions for label: "$labelName".');
    return;
  }

  ae.loggingService.info('For label "$labelName", sending email to ${subscribedEmails}.');

  var subject =
      '${request.repository.fullName}#${request.issue.number} labeled ${request.label.name} by ${request.sender.user}';

  var body = '''
 User: ${request.sender.user} - ${request.sender.githubUrl}
Label: ${request.label.name}

${request.issue.githubUrl}
${request.issue.title}

${request.issue.body}
''';

  await sendEmail(subject, body, bccEmails: subscribedEmails);
}
{'fraction_non_alphanumeric': 0.1350042722870977, 'fraction_numerical': 0.0, 'mean_word_length': 4.664516129032258, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, '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': '5195808', 'n_tokens_mistral': 1132, 'n_tokens_neox': 1101, 'n_words': 229}