Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
38 changes: 38 additions & 0 deletions .github/scripts/__tests__/source-context.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const {
SOURCE_TYPES,
extractIssueNumberFromPull,
normalizeSourceType,
parseDependencyRepairPromotionSource,
parseWorkflowSourceBlock,
hasNoAutomationWorkflowContext,
resolvePrSourceContext,
Expand Down Expand Up @@ -168,6 +169,43 @@ automation: verifier_required
});
});

test('dependency repair promotion marker supplies explicit dependency source context', () => {
const marker = [
'<!-- dependency-repair-promotion:v1 ',
JSON.stringify({
source_pr: 2795,
source_base_sha: 'a'.repeat(40),
source_head_sha: 'b'.repeat(40),
promotion_base_sha: 'c'.repeat(40),
}),
' -->',
].join('');
const context = resolvePrSourceContext({
body: marker,
head: { ref: 'agent/deps-repair-2795' },
title: 'fix(deps): repair setup-python v7 compatibility',
});

assert.equal(parseDependencyRepairPromotionSource(marker).source_pr, 2795);
assert.equal(context.sourceType, SOURCE_TYPES.DEPENDABOT);
assert.equal(context.sourceRef, 'dependency-pr:#2795');
assert.equal(context.isExplicit, true);
assert.equal(context.isValid, true);
assert.equal(context.requiresIssue, false);
});

test('malformed dependency repair promotion marker does not authorize source context', () => {
const context = resolvePrSourceContext({
body: '<!-- dependency-repair-promotion:v1 {"source_pr":2795} -->',
head: { ref: 'agent/deps-repair-2795' },
title: 'fix(deps): repair setup-python v7 compatibility',
});

assert.equal(context.sourceType, SOURCE_TYPES.UNKNOWN);
assert.equal(context.isExplicit, false);
assert.equal(context.isValid, false);
});

test('sourceTypeFromCheckedTemplate reads direct GitHub PR source choice', () => {
const body = `
## Workflow Source
Expand Down
35 changes: 34 additions & 1 deletion .github/scripts/source_context.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ const CHECKBOX_SOURCE_PATTERNS = Object.freeze([
]);

const NO_AUTOMATION_CHECKBOX_PATTERN = /\bdo\s+not\s+automate\b|\bhuman[- ]only\b/i;
const DEPENDENCY_REPAIR_PROMOTION_PATTERN =
/<!--\s*dependency-repair-promotion:v1\s+(\{[^\n]*\})\s*-->/i;
Comment thread
stranske marked this conversation as resolved.
Outdated
Comment thread
stranske marked this conversation as resolved.
Outdated

function cleanString(value) {
return String(value || '').trim();
Expand Down Expand Up @@ -281,6 +283,29 @@ function parseWorkflowSourceBlock(body) {
return result;
}

function parseDependencyRepairPromotionSource(body) {
const match = String(body || '').match(DEPENDENCY_REPAIR_PROMOTION_PATTERN);
if (!match) {
return null;
}

try {
const metadata = JSON.parse(match[1]);
const sourcePr = Number(metadata?.source_pr);
const validShas = [
metadata?.source_base_sha,
metadata?.source_head_sha,
metadata?.promotion_base_sha,
].every((value) => /^[0-9a-f]{40}$/i.test(String(value || '')));
if (!Number.isInteger(sourcePr) || sourcePr <= 0 || !validShas) {
return null;
}
return { ...metadata, source_pr: sourcePr };
} catch {
return null;
}
}

function sourceTypeFromCheckedTemplate(body) {
const sectionLines = workflowSourceSectionLines(body);
if (!sectionLines.length) {
Expand Down Expand Up @@ -355,6 +380,7 @@ function inferredSourceType(pull = {}) {
function resolvePrSourceContext(pull = {}) {
const body = String(pull?.body || '');
const block = parseWorkflowSourceBlock(body);
const dependencyRepairPromotion = parseDependencyRepairPromotionSource(body);
const issueNumber = extractIssueNumberFromPull(pull);
const noAutomation = hasNoAutomationWorkflowContext(pull);

Expand All @@ -363,7 +389,9 @@ function resolvePrSourceContext(pull = {}) {
const checkboxType = sourceTypeFromCheckedTemplate(body);
const labelType = sourceTypeFromLabels(pull);
const inferredType = inferredSourceType(pull);
const detectedSourceType = issueNumber
const detectedSourceType = dependencyRepairPromotion
? SOURCE_TYPES.DEPENDABOT
: issueNumber
? SOURCE_TYPES.GITHUB_ISSUE
: [markerType, blockType, checkboxType, labelType, inferredType].find((type) => type !== SOURCE_TYPES.UNKNOWN)
|| SOURCE_TYPES.UNKNOWN;
Expand All @@ -372,6 +400,9 @@ function resolvePrSourceContext(pull = {}) {
: detectedSourceType;

const sourceRef =
(dependencyRepairPromotion
? `dependency-pr:#${dependencyRepairPromotion.source_pr}`
: '') ||
cleanString(parseHtmlMarker(body, 'workflow-source-ref')) ||
cleanString(block.source_ref || block.ref || block.reference) ||
(issueNumber ? `#${issueNumber}` : '');
Expand All @@ -392,6 +423,7 @@ function resolvePrSourceContext(pull = {}) {
isValid: VALID_SOURCE_TYPES.has(sourceType),
isExplicit: Boolean(
issueNumber ||
dependencyRepairPromotion ||
markerType !== SOURCE_TYPES.UNKNOWN ||
blockType !== SOURCE_TYPES.UNKNOWN ||
checkboxType !== SOURCE_TYPES.UNKNOWN ||
Expand Down Expand Up @@ -433,6 +465,7 @@ module.exports = {
extractIssueNumbersFromText,
extractIssueNumberFromPull,
parseWorkflowSourceBlock,
parseDependencyRepairPromotionSource,
sourceTypeFromCheckedTemplate,
sourceTypeFromLabels,
hasNoAutomationWorkflowContext,
Expand Down
5 changes: 5 additions & 0 deletions docs/ops/DEPENDENCY_REPAIR_PROMOTION.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ branch. The PR remains attributable to dependency automation through
`workflow:source-dependabot` and identifiable through
`dependency:repair-promotion`.

A structurally valid promotion marker is itself explicit dependency source
context, so keepalive can dispatch the coding agent without a linked issue or a
manually applied source label. PR 46 still validates the referenced bot PR,
commit ancestry, and patch identity before the promotion can merge.

## Machine-enforced contract

`PR 46 Dependency Repair Contract` runs only for dependency-bot PRs and marked
Expand Down
35 changes: 34 additions & 1 deletion templates/consumer-repo/.github/scripts/source_context.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ const CHECKBOX_SOURCE_PATTERNS = Object.freeze([
]);

const NO_AUTOMATION_CHECKBOX_PATTERN = /\bdo\s+not\s+automate\b|\bhuman[- ]only\b/i;
const DEPENDENCY_REPAIR_PROMOTION_PATTERN =
/<!--\s*dependency-repair-promotion:v1\s+(\{[^\n]*\})\s*-->/i;

function cleanString(value) {
return String(value || '').trim();
Expand Down Expand Up @@ -281,6 +283,29 @@ function parseWorkflowSourceBlock(body) {
return result;
}

function parseDependencyRepairPromotionSource(body) {
const match = String(body || '').match(DEPENDENCY_REPAIR_PROMOTION_PATTERN);
if (!match) {
return null;
}

try {
const metadata = JSON.parse(match[1]);
const sourcePr = Number(metadata?.source_pr);
const validShas = [
metadata?.source_base_sha,
metadata?.source_head_sha,
metadata?.promotion_base_sha,
].every((value) => /^[0-9a-f]{40}$/i.test(String(value || '')));
if (!Number.isInteger(sourcePr) || sourcePr <= 0 || !validShas) {
return null;
}
return { ...metadata, source_pr: sourcePr };
} catch {
return null;
}
}

function sourceTypeFromCheckedTemplate(body) {
const sectionLines = workflowSourceSectionLines(body);
if (!sectionLines.length) {
Expand Down Expand Up @@ -355,6 +380,7 @@ function inferredSourceType(pull = {}) {
function resolvePrSourceContext(pull = {}) {
const body = String(pull?.body || '');
const block = parseWorkflowSourceBlock(body);
const dependencyRepairPromotion = parseDependencyRepairPromotionSource(body);
const issueNumber = extractIssueNumberFromPull(pull);
const noAutomation = hasNoAutomationWorkflowContext(pull);

Expand All @@ -363,7 +389,9 @@ function resolvePrSourceContext(pull = {}) {
const checkboxType = sourceTypeFromCheckedTemplate(body);
const labelType = sourceTypeFromLabels(pull);
const inferredType = inferredSourceType(pull);
const detectedSourceType = issueNumber
const detectedSourceType = dependencyRepairPromotion
? SOURCE_TYPES.DEPENDABOT
: issueNumber
? SOURCE_TYPES.GITHUB_ISSUE
: [markerType, blockType, checkboxType, labelType, inferredType].find((type) => type !== SOURCE_TYPES.UNKNOWN)
|| SOURCE_TYPES.UNKNOWN;
Expand All @@ -372,6 +400,9 @@ function resolvePrSourceContext(pull = {}) {
: detectedSourceType;

const sourceRef =
(dependencyRepairPromotion
? `dependency-pr:#${dependencyRepairPromotion.source_pr}`
: '') ||
cleanString(parseHtmlMarker(body, 'workflow-source-ref')) ||
cleanString(block.source_ref || block.ref || block.reference) ||
(issueNumber ? `#${issueNumber}` : '');
Expand All @@ -392,6 +423,7 @@ function resolvePrSourceContext(pull = {}) {
isValid: VALID_SOURCE_TYPES.has(sourceType),
isExplicit: Boolean(
issueNumber ||
dependencyRepairPromotion ||
markerType !== SOURCE_TYPES.UNKNOWN ||
blockType !== SOURCE_TYPES.UNKNOWN ||
checkboxType !== SOURCE_TYPES.UNKNOWN ||
Expand Down Expand Up @@ -433,6 +465,7 @@ module.exports = {
extractIssueNumbersFromText,
extractIssueNumberFromPull,
parseWorkflowSourceBlock,
parseDependencyRepairPromotionSource,
sourceTypeFromCheckedTemplate,
sourceTypeFromLabels,
hasNoAutomationWorkflowContext,
Expand Down
Loading