Skip to content
Open
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
78 changes: 78 additions & 0 deletions .github/actions/report-fix-patch/action.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
name: Report formatter patch artifact
description: Report the immutable formatter snapshot and patch-only result.

inputs:
tool:
description: Fixed formatter identifier.
required: true
source-sha:
description: Authorized source commit SHA.
required: true
changes:
description: Whether a patch artifact was created.
required: true
patch-name:
description: Expected fixed patch file name.
required: true
run-id:
description: Workflow run containing the artifact.
required: true

runs:
using: composite
steps:
- name: Post patch-only result
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
env:
TOOL: ${{ inputs.tool }}
SOURCE_SHA: ${{ inputs.source-sha }}
CHANGES: ${{ inputs.changes }}
PATCH_NAME: ${{ inputs.patch-name }}
RUN_ID: ${{ inputs.run-id }}
with:
script: |
const { owner, repo } = context.repo;
const pullNumber = context.payload.issue?.number;
if (!context.payload.issue?.pull_request || !Number.isSafeInteger(pullNumber)) {
throw new Error('Patch reports are only valid for pull requests.');
}
if (!/^[0-9a-f]{40}$/i.test(process.env.SOURCE_SHA || '') ||
!/^[a-z0-9-]{1,40}$/.test(process.env.TOOL || '') ||
!/^[0-9]+$/.test(process.env.RUN_ID || '') ||
!['true', 'false'].includes(process.env.CHANGES)) {
throw new Error('Invalid patch report metadata.');
}
if (process.env.CHANGES === 'true' &&
process.env.PATCH_NAME !== `${process.env.TOOL}.patch`) {
throw new Error('Patch report file name does not match the formatter.');
}
const { data: pull } = await github.rest.pulls.get({
owner,
repo,
pull_number: pullNumber
});
const isCurrent = pull.state === 'open' && pull.head.sha === process.env.SOURCE_SHA;
const artifactUrl = `${context.serverUrl}/${owner}/${repo}/actions/runs/${process.env.RUN_ID}#artifacts`;
const result = process.env.CHANGES === 'true'
? `A patch-only artifact was created: \`${process.env.PATCH_NAME}\`.`
: 'No formatting changes were produced.';
const freshness = isCurrent
? 'The PR still points to the approved snapshot.'
: 'The PR head moved after approval; do not apply this patch. Rerun against the current full SHA.';
const artifact = process.env.CHANGES === 'true'
? `Download it from [this run's artifacts](${artifactUrl}).`
: '';
await github.rest.issues.createComment({
owner,
repo,
issue_number: pullNumber,
body: [
`**${process.env.TOOL} formatter result**`,
'',
'No repository branch was modified.',
`Approved source snapshot: \`${process.env.SOURCE_SHA}\`.`,
result,
freshness,
artifact
].filter(Boolean).join('\n\n')
});
109 changes: 109 additions & 0 deletions .github/actions/resolve-fix-snapshot/action.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
name: Resolve authorized formatter snapshot
description: Resolve and pin the PR source SHA before a patch-only formatter run.

inputs:
command:
description: Bot command required for direct issue-comment requests.
required: true
ref:
description: Target branch resolved by the workflow setup action.
required: true
repo:
description: Target repository resolved by the workflow setup action.
required: true
source-sha:
description: Optional exact source SHA from a reusable workflow or manual run.
required: false

outputs:
source_sha:
description: Authorized immutable source SHA.
value: ${{ steps.authorize.outputs.source_sha }}
source_repo:
description: Repository containing the authorized source SHA.
value: ${{ steps.authorize.outputs.source_repo }}

runs:
using: composite
steps:
- name: Validate source authorization
id: authorize
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
env:
COMMAND: ${{ inputs.command }}
TARGET_REF: ${{ inputs.ref }}
TARGET_REPO: ${{ inputs.repo }}
REQUESTED_SHA: ${{ inputs.source-sha }}
COMMENT_BODY: ${{ github.event.comment.body }}
WORKFLOW_REF: ${{ github.workflow_ref }}
with:
script: |
const { owner, repo } = context.repo;
const currentRepo = `${owner}/${repo}`;
const shaPattern = /^[0-9a-f]{40}$/i;
const requestedRepo = process.env.TARGET_REPO;
const requestedRef = process.env.TARGET_REF;
const command = process.env.COMMAND;
let sourceSha = process.env.REQUESTED_SHA;
let sourceRepo = requestedRepo;
if (!requestedRef || requestedRef.startsWith('refs/') || requestedRef.startsWith('-') ||
!/^[A-Za-z0-9_.-]+\/[A-Za-z0-9_.-]+$/.test(requestedRepo)) {
throw new Error('The formatter target repository or branch is invalid.');
}

if (context.eventName === 'issue_comment') {
if (!context.payload.issue?.pull_request) {
throw new Error('Formatter commands are available only on pull requests.');
}
if (!sourceSha) {
const expected = new RegExp(`^@${repo}bot ${command}(?: --sha=([0-9a-f]{40}))?$`);
const match = process.env.COMMENT_BODY.match(expected);
if (!match) {
throw new Error(`Use @${repo}bot ${command} with no arguments, or append an optional full SHA.`);
}
sourceSha = match[1];
}
if (sourceSha && !shaPattern.test(sourceSha)) {
throw new Error('Optional formatter SHA must contain exactly 40 hexadecimal characters.');
}
}

if (context.payload.pull_request || context.payload.issue?.pull_request) {
const pullNumber = context.payload.pull_request?.number || context.payload.issue.number;
const { data: pull } = await github.rest.pulls.get({
owner,
repo,
pull_number: pullNumber
});
if (pull.state !== 'open' || pull.head.ref !== requestedRef ||
pull.head.repo?.full_name !== requestedRepo ||
(sourceSha && pull.head.sha.toLowerCase() !== sourceSha.toLowerCase())) {
throw new Error('The PR head moved or does not match the formatter target.');
}
sourceSha = pull.head.sha;
sourceRepo = pull.head.repo.full_name;
} else {
if (requestedRepo !== currentRepo) {
throw new Error('Non-PR formatter runs must target this repository.');
}
if (context.eventName === 'workflow_dispatch') {
const { data: repository } = await github.rest.repos.get({ owner, repo });
if (!process.env.WORKFLOW_REF.endsWith(`@refs/heads/${repository.default_branch}`)) {
throw new Error('Manual formatter runs must use the default-branch workflow.');
}
}
const { data: branch } = await github.rest.repos.getBranch({
owner,
repo,
branch: requestedRef
});
if (sourceSha && !shaPattern.test(sourceSha)) {
throw new Error('Optional formatter SHA must contain exactly 40 hexadecimal characters.');
}
if (sourceSha && branch.commit.sha.toLowerCase() !== sourceSha.toLowerCase()) {
throw new Error('The target branch head does not match the approved source SHA.');
}
sourceSha = branch.commit.sha;
}
core.setOutput('source_sha', sourceSha);
core.setOutput('source_repo', sourceRepo);
169 changes: 169 additions & 0 deletions .github/actions/resolve-source-snapshot/action.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
name: Resolve immutable source snapshot
description: Bind workflow execution to the approved repository commit.

inputs:
command:
description: Bot command for issue-comment requests; leave empty otherwise.
required: false
ref:
description: Target ref from workflow setup.
required: true
repo:
description: Target repository from workflow setup.
required: true
source-sha:
description: Explicit source SHA for manual or reusable workflow requests.
required: false
allow-arguments:
description: Allow a constrained argument list and optional SHA in issue comments.
required: false
default: "false"

outputs:
source_sha:
description: Validated immutable source SHA.
value: ${{ steps.resolve.outputs.source_sha }}
source_repo:
description: Repository containing the validated source SHA.
value: ${{ steps.resolve.outputs.source_repo }}
source_ref:
description: Source branch or event ref associated with the snapshot.
value: ${{ steps.resolve.outputs.source_ref }}
command_args:
description: Constrained arguments preceding an optional SHA in a bot command.
value: ${{ steps.resolve.outputs.command_args }}

runs:
using: composite
steps:
- name: Authorize the immutable source
id: resolve
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
env:
COMMAND: ${{ inputs.command }}
TARGET_REF: ${{ inputs.ref }}
TARGET_REPO: ${{ inputs.repo }}
REQUESTED_SHA: ${{ inputs.source-sha }}
ALLOW_ARGUMENTS: ${{ inputs.allow-arguments }}
COMMENT_BODY: ${{ github.event.comment.body }}
WORKFLOW_REF: ${{ github.workflow_ref }}
with:
script: |
const { owner, repo } = context.repo;
const currentRepo = `${owner}/${repo}`;
const shaPattern = /^[0-9a-f]{40}$/i;
const targetRepo = process.env.TARGET_REPO;
const targetRef = process.env.TARGET_REF;
let sourceSha = process.env.REQUESTED_SHA;
let sourceRepo = targetRepo;
let sourceRef = targetRef;
if (!/^[A-Za-z0-9_.-]+\/[A-Za-z0-9_.-]+$/.test(targetRepo || '') ||
!targetRef || targetRef.startsWith('-')) {
throw new Error('Workflow setup returned an invalid target repository or ref.');
}

if (context.eventName === 'pull_request' ||
(context.eventName === 'workflow_call' && context.payload.pull_request)) {
const pull = context.payload.pull_request;
if (!pull.head.repo) throw new Error('The PR source repository is unavailable.');
if (sourceSha && sourceSha.toLowerCase() !== pull.head.sha.toLowerCase()) {
throw new Error('The requested SHA does not match the PR head.');
}
const { data: currentPull } = await github.rest.pulls.get({
owner,
repo,
pull_number: pull.number
});
if (currentPull.state !== 'open' || currentPull.head.sha !== pull.head.sha ||
currentPull.head.repo?.full_name !== pull.head.repo.full_name ||
currentPull.head.ref !== pull.head.ref) {
throw new Error('The PR head moved while resolving the source snapshot.');
}
sourceSha = currentPull.head.sha;
sourceRepo = currentPull.head.repo.full_name;
sourceRef = currentPull.head.ref;
} else if (context.eventName === 'issue_comment') {
if (!context.payload.issue?.pull_request) {
throw new Error('This workflow command is supported only on pull requests.');
}
const command = process.env.COMMAND;
const prefix = `^@${repo}bot ${command}`;
const expected = process.env.ALLOW_ARGUMENTS === 'true'
? new RegExp(`${prefix}(?:[ \\t]+([A-Za-z0-9+/, -]*?))?(?:[ \\t]+--sha=([0-9a-f]{40}))?$`)
: new RegExp(`${prefix}(?:[ \\t]+--sha=([0-9a-f]{40}))?$`);
const match = process.env.COMMENT_BODY.match(expected);
if (!match) throw new Error(`Invalid @${repo}bot ${command} command.`);
const approvedSha = process.env.ALLOW_ARGUMENTS === 'true' ? match[2] : match[1];
if (sourceSha && !shaPattern.test(sourceSha)) {
throw new Error('The requested source SHA is invalid.');
}
if (approvedSha && !shaPattern.test(approvedSha)) {
throw new Error('The optional command SHA is invalid.');
}
if (approvedSha && sourceSha && sourceSha.toLowerCase() !== approvedSha.toLowerCase()) {
throw new Error('The requested SHA does not match the optional command SHA.');
}
if (process.env.ALLOW_ARGUMENTS === 'true') {
core.setOutput('command_args', (match[1] || '').trim());
}
const { data: pull } = await github.rest.pulls.get({
owner,
repo,
pull_number: context.payload.issue.number
});
const expectedSha = approvedSha || sourceSha;
if (pull.state !== 'open' || (expectedSha && pull.head.sha.toLowerCase() !== expectedSha.toLowerCase()) ||
pull.head.repo?.full_name !== targetRepo ||
pull.head.ref !== targetRef.replace(/^refs\/heads\//, '')) {
throw new Error('The PR head does not match the command target or optional SHA.');
}
sourceSha = pull.head.sha;
sourceRepo = pull.head.repo.full_name;
sourceRef = pull.head.ref;
} else if (context.eventName === 'workflow_dispatch') {
const { data: repository } = await github.rest.repos.get({ owner, repo });
const branchName = context.ref.replace(/^refs\/heads\//, '');
const targetBranch = targetRef.replace(/^refs\/heads\//, '');
if (!context.ref.startsWith('refs/heads/') || targetRepo !== currentRepo ||
targetBranch !== branchName || process.env.WORKFLOW_REF.split('@').pop() !== context.ref ||
!shaPattern.test(sourceSha || '') || sourceSha.toLowerCase() !== context.sha.toLowerCase()) {
throw new Error('Manual runs require an exact SHA for the selected workflow branch.');
}
const { data: branch } = await github.rest.repos.getBranch({ owner, repo, branch: branchName });
if (branch.commit.sha.toLowerCase() !== sourceSha.toLowerCase()) {
throw new Error('The selected branch head does not match the supplied source SHA.');
}
if (branchName === repository.default_branch && branch.commit.sha !== context.sha) {
throw new Error('The default branch moved after this workflow was selected.');
}
sourceSha = branch.commit.sha;
sourceRepo = currentRepo;
sourceRef = context.ref;
} else if (context.eventName === 'push' || context.eventName === 'schedule') {
if (targetRepo !== currentRepo || (sourceSha && sourceSha !== context.sha)) {
throw new Error('Push and schedule runs must use this repository event SHA.');
}
sourceSha = context.sha;
sourceRepo = currentRepo;
sourceRef = context.ref;
} else if (context.eventName === 'workflow_call') {
if (targetRepo !== currentRepo || !shaPattern.test(sourceSha || '')) {
throw new Error('Reusable non-PR runs require this repository and a full source SHA.');
}
const branchName = targetRef.replace(/^refs\/heads\//, '');
const { data: branch } = await github.rest.repos.getBranch({ owner, repo, branch: branchName });
if (branch.commit.sha.toLowerCase() !== sourceSha.toLowerCase()) {
throw new Error('Reusable workflow target branch does not match its source SHA.');
}
sourceSha = branch.commit.sha;
sourceRepo = currentRepo;
sourceRef = branchName;
} else {
throw new Error(`Unsupported source event: ${context.eventName}`);
}

if (!shaPattern.test(sourceSha || '')) throw new Error('Resolved source SHA is invalid.');
core.setOutput('source_sha', sourceSha);
core.setOutput('source_repo', sourceRepo);
core.setOutput('source_ref', sourceRef);
if (process.env.ALLOW_ARGUMENTS !== 'true') core.setOutput('command_args', '');
2 changes: 2 additions & 0 deletions .github/codecov/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Dependabot updates this pin; workflows pass its version to codecov-action.
codecov-cli==11.3.2
4 changes: 3 additions & 1 deletion .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ updates:
schedule:
interval: "weekly"
- package-ecosystem: "pip"
directory: "/ci"
directories:
- "/ci"
- "/.github/codecov"
schedule:
interval: "weekly"
Loading
Loading