Skip to content
Merged
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
124 changes: 4 additions & 120 deletions apps/docs/app/api/code-storage/github/installations/route.ts
Original file line number Diff line number Diff line change
@@ -1,124 +1,8 @@
import type { components } from '@octokit/openapi-types';
import { type NextRequest, NextResponse } from 'next/server';
import { CodeStorageGitHubInstallations } from '@pierre/storage-elements-next';
import { type NextRequest } from 'next/server';

type Installation = components['schemas']['installation'];

type Owner = components['schemas']['simple-user'] &
components['schemas']['enterprise'];

type InstallationResponse = {
installations: Installation[];
};

type FilteredInstallation = {
id: string;
app_id: string;
app_slug: string;
permissions: Record<string, string>;
owner_id: string | null;
events: string[];
type: string | null;
};

type FilteredOwner = {
id: string;
login: string;
type: string | null;
avatar_url: string | null;
};

function filterInstallations(installations: Installation[]) {
return installations.map((installation) => {
return {
id: `${installation.id}`,
app_id: `${installation.app_id}`,
app_slug: installation.app_slug,
owner_id:
installation.account != null && 'id' in installation.account
? `${installation.account.id}`
: null,
permissions: installation.permissions,
events: installation.events,
type:
installation.account != null && 'type' in installation.account
? installation.account.type
: null,
} satisfies FilteredInstallation;
});
}

function filterOwners(owners: Owner[]) {
return owners.map((owner) => {
return {
id: `${owner.id}`,
login: owner.login,
type: owner.type,
avatar_url: owner.avatar_url,
} satisfies FilteredOwner;
});
}

function getOwnersFromInstallations(installations: Installation[]) {
return installations
.map((installation) => installation.account)
.filter((account): account is Owner => account !== null);
}
const installations = new CodeStorageGitHubInstallations({});

export async function GET(request: NextRequest) {
const token = request.cookies.get('github_token')?.value;

if (token == null || token.trim() === '') {
return NextResponse.json({ data: { installations: [], owners: [] } });
}

try {
const response = await fetch(`https://api.github.com/user/installations`, {
headers: {
Authorization: `Bearer ${token}`,
Accept: 'application/vnd.github.v3+json',
},
});

if (!response.ok) {
return NextResponse.json(
{ error: 'Failed to fetch installations' },
{ status: 400 }
);
}

const data = (await response.json()) as InstallationResponse;

if ((data?.installations?.length ?? 0) > 0) {
return NextResponse.json({
data: {
installations: [],
owners: [],
},
});
}

const filteredInstallations = filterInstallations(data.installations);

const filteredOwners = filterOwners(
getOwnersFromInstallations(data.installations)
);

return NextResponse.json({
data: {
installations: filteredInstallations,
owners: filteredOwners,
},
// for debugging this can be nice
// _raw: data.installations,
});
} catch (error) {
console.error('Error fetching installations:', error);
return NextResponse.json(
{
error: 'Failed to fetch installations',
errorMessage: error instanceof Error ? error.message : 'Unknown error',
},
{ status: 500 }
);
}
return installations.handleRequest(request);
}
64 changes: 8 additions & 56 deletions apps/docs/app/api/code-storage/repo/route.ts
Original file line number Diff line number Diff line change
@@ -1,59 +1,11 @@
import { GitStorage } from '@pierre/storage';
import { type NextRequest, NextResponse } from 'next/server';
import { CodeStorageRepo } from '@pierre/storage-elements-next';
import { type NextRequest } from 'next/server';

export async function POST(request: NextRequest) {
try {
const store = new GitStorage({
name: 'pierre',
key: process.env.CODE_STORAGE_SYNC_PRIVATE_KEY ?? '',
});

const body = await request.json();
const { owner, name, defaultBranch } = body;

// eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
if (!owner || !name) {
return NextResponse.json(
{ success: false, error: 'Repository owner and name are required' },
{ status: 400 }
);
}

// TODO: Authenticate the user for this operation
const repo = new CodeStorageRepo({
storageName: 'pierre',
privateKey: process.env.CODE_STORAGE_SYNC_PRIVATE_KEY!,
});

const repo = await store.createRepo({
baseRepo: {
owner,
name,
// NOTE(amadeus): Given these types are `any`, not sure the safest way
// to convert fix them...
// eslint-disable-next-line @typescript-eslint/strict-boolean-expressions, @typescript-eslint/prefer-nullish-coalescing
defaultBranch: defaultBranch || 'main', // Optional, defaults to 'main'
},
// eslint-disable-next-line @typescript-eslint/strict-boolean-expressions, @typescript-eslint/prefer-nullish-coalescing
defaultBranch: defaultBranch || 'main', // Optional, defaults to 'main' for the Git Storage repo
});

const ciUrl = await repo.getRemoteURL({
permissions: ['git:read', 'git:write'],
ttl: 86400, // 24 hours
});

return NextResponse.json({
success: true,
url: ciUrl,
repository: {
owner,
name,
// eslint-disable-next-line @typescript-eslint/strict-boolean-expressions, @typescript-eslint/prefer-nullish-coalescing
defaultBranch: defaultBranch || 'main',
},
});
} catch (error) {
console.error('Error syncing storage:', error);
return NextResponse.json(
{ error: 'Failed to sync storage' },
{ status: 500 }
);
}
export async function POST(request: NextRequest) {
return repo.handlePostRequest(request);
}
21 changes: 21 additions & 0 deletions apps/docs/registry.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"select",
"tooltip"
],
"dependencies": ["@pierre/storage-elements-next"],
"files": [
{
"path": "registry/new-york/blocks/git-platform-sync/git-platform-sync.tsx",
Expand All @@ -37,6 +38,26 @@
{
"path": "registry/new-york/blocks/git-platform-sync/github/owners.ts",
"type": "registry:component"
},
{
"path": "registry/new-york/blocks/git-platform-sync/pages/success/page.tsx",
"type": "registry:page",
"target": "code-storage/success/page.tsx"
},
{
"path": "registry/new-york/blocks/git-platform-sync/api/github/callback/route.ts",
"type": "registry:page",
"target": "api/code-storage/github/callback/route.ts"
},
{
"path": "registry/new-york/blocks/git-platform-sync/api/github/installations/route.ts",
"type": "registry:page",
"target": "api/code-storage/github/installations/route.ts"
},
{
"path": "registry/new-york/blocks/git-platform-sync/api/repo/route.ts",
"type": "registry:page",
"target": "api/code-storage/repo/route.ts"
}
]
}
Expand Down
3 changes: 3 additions & 0 deletions bun.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions packages/storage-elements-next/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@pierre/storage-elements-next",
"version": "0.0.2",
"version": "0.0.3",
"type": "module",
"scripts": {
"tsc": "tsc --build",
Expand All @@ -17,7 +17,9 @@
"publishConfig": {
"access": "public"
},
"dependencies": {},
"dependencies": {
"@pierre/storage": "catalog:"
},
"peerDependencies": {
"next": ">=15"
},
Expand Down
Loading