github_template.dart 6 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
import 'dart:async';

import 'package:file/file.dart';
import 'package:intl/intl.dart';

import '../base/file_system.dart';
import '../base/io.dart';
import '../base/logger.dart';
import '../base/process.dart';
import '../build_system/exceptions.dart';
import '../convert.dart';
import '../devfs.dart';
import '../flutter_manifest.dart';
import '../flutter_project_metadata.dart';
import '../project.dart';
import '../version.dart';
21 22 23

/// Provide suggested GitHub issue templates to user when Flutter encounters an error.
class GitHubTemplateCreator {
24
  GitHubTemplateCreator({
25 26 27
    required FileSystem fileSystem,
    required Logger logger,
    required FlutterProjectFactory flutterProjectFactory,
28 29
  }) : _fileSystem = fileSystem,
      _logger = logger,
30
      _flutterProjectFactory = flutterProjectFactory;
31 32 33 34

  final FileSystem _fileSystem;
  final Logger _logger;
  final FlutterProjectFactory _flutterProjectFactory;
35

36 37
  static String toolCrashSimilarIssuesURL(String errorString) {
    return 'https://github.com/flutter/flutter/issues?q=is%3Aissue+${Uri.encodeQueryComponent(errorString)}';
38 39
  }

40
  /// Restricts exception object strings to contain only information about tool internals.
41
  static String sanitizedCrashException(Object error) {
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
    if (error is ProcessException) {
      // Suppress args.
      return 'ProcessException: ${error.message} Command: ${error.executable}, OS error code: ${error.errorCode}';
    } else if (error is FileSystemException) {
      // Suppress path.
      return 'FileSystemException: ${error.message}, ${error.osError}';
    } else if (error is SocketException) {
      // Suppress address and port.
      return 'SocketException: ${error.message}, ${error.osError}';
    } else if (error is DevFSException) {
      // Suppress underlying error.
      return 'DevFSException: ${error.message}';
    } else if (error is NoSuchMethodError
      || error is ArgumentError
      || error is VersionCheckError
      || error is MissingDefineException
      || error is UnsupportedError
      || error is UnimplementedError
      || error is StateError
      || error is ProcessExit
      || error is OSError) {
      // These exception objects only reference tool internals, print the whole error.
      return '${error.runtimeType}: $error';
    } else if (error is Error) {
      return '${error.runtimeType}: ${LineSplitter.split(error.stackTrace.toString()).take(1)}';
    } else if (error is String) {
      // Force comma separator to standardize.
      return 'String: <${NumberFormat(null, 'en_US').format(error.length)} characters>';
    }
    // Exception, other.
    return error.runtimeType.toString();
  }

75 76 77 78 79
  /// GitHub URL to present to the user containing encoded suggested template.
  ///
  /// Shorten the URL, if possible.
  Future<String> toolCrashIssueTemplateGitHubURL(
      String command,
80
      Object error,
81 82 83
      StackTrace stackTrace,
      String doctorText
    ) async {
84
    final String errorString = sanitizedCrashException(error);
85
    final String title = '[tool_crash] $errorString';
86 87
    final String body = '''
## Command
88 89 90
```
$command
```
91

92 93 94 95
## Steps to Reproduce
1. ...
2. ...
3. ...
96

97
## Logs
98
$errorString
99
```
100
${LineSplitter.split(stackTrace.toString()).take(25).join('\n')}
101 102 103 104
```
```
$doctorText
```
105

106 107 108
## Flutter Application Metadata
${_projectMetadataInformation()}
''';
109

110
    return 'https://github.com/flutter/flutter/issues'
111 112
      '/new' // We split this here to appease our lint that looks for bad "new bug" links.
      '?title=${Uri.encodeQueryComponent(title)}'
113 114 115 116 117 118 119 120
      '&body=${Uri.encodeQueryComponent(body)}'
      '&labels=${Uri.encodeQueryComponent('tool,severe: crash')}';
  }

  /// Provide information about the Flutter project in the working directory, if present.
  String _projectMetadataInformation() {
    FlutterProject project;
    try {
121
      project = _flutterProjectFactory.fromDirectory(_fileSystem.currentDirectory);
122 123 124 125 126
    } on Exception catch (exception) {
      // pubspec may be malformed.
      return exception.toString();
    }
    try {
127
      final FlutterManifest manifest = project.manifest;
128
      if (manifest.isEmpty) {
129 130
        return 'No pubspec in working directory.';
      }
131
      final FlutterProjectMetadata metadata = FlutterProjectMetadata(project.metadataFile, _logger);
132
      final FlutterProjectType? projectType = metadata.projectType;
133
      final StringBuffer description = StringBuffer()
134
        ..writeln('**Type**: ${projectType == null ? 'malformed' : projectType.cliName}')
135 136 137 138 139 140
        ..writeln('**Version**: ${manifest.appVersion}')
        ..writeln('**Material**: ${manifest.usesMaterialDesign}')
        ..writeln('**Android X**: ${manifest.usesAndroidX}')
        ..writeln('**Module**: ${manifest.isModule}')
        ..writeln('**Plugin**: ${manifest.isPlugin}')
        ..writeln('**Android package**: ${manifest.androidPackage}')
141 142 143
        ..writeln('**iOS bundle identifier**: ${manifest.iosBundleIdentifier}')
        ..writeln('**Creation channel**: ${metadata.versionChannel}')
        ..writeln('**Creation framework version**: ${metadata.versionRevision}');
144

145 146
      final File file = project.flutterPluginsFile;
      if (file.existsSync()) {
147 148 149
        description.writeln('### Plugins');
        // Format is:
        // camera=/path/to/.pub-cache/hosted/pub.dartlang.org/camera-0.5.7+2/
150
        for (final String plugin in project.flutterPluginsFile.readAsLinesSync()) {
151 152 153 154
          final List<String> pluginParts = plugin.split('=');
          if (pluginParts.length != 2) {
            continue;
          }
155 156
          // Write the last part of the path, which includes the plugin name and version.
          // Example: camera-0.5.7+2
157
          final List<String> pathParts = _fileSystem.path.split(pluginParts[1]);
158
          description.writeln(pathParts.isEmpty ? pluginParts.first : pathParts.last);
159 160 161
        }
      }

162
      return description.toString();
163 164 165 166 167
    } on Exception catch (exception) {
      return exception.toString();
    }
  }
}