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
43 changes: 43 additions & 0 deletions apps/staged/src/lib/features/branches/branchCardHelpers.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { describe, expect, it } from 'vitest';
import { extractPrNumber, extractPrUrl } from './branchCardHelpers';

describe('extractPrUrl', () => {
it('returns the canonical GitHub PR URL from a PR_URL marker', () => {
expect(
extractPrUrl([
{
role: 'assistant',
content: 'PR_URL: https://github.com/block/builderbot/pull/123?expand=1',
},
])
).toBe('https://github.com/block/builderbot/pull/123');
});

it('ignores placeholder PR_URL markers that do not contain a real PR number', () => {
expect(
extractPrUrl([
{
role: 'assistant',
content: 'Return the required line exactly like this: PR_URL: https://github.com/...',
},
])
).toBeNull();
});

it('strips surrounding markdown punctuation from fallback URL matches', () => {
expect(
extractPrUrl([
{
role: 'assistant',
content: 'Created it successfully: <https://github.com/block/builderbot/pull/456>.',
},
])
).toBe('https://github.com/block/builderbot/pull/456');
});
});

describe('extractPrNumber', () => {
it('reads the PR number from a canonical URL', () => {
expect(extractPrNumber('https://github.com/block/builderbot/pull/456')).toBe(456);
});
});
24 changes: 19 additions & 5 deletions apps/staged/src/lib/features/branches/branchCardHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,19 +76,33 @@ export function formatBaseBranch(baseBranch: string): string {
return baseBranch.replace(/^origin\//, '');
}

function normalizePrUrl(candidate: string): string | null {
const trimmed = candidate.trim().replace(/^[<`'"\[(]+|[>`'"\]),.?!;:]+$/g, '');
const match = trimmed.match(
/^https:\/\/github\.com\/([A-Za-z0-9_.-]+)\/([A-Za-z0-9_.-]+)\/pull\/(\d+)(?:[/?#].*)?$/
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Parse GitHub host case-insensitively in PR URL normalization

normalizePrUrl currently accepts only lowercase https://github.com/..., so valid URLs like https://GitHub.com/org/repo/pull/123 are rejected and extractPrUrl can report no PR URL (or fall back to an unrelated one). Because hostnames are case-insensitive and the prior logic accepted these marker URLs, this introduces a regression in PR detection for some assistant/tool outputs.

Useful? React with 👍 / 👎.

);

if (!match) return null;

return `https://github.com/${match[1]}/${match[2]}/pull/${match[3]}`;
}

export function extractPrUrl(messages: { content: string; role: string }[]): string | null {
for (const msg of messages) {
if (msg.role !== 'assistant' && msg.role !== 'tool_result') continue;
const markerMatch = msg.content.match(/PR_URL:\s*(https?:\/\/\S+)/);
const markerMatch = msg.content.match(/PR_URL:\s*(\S+)/);
if (markerMatch) {
// Strip trailing markdown characters (*, ), ], etc.) from the URL
return markerMatch[1].replace(/[\*\)\]]+$/, '');
const normalized = normalizePrUrl(markerMatch[1]);
if (normalized) return normalized;
}
}

for (const msg of messages) {
const ghMatch = msg.content.match(/https:\/\/github\.com\/[^/]+\/[^/]+\/pull\/\d+/);
if (ghMatch) return ghMatch[0];
const urlMatches = msg.content.match(/https?:\/\/\S+/g) ?? [];
for (const url of urlMatches) {
const normalized = normalizePrUrl(url);
if (normalized) return normalized;
}
}

return null;
Expand Down