1. /**
    
  2.  * Copyright (c) Meta Platforms, Inc. and affiliates.
    
  3.  *
    
  4.  * This source code is licensed under the MIT license found in the
    
  5.  * LICENSE file in the root directory of this source tree.
    
  6.  *
    
  7.  * @flow
    
  8.  */
    
  9. 
    
  10. export type GitHubIssue = {
    
  11.   title: string,
    
  12.   url: string,
    
  13. };
    
  14. 
    
  15. const GITHUB_ISSUES_API = 'https://api.github.com/search/issues';
    
  16. 
    
  17. export function searchGitHubIssuesURL(message: string): string {
    
  18.   // Remove Fiber IDs from error message (as those will be unique).
    
  19.   message = message.replace(/"[0-9]+"/g, '');
    
  20. 
    
  21.   const filters = [
    
  22.     'in:title',
    
  23.     'is:issue',
    
  24.     'is:open',
    
  25.     'is:public',
    
  26.     'label:"Component: Developer Tools"',
    
  27.     'repo:facebook/react',
    
  28.   ];
    
  29. 
    
  30.   return (
    
  31.     GITHUB_ISSUES_API +
    
  32.     '?q=' +
    
  33.     encodeURIComponent(message) +
    
  34.     '%20' +
    
  35.     filters.map(encodeURIComponent).join('%20')
    
  36.   );
    
  37. }
    
  38. 
    
  39. export async function searchGitHubIssues(
    
  40.   message: string,
    
  41. ): Promise<GitHubIssue | null> {
    
  42.   const response = await fetch(searchGitHubIssuesURL(message));
    
  43.   const data = await response.json();
    
  44.   if (data.items.length > 0) {
    
  45.     const item = data.items[0];
    
  46.     return {
    
  47.       title: item.title,
    
  48.       url: item.html_url,
    
  49.     };
    
  50.   } else {
    
  51.     return null;
    
  52.   }
    
  53. }