Skip to content

Commit

Permalink
feat: add initial github api call scaffolding, and logstream for stre…
Browse files Browse the repository at this point in the history
…aming logs to the frontend
  • Loading branch information
xynydev committed Apr 14, 2024
1 parent 2238732 commit 77be8d6
Show file tree
Hide file tree
Showing 10 changed files with 79 additions and 15 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ pnpm tauri dev

### GitHub OAuth

For testing features requiring GitHub OAuth, create a `.env` file with the `GITHUB_CLIENT_ID` abd `GITHUB_CLIENT_SECRET` of your development OAuth app. The callback URL should be `http://localhost:5173/login/callback`.
For testing features requiring GitHub OAuth, create a `.env` file with the `GITHUB_CLIENT_ID` abd `GITHUB_CLIENT_SECRET` of your development OAuth app. The callback URL should be `http://localhost:5173/api/login/callback`.

## Building

Expand Down
Empty file removed src/lib/ts/github/.gitkeep
Empty file.
25 changes: 25 additions & 0 deletions src/lib/ts/misc/logStream.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
export async function createLogStream(
func: (logStream: ReadableStreamDefaultController<string>) => void
): Promise<Response> {
const logStream = new ReadableStream({
async start(logStream) {
await func(logStream);
}
});

return new Response(logStream, {
headers: {
"Content-Type": "text/event-stream"
}
});
}

export async function readLogStream(res: Response, onNewValue: (value: string) => void) {
const reader = res.body.pipeThrough(new TextDecoderStream()).getReader();
// eslint-disable-next-line no-constant-condition
while (true) {
const { value, done } = await reader.read();
if (done) break;
onNewValue(value);
}
}
Empty file removed src/lib/ui/.gitkeep
Empty file.
2 changes: 1 addition & 1 deletion src/routes/+layout.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

<div class="min-h-screen bg-background text-foreground">
<header class="flex flex-row border-b border-muted px-8 py-4">
<h1 class="place-self-center text-xl">BlueBuild Workshop</h1>
<h1 class="place-self-center text-xl"><a href="/">BlueBuild Workshop</a></h1>
<div class="ml-auto flex flex-row gap-2">
<Button
on:click={toggleMode}
Expand Down
2 changes: 1 addition & 1 deletion src/routes/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
My repositories
</h2>
{#if !data.githubUser.login}
<Button href="/login" size="lg" variant="default" class="mx-auto max-w-xl">
<Button href="/api/login" size="lg" variant="default" class="mx-auto max-w-xl">
Log in with GitHub</Button
>
{:else}
Expand Down
24 changes: 24 additions & 0 deletions src/routes/api/github/createRepo/+server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { createLogStream } from "$lib/ts/misc/logStream.js";

export async function POST({ request, cookies }): Promise<Response> {
return await createLogStream(async (logStream) => {
const token = cookies.get("github_oauth_token");
const { login, name } = await request.json();

logStream.enqueue("Creating repo: " + login + "/" + name);
const res = await fetch(`https://api.github.com/repos/blue-build/template/generate`, {
method: "post",
headers: { Authorization: `Bearer ${token}` },
body: JSON.stringify({
owner: login,
name: name
})
});
const repo = await res.json();
if (!res.ok) {
logStream.enqueue("Repo creation failed: " + JSON.stringify(repo));
} else {
logStream.enqueue("Repo created successfully!");
}
});
}
File renamed without changes.
File renamed without changes.
39 changes: 27 additions & 12 deletions src/routes/new/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,29 @@
import { Label } from "$lib/ui/components/ui/label";
import { Progress } from "$lib/ui/components/ui/progress";
import Log from "$lib/ui/components/newImage/log.svelte";
import { readLogStream } from "$lib/ts/misc/logStream.js";
type SetupStep = "start" | "inprogress" | "cosign" | "done" | "failed";
let setupStep = "start" as SetupStep;
let log: Array<string> = [];
export let data;
async function createRepo() {
setupStep = "inprogress";
const res = await fetch("/api/github/createRepo", {
method: "POST",
body: JSON.stringify({
name: document.getElementById("reponame").value,
login: data.githubUser.login
}),
headers: {
"content-type": "application/json"
}
});
readLogStream(res, (value) => {
log = [...log, value];
});
}
function generateCosignKeys() {
// @ts-ignore
Expand All @@ -25,11 +44,6 @@
}
);
}
// add to log something random every second
setInterval(testLog, 500);
function testLog() {
log = [...log, Math.random().toPrecision(10)];
}
</script>

<svelte:head>
Expand Down Expand Up @@ -59,14 +73,15 @@
<Label for="reponame">
Please enter a name for your new custom image repository:
</Label>
<Input id="reponame" type="text" placeholder="weird-os" class="font-mono" required
<Input
id="reponame"
name="reponame"
type="text"
placeholder="weird-os"
class="font-mono"
required
></Input>
<Button
on:click={() => (setupStep = "inprogress")}
size="lg"
variant="default"
class="mt-6"
>
<Button on:click={createRepo} size="lg" variant="default" class="mt-6">
Create repository
</Button>
{:else if setupStep === "inprogress"}
Expand Down

0 comments on commit 77be8d6

Please sign in to comment.