Future onIssueOpened()

in github-label-notifier/functions/node/index.dart [155:257]


Future<void> onIssueOpened(Map<String, dynamic> event) async {
  SymbolizationResult symbolizedCrashes;

  final repositoryName = event['repository']['full_name'];
  final subscription = await db.lookupKeywordSubscription(repositoryName);

  if (subscription != null &&
      subscription.keywords.contains('crash') &&
      containsCrash(event['issue']['body'])) {
    symbolizedCrashes = await _trySymbolize(event['issue']);
  }

  final match = (symbolizedCrashes is SymbolizationResultOk &&
          symbolizedCrashes.results.isNotEmpty)
      ? 'crash'
      : subscription?.match(event['issue']['body']);
  if (match == null) {
    return;
  }

  final subscribers =
      await db.lookupLabelSubscriberEmails(repositoryName, subscription.label);

  final issueData = event['issue'];

  final issueTitle = issueData['title'];
  final issueNumber = issueData['number'];
  final issueUrl = issueData['html_url'];
  final issueReporterUsername = issueData['user']['login'];
  final issueReporterUrl = issueData['user']['html_url'];

  final escape = htmlEscape.convert;

  final symbolizedCrashesText = symbolizedCrashes
          ?.when(
              ok: (results) {
                return [
                  '',
                  ...results.expand((r) => [
                        if (r.symbolized != null)
                          '# engine ${r.engineBuild.engineHash} ${r.engineBuild.variant.pretty} crash'
                        else
                          '# engine crash',
                        for (var note in r.notes)
                          if (note.message != null)
                            '# ${noteMessage[note.kind]}: ${note.message}'
                          else
                            '# ${noteMessage[note.kind]}',
                        r.symbolized ?? r.crash.frames.toString(),
                      ]),
                  ''
                ];
              },
              error: (note) => null)
          ?.join('\n') ??
      '';

  final symbolizedCrashesHtml = symbolizedCrashes
          ?.when(
            ok: (results) {
              return results.expand((r) => [
                    if (r.symbolized != null)
                      '<p>engine ${r.engineBuild.engineHash} ${r.engineBuild.variant.pretty} crash</p>'
                    else
                      '<p>engine crash</p>',
                    for (var note in r.notes)
                      if (note.message != null)
                        '<em>${noteMessage[note.kind]}: <pre>${escape(note.message)}</pre></em>'
                      else
                        '<em>${noteMessage[note.kind]}</em>',
                    '<pre>${escape(r.symbolized ?? r.crash.frames.toString())}</pre>',
                  ]);
            },
            error: (note) => null,
          )
          ?.join('') ??
      '';

  await sendgrid.sendMultiple(
      from: 'noreply@dart.dev',
      to: subscribers,
      subject: '[$repositoryName] ${issueTitle} (#${issueNumber})',
      text: '''
${issueUrl}

Reported by ${issueReporterUsername}

Matches keyword: ${match}
${symbolizedCrashesText}
You are getting this mail because you are subscribed to label ${subscription.label}.
--
Sent by dart-github-label-notifier.web.app
''',
      html: '''
<p><strong><a href="${issueUrl}">${escape(issueTitle)}</a>&nbsp;(${escape(repositoryName)}#${escape(issueNumber.toString())})</strong></p>
<p>Reported by <a href="${issueReporterUrl}">${escape(issueReporterUsername)}</a></p>
<p>Matches keyword: <b>${match}</b></p>
${symbolizedCrashesHtml}
<p>You are getting this mail because you are subscribed to label ${subscription.label}</p>
<hr>
<p>Sent by <a href="https://dart-github-label-notifier.web.app/">GitHub Label Notifier</a></p>
''');
}