Skip to content

Beautiful, AI-native markdown IDE and LLM wiki

Require current-head CLA status on the merge path (#5120)

Nick Gomezf19d745ebba5
diff --git a/.github/scripts/bridge-public-pr-to-monorepo.mjs b/.github/scripts/bridge-public-pr-to-monorepo.mjs
index e85d30601..cc49b80e9 100644
--- a/.github/scripts/bridge-public-pr-to-monorepo.mjs
+++ b/.github/scripts/bridge-public-pr-to-monorepo.mjs
@@ -8,6 +8,8 @@ import { applyClaGate } from './cla-gate.mjs';
const OSS_SYNC_BOT_NAME = 'inkeep-oss-sync[bot]';
const OSS_SYNC_BOT_EMAIL = '274976938+inkeep-oss-sync[bot]@users.noreply.github.com';
+const MAX_COMMAND_ERROR_LENGTH = 4096;
+const MAX_COMMAND_LABEL_LENGTH = 256;
function sanitizeErrorMessage(value) {
if (typeof value !== 'string') return value;
@@ -15,6 +17,7 @@ function sanitizeErrorMessage(value) {
}
function run(command, args, options = {}) {
+ const { preserveOutput = false, ...execOptions } = options;
const {
GIT_DIR: _d,
GIT_WORK_TREE: _w,
@@ -25,34 +28,46 @@ function run(command, args, options = {}) {
GIT_NAMESPACE: _n,
GIT_PREFIX: _p,
...cleanEnv
- } = { ...process.env, ...options.env };
+ } = { ...process.env, ...execOptions.env };
try {
- return execFileSync(command, args, {
+ const output = execFileSync(command, args, {
encoding: 'utf8',
stdio: ['ignore', 'pipe', 'pipe'],
- ...options,
+ ...execOptions,
env: cleanEnv,
- }).trim();
+ });
+ return preserveOutput ? output : output.trim();
} catch (error) {
+ const invocation = sanitizeErrorMessage(`${command} ${args.join(' ')}`);
+ const commandLabel =
+ invocation.length > MAX_COMMAND_LABEL_LENGTH
+ ? `${invocation.slice(0, MAX_COMMAND_LABEL_LENGTH)}...`
+ : invocation;
+ if (error.code === 'ENOBUFS') {
+ const limit =
+ execOptions.maxBuffer === undefined
+ ? 'the configured limit'
+ : `${execOptions.maxBuffer} bytes`;
+ throw new Error(
+ `${commandLabel} exceeded the output buffer limit (${limit}).`.slice(
+ 0,
+ MAX_COMMAND_ERROR_LENGTH,
+ ),
+ );
+ }
const stderr = sanitizeErrorMessage(error.stderr?.toString().trim() ?? '');
const stdout = sanitizeErrorMessage(error.stdout?.toString().trim() ?? '');
- const details = [stderr, stdout].filter(Boolean).join('\n');
- const fallback = sanitizeErrorMessage(`${command} ${args.join(' ')} failed`);
- throw new Error(details || fallback);
+ const details =
+ [stderr, stdout].filter(Boolean).join('\n') || sanitizeErrorMessage(error.message);
+ throw new Error(`${commandLabel} failed: ${details}`.slice(0, MAX_COMMAND_ERROR_LENGTH));
}
}
-async function githubRequest({
- token,
- method = 'GET',
- path: requestPath,
- body,
- accept = 'application/vnd.github+json',
-}) {
+async function githubRequest({ token, method = 'GET', path: requestPath, body }) {
const response = await fetch(`https://api.github.com${requestPath}`, {
method,
headers: {
- Accept: accept,
+ Accept: 'application/vnd.github+json',
Authorization: `Bearer ${token}`,
'User-Agent': 'inkeep-public-pr-bridge',
...(body ? { 'Content-Type': 'application/json' } : {}),
@@ -67,9 +82,7 @@ async function githubRequest({
throw error;
}
- const isTextResponse =
- accept === 'application/vnd.github.patch' || accept === 'application/vnd.github.diff';
- return isTextResponse ? text : text ? JSON.parse(text) : null;
+ return text ? JSON.parse(text) : null;
}
async function githubGraphql({ token, query, variables }) {
@@ -110,52 +123,48 @@ function parseJsonEnv(name, fallback) {
}
}
-function isDiffTooLargeError(error) {
- if (!error || typeof error.message !== 'string') return false;
- return /diff exceeded the maximum number of lines|diff is too large|diff_too_large/i.test(
- error.message,
- );
-}
-
-function fetchPullRequestDiffViaLocalGit({ internalRepoDir, sourceBaseRef, sourceHeadRef }) {
- return run('git', ['-C', internalRepoDir, 'diff', `${sourceBaseRef}...${sourceHeadRef}`], {
- maxBuffer: 50 * 1024 * 1024,
- });
+function hasSourceCommit(internalRepoDir, sha) {
+ try {
+ return run('git', ['-C', internalRepoDir, 'cat-file', '-t', sha]) === 'commit';
+ } catch {
+ return false;
+ }
}
-async function fetchPullRequestDiff({
- publicToken,
- publicRepo,
- publicPr,
- internalRepoDir,
- sourceBaseRef,
- sourceHeadRef,
- refsFetched,
-}) {
+function fetchSourceCommit({ internalRepoDir, remoteUrl, sha, role }) {
+ if (!/^(?:[0-9a-f]{40}|[0-9a-f]{64})$/.test(sha)) {
+ throw new Error(`Bridge: recorded public ${role} commit has an invalid SHA.`);
+ }
+ if (hasSourceCommit(internalRepoDir, sha)) return;
try {
- return await githubRequest({
- token: publicToken,
- path: `/repos/${publicRepo}/pulls/${publicPr.number}`,
- accept: 'application/vnd.github.diff',
- });
+ run('git', ['-C', internalRepoDir, 'fetch', '--no-tags', remoteUrl, sha]);
} catch (error) {
- if (!isDiffTooLargeError(error)) throw error;
- if (!refsFetched) {
- throw new Error(
- `Bridge: cannot use local-git-diff fallback for PR #${publicPr.number} — ` +
- `the public PR refs failed to fetch into agents-private earlier in this run. ` +
- `See the preceding "Bridge: fetch at --depth=..." warning for the original ` +
- `fetch failure; resolve that and re-run.`,
- );
- }
- console.log(
- `Bridge: GitHub diff API rejected PR #${publicPr.number} as too large; ` +
- 'falling back to local git diff against fetched public PR refs.',
+ throw new Error(
+ `Bridge: recorded public ${role} commit ${sha} is unavailable: ${error.message}`,
);
- return fetchPullRequestDiffViaLocalGit({ internalRepoDir, sourceBaseRef, sourceHeadRef });
+ }
+ if (!hasSourceCommit(internalRepoDir, sha)) {
+ throw new Error(`Bridge: recorded public ${role} commit ${sha} is unavailable.`);
}
}
+function diffRecordedPublicCommits(internalRepoDir, baseSha, headSha) {
+ return run(
+ 'git',
+ [
+ '-C',
+ internalRepoDir,
+ 'diff',
+ '--no-ext-diff',
+ '--no-textconv',
+ '--binary',
+ '--full-index',
+ `${baseSha}...${headSha}`,
+ ],
+ { maxBuffer: 50 * 1024 * 1024, preserveOutput: true },
+ );
+}
+
function filterDiffByPath(patch, excludedPrefixes) {
if (!excludedPrefixes || excludedPrefixes.length === 0) return patch;
@@ -277,11 +286,13 @@ function bridgeCommitSubject({ publicRepo, publicPr, hasConflicts }) {
].join('');
}
-function buildBridgeMetadata(publicPr, mirrorPath) {
+function buildBridgeMetadata(publicPr, mirrorPath, internalHeadSha) {
return [
'<!-- public-pr-sync',
`public_repo=${publicPr.base.repo.full_name}`,
`public_pr_number=${publicPr.number}`,
+ `public_head_sha=${publicPr.head.sha}`,
+ `internal_head_sha=${internalHeadSha ?? ''}`,
`public_pr_url=${publicPr.html_url}`,
`public_author_login=${publicPr.user.login}`,
`public_author_id=${publicPr.user.id}`,
@@ -290,6 +301,43 @@ function buildBridgeMetadata(publicPr, mirrorPath) {
].join('\n');
}
+function readImportMetadata(internalPr) {
+ const marker = [...(internalPr.body ?? '').matchAll(/<!-- public-pr-sync\n([\s\S]*?)\n-->/g)].at(
+ -1,
+ );
+ if (!marker) return null;
+ return Object.fromEntries(
+ marker[1].split('\n').map((line) => {
+ const separator = line.indexOf('=');
+ return [line.slice(0, separator), line.slice(separator + 1)];
+ }),
+ );
+}
+
+function importedHeadMatches({ internalPr, publicPr, publicRepo, branchName }) {
+ const metadata = readImportMetadata(internalPr);
+ return (
+ internalPr.state === 'open' &&
+ internalPr.head.ref === branchName &&
+ metadata?.public_repo === publicRepo &&
+ metadata?.public_pr_number === String(publicPr.number) &&
+ metadata?.public_head_sha === publicPr.head.sha &&
+ metadata?.internal_head_sha === internalPr.head.sha
+ );
+}
+
+function legacyImportNeedsRebuild({ internalPr, publicPr, publicRepo, branchName }) {
+ const metadata = readImportMetadata(internalPr);
+ return (
+ internalPr.state === 'open' &&
+ internalPr.head.ref === branchName &&
+ metadata?.public_repo === publicRepo &&
+ metadata?.public_pr_number === String(publicPr.number) &&
+ !Object.hasOwn(metadata, 'public_head_sha') &&
+ !Object.hasOwn(metadata, 'internal_head_sha')
+ );
+}
+
function publicPrAuthor(publicPr) {
return {
name: publicPr.user.login,
@@ -415,7 +463,7 @@ function buildCommitAttribution({ commitAuthors, commitMessages = [], publicRepo
const GITHUB_PR_BODY_LIMIT = 65536;
-function buildInternalPrBody({ publicPr, branchName, mirrorPath }) {
+function buildInternalPrBody({ publicPr, branchName, mirrorPath, internalHeadSha }) {
const rawOriginal = publicPr.body?.trim()
? publicPr.body.trim()
: '_No public PR body was provided._';
@@ -443,7 +491,7 @@ ${original}
- To accept the contribution, merge this monorepo PR. The change will sync back to the public repo automatically, and the public PR will close automatically.
- To make edits or updates to these changes, they should be made directly to the public PR. Contributor updates there will sync back into this monorepo PR.
-${buildBridgeMetadata(publicPr, mirrorPath)}`;
+${buildBridgeMetadata(publicPr, mirrorPath, internalHeadSha)}`;
let body = compose(rawOriginal);
if (body.length > GITHUB_PR_BODY_LIMIT) {
@@ -582,6 +630,98 @@ function createClaGateGh({
};
}
+async function refreshPublicPrCla() {
+ if (requireEnv('PUBLIC_STATUS_CONTEXT') !== 'license/cla') {
+ console.log('Bridge: skipped CLA refresh for an unrelated public status.');
+ return;
+ }
+ const sha = requireEnv('PUBLIC_STATUS_SHA');
+ const publicToken = requireEnv('PUBLIC_TOKEN');
+ const internalToken = requireEnv('INTERNAL_TOKEN');
+ const publicRepo = requireEnv('PUBLIC_REPO');
+ const internalRepo = requireEnv('INTERNAL_REPO');
+ const internalBranchPrefix = requireEnv('INTERNAL_BRANCH_PREFIX');
+ const owner = internalRepo.split('/')[0];
+ let matchedPublicPrs = 0;
+ for (let page = 1; ; page += 1) {
+ const openPrs = await githubRequest({
+ token: publicToken,
+ path: `/repos/${publicRepo}/pulls?state=open&per_page=100&page=${page}`,
+ });
+ for (const candidate of openPrs) {
+ if (candidate.head?.sha !== sha) continue;
+ matchedPublicPrs += 1;
+ const publicPr = await githubRequest({
+ token: publicToken,
+ path: `/repos/${publicRepo}/pulls/${candidate.number}`,
+ });
+ const publicHead = `public PR #${publicPr.number} head ${sha}`;
+ if (
+ publicPr.state !== 'open' ||
+ publicPr.head.sha !== sha ||
+ publicPr.base.repo.full_name !== publicRepo ||
+ (publicPr.head.repo?.full_name === publicRepo && publicPr.head.ref === 'copybara/sync')
+ ) {
+ console.log(`Bridge: skipped ${publicHead}; public PR is no longer eligible.`);
+ continue;
+ }
+ const branchName = getPublicPrBranchName(internalBranchPrefix, publicPr.number);
+ const linkedPr = await findOpenInternalPr({
+ token: internalToken,
+ repo: internalRepo,
+ owner,
+ branchName,
+ });
+ if (!linkedPr) {
+ console.log(`Bridge: skipped ${publicHead}; no linked import is open.`);
+ continue;
+ }
+ const internalPr = linkedPr;
+ if (!importedHeadMatches({ internalPr, publicPr, publicRepo, branchName })) {
+ console.log(`Bridge: skipped ${publicHead}; recorded import does not match.`);
+ continue;
+ }
+ const headCommit = await githubRequest({
+ token: internalToken,
+ path: `/repos/${internalRepo}/commits/${internalPr.head.sha}`,
+ });
+ const currentPublicPr = await githubRequest({
+ token: publicToken,
+ path: `/repos/${publicRepo}/pulls/${publicPr.number}`,
+ });
+ const currentInternalPr = await githubRequest({
+ token: internalToken,
+ path: `/repos/${internalRepo}/pulls/${internalPr.number}`,
+ });
+ if (
+ currentPublicPr.state !== 'open' ||
+ currentPublicPr.head.sha !== sha ||
+ currentInternalPr.head.sha !== internalPr.head.sha ||
+ !importedHeadMatches({
+ internalPr: currentInternalPr,
+ publicPr: currentPublicPr,
+ publicRepo,
+ branchName,
+ })
+ ) {
+ console.log(`Bridge: skipped ${publicHead}; public PR or import changed during refresh.`);
+ continue;
+ }
+ await applyClaGate({
+ gh: createClaGateGh({ publicToken, publicRepo, internalToken, internalRepo }),
+ publicPr: currentPublicPr,
+ internalPr: currentInternalPr,
+ forceDraft: commitIndicatesConflicts(headCommit.commit.message),
+ });
+ console.log(`Bridge: refreshed CLA for ${publicHead}.`);
+ }
+ if (openPrs.length < 100) break;
+ }
+ if (matchedPublicPrs === 0) {
+ console.log(`Bridge: no open public PR has head ${sha}.`);
+ }
+}
+
const CONFLICT_COMMIT_MARKER = 'with conflicts; needs manual resolution';
function commitIndicatesConflicts(commitMessage) {
@@ -643,11 +783,21 @@ async function syncPublicPr() {
branchName,
});
- const metadataOnlyAction =
+ const metadataAction =
+ publicPrAction === 'edited' ||
+ publicPrAction === 'ready_for_review' ||
+ publicPrAction === 'converted_to_draft';
+ const legacyNeedsRebuild =
internalPr &&
- (publicPrAction === 'edited' ||
- publicPrAction === 'ready_for_review' ||
- publicPrAction === 'converted_to_draft');
+ metadataAction &&
+ legacyImportNeedsRebuild({ internalPr, publicPr, publicRepo, branchName });
+ const metadataOnlyAction = internalPr && metadataAction && !legacyNeedsRebuild;
+
+ if (legacyNeedsRebuild) {
+ console.log(
+ `Bridge: public PR #${publicPr.number} head ${publicPr.head.sha} has a legacy import; rebuilding after approval.`,
+ );
+ }
let hasStagedChanges = false;
let hasConflicts = false;
@@ -659,57 +809,53 @@ async function syncPublicPr() {
const sourceBaseRef = `refs/remotes/${sourceRemote}/pr-base`;
const sourceHeadRef = `refs/remotes/${sourceRemote}/pr-head`;
const publicRepoUrl = `https://x-access-token:${publicToken}@github.com/${publicRepo}.git`;
+ const publicHeadRepo = publicPr.head.repo?.full_name ?? publicRepo;
+ const publicHeadRepoUrl = `https://x-access-token:${publicToken}@github.com/${publicHeadRepo}.git`;
try {
run('git', ['-C', internalRepoDir, 'remote', 'remove', sourceRemote]);
- } catch {
- }
+ } catch {}
run('git', ['-C', internalRepoDir, 'remote', 'add', sourceRemote, publicRepoUrl]);
try {
- let refsFetched = false;
- for (const depth of [10000, 50000]) {
- try {
- run('git', [
- '-C',
- internalRepoDir,
- 'fetch',
- '--no-tags',
- `--depth=${depth}`,
- sourceRemote,
- `+refs/pull/${publicPrNumber}/head:${sourceHeadRef}`,
- `+refs/heads/${publicPr.base.ref}:${sourceBaseRef}`,
- ]);
- refsFetched = true;
- break;
- } catch (error) {
- console.log(
- `Bridge: fetch at --depth=${depth} failed: ${error.message}. ` +
- `Retrying with deeper history if available.`,
- );
- }
- }
- if (!refsFetched) {
- console.log(
- 'Bridge: warning: could not fetch public PR refs into agents-private at any depth. ' +
- "Continuing — `git apply --3way` will still succeed if the public mirror's blobs already match agents-private/main, " +
- 'but the local-git-diff fallback for oversized PRs will not be available.',
- );
+ try {
+ run('git', [
+ '-C',
+ internalRepoDir,
+ 'fetch',
+ '--no-tags',
+ sourceRemote,
+ `+refs/pull/${publicPrNumber}/head:${sourceHeadRef}`,
+ `+refs/heads/${publicPr.base.ref}:${sourceBaseRef}`,
+ ]);
+ } catch (error) {
+ console.warn(`Bridge: public PR refs were unavailable: ${error.message}`);
}
- const rawPatch = await fetchPullRequestDiff({
- publicToken,
- publicRepo,
- publicPr,
+ fetchSourceCommit({
+ internalRepoDir,
+ remoteUrl: publicRepoUrl,
+ sha: publicPr.base.sha,
+ role: 'base',
+ });
+ fetchSourceCommit({
internalRepoDir,
- sourceBaseRef,
- sourceHeadRef,
- refsFetched,
+ remoteUrl: publicHeadRepoUrl,
+ sha: publicPr.head.sha,
+ role: 'head',
});
+ const rawPatch = diffRecordedPublicCommits(
+ internalRepoDir,
+ publicPr.base.sha,
+ publicPr.head.sha,
+ );
const excludedPrefixes = parseJsonEnv('BRIDGE_EXCLUDED_PATHS', []);
const patch = filterDiffByPath(rawPatch, excludedPrefixes);
if (!patch.trim()) {
+ console.log(
+ `Bridge: public PR #${publicPr.number} head ${publicPr.head.sha} has no importable diff; no new import recorded.`,
+ );
return;
}
@@ -788,8 +934,7 @@ async function syncPublicPr() {
} finally {
try {
run('git', ['-C', internalRepoDir, 'remote', 'remove', sourceRemote]);
- } catch {
- }
+ } catch {}
}
internalPr = await findOpenInternalPr({
@@ -800,10 +945,27 @@ async function syncPublicPr() {
});
if (!internalPr && !hasStagedChanges) {
+ console.log(
+ `Bridge: public PR #${publicPr.number} head ${publicPr.head.sha} produced no staged change; no import recorded.`,
+ );
return;
}
}
+ if (
+ !hasStagedChanges &&
+ internalPr &&
+ !importedHeadMatches({ internalPr, publicPr, publicRepo, branchName })
+ ) {
+ const reason = metadataOnlyAction
+ ? 'recorded import does not match the current heads'
+ : 'produced no staged change and has no matching import';
+ console.log(
+ `Bridge: public PR #${publicPr.number} head ${publicPr.head.sha} ${reason}; no new receipt recorded.`,
+ );
+ return;
+ }
+
if (metadataOnlyAction && internalPr) {
const headCommit = await githubRequest({
token: internalToken,
@@ -817,8 +979,38 @@ async function syncPublicPr() {
hasConflicts = commitIndicatesConflicts(headCommit?.commit?.message);
}
- const title = internalPullRequestTitle(publicPr);
- const body = buildInternalPrBody({ publicPr, branchName, mirrorPath });
+ const internalHeadSha = hasStagedChanges
+ ? run('git', ['-C', internalRepoDir, 'rev-parse', 'HEAD'])
+ : internalPr.head.sha;
+ const currentPublicPr = await githubRequest({
+ token: publicToken,
+ path: `/repos/${publicRepo}/pulls/${publicPr.number}`,
+ });
+ if (currentPublicPr.state !== 'open' || currentPublicPr.head.sha !== publicPr.head.sha) {
+ console.log(
+ `Bridge: public PR #${publicPr.number} head ${publicPr.head.sha} changed during sync; no matching receipt recorded.`,
+ );
+ if (hasStagedChanges) {
+ await createClaGateGh({
+ publicToken,
+ publicRepo,
+ internalToken,
+ internalRepo,
+ }).setVerifiedStatus(
+ { head: { sha: internalHeadSha } },
+ 'failure',
+ 'Public head changed during sync; awaiting the next approved sync.',
+ );
+ }
+ return;
+ }
+ const title = internalPullRequestTitle(currentPublicPr);
+ const body = buildInternalPrBody({
+ publicPr: currentPublicPr,
+ branchName,
+ mirrorPath,
+ internalHeadSha,
+ });
if (internalPr) {
internalPr = await githubRequest({
@@ -837,17 +1029,22 @@ async function syncPublicPr() {
head: branchName,
base: internalBaseRef,
body,
- draft: publicPr.draft,
+ draft: currentPublicPr.draft,
},
});
}
+ internalPr.head.sha = internalHeadSha;
+
await applyClaGate({
gh: createClaGateGh({ publicToken, publicRepo, internalToken, internalRepo }),
- publicPr,
+ publicPr: currentPublicPr,
internalPr,
forceDraft: hasConflicts,
});
+ console.log(
+ `Bridge: applied CLA gate for public PR #${publicPr.number} head ${publicPr.head.sha}.`,
+ );
}
async function closeLinkedInternalPr() {
@@ -902,8 +1099,7 @@ async function closeLinkedInternalPr() {
method: 'DELETE',
path: `/repos/${internalRepo}/git/refs/heads/${branchName}`,
});
- } catch {
- }
+ } catch {}
}
async function main() {
@@ -918,6 +1114,11 @@ async function main() {
return;
}
+ if (mode === 'refresh-cla') {
+ await refreshPublicPrCla();
+ return;
+ }
+
if (mode === 'close') {
await closeLinkedInternalPr();
return;
@@ -947,5 +1148,6 @@ export {
postCommitStatus,
prefixPatchPaths,
readCommitClaStatus,
+ refreshPublicPrCla,
syncPublicPr,
};
diff --git a/.github/scripts/bridge-public-pr-to-monorepo.test.mjs b/.github/scripts/bridge-public-pr-to-monorepo.test.mjs
index 62d91be21..a44d37f52 100644
--- a/.github/scripts/bridge-public-pr-to-monorepo.test.mjs
+++ b/.github/scripts/bridge-public-pr-to-monorepo.test.mjs
@@ -1,9 +1,10 @@
import { execFileSync } from 'node:child_process';
-import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs';
+import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
import { tmpdir } from 'node:os';
import path from 'node:path';
import { describe, expect, test } from 'vitest';
import { gitCleanEnv } from '../../scripts/git-clean-env.mjs';
+import * as bridge from './bridge-public-pr-to-monorepo.mjs';
import {
applyPatchWithConflictDetection,
bridgeCommitSubject,
@@ -494,13 +495,66 @@ const CONFLICT_HEAD =
'sync(oss): mirror inkeep/open-knowledge#310 (with conflicts; needs manual resolution)';
const CLEAN_HEAD = 'sync(oss): mirror inkeep/open-knowledge#310';
-async function runMetadataSync({ headCommitMessage, internalPrStartsDraft }) {
- const recorded = { draftMutation: null, comment: null };
+async function runBridgeSync({
+ headCommitMessage = CLEAN_HEAD,
+ internalPrStartsDraft = false,
+ internalRepoDir = '/tmp/unused-on-metadata-path',
+ action = 'edited',
+ claStatus = 'success',
+ existingPr = true,
+ readHead = () => 'internal-head-sha',
+ patchHead = readHead,
+ mode = 'sync',
+ publicHead = 'public-head-sha',
+ publicBase = 'public-base-sha',
+ publicHeadOnReread = publicHead,
+ changedPublicHeadAfterCommit = false,
+ changedInternalHeadAfterCommit = false,
+ importedPublicHead = 'public-head-sha',
+ importedRepo = 'inkeep/open-knowledge',
+ importedPrNumber = 310,
+ importedInternalHead = readHead(),
+ eventSha = 'public-head-sha',
+ eventContext = 'license/cla',
+ eventState = 'success',
+ publicState = 'open',
+ internalState = 'open',
+ hasMarker = true,
+ markerHeadFields = 'current',
+ earlierMarker = '',
+ headRef = 'contribution',
+ headRepo = 'octocat/open-knowledge',
+ internalHeadRef = 'public-pr/open-knowledge-310',
+ associations = [{ number: 310 }],
+ publicCandidates,
+}) {
+ const recorded = {
+ draftMutation: null,
+ comment: null,
+ statuses: [],
+ validationRequests: [],
+ internalBodies: [],
+ };
+ const marker = [
+ '<!-- public-pr-sync',
+ `public_repo=${importedRepo}`,
+ `public_pr_number=${importedPrNumber}`,
+ ...(markerHeadFields === 'current' || markerHeadFields === 'partial'
+ ? [`public_head_sha=${importedPublicHead}`]
+ : []),
+ ...(markerHeadFields === 'current' || markerHeadFields === 'internal-only'
+ ? [`internal_head_sha=${importedInternalHead}`]
+ : []),
+ '-->',
+ ].join('\n');
const internalPr = {
number: 42,
node_id: 'PR_node_42',
draft: internalPrStartsDraft,
- head: { sha: 'internal-head-sha' },
+ head: { sha: readHead(), ref: internalHeadRef, repo: { full_name: 'inkeep/agents-private' } },
+ state: internalState,
+ base: { ref: 'main' },
+ body: earlierMarker + (hasMarker ? marker : ''),
html_url: 'https://github.com/inkeep/agents-private/pull/42',
};
const publicPr = {
@@ -509,8 +563,9 @@ async function runMetadataSync({ headCommitMessage, internalPrStartsDraft }) {
body: 'body',
html_url: 'https://github.com/inkeep/open-knowledge/pull/310',
user: { login: 'octocat', id: 99 },
- base: { ref: 'main', repo: { full_name: 'inkeep/open-knowledge' } },
- head: { label: 'octocat:branch', sha: 'public-head-sha' },
+ base: { ref: 'main', sha: publicBase, repo: { full_name: 'inkeep/open-knowledge' } },
+ head: { label: 'octocat:branch', sha: publicHead, ref: headRef, repo: { full_name: headRepo } },
+ state: publicState,
draft: false,
};
const json = (obj, status = 200) => ({
@@ -519,23 +574,96 @@ async function runMetadataSync({ headCommitMessage, internalPrStartsDraft }) {
text: async () => JSON.stringify(obj),
});
+ let publicReads = 0;
+ let internalHeadOverride;
+ const apiHead = () => internalHeadOverride ?? readHead();
+ const candidatePrs = publicCandidates ?? [{ number: 310, head: { sha: publicHead } }];
const realFetch = globalThis.fetch;
globalThis.fetch = async (url, init = {}) => {
const method = init.method || 'GET';
- if (method === 'GET' && url.includes('/repos/inkeep/open-knowledge/pulls/310'))
- return json(publicPr);
- if (method === 'GET' && url.includes('/pulls?state=open')) return json([internalPr]);
+ if (method === 'GET' && url.includes('/pulls/310/commits?')) return json([]);
+ if (method === 'GET' && /\/commits\/[^/]+\/pulls\?/.test(url)) {
+ const page = Number(new URL(url).searchParams.get('page') ?? 1);
+ return json(associations.slice((page - 1) * 100, page * 100));
+ }
+ if (method === 'GET' && url.includes('/repos/inkeep/agents-private/pulls/42'))
+ return json({ ...internalPr, head: { ...internalPr.head, sha: apiHead() } });
+ if (method === 'GET' && url.includes('/repos/inkeep/open-knowledge/pulls/311'))
+ return json({ ...publicPr, number: 311, head: { ...publicPr.head, sha: 'another-head' } });
+ if (method === 'GET' && url.includes('/repos/inkeep/open-knowledge/pulls/310')) {
+ publicReads += 1;
+ return json({
+ ...publicPr,
+ head: {
+ ...publicPr.head,
+ sha:
+ publicReads > 1
+ ? changedPublicHeadAfterCommit
+ ? publicPr.head.sha
+ : publicHeadOnReread
+ : publicPr.head.sha,
+ },
+ });
+ }
+ if (method === 'GET' && url.includes('/repos/inkeep/open-knowledge/pulls?state=open')) {
+ const page = Number(new URL(url).searchParams.get('page') ?? 1);
+ return json(candidatePrs.slice((page - 1) * 100, page * 100));
+ }
+ if (method === 'GET' && url.includes('/pulls?state=open'))
+ return json(
+ existingPr ? [{ ...internalPr, head: { ...internalPr.head, sha: readHead() } }] : [],
+ );
if (
method === 'GET' &&
url.includes('/commits/internal-head-sha') &&
!url.includes('/status')
) {
+ if (changedPublicHeadAfterCommit) publicPr.head.sha = 'new-public-head';
+ if (changedInternalHeadAfterCommit) {
+ internalHeadOverride = 'new-internal-head';
+ internalPr.body = internalPr.body.replace(
+ `internal_head_sha=${importedInternalHead}`,
+ `internal_head_sha=${internalHeadOverride}`,
+ );
+ }
return json({ commit: { message: headCommitMessage } });
}
- if (method === 'PATCH' && url.includes('/pulls/42')) return json(internalPr);
+ if (method === 'PATCH' && url.includes('/pulls/42')) {
+ const update = JSON.parse(init.body);
+ recorded.internalBodies.push(update.body);
+ return json({ ...internalPr, ...update, head: { ...internalPr.head, sha: patchHead() } });
+ }
+ if (method === 'POST' && url.endsWith('/repos/inkeep/agents-private/pulls')) {
+ const update = JSON.parse(init.body);
+ recorded.internalBodies.push(update.body);
+ return json({
+ ...internalPr,
+ body: update.body,
+ head: { ...internalPr.head, sha: patchHead() },
+ });
+ }
if (method === 'GET' && url.includes('/orgs/')) return json({ message: 'Not Found' }, 404);
- if (method === 'GET' && url.includes('/commits/public-head-sha/status')) {
- return json({ statuses: [{ context: 'license/cla', state: 'success' }] });
+ if (method === 'GET' && url.includes(`/commits/${publicHead}/status`)) {
+ return json({ statuses: [{ context: 'license/cla', state: claStatus }] });
+ }
+ if (method === 'GET' && url.includes('/actions/workflows/')) {
+ const requestUrl = new URL(url);
+ recorded.validationRequests.push({
+ method,
+ path: requestUrl.pathname,
+ authorization: init.headers.Authorization,
+ query: Object.fromEntries(requestUrl.searchParams),
+ });
+ return json({ workflow_runs: [] });
+ }
+ if (method === 'POST' && url.includes('/actions/runs/')) {
+ recorded.validationRequests.push({
+ method,
+ path: new URL(url).pathname,
+ authorization: init.headers.Authorization,
+ verifiedStatus: recorded.statuses.at(-1),
+ });
+ return json(null, 201);
}
if (method === 'POST' && url.endsWith('/graphql')) {
const q = JSON.parse(init.body).query;
@@ -546,7 +674,13 @@ async function runMetadataSync({ headCommitMessage, internalPrStartsDraft }) {
: 'unknown';
return json({ data: {} });
}
- if (method === 'POST' && url.includes('/statuses/')) return json({});
+ if (method === 'POST' && url.includes('/statuses/')) {
+ recorded.statuses.push({
+ sha: new URL(url).pathname.split('/').at(-1),
+ ...JSON.parse(init.body),
+ });
+ return json({});
+ }
if (method === 'POST' && url.includes('/issues/310/comments')) {
recorded.comment = JSON.parse(init.body).body;
return json({ html_url: 'https://github.com/x/comments/1' });
@@ -559,18 +693,23 @@ async function runMetadataSync({ headCommitMessage, internalPrStartsDraft }) {
INTERNAL_TOKEN: 'int',
PUBLIC_REPO: 'inkeep/open-knowledge',
INTERNAL_REPO: 'inkeep/agents-private',
- INTERNAL_REPO_DIR: '/tmp/unused-on-metadata-path',
+ INTERNAL_REPO_DIR: internalRepoDir,
MONOREPO_PATH_PREFIX: 'public/open-knowledge',
INTERNAL_BASE_REF: 'main',
INTERNAL_BRANCH_PREFIX: 'public-pr/open-knowledge',
PUBLIC_PR_NUMBER: '310',
- PUBLIC_PR_ACTION: 'edited',
+ PUBLIC_PR_ACTION: action,
+ PUBLIC_STATUS_SHA: eventSha,
+ PUBLIC_STATUS_CONTEXT: eventContext,
+ PUBLIC_STATUS_STATE: eventState,
+ GIT_ALLOW_PROTOCOL: 'file',
};
const saved = {};
for (const k of Object.keys(setKeys)) saved[k] = process.env[k];
Object.assign(process.env, setKeys);
try {
- await syncPublicPr();
+ if (mode === 'sync') await syncPublicPr();
+ else await bridge.refreshPublicPrCla();
} finally {
globalThis.fetch = realFetch;
for (const k of Object.keys(setKeys)) {
@@ -583,7 +722,7 @@ async function runMetadataSync({ headCommitMessage, internalPrStartsDraft }) {
describe('syncPublicPr metadata-event composition (conflict-hold fail-open guard)', () => {
test('a metadata event on a DRAFT conflict PR keeps it draft and posts no public comment', async () => {
- const r = await runMetadataSync({
+ const r = await runBridgeSync({
headCommitMessage: CONFLICT_HEAD,
internalPrStartsDraft: true,
});
@@ -592,13 +731,13 @@ describe('syncPublicPr metadata-event composition (conflict-hold fail-open guard
});
test('a metadata event on a clean PR readies it and posts no public comment', async () => {
- const r = await runMetadataSync({ headCommitMessage: CLEAN_HEAD, internalPrStartsDraft: true });
+ const r = await runBridgeSync({ headCommitMessage: CLEAN_HEAD, internalPrStartsDraft: true });
expect(r.draftMutation).toBe('to-ready');
expect(r.comment).toBeNull();
});
test('a metadata event on a non-draft PR whose head now carries conflicts re-drafts it without a public comment', async () => {
- const r = await runMetadataSync({
+ const r = await runBridgeSync({
headCommitMessage: CONFLICT_HEAD,
internalPrStartsDraft: false,
});
@@ -606,3 +745,585 @@ describe('syncPublicPr metadata-event composition (conflict-hold fail-open guard
expect(r.comment).toBeNull();
});
});
+
+describe('bridge CLA status refresh', () => {
+ test('a successful sync only posts status and never drives Actions', async () => {
+ const result = await runBridgeSync({ claStatus: 'success' });
+ expect(result.validationRequests).toEqual([]);
+ });
+
+ test.each(['success', 'failure', 'pending', null])(
+ 'uses the current API status %s for the imported public head',
+ async (claStatus) => {
+ const result = await runBridgeSync({
+ mode: 'refresh-cla',
+ claStatus,
+ eventState: claStatus === 'success' ? 'failure' : 'success',
+ });
+ expect(result.statuses).toEqual([
+ expect.objectContaining({
+ sha: 'internal-head-sha',
+ context: 'cla/verified',
+ state: claStatus === 'success' ? 'success' : 'failure',
+ }),
+ ]);
+ expect(result.validationRequests).toEqual([]);
+ expect(result.internalBodies).toEqual([]);
+ },
+ );
+
+ test.each([
+ ['other status context', { eventContext: 'another-check' }],
+ [
+ 'replaced public head',
+ { publicHead: 'new-public-head', importedPublicHead: 'new-public-head' },
+ ],
+ ['other imported repository', { importedRepo: 'another/repository' }],
+ ['other imported PR', { importedPrNumber: 311 }],
+ ['pending import', { importedPublicHead: 'previous-public-head' }],
+ ['replaced internal head', { importedInternalHead: 'previous-internal-head' }],
+ ['public head changed during refresh', { changedPublicHeadAfterCommit: true }],
+ ['internal head changed during refresh', { changedInternalHeadAfterCommit: true }],
+ ['missing marker', { hasMarker: false }],
+ ['closed public PR', { publicState: 'closed' }],
+ ['closed internal PR', { internalState: 'closed' }],
+ ['missing internal PR', { existingPr: false }],
+ ['other internal branch', { internalHeadRef: 'fix/ordinary' }],
+ ['mirror sync PR', { headRepo: 'inkeep/open-knowledge', headRef: 'copybara/sync' }],
+ ])('leaves %s unchanged', async (_name, overrides) => {
+ const result = await runBridgeSync({
+ mode: 'refresh-cla',
+ internalPrStartsDraft: true,
+ ...overrides,
+ });
+ expect(result.statuses).toEqual([]);
+ expect(result.draftMutation).toBeNull();
+ expect(result.internalBodies).toEqual([]);
+ });
+
+ test('uses the final sync marker after the original PR body', async () => {
+ const result = await runBridgeSync({
+ mode: 'refresh-cla',
+ earlierMarker:
+ '<!-- public-pr-sync\npublic_head_sha=previous-public-head\ninternal_head_sha=previous-internal-head\n-->\n',
+ });
+ expect(result.statuses).toEqual([
+ expect.objectContaining({ sha: 'internal-head-sha', state: 'success' }),
+ ]);
+ });
+
+ test('finds the current public PR after a page of other open PRs', async () => {
+ const result = await runBridgeSync({
+ mode: 'refresh-cla',
+ associations: [],
+ publicCandidates: [
+ ...Array.from({ length: 100 }, () => ({ number: 311, head: { sha: 'other-head' } })),
+ { number: 310, head: { sha: 'public-head-sha' } },
+ ],
+ });
+ expect(result.statuses).toEqual([
+ expect.objectContaining({ sha: 'internal-head-sha', state: 'success' }),
+ ]);
+ });
+
+ test('finds a fork PR when commit associations are empty', async () => {
+ const result = await runBridgeSync({ mode: 'refresh-cla', associations: [] });
+ expect(result.statuses).toEqual([
+ expect.objectContaining({
+ sha: 'internal-head-sha',
+ context: 'cla/verified',
+ state: 'success',
+ }),
+ ]);
+ expect(result.internalBodies).toEqual([]);
+ });
+
+ test('refresh preserves the conflict hold on an imported head', async () => {
+ const result = await runBridgeSync({
+ mode: 'refresh-cla',
+ headCommitMessage: CONFLICT_HEAD,
+ internalPrStartsDraft: true,
+ });
+ expect(result.statuses).toEqual([expect.objectContaining({ state: 'success' })]);
+ expect(result.draftMutation).toBeNull();
+ });
+
+ test('metadata edits do not attest to a public head that has not been imported', async () => {
+ const result = await runBridgeSync({
+ publicHead: 'new-public-head',
+ internalPrStartsDraft: true,
+ });
+ expect(result.statuses).toEqual([]);
+ expect(result.draftMutation).toBeNull();
+ expect(result.internalBodies).toEqual([]);
+ });
+});
+
+function setupSyncRepo({ sourceContent = 'updated\n', mapForkToSource = true } = {}) {
+ const root = mkdtempSync(path.join(tmpdir(), 'bridge-head-status-'));
+ const remote = path.join(root, 'remote.git');
+ const internal = path.join(root, 'internal');
+ const branch = 'public-pr/open-knowledge-310';
+ git(root, 'init', '--bare', '--initial-branch=main', remote);
+ git(root, 'init', '--initial-branch=main', internal);
+ git(internal, 'config', 'user.name', 'Fixture');
+ git(internal, 'config', 'user.email', '[email protected]');
+ mkdirSync(path.join(internal, 'public/open-knowledge'), { recursive: true });
+ const file = path.join(internal, 'public/open-knowledge/fixture.txt');
+ writeFileSync(file, 'base\n');
+ git(internal, 'add', 'public/open-knowledge/fixture.txt');
+ git(internal, 'commit', '-m', 'Base');
+ git(internal, 'remote', 'add', 'origin', remote);
+ git(internal, 'push', '-u', 'origin', 'main');
+ git(internal, 'checkout', '-b', branch);
+ writeFileSync(file, 'previous\n');
+ git(internal, 'commit', '-am', 'Previous head');
+ git(internal, 'push', '-u', 'origin', branch);
+ const oldHead = git(internal, 'rev-parse', 'HEAD');
+ git(internal, 'checkout', 'main');
+ const source = path.join(root, 'public');
+ const sourceRepoDir = path.join(root, 'public.git');
+ git(root, 'init', '--bare', '--initial-branch=main', sourceRepoDir);
+ git(root, 'init', '--initial-branch=main', source);
+ git(source, 'config', 'user.name', 'Contributor');
+ git(source, 'config', 'user.email', '[email protected]');
+ writeFileSync(path.join(source, 'fixture.txt'), 'base\n');
+ git(source, 'add', 'fixture.txt');
+ git(source, 'commit', '-m', 'Base');
+ const baseHead = git(source, 'rev-parse', 'HEAD');
+ git(source, 'remote', 'add', 'origin', sourceRepoDir);
+ git(source, 'push', 'origin', 'main');
+ git(source, 'checkout', '-b', 'contribution');
+ writeFileSync(path.join(source, 'fixture.txt'), sourceContent);
+ git(source, 'commit', '-am', 'Recorded head');
+ const approvedHead = git(source, 'rev-parse', 'HEAD');
+ git(source, 'push', 'origin', 'HEAD:refs/pull/310/head');
+ const rewriteKey = `url.${sourceRepoDir}.insteadOf`;
+ git(
+ internal,
+ 'config',
+ '--add',
+ rewriteKey,
+ 'https://x-access-token:[email protected]/inkeep/open-knowledge.git',
+ );
+ if (mapForkToSource) {
+ git(
+ internal,
+ 'config',
+ '--add',
+ rewriteKey,
+ 'https://x-access-token:[email protected]/octocat/open-knowledge.git',
+ );
+ }
+ return {
+ root,
+ internal,
+ remote,
+ branch,
+ oldHead,
+ source,
+ sourceRepoDir,
+ baseHead,
+ approvedHead,
+ publishedHead: () => git(remote, 'rev-parse', `refs/heads/${branch}`),
+ cleanup: () => rmSync(root, { recursive: true, force: true }),
+ };
+}
+
+function setupSyncSourceRepo() {
+ const fixture = setupSyncRepo({ sourceContent: 'approved' });
+ writeFileSync(path.join(fixture.source, 'fixture.txt'), 'later');
+ git(fixture.source, 'commit', '-am', 'Later head');
+ git(fixture.source, 'push', 'origin', 'HEAD:refs/pull/310/head');
+ return fixture;
+}
+
+function setupForkOnlySyncRepo() {
+ const fixture = setupSyncRepo({ mapForkToSource: false });
+ const fork = path.join(fixture.root, 'fork');
+ const forkRepoDir = path.join(fixture.root, 'fork.git');
+ git(fixture.root, 'init', '--bare', '--initial-branch=main', forkRepoDir);
+ git(fixture.root, 'clone', fixture.sourceRepoDir, fork);
+ git(fork, 'config', 'user.name', 'Fork Contributor');
+ git(fork, 'config', 'user.email', '[email protected]');
+ writeFileSync(path.join(fork, 'fixture.txt'), 'fork-only\n');
+ git(fork, 'commit', '-am', 'Fork-only head');
+ const forkHead = git(fork, 'rev-parse', 'HEAD');
+ git(fork, 'remote', 'add', 'fork', forkRepoDir);
+ git(fork, 'push', 'fork', 'HEAD:main');
+ git(
+ fixture.internal,
+ 'config',
+ '--add',
+ `url.${forkRepoDir}.insteadOf`,
+ 'https://x-access-token:[email protected]/octocat/open-knowledge.git',
+ );
+ return { ...fixture, forkHead };
+}
+
+async function captureBridgeLogs(action) {
+ const logs = [];
+ const originalLog = console.log;
+ const originalWarn = console.warn;
+ console.log = (...parts) => logs.push(parts.join(' '));
+ console.warn = (...parts) => logs.push(parts.join(' '));
+ try {
+ return { result: await action(), logs };
+ } finally {
+ console.log = originalLog;
+ console.warn = originalWarn;
+ }
+}
+
+describe('syncPublicPr legacy metadata import', () => {
+ test('an approved metadata event rebuilds content and records the current public head', async () => {
+ const fixture = setupSyncRepo();
+ try {
+ const recorded = await runBridgeSync({
+ internalRepoDir: fixture.internal,
+ action: 'edited',
+ markerHeadFields: 'legacy',
+ publicHead: fixture.approvedHead,
+ publicBase: fixture.baseHead,
+ readHead: fixture.publishedHead,
+ });
+ const publishedHead = fixture.publishedHead();
+ expect(publishedHead).not.toBe(fixture.oldHead);
+ expect(
+ git(fixture.remote, 'show', `${fixture.branch}:public/open-knowledge/fixture.txt`),
+ ).toBe('updated');
+ expect(recorded.internalBodies.at(-1)).toContain(`public_head_sha=${fixture.approvedHead}`);
+ expect(recorded.internalBodies.at(-1)).toContain(`internal_head_sha=${publishedHead}`);
+ expect(recorded.statuses).toContainEqual(
+ expect.objectContaining({
+ sha: publishedHead,
+ context: 'cla/verified',
+ state: 'success',
+ }),
+ );
+ } finally {
+ fixture.cleanup();
+ }
+ });
+
+ test.each([
+ ['partial marker', { markerHeadFields: 'partial' }],
+ ['internal-only marker', { markerHeadFields: 'internal-only' }],
+ ['modern mismatch', { importedPublicHead: 'previous-public-head' }],
+ ['other repository', { markerHeadFields: 'legacy', importedRepo: 'another/repository' }],
+ ['other PR', { markerHeadFields: 'legacy', importedPrNumber: 311 }],
+ ['other branch', { markerHeadFields: 'legacy', internalHeadRef: 'fix/ordinary' }],
+ ])('leaves %s without a new receipt or status', async (_name, overrides) => {
+ const fixture = setupSyncRepo();
+ try {
+ const recorded = await runBridgeSync({
+ internalRepoDir: fixture.internal,
+ action: 'edited',
+ publicHead: fixture.approvedHead,
+ publicBase: fixture.baseHead,
+ readHead: fixture.publishedHead,
+ ...overrides,
+ });
+ expect(fixture.publishedHead()).toBe(fixture.oldHead);
+ expect(recorded.internalBodies).toEqual([]);
+ expect(recorded.statuses).toEqual([]);
+ } finally {
+ fixture.cleanup();
+ }
+ });
+
+ test('an empty pinned diff leaves a legacy head without a receipt or status and explains why', async () => {
+ const fixture = setupSyncRepo();
+ try {
+ git(fixture.source, 'checkout', 'main');
+ git(fixture.source, 'merge', '--ff-only', 'contribution');
+ git(fixture.source, 'push', 'origin', 'main');
+ git(fixture.source, 'checkout', 'contribution');
+ git(fixture.source, 'commit', '--allow-empty', '-m', 'Empty change');
+ const emptyHead = git(fixture.source, 'rev-parse', 'HEAD');
+ git(fixture.source, 'push', 'origin', 'HEAD:refs/pull/310/head');
+ const { result, logs } = await captureBridgeLogs(() =>
+ runBridgeSync({
+ internalRepoDir: fixture.internal,
+ action: 'edited',
+ markerHeadFields: 'legacy',
+ publicHead: emptyHead,
+ publicBase: fixture.approvedHead,
+ readHead: fixture.publishedHead,
+ }),
+ );
+ expect(fixture.publishedHead()).toBe(fixture.oldHead);
+ expect(result.internalBodies).toEqual([]);
+ expect(result.statuses).toEqual([]);
+ expect(
+ logs.some(
+ (line) =>
+ line.includes('PR #310') &&
+ line.includes(emptyHead) &&
+ line.includes('no new import recorded'),
+ ),
+ ).toBe(true);
+ expect(logs.join('\n')).not.toContain(fixture.oldHead);
+ } finally {
+ fixture.cleanup();
+ }
+ });
+
+ test('an already present change leaves a legacy head without a receipt or status and explains why', async () => {
+ const fixture = setupSyncRepo();
+ try {
+ writeFileSync(path.join(fixture.internal, 'public/open-knowledge/fixture.txt'), 'updated\n');
+ git(fixture.internal, 'commit', '-am', 'Already present');
+ git(fixture.internal, 'push', 'origin', 'main');
+ const { result, logs } = await captureBridgeLogs(() =>
+ runBridgeSync({
+ internalRepoDir: fixture.internal,
+ action: 'edited',
+ markerHeadFields: 'legacy',
+ publicHead: fixture.approvedHead,
+ publicBase: fixture.baseHead,
+ readHead: fixture.publishedHead,
+ }),
+ );
+ expect(fixture.publishedHead()).toBe(fixture.oldHead);
+ expect(result.internalBodies).toEqual([]);
+ expect(result.statuses).toEqual([]);
+ expect(
+ logs.some(
+ (line) =>
+ line.includes('PR #310') &&
+ line.includes(fixture.approvedHead) &&
+ line.includes('no new receipt recorded'),
+ ),
+ ).toBe(true);
+ expect(logs.join('\n')).not.toContain(fixture.oldHead);
+ } finally {
+ fixture.cleanup();
+ }
+ });
+});
+
+describe('syncPublicPr published-head status', () => {
+ test('imports a recorded head reachable only from its fork', async () => {
+ const fixture = setupForkOnlySyncRepo();
+ try {
+ const recorded = await runBridgeSync({
+ internalRepoDir: fixture.internal,
+ action: 'synchronize',
+ publicHead: fixture.forkHead,
+ publicBase: fixture.baseHead,
+ readHead: fixture.publishedHead,
+ });
+ expect(
+ git(fixture.remote, 'show', `${fixture.branch}:public/open-knowledge/fixture.txt`),
+ ).toBe('fork-only');
+ expect(recorded.internalBodies.at(-1)).toContain(`public_head_sha=${fixture.forkHead}`);
+ expect(recorded.statuses).toContainEqual(
+ expect.objectContaining({
+ sha: fixture.publishedHead(),
+ context: 'cla/verified',
+ state: 'success',
+ }),
+ );
+ } finally {
+ fixture.cleanup();
+ }
+ });
+
+ test('publishes the recorded public head when its ref advances', async () => {
+ const fixture = setupSyncSourceRepo();
+ try {
+ const recorded = await runBridgeSync({
+ internalRepoDir: fixture.internal,
+ action: 'synchronize',
+ publicHead: fixture.approvedHead,
+ publicBase: fixture.baseHead,
+ readHead: fixture.publishedHead,
+ });
+ expect(recorded.internalBodies.at(-1)).toContain(`public_head_sha=${fixture.approvedHead}`);
+ expect(
+ git(fixture.remote, 'show', `${fixture.branch}:public/open-knowledge/fixture.txt`),
+ ).toBe('approved');
+ } finally {
+ fixture.cleanup();
+ }
+ });
+
+ test('fetches the recorded head commit after the public ref moves to another history', async () => {
+ const fixture = setupSyncRepo({ sourceContent: 'approved' });
+ try {
+ git(fixture.source, 'push', 'origin', `${fixture.approvedHead}:refs/heads/saved-head`);
+ git(fixture.source, 'reset', '--hard', fixture.baseHead);
+ writeFileSync(path.join(fixture.source, 'fixture.txt'), 'later');
+ git(fixture.source, 'commit', '-am', 'Later head');
+ git(fixture.source, 'push', 'origin', '+HEAD:refs/pull/310/head');
+ const recorded = await runBridgeSync({
+ internalRepoDir: fixture.internal,
+ action: 'synchronize',
+ publicHead: fixture.approvedHead,
+ publicBase: fixture.baseHead,
+ readHead: fixture.publishedHead,
+ });
+ expect(
+ git(fixture.remote, 'show', `${fixture.branch}:public/open-knowledge/fixture.txt`),
+ ).toBe('approved');
+ expect(recorded.internalBodies.at(-1)).toContain(`public_head_sha=${fixture.approvedHead}`);
+ } finally {
+ fixture.cleanup();
+ }
+ });
+
+ test('publishes the recorded diff when the base ref advances', async () => {
+ const fixture = setupSyncRepo();
+ try {
+ git(fixture.source, 'checkout', 'main');
+ git(fixture.source, 'merge', '--ff-only', 'contribution');
+ git(fixture.source, 'push', 'origin', 'main');
+ const recorded = await runBridgeSync({
+ internalRepoDir: fixture.internal,
+ action: 'synchronize',
+ publicHead: fixture.approvedHead,
+ publicBase: fixture.baseHead,
+ readHead: fixture.publishedHead,
+ });
+ expect(
+ git(fixture.remote, 'show', `${fixture.branch}:public/open-knowledge/fixture.txt`),
+ ).toBe('updated');
+ expect(recorded.internalBodies.at(-1)).toContain(`public_head_sha=${fixture.approvedHead}`);
+ } finally {
+ fixture.cleanup();
+ }
+ });
+
+ test('stops before publication when the recorded head commit is unavailable', async () => {
+ const fixture = setupSyncRepo();
+ try {
+ await expect(
+ runBridgeSync({
+ internalRepoDir: fixture.internal,
+ action: 'synchronize',
+ publicHead: 'f'.repeat(40),
+ publicBase: fixture.baseHead,
+ readHead: fixture.publishedHead,
+ }),
+ ).rejects.toThrow();
+ expect(fixture.publishedHead()).toBe(fixture.oldHead);
+ } finally {
+ fixture.cleanup();
+ }
+ });
+
+ test('publishes a large diff with complete text bytes', async () => {
+ const fixture = setupSyncRepo();
+ const largeContent = `${'x'.repeat(1024 * 1024 + 1)}\n`;
+ try {
+ writeFileSync(path.join(fixture.source, 'fixture.txt'), largeContent);
+ git(fixture.source, 'commit', '-am', 'Large text change');
+ const largeHead = git(fixture.source, 'rev-parse', 'HEAD');
+ git(fixture.source, 'push', 'origin', 'HEAD:refs/pull/310/head');
+ const recorded = await runBridgeSync({
+ internalRepoDir: fixture.internal,
+ action: 'synchronize',
+ publicHead: largeHead,
+ publicBase: fixture.baseHead,
+ readHead: fixture.publishedHead,
+ });
+ expect(recorded.internalBodies.at(-1)).toContain(`public_head_sha=${largeHead}`);
+ expect(
+ readFileSync(path.join(fixture.internal, 'public/open-knowledge/fixture.txt'), 'utf8'),
+ ).toBe(largeContent);
+ } finally {
+ fixture.cleanup();
+ }
+ });
+
+ test('publishes binary file bytes from the recorded commit', async () => {
+ const fixture = setupSyncRepo();
+ const binaryContent = Buffer.from([0, 1, 2, 127, 128, 255]);
+ try {
+ writeFileSync(path.join(fixture.source, 'asset.bin'), binaryContent);
+ git(fixture.source, 'add', 'asset.bin');
+ git(fixture.source, 'commit', '-m', 'Add binary file');
+ const binaryHead = git(fixture.source, 'rev-parse', 'HEAD');
+ git(fixture.source, 'push', 'origin', 'HEAD:refs/pull/310/head');
+ const recorded = await runBridgeSync({
+ internalRepoDir: fixture.internal,
+ action: 'synchronize',
+ publicHead: binaryHead,
+ publicBase: fixture.baseHead,
+ readHead: fixture.publishedHead,
+ });
+ expect(recorded.internalBodies.at(-1)).toContain(`public_head_sha=${binaryHead}`);
+ expect(readFileSync(path.join(fixture.internal, 'public/open-knowledge/asset.bin'))).toEqual(
+ binaryContent,
+ );
+ } finally {
+ fixture.cleanup();
+ }
+ });
+
+ test('does not record an imported head when the public head changed during sync', async () => {
+ const fixture = setupSyncRepo();
+ try {
+ const result = await runBridgeSync({
+ internalRepoDir: fixture.internal,
+ action: 'synchronize',
+ publicHead: fixture.approvedHead,
+ publicBase: fixture.baseHead,
+ readHead: fixture.publishedHead,
+ publicHeadOnReread: 'new-public-head',
+ });
+ expect(result.internalBodies).toEqual([]);
+ expect(result.statuses).toEqual([
+ expect.objectContaining({
+ sha: fixture.publishedHead(),
+ context: 'cla/verified',
+ state: 'failure',
+ }),
+ ]);
+ } finally {
+ fixture.cleanup();
+ }
+ });
+
+ test.each([
+ ['success', 'stale metadata', 'existing'],
+ ['failure', 'stale metadata', 'existing'],
+ ['success', 'stale reads', 'existing'],
+ ['failure', 'stale reads', 'existing'],
+ ['success', 'current metadata', 'existing'],
+ ['failure', 'current metadata', 'existing'],
+ ['success', 'stale metadata', 'new'],
+ ['failure', 'stale metadata', 'new'],
+ ])('posts %s on the published head with %s for %s PRs', async (claStatus, freshness, entry) => {
+ const fixture = setupSyncRepo();
+ try {
+ const recorded = await runBridgeSync({
+ internalRepoDir: fixture.internal,
+ action: 'synchronize',
+ publicHead: fixture.approvedHead,
+ publicBase: fixture.baseHead,
+ claStatus,
+ existingPr: entry === 'existing',
+ readHead: freshness === 'stale reads' ? () => fixture.oldHead : fixture.publishedHead,
+ patchHead: freshness === 'current metadata' ? fixture.publishedHead : () => fixture.oldHead,
+ });
+ const head = fixture.publishedHead();
+ expect(recorded.internalBodies.at(-1)).toContain(`public_head_sha=${fixture.approvedHead}`);
+ expect(recorded.internalBodies.at(-1)).toContain(`internal_head_sha=${head}`);
+ expect(head).not.toBe(fixture.oldHead);
+ expect(
+ git(fixture.remote, 'show', `${fixture.branch}:public/open-knowledge/fixture.txt`),
+ ).toBe('updated');
+ expect(recorded.statuses).toContainEqual(
+ expect.objectContaining({
+ sha: head,
+ context: 'cla/verified',
+ state: claStatus,
+ }),
+ );
+ } finally {
+ fixture.cleanup();
+ }
+ });
+});
diff --git a/.github/workflows/monorepo-pr-bridge.yml b/.github/workflows/monorepo-pr-bridge.yml
index bb32d5c79..cc48a0088 100644
--- a/.github/workflows/monorepo-pr-bridge.yml
+++ b/.github/workflows/monorepo-pr-bridge.yml
@@ -1,6 +1,7 @@
name: Monorepo PR Bridge
on:
+ status:
pull_request_target:
types: [opened, reopened, synchronize, ready_for_review, converted_to_draft, edited, closed]
@@ -10,7 +11,7 @@ permissions:
issues: write
concurrency:
- group: monorepo-pr-bridge-${{ github.event.pull_request.number }}
+ group: monorepo-pr-bridge-${{ github.event_name == 'status' && format('status-{0}-{1}', github.event.context, github.event.sha) || github.event.pull_request.number }}
cancel-in-progress: true
jobs:
@@ -24,7 +25,8 @@ jobs:
# call theirs `copybara/sync` would silently receive no acknowledgement, never
# be imported, and never be told why. Only this repo can own a branch here.
if: >-
- github.event.action == 'opened'
+ github.event_name == 'pull_request_target'
+ && github.event.action == 'opened'
&& (github.event.pull_request.head.repo.full_name != github.repository
|| github.event.pull_request.head.ref != 'copybara/sync')
runs-on: ubuntu-latest
@@ -53,7 +55,8 @@ jobs:
# repo, so every public PR revision needs maintainer approval before crossing
# into agents-private.
if: >-
- github.event.action != 'closed'
+ github.event_name == 'pull_request_target'
+ && github.event.action != 'closed'
&& (github.event.pull_request.head.repo.full_name != github.repository
|| github.event.pull_request.head.ref != 'copybara/sync')
environment: inkeep-oss-sync
@@ -129,7 +132,8 @@ jobs:
# Skip PRs that originated from agents-private via Copybara, identified by
# head repository rather than branch name for the same reason as above.
if: >-
- github.event.action == 'closed'
+ github.event_name == 'pull_request_target'
+ && github.event.action == 'closed'
&& (github.event.pull_request.head.repo.full_name != github.repository
|| github.event.pull_request.head.ref != 'copybara/sync')
runs-on: ubuntu-latest
@@ -163,3 +167,44 @@ jobs:
PUBLIC_REPO: ${{ github.repository }}
PUBLIC_TOKEN: ${{ github.token }}
run: node .github/scripts/bridge-public-pr-to-monorepo.mjs close
+
+ refresh-cla:
+ if: github.event_name == 'status' && github.event.context == 'license/cla'
+ permissions:
+ contents: read
+ pull-requests: read
+ statuses: read
+ runs-on: ubuntu-latest
+ timeout-minutes: 15
+ steps:
+ - name: Checkout public repo automation
+ uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
+ with:
+ ref: ${{ github.event.repository.default_branch }}
+ persist-credentials: false
+ - name: Setup Node.js
+ uses: actions/setup-node@48b55a011bda9f5d6aeb4c2d9c7362e8dae4041e # v6.4.0
+ with:
+ node-version: 22
+ - name: Generate inkeep-oss-sync App token
+ id: app-token
+ uses: actions/create-github-app-token@1b10c78c7865c340bc4f6099eb2f838309f1e8c3 # v3.1.1
+ with:
+ app-id: ${{ secrets.OSS_SYNC_APP_ID }}
+ private-key: ${{ secrets.OSS_SYNC_APP_PRIVATE_KEY }}
+ owner: inkeep
+ repositories: agents-private
+ permission-contents: read
+ permission-members: read
+ permission-pull-requests: write
+ permission-statuses: write
+ - name: Refresh CLA status on the imported head
+ env:
+ INTERNAL_BRANCH_PREFIX: public-pr/open-knowledge
+ INTERNAL_REPO: inkeep/agents-private
+ INTERNAL_TOKEN: ${{ steps.app-token.outputs.token }}
+ PUBLIC_REPO: ${{ github.repository }}
+ PUBLIC_TOKEN: ${{ github.token }}
+ PUBLIC_STATUS_CONTEXT: ${{ github.event.context }}
+ PUBLIC_STATUS_SHA: ${{ github.event.sha }}
+ run: node .github/scripts/bridge-public-pr-to-monorepo.mjs refresh-cla