Skip to content

Commit

Permalink
chore: full sha support (#287)
Browse files Browse the repository at this point in the history
  • Loading branch information
Aslemammad authored Nov 23, 2024
1 parent caa576b commit 2871238
Show file tree
Hide file tree
Showing 5 changed files with 4,406 additions and 3,505 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,46 @@ type Params = Omit<WorkflowData, "sha" | "ref"> & {

export default eventHandler(async (event) => {
const params = getRouterParams(event) as Params;
let [encodedPackageName, refOrSha] = params.packageAndRefOrSha.split("@");
let [encodedPackageName, longerRefOrSha] = params.packageAndRefOrSha.split("@");
const packageName = decodeURIComponent(encodedPackageName);
refOrSha = refOrSha.split('.tgz')[0] // yarn support
longerRefOrSha = longerRefOrSha.split('.tgz')[0] // yarn support
const isSha = isValidGitHash(longerRefOrSha);
const refOrSha = isSha ? abbreviateCommitHash(longerRefOrSha) : longerRefOrSha;

if (isValidGitHash(refOrSha)) {
refOrSha = abbreviateCommitHash(refOrSha);
}
let base = `${params.owner}:${params.repo}:${refOrSha}`;
let packageKey = `${base}:${packageName}`;

const base = `${params.owner}:${params.repo}:${refOrSha}`
const packageKey = `${base}:${packageName}`;
const cursorKey = base;

const packagesBucket = usePackagesBucket(event);
const downloadedAtBucket = useDownloadedAtBucket(event);
const cursorBucket = useCursorsBucket(event);

if (await cursorBucket.hasItem(cursorKey)) {
const currentCursor = (await cursorBucket.getItem(cursorKey))!;

sendRedirect(
event,
`/${params.owner}/${params.repo}/${packageName}@${currentCursor.sha}`,
);
return;
}

// longer sha support with precision
const binding = useBinding(event);
const { objects } = await binding.list({ prefix: `${usePackagesBucket.base}:${base}` })
for (const { key } of objects) {
// bucket:package:stackblitz-labs:pkg.pr.new:ded05e838c418096e5dd77a29101c8af9e73daea:playground-b
const trimmedKey = key.slice(usePackagesBucket.base.length + 1);
const [keySha, keyPackageName] = trimmedKey.split(":").slice(2);
if (keyPackageName !== packageName) continue;

if (keySha.startsWith(longerRefOrSha)) {
packageKey = trimmedKey;
break;
}
}

if (await packagesBucket.hasItem(packageKey)) {
const stream = await getItemStream(
event,
Expand All @@ -41,14 +65,6 @@ export default eventHandler(async (event) => {
setResponseHeader(event, "content-type", "application/tar+gzip");
// TODO: add HTTP caching
return stream;
} else if (await cursorBucket.hasItem(cursorKey)) {
const currentCursor = (await cursorBucket.getItem(cursorKey))!;

sendRedirect(
event,
`/${params.owner}/${params.repo}/${packageName}@${currentCursor.sha}`,
);
return;
}

throw createError({
Expand Down
5 changes: 2 additions & 3 deletions packages/backend/server/routes/multipart/create.post.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { joinKeys } from "unstorage";
import { abbreviateCommitHash, isWhitelisted } from "@pkg-pr-new/utils";
import { isWhitelisted } from "@pkg-pr-new/utils";

export default eventHandler(async (event) => {
const { "sb-key": workflowKey, "sb-name": packageName } = getHeaders(event);
Expand Down Expand Up @@ -37,8 +37,7 @@ export default eventHandler(async (event) => {

const binding = useBinding(event);

const abbreviatedSha = abbreviateCommitHash(workflowData.sha);
const base = `${workflowData.owner}:${workflowData.repo}:${abbreviatedSha}`
const base = `${workflowData.owner}:${workflowData.repo}:${workflowData.sha}`
const packageKey = `${base}:${packageName}`;

const key = joinKeys(usePackagesBucket.base, packageKey);
Expand Down
6 changes: 2 additions & 4 deletions packages/backend/server/routes/publish.post.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {
Comment,
abbreviateCommitHash,
isPullRequest,
isWhitelisted,
} from "@pkg-pr-new/utils";
Expand Down Expand Up @@ -89,7 +88,6 @@ export default eventHandler(async (event) => {
});
}

const abbreviatedSha = abbreviateCommitHash(workflowData.sha);
const baseKey = `${workflowData.owner}:${workflowData.repo}`;

const cursorKey = `${baseKey}:${workflowData.ref}`;
Expand All @@ -99,7 +97,7 @@ export default eventHandler(async (event) => {
await Promise.all(
packages.map(async (packageNameWithPrefix) => {
const packageName = packageNameWithPrefix.slice("package:".length);
const packageKey = `${baseKey}:${abbreviatedSha}:${packageName}`;
const packageKey = `${baseKey}:${workflowData.sha}:${packageName}`;

const file = formData.get(packageNameWithPrefix)!;
if (file instanceof File) {
Expand Down Expand Up @@ -158,7 +156,7 @@ export default eventHandler(async (event) => {

if (!currentCursor || currentCursor.timestamp < runId) {
await cursorBucket.setItem(cursorKey, {
sha: abbreviatedSha,
sha: workflowData.sha,
timestamp: runId,
});
}
Expand Down
5 changes: 2 additions & 3 deletions packages/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,6 @@ const main = defineCommand({
}

const { sha } = await checkResponse.json();
const abbreviatedSha = abbreviateCommitHash(sha);

const deps: Map<string, string> = new Map(); // pkg.pr.new versions of the package
const realDeps: Map<string, string> | null = isPeerDepsEnabled
Expand Down Expand Up @@ -189,7 +188,7 @@ const main = defineCommand({
}

const depUrl = new URL(
`/${owner}/${repo}/${pJson.name}@${abbreviatedSha}`,
`/${owner}/${repo}/${pJson.name}@${sha}`,
apiUrl,
).href;
deps.set(pJson.name, depUrl);
Expand All @@ -198,7 +197,7 @@ const main = defineCommand({
const resource = await fetch(depUrl);
if (resource.ok) {
console.warn(
`${pJson.name}@${abbreviatedSha} was already published on ${depUrl}`,
`${pJson.name}@${abbreviateCommitHash(sha)} was already published on ${depUrl}`,
);
}

Expand Down
Loading

0 comments on commit 2871238

Please sign in to comment.