Skip to content

Solving issue #22, Add export to github #27

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { NextResponse } from 'next/server';

export async function POST(request: Request) {
const { token, user, repo, filePath, content, message } = await request.json();

if (!token || !user || !repo || !filePath || !content || !message) {
return NextResponse.json({ success: false, error: 'Missing required fields' }, { status: 400 });
}

const url = `https://api.github.com/repos/${user}/${repo}/contents/${filePath}`;

const response = await fetch(url, {
method: 'PUT',
headers: {
'Accept': 'application/vnd.github+json',
'Authorization': `Bearer ${token}`,
'X-GitHub-Api-Version': '2022-11-28'
},
body: JSON.stringify({
message: message,
committer: {
name: user,
email: `${user}@gmail.com`
},
content: Buffer.from(content).toString('base64')
})
});

const data = await response.json();

if (response.status === 201) {
return NextResponse.json({ success: true, message: 'File created successfully', data });
} else {
return NextResponse.json({ success: false, error: data.message || 'Something went wrong' });
}
}
273 changes: 273 additions & 0 deletions mobile-magic/apps/frontend/app/api/auth/createRepo/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,273 @@
import { NextRequest, NextResponse } from 'next/server';

export async function POST(req: NextRequest) {
const { accessToken, name, description, homepage, isPrivate, isTemplate } = await req.json();

if (!accessToken || !name) {
return NextResponse.json({ error: 'Access token and repository name are required.' }, { status: 400 });
}

const response = await fetch('https://api.github.com/user/repos', {
method: 'POST',
headers: {
'Accept': 'application/vnd.github+json',
'Authorization': `Bearer ${accessToken}`,
'X-GitHub-Api-Version': '2022-11-28'
},
body: JSON.stringify({
name,
description,
homepage,
private: isPrivate,
is_template: isTemplate
})
});

const data = await response.json();

if (!response.ok) {
return NextResponse.json({ error: data.message || 'Failed to create repository' }, { status: response.status });
}

return NextResponse.json({ message: 'Repository created successfully!', data });
}




// import { NextRequest, NextResponse } from 'next/server';
// import fs from 'fs';
// import path from 'path';

// export async function POST(req: NextRequest) {
// const { accessToken, name, description, homepage, isPrivate, isTemplate } = await req.json();

// if (!accessToken || !name) {
// return NextResponse.json({ error: 'Access token and repository name are required.' }, { status: 400 });
// }

// // Create the repository
// const response = await fetch('https://api.github.com/user/repos', {
// method: 'POST',
// headers: {
// 'Accept': 'application/vnd.github+json',
// 'Authorization': `Bearer ${accessToken}`,
// 'X-GitHub-Api-Version': '2022-11-28'
// },
// body: JSON.stringify({
// name,
// description,
// homepage,
// private: isPrivate,
// is_template: isTemplate
// })
// });

// const data = await response.json();

// if (!response.ok) {
// return NextResponse.json({ error: data.message || 'Failed to create repository' }, { status: response.status });
// }

// // Push README file after repository creation
// try {
// const owner = data.owner.login;
// const repoName = data.name;

// // Path to the README file
// const readmePath = path.join(process.cwd(), './Readme');

// // Check if file exists
// if (!fs.existsSync(readmePath)) {
// throw new Error(`README file not found at: ${readmePath}`);
// }

// // Read the README file content
// const readmeContent = fs.readFileSync(readmePath, 'utf8');

// // Convert content to base64
// const contentBase64 = Buffer.from(readmeContent).toString('base64');

// // Push README to repository
// const uploadResponse = await fetch(`https://api.github.com/repos/${owner}/${repoName}/contents/README.md`, {
// method: 'PUT',
// headers: {
// 'Accept': 'application/vnd.github+json',
// 'Authorization': `Bearer ${accessToken}`,
// 'X-GitHub-Api-Version': '2022-11-28'
// },
// body: JSON.stringify({
// message: 'Add README file',
// content: contentBase64
// })
// });

// const uploadData = await uploadResponse.json();

// if (!uploadResponse.ok) {
// throw new Error(uploadData.message || 'Failed to upload README file');
// }

// return NextResponse.json({
// message: 'Repository created successfully and README added!',
// repository: data,
// readme: {
// path: 'README.md',
// status: 'success',
// url: uploadData.content.html_url
// }
// });
// } catch (error: any) {
// console.error(`Error uploading README: ${error.message}`);
// return NextResponse.json({
// message: 'Repository created but failed to add README',
// repository: data,
// error: error.message
// }, { status: 207 }); // 207 Multi-Status
// }
// }



// import { NextRequest, NextResponse } from 'next/server';
// import fs from 'fs';
// import path from 'path';

// export async function POST(req: NextRequest) {
// const { accessToken, name, description, homepage, isPrivate, isTemplate, files } = await req.json();

// if (!accessToken || !name) {
// return NextResponse.json({ error: 'Access token and repository name are required.' }, { status: 400 });
// }
// console.log("accessToken", accessToken)

// // Create the repository
// const response = await fetch('https://api.github.com/user/repos', {
// method: 'POST',
// headers: {
// 'Accept': 'application/vnd.github+json',
// 'Authorization': `Bearer ${accessToken}`,
// 'X-GitHub-Api-Version': '2022-11-28'
// },
// body: JSON.stringify({
// name,
// description,
// homepage,
// private: isPrivate,
// is_template: isTemplate
// })
// });

// const data = await response.json();

// if (!response.ok) {
// return NextResponse.json({ error: data.message || 'Failed to create repository' }, { status: response.status });
// }

// const owner = data.owner.login;
// const repoName = data.name;
// const uploadResults = [];

// try {
// // Process README first
// // Path to the README file
// const readmePath = path.join(process.cwd(), './Readme');

// // Check if file exists
// if (fs.existsSync(readmePath)) {
// // Read the README file content
// const readmeContent = fs.readFileSync(readmePath, 'utf8');

// // Convert content to base64
// const contentBase64 = Buffer.from(readmeContent).toString('base64');

// // Push README to repository
// const uploadResponse = await fetch(`https://api.github.com/repos/${owner}/${repoName}/contents/README.md`, {
// method: 'PUT',
// headers: {
// 'Accept': 'application/vnd.github+json',
// 'Authorization': `Bearer ${accessToken}`,
// 'X-GitHub-Api-Version': '2022-11-28'
// },
// body: JSON.stringify({
// message: 'Add README file',
// content: contentBase64
// })
// });

// const uploadData = await uploadResponse.json();

// if (!uploadResponse.ok) {
// throw new Error(uploadData.message || 'Failed to upload README file');
// }

// uploadResults.push({
// path: 'README.md',
// status: 'success',
// url: uploadData.content.html_url
// });
// }

// // Process additional files if provided
// if (files && Array.isArray(files)) {
// for (const file of files) {
// try {
// const { path: filePath, content } = file;

// if (!filePath || !content) {
// throw new Error('File path and content are required');
// }

// // Convert content to base64
// const contentBase64 = Buffer.from(content).toString('base64');

// // Push file to repository
// const fileUploadResponse = await fetch(`https://api.github.com/repos/${owner}/${repoName}/contents/${filePath}`, {
// method: 'PUT',
// headers: {
// 'Accept': 'application/vnd.github+json',
// 'Authorization': `Bearer ${accessToken}`,
// 'X-GitHub-Api-Version': '2022-11-28'
// },
// body: JSON.stringify({
// message: `Add ${filePath}`,
// content: contentBase64
// })
// });

// const fileUploadData = await fileUploadResponse.json();

// if (!fileUploadResponse.ok) {
// throw new Error(fileUploadData.message || `Failed to upload ${filePath}`);
// }

// uploadResults.push({
// path: filePath,
// status: 'success',
// url: fileUploadData.content.html_url
// });
// } catch (fileError: any) {
// uploadResults.push({
// path: file.path,
// status: 'error',
// error: fileError.message
// });
// }
// }
// }

// return NextResponse.json({
// message: 'Repository created successfully and files added!',
// repository: data,
// files: uploadResults
// });
// } catch (error: any) {
// console.error(`Error processing files: ${error.message}`);
// return NextResponse.json({
// message: 'Repository created but failed to add some files',
// repository: data,
// files: uploadResults,
// error: error.message
// }, { status: 207 }); // 207 Multi-Status
// }
// }
33 changes: 33 additions & 0 deletions mobile-magic/apps/frontend/app/api/auth/github/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { NextRequest, NextResponse } from 'next/server';

export async function POST(req: any) {
try {
const { searchParams } = new URL(req.url);
const code = searchParams.get("code");
console.log(code)
if (!code) {
return NextResponse.json({ message: 'Missing code' }, { status: 400 });
}

const response = await fetch('https://github.com/login/oauth/access_token', {
method: 'POST',
headers: { 'Accept': 'application/json', 'Content-Type': 'application/json' },
body: JSON.stringify({
client_id: "Ov23ligJEGHq8tCMte5X",
client_secret: "6e7c841b0630251a386ad3b935ddf6d71157f999",
code,
}),
});

const data = await response.json();
if (!data.access_token) {
return NextResponse.json({ message: 'Failed to get access token' }, { status: 500 });
}

console.log({ access_token: data.access_token })

return NextResponse.json({ access_token: data.access_token }, { status: 200 });
} catch (error) {
return NextResponse.json({ message: (error as Error).message }, { status: 500 });
}
}
3 changes: 3 additions & 0 deletions mobile-magic/apps/frontend/components/Appbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { Header } from '@/components/Header'
import { motion } from 'motion/react'
import { containerVariants, itemVariants } from '@/lib/animation-variants'
import { ThemeButton } from '@/components/theme-button'
import ConnetToGithub from '@/components/Github/ConnectToGithub'

export function Appbar() {
return (
Expand Down Expand Up @@ -43,6 +44,8 @@ export function Appbar() {
</SignUpButton>
</SignedOut>

<ConnetToGithub/>

<SignedIn>
<UserButton />
</SignedIn>
Expand Down
Loading