fetchGitHubLogin function

Future<String> fetchGitHubLogin({
  1. required String githubToken,
  2. Client? client,
})

The GitHub login behind a token (GET https://api.github.com/user, goal: the account name is the default Copilot entry name copilot-<login>). Throws CopilotAuthException on a non-200.

Implementation

Future<String> fetchGitHubLogin({
  required String githubToken,
  http.Client? client,
}) async {
  final transport = client ?? sharedProviderHttpClient();
  final response = await transport
      .get(
        Uri.parse('https://api.github.com/user'),
        headers: {
          'authorization': 'token $githubToken',
          'accept': 'application/json',
        },
      )
      .timeout(effectiveProviderConnectTimeout);
  if (response.statusCode != 200) {
    throw CopilotAuthException(
      'could not resolve the GitHub account (HTTP ${response.statusCode}): '
      '${response.body.trim()}',
    );
  }
  final decoded = jsonDecode(response.body);
  if (decoded is! Map<String, dynamic> || decoded['login'] is! String) {
    throw const CopilotAuthException(
      'the GitHub user response had an unexpected shape.',
    );
  }
  return decoded['login'] as String;
}