Skip to content

Commit

Permalink
publish impl (not done yet)
Browse files Browse the repository at this point in the history
  • Loading branch information
Aslemammad committed Mar 23, 2024
1 parent 41cc807 commit 7078e5e
Show file tree
Hide file tree
Showing 9 changed files with 81 additions and 32 deletions.
24 changes: 11 additions & 13 deletions packages/backend/nitro.config.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { KVNamespace, R2Bucket } from '@cloudflare/workers-types';
import { R2Bucket } from '@cloudflare/workers-types';
import ncb from 'nitro-cloudflare-dev'

declare module "nitro-cloudflare-dev" {
interface Env {
PKGS: R2Bucket
WORKFLOWS: KVNamespace
BUCKET: R2Bucket
}
}

Expand All @@ -22,20 +21,19 @@ export default defineNitroConfig({
modules: [ncb],
srcDir: "server",
storage: {
'workflows': {
driver: 'cloudflare-kv-binding',
base: 'workflows',
binding: 'WORKFLOWS'
'bucket': {
driver: 'cloudflareR2Binding',
base: 'bucket',
binding: 'BUCKET'
},
},
devStorage: {
'workflows': {
driver: 'cloudflare-kv-binding',
base: 'workflows',
binding: 'WORKFLOWS'
}
'bucket': {
driver: 'cloudflareR2Binding',
base: 'bucket',
binding: 'BUCKET'
},
},

runtimeConfig: {
appId: "",
webhookSecret: "",
Expand Down
16 changes: 16 additions & 0 deletions packages/backend/server/routes/publish.post.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { WorkflowData } from "../types"

export default eventHandler(async (event) => {
const key = getRequestHeader(event, 'sb-key')
const {getItem, hasItem, removeItem} = useBucket()

// if (!await hasItem(key)) {
// return new Response("", {status: 401})
// }

const workflowData = JSON.parse(await getItem(key)) as WorkflowData
console.log(workflowData)
console.log(readRawBody(event))

// await removeItem(key)
})
13 changes: 10 additions & 3 deletions packages/backend/server/routes/webhook.post.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { HandlerFunction } from "@octokit/webhooks/dist-types/types";
import { objectHash, sha256 } from "ohash";
import { App } from "octokit";
import { useWorkflows } from "../../utils/workflows";
import { WorkflowData } from "../types";

export default eventHandler(async (event) => {
const { appId, privateKey, webhookSecret } = useRuntimeConfig(event);
Expand All @@ -12,7 +12,7 @@ export default eventHandler(async (event) => {
secret: webhookSecret,
},
});
const { setItem, removeItem } = useWorkflows();
const { setItem, removeItem } = useBucket();

const workflowHandler: HandlerFunction<"workflow_job", unknown> = async ({
payload,
Expand All @@ -24,8 +24,15 @@ export default eventHandler(async (event) => {
};
const key = sha256(objectHash(metadata));
if (payload.action === 'queued') {
const [orgOrAuthor, repo] = payload.repository.full_name.split('/')
const data: WorkflowData = {
orgOrAuthor,
repo,
sha: payload.workflow_job.head_sha,
branch: payload.workflow_job.head_branch
}
// Publishing is only available throughout the lifetime of a worklow_job
await setItem(key, {});
await setItem(key, JSON.stringify(data));
} else if (payload.action === 'completed') {
// Publishing is not available anymore
await removeItem(key);
Expand Down
6 changes: 6 additions & 0 deletions packages/backend/server/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export type WorkflowData = {
orgOrAuthor: string,
repo: string,
sha: string,
branch: string
}
7 changes: 7 additions & 0 deletions packages/backend/server/utils/bucket.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { R2Bucket } from "@cloudflare/workers-types";

type Binary = Parameters<R2Bucket["put"]>[1];

export function useBucket() {
return useStorage<Binary>("bucket");
}
7 changes: 0 additions & 7 deletions packages/backend/utils/workflows.ts

This file was deleted.

10 changes: 5 additions & 5 deletions packages/backend/wrangler.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ compatibility_date = "2024-03-14"
# [site]
# bucket = ".output/public"

kv_namespaces = [
{ binding = "WORKFLOWS", id = "Workflows" }
]
# kv_namespaces = [
# { binding = "WORKFLOWS", id = "Workflows" }
# ]

[[r2_buckets]]
binding = 'PKGS' # <~ valid JavaScript variable name
bucket_name = 'Packages'
binding = 'BUCKET' # <~ valid JavaScript variable name
bucket_name = 'Bucket'


27 changes: 23 additions & 4 deletions packages/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,23 @@ import {objectHash, sha256} from 'ohash'
import { Octokit } from "@octokit/action";
import "./environments";

declare global {
var API_URL: string
}

const publishUrl = new URL('/publish', API_URL)

if (process.env.GITHUB_ACTIONS !== 'true') {
console.error('Stackblitz Continuous Releases are only available in Github Actions.')
process.exit(1)
}

const {GITHUB_SERVER_URL, GITHUB_REPOSITORY, GITHUB_RUN_ID, GITHUB_RUN_ATTEMPT, GITHUB_ACTOR_ID} = process.env
const octokit = new Octokit();
// const octokit = new Octokit();
// const eventPayload = await import(process.env.GITHUB_EVENT_PATH, {
// with: { type: "json" },
// });
console.log(process.env)

// Note: If you need to use a workflow run's URL from within a job, you can combine these variables: $GITHUB_SERVER_URL/$GITHUB_REPOSITORY/actions/runs/$GITHUB_RUN_ID
const url = `${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}/actions/runs/${GITHUB_RUN_ID}`
Expand All @@ -27,8 +34,7 @@ const metadata = {

const key = sha256(objectHash(metadata))

console.log(key)
fetch('/',{body})


// console.log(octokit)
// console.log(eventPayload)
Expand All @@ -43,7 +49,20 @@ const main = defineCommand({
publish: () => {
return {
meta: {},
run: () => {},
run: async () => {
await fetch(publishUrl,{
method: "POST",
headers: {
'sb-key': key
},
body: new ReadableStream({
start(c) {
c.enqueue(new Uint8Array([1,2,3]))
c.close()
}
})
})
},
};
},
link: () => {
Expand Down
3 changes: 3 additions & 0 deletions packages/cli/tsup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ export default defineConfig({
minify: false,
splitting: false,
sourcemap: "inline",
define: {
API_URL: JSON.stringify(process.env.API_URL ?? 'https://localhost:3000')
},
clean: true,
bundle: true,
dts: false,
Expand Down

0 comments on commit 7078e5e

Please sign in to comment.