Skip to content

Commit

Permalink
fix: GitHub authorization logic
Browse files Browse the repository at this point in the history
  • Loading branch information
maneike committed Oct 15, 2024
1 parent af60e27 commit 4482243
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 43 deletions.
8 changes: 4 additions & 4 deletions packages/core/utils/github/install.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,17 @@ function checkGitHubCLI() {

// Helper function to ensure GitHub authentication
function ensureGitHubAuthentication() {
console.log('πŸ–‡οΈ Checking GitHub authentication...');
console.log('πŸ–‡οΈ Checking GitHub authentication status...');

// Check if the user is already authenticated
if (isGitHubAuthenticated()) {
console.log('πŸ–‡οΈ You are already logged in to GitHub.');
return; // Exit early if authenticated
}

if (!authenticateGitHub()) {
console.error('πŸ–‡οΈ Authentication failed. Run "gh auth login" in your terminal and try again.');
process.exit(1);
if (!isGitHubAuthenticated()) {
console.error(`πŸ–‡οΈ It looks like you're not logged in...`);
authenticateGitHub();
}
}

Expand Down
56 changes: 17 additions & 39 deletions packages/core/utils/github/repositoryManager.ts
Original file line number Diff line number Diff line change
@@ -1,53 +1,31 @@
import { execSync } from 'child_process';

async function executeCommand(command: string, silent = false): Promise<string | null> {
try {
const result = execSync(command, { stdio: 'pipe' }); // Use 'pipe' to capture output
const output = result.toString().trim(); // Convert buffer to string
return output || null; // Ensure we return null if output is empty
} catch (error) {
console.error(`Error executing command: ${command}`);
if (error instanceof Error) {
console.error(`Error message: ${error.message}`);
if (error) {
console.error(`Command stdout: ${error.toString()}`); // Log stdout
}
}
return null;
}
}
import { type StdioOptions, execSync } from 'child_process';

export function isGitHubAuthenticated(): boolean {
console.log('πŸ–‡οΈ Checking GitHub authentication status...');

try {
// Use execSync to run the command and capture output
const result = execSync('gh auth status', { stdio: 'pipe' }).toString();
const result = execSync('gh auth status', { stdio: 'pipe' }).toString().trim();

// Check if the output includes "Logged in" - this is to be changed in the future but couldn't find a better way
return result.includes('Logged in');
} catch (error) {
console.error(`πŸ–‡οΈ Error checking authentication status: ${error}`);
return false;
}
}

export async function authenticateGitHub(): Promise<boolean> {
console.log('πŸ–‡οΈ Attempting to authenticate with GitHub...');

const result = await executeCommand('gh auth login');
execSync('gh auth login', { stdio: 'inherit' });

if (result) {
// Immediately check authentication status after login attempt
const isAuthenticated = isGitHubAuthenticated();
// Immediately check authentication status after login attempt
const isAuthenticated = isGitHubAuthenticated();

if (isAuthenticated) {
console.log('πŸ–‡οΈ Authentication was successful.');
return true;
} else {
console.error('πŸ–‡οΈ Authentication failed after login attempt.');
return false;
}
if (isAuthenticated) {
console.log('πŸ–‡οΈ Authentication was successful.');
return true;
} else {
console.error('πŸ–‡οΈ Authentication failed after login attempt.');
return false;
}

console.error('πŸ–‡οΈ No output from gh auth login command.');
Expand All @@ -57,7 +35,7 @@ export async function authenticateGitHub(): Promise<boolean> {
export async function fetchGitHubUsername(): Promise<string | null> {
try {
// Run the command without --jq first to inspect raw output
const username = await executeCommand('echo "$(gh api user --jq .login)"');
const username = execSync('echo "$(gh api user --jq .login)"', { stdio: 'pipe' }).toString().trim();

if (username) {
console.log(`πŸ–‡οΈ Hello \x1b[36m${username}\x1b[0m!`);
Expand All @@ -81,7 +59,7 @@ export async function createGitHubRepository(

// Check if the repository exists
const repoCheckCommand = `echo "$(gh repo view ${username}/${projectName} --json name)"`;
const existingRepo = await executeCommand(repoCheckCommand, true); // Silent mode to suppress output
const existingRepo = execSync(repoCheckCommand, { stdio: 'pipe' }).toString().trim();

if (existingRepo) {
console.error(`πŸ–‡οΈ Repository "${projectName}" already exists.`);
Expand All @@ -91,12 +69,12 @@ export async function createGitHubRepository(
console.log(`πŸ–‡οΈ Creating GitHub repository: \x1b[36m${projectName}\x1b[0m`);

const visibility = repositoryVisibility === 'public' ? '--public' : '--private';
const command = `gh repo create ${projectName} ${visibility} --confirm`;
const command = `gh repo create ${projectName} ${visibility}`;

const result = await executeCommand(command);
const result = execSync(command);

if (result) {
console.log(`πŸ–‡οΈ Repository created successfully at \x1b[36m${result}\x1b[0m`);
console.log(`πŸ–‡οΈ Repository successfully created at \x1b[36m${result}\x1b[0m`);
return true; // Return true to indicate success
}

Expand All @@ -119,7 +97,7 @@ export async function setupGitRepository(projectName: string, username: string)
];

for (const cmd of commands) {
const result = executeCommand(cmd);
const result = execSync(cmd, { stdio: 'pipe' });
if (!result) {
console.error(`πŸ–‡οΈ Failed to execute command: ${cmd}`);
process.exit(1);
Expand Down

0 comments on commit 4482243

Please sign in to comment.