All files / src/cli/commands/setup-app credentials.ts

0% Statements 0/7
0% Branches 0/2
0% Functions 0/1
0% Lines 0/7

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46                                                                                           
/**
 * Exchange temporary code for GitHub App credentials via GitHub API.
 */
 
export interface AppCredentials {
  id: number;
  name: string;
  pem: string;
  htmlUrl: string;
}
 
/**
 * Exchange a temporary code for GitHub App credentials.
 * This uses the GitHub API to convert the code into full app credentials.
 */
export async function exchangeCodeForCredentials(code: string): Promise<AppCredentials> {
  const url = `https://api.github.com/app-manifests/${code}/conversions`;
 
  const response = await fetch(url, {
    method: 'POST',
    headers: {
      Accept: 'application/vnd.github+json',
      'X-GitHub-Api-Version': '2022-11-28',
    },
  });
 
  if (!response.ok) {
    const errorText = await response.text();
    throw new Error(`Failed to exchange code for credentials: ${response.status} ${response.statusText}\n${errorText}`);
  }
 
  const data = (await response.json()) as {
    id: number;
    name: string;
    pem: string;
    html_url: string;
  };
 
  return {
    id: data.id,
    name: data.name,
    pem: data.pem,
    htmlUrl: data.html_url,
  };
}