Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

fix(infra): append /app to posted feature url (#851)

teof87eb1dc8b16
diff --git a/js/app/scripts/preview/get-or-create-id.ts b/js/app/scripts/preview/get-or-create-id.ts
index 7d677214ce..3e668167b5 100644
--- a/js/app/scripts/preview/get-or-create-id.ts
+++ b/js/app/scripts/preview/get-or-create-id.ts
@@ -47,7 +47,7 @@ function parseArgs(): Args {
return result;
}
-function generatePreviewId(branchOverride?: string): string {
+export function generatePreviewId(branchOverride?: string): string {
let branch = branchOverride;
if (!branch) {
@@ -73,6 +73,13 @@ function generatePreviewId(branchOverride?: string): string {
return `${sanitized}-${nanoid}`;
}
+export const PREVIEW_URL_REGEX = /https:\/\/([a-z0-9-]+)\.preview\.macro\.com/;
+
+export function extractPreviewIdFromBody(body: string): string | null {
+ const match = body.match(PREVIEW_URL_REGEX);
+ return match?.[1] ?? null;
+}
+
async function getExistingPreviewId(pr: number, repo: string, token: string): Promise<string | null> {
const [owner, repoName] = repo.split('/');
@@ -98,10 +105,7 @@ async function getExistingPreviewId(pr: number, repo: string, token: string): Pr
);
if (previewComment?.body) {
- const match = previewComment.body.match(/https:\/\/([a-z0-9-]+)\.preview\.macro\.com/);
- if (match?.[1]) {
- return match[1];
- }
+ return extractPreviewIdFromBody(previewComment.body);
}
return null;
@@ -122,4 +126,6 @@ async function main() {
console.log(generatePreviewId(args.branch));
}
-main();
+if (import.meta.main) {
+ main();
+}
diff --git a/js/app/scripts/preview/post-comment.ts b/js/app/scripts/preview/post-comment.ts
index bc944d2090..f1d2f73745 100644
--- a/js/app/scripts/preview/post-comment.ts
+++ b/js/app/scripts/preview/post-comment.ts
@@ -52,10 +52,20 @@ function parseArgs(): Args {
return result as Args;
}
+export function buildPreviewUrl(previewId: string): string {
+ return `https://${previewId}.preview.macro.com/app`;
+}
+
+export function buildCommentBody(previewId: string, sha: string): string {
+ const previewUrl = buildPreviewUrl(previewId);
+ const shortSha = sha.slice(0, 7);
+ return `**Preview:** [${previewUrl}](${previewUrl}) (${shortSha})`;
+}
+
async function main() {
const args = parseArgs();
const [owner, repo] = args.repo.split('/');
- const previewUrl = `https://${args.previewId}.preview.macro.com`;
+ const previewUrl = buildPreviewUrl(args.previewId);
const shortSha = args.sha.slice(0, 7);
// Check for existing preview comment
@@ -133,4 +143,6 @@ async function main() {
console.log(`Preview URL: ${previewUrl}`);
}
-main();
+if (import.meta.main) {
+ main();
+}
diff --git a/js/app/scripts/preview/preview.test.ts b/js/app/scripts/preview/preview.test.ts
new file mode 100644
index 0000000000..e54bbd881c
--- /dev/null
+++ b/js/app/scripts/preview/preview.test.ts
@@ -0,0 +1,101 @@
+import { describe, expect, it } from 'vitest';
+import {
+ extractPreviewIdFromBody,
+ generatePreviewId,
+ PREVIEW_URL_REGEX,
+} from './get-or-create-id';
+import { buildCommentBody, buildPreviewUrl } from './post-comment';
+
+describe('buildPreviewUrl', () => {
+ it('appends /app to preview domain', () => {
+ expect(buildPreviewUrl('my-feature-abc123')).toBe(
+ 'https://my-feature-abc123.preview.macro.com/app'
+ );
+ });
+
+ it('handles simple preview ids', () => {
+ expect(buildPreviewUrl('test')).toBe('https://test.preview.macro.com/app');
+ });
+});
+
+describe('buildCommentBody', () => {
+ it('creates markdown link with /app suffix', () => {
+ const body = buildCommentBody('my-feature-abc123', 'abcdef1234567890');
+ expect(body).toBe(
+ '**Preview:** [https://my-feature-abc123.preview.macro.com/app](https://my-feature-abc123.preview.macro.com/app) (abcdef1)'
+ );
+ });
+
+ it('truncates sha to 7 characters', () => {
+ const body = buildCommentBody('test', '1234567890abcdef');
+ expect(body).toContain('(1234567)');
+ });
+});
+
+describe('extractPreviewIdFromBody', () => {
+ it('extracts preview id from url without path', () => {
+ const body = '**Preview:** [https://my-feature-abc123.preview.macro.com](...)';
+ expect(extractPreviewIdFromBody(body)).toBe('my-feature-abc123');
+ });
+
+ it('extracts preview id from url with /app path', () => {
+ const body =
+ '**Preview:** [https://my-feature-abc123.preview.macro.com/app](https://my-feature-abc123.preview.macro.com/app) (abcdef1)';
+ expect(extractPreviewIdFromBody(body)).toBe('my-feature-abc123');
+ });
+
+ it('returns null for non-matching body', () => {
+ expect(extractPreviewIdFromBody('no preview here')).toBe(null);
+ });
+
+ it('handles complex preview ids with numbers and hyphens', () => {
+ const body = 'https://feat-123-some-thing-xyz789.preview.macro.com/app';
+ expect(extractPreviewIdFromBody(body)).toBe('feat-123-some-thing-xyz789');
+ });
+});
+
+describe('PREVIEW_URL_REGEX', () => {
+ it('matches urls with /app suffix', () => {
+ const url = 'https://test-abc123.preview.macro.com/app';
+ expect(url.match(PREVIEW_URL_REGEX)?.[1]).toBe('test-abc123');
+ });
+
+ it('matches urls without path', () => {
+ const url = 'https://test-abc123.preview.macro.com';
+ expect(url.match(PREVIEW_URL_REGEX)?.[1]).toBe('test-abc123');
+ });
+});
+
+describe('generatePreviewId', () => {
+ it('sanitizes branch names', () => {
+ const id = generatePreviewId('Feature/MY-Branch_Name');
+ expect(id).toMatch(/^feature-my-branch-name-[a-z0-9]{6}$/);
+ });
+
+ it('truncates long branch names to 30 chars', () => {
+ const longBranch = 'this-is-a-very-long-branch-name-that-exceeds-thirty-chars';
+ const id = generatePreviewId(longBranch);
+ const prefixWithoutNanoid = id.slice(0, -7);
+ expect(prefixWithoutNanoid.length).toBeLessThanOrEqual(30);
+ });
+
+ it('falls back to git branch when override is empty', () => {
+ const id = generatePreviewId('');
+ expect(id).toMatch(/^[a-z0-9-]+-[a-z0-9]{6}$/);
+ });
+
+ it('removes leading and trailing hyphens', () => {
+ const id = generatePreviewId('--branch--');
+ expect(id).not.toMatch(/^-/);
+ expect(id.slice(0, -7)).not.toMatch(/-$/);
+ });
+});
+
+describe('roundtrip: build url then extract id', () => {
+ it('extracts same id from generated comment', () => {
+ const originalId = 'my-feature-abc123';
+ const body = buildCommentBody(originalId, 'deadbeef');
+ const extractedId = extractPreviewIdFromBody(body);
+ expect(extractedId).toBe(originalId);
+ });
+});
diff --git a/js/app/vitest.config.ts b/js/app/vitest.config.ts
index 7974096772..f58fc2d36b 100644
--- a/js/app/vitest.config.ts
+++ b/js/app/vitest.config.ts
@@ -31,6 +31,12 @@ export default defineConfig({
name: 'queries',
},
},
+ {
+ test: {
+ include: ['scripts/**/*.{test,spec}.{ts,tsx}'],
+ name: 'scripts',
+ },
+ },
],
},
});