UNPKG

git-bolt

Version:

Git utilities for faster development

109 lines (95 loc) • 4.52 kB
import chalk from 'chalk'; import fs from 'fs'; import git from 'isomorphic-git'; import { getGitHubToken } from '../utils/auth.js'; async function getRepoInfo() { try { const remotes = await git.listRemotes({ fs, dir: '.' }); const origin = remotes.find(remote => remote.remote === 'origin'); if (!origin) { throw new Error('No origin remote found'); } // Parse GitHub URL to get owner and repo const url = origin.url; const match = url.match(/github\.com[:/]([^/]+)\/([^.]+)(?:\.git)?$/); if (!match) { throw new Error('Not a GitHub repository URL'); } return { owner: match[1], repo: match[2].replace('.git', '') }; } catch (error) { throw new Error(`Failed to get repository info: ${error.message}`); } } async function verifyCommand() { try { // Get token const token = await getGitHubToken(); if (!token) { console.error(chalk.red('No GitHub token found.')); console.log(chalk.yellow('Set your token using:')); console.log(' npx git-bolt config github.token=your_token'); process.exit(1); } // Get repository info const { owner, repo } = await getRepoInfo(); console.log(chalk.blue('šŸ” Verifying GitHub token access...')); console.log(chalk.blue(`Repository: ${owner}/${repo}`)); // Try to access the repository using the GitHub API const response = await fetch(`https://api.github.com/repos/${owner}/${repo}`, { headers: { 'Authorization': `Bearer ${token}`, 'Accept': 'application/vnd.github.v3+json', 'User-Agent': 'git-bolt' } }); if (response.ok) { const data = await response.json(); console.log(chalk.green('\nāœ“ Token has access to this repository')); console.log(chalk.blue('\nRepository details:')); console.log(`Name: ${data.name}`); console.log(`Owner: ${data.owner.login}`); console.log(`Private: ${data.private ? 'Yes' : 'No'}`); console.log(`Default branch: ${data.default_branch}`); // Check specific permissions const permissions = data.permissions || {}; console.log(chalk.blue('\nPermissions:')); console.log(`āœ“ Read: ${permissions.pull ? 'Yes' : 'No'}`); console.log(`āœ“ Write: ${permissions.push ? 'Yes' : 'No'}`); console.log(`āœ“ Admin: ${permissions.admin ? 'Yes' : 'No'}`); } else { const errorData = await response.json(); console.error(chalk.red('\nāœ— Token does not have access to this repository')); console.error(chalk.yellow('\nError details:')); console.error(`Status: ${response.status} ${response.statusText}`); console.error(`Message: ${errorData.message}`); if (response.status === 404) { console.log(chalk.yellow('\nPossible issues:')); console.log('1. The repository does not exist'); console.log('2. The token does not have access to this repository'); console.log('3. If this is a private repository, make sure your token has the right scope'); } else if (response.status === 401) { console.log(chalk.yellow('\nPossible issues:')); console.log('1. The token is invalid or expired'); console.log('2. The token is not properly formatted'); } else if (response.status === 403) { console.log(chalk.yellow('\nPossible issues:')); console.log('1. Rate limit exceeded'); console.log('2. Token lacks required scope'); console.log('3. Token has been revoked'); } console.log(chalk.blue('\nTo fix:')); console.log('1. Check if the token is valid on GitHub'); console.log('2. Ensure the token has the right scopes (repo, workflow)'); console.log('3. Set a new token using:'); console.log(' npx git-bolt config github.token=your_new_token'); process.exit(1); } } catch (error) { console.error(chalk.red('Error:'), error.message); process.exit(1); } } export { verifyCommand };