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
129 changes: 129 additions & 0 deletions .github/actions/collect-review/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
name: 'Collect the review'
description: >
Collects a pull request and the maintainers' comments on it into review.json in the directory,
for the revision agent, and uploads the directory as an artifact. Only people with write access
count as maintainers: nobody else's comments get through, and no resolved threads. Fails when
there is nothing to address.
inputs:
pull-request:
description: 'Number of the pull request'
required: true
dir:
description: 'Directory to write review.json to; uploaded as the artifact'
required: false
default: ${{ runner.temp }}/review
artifact-name:
description: 'Name of the uploaded artifact'
required: false
default: 'review'
token:
description: 'Token that reads the pull request and the collaborators'
required: false
default: ${{ github.token }}
outputs:
head:
description: 'Head commit of the pull request'
value: ${{ steps.collect.outputs.head }}
base:
description: 'Merge base of the pull request with its base branch'
value: ${{ steps.collect.outputs.base }}
branch:
description: 'Head branch of the pull request'
value: ${{ steps.collect.outputs.branch }}
runs:
using: composite
steps:
- name: Collect the pull request and the maintainers' comments
id: collect
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
env:
PULL_NUMBER: ${{ inputs.pull-request }}
DIR: ${{ inputs.dir }}
with:
github-token: ${{ inputs.token }}
script: |
const fs = require('fs');
const path = require('path');
const { PULL_NUMBER, DIR } = process.env;
const { owner, repo } = context.repo;
const pull_number = Number(PULL_NUMBER);
fs.mkdirSync(DIR, { recursive: true });

const { data: pr } = await github.rest.pulls.get({ owner, repo, pull_number });
const { data: { merge_base_commit: base } } = await github.rest.repos.compareCommitsWithBasehead({ owner, repo, basehead: `${pr.base.ref}...${pr.head.sha}` });
const commits = await github.paginate(github.rest.pulls.listCommits, { owner, repo, pull_number, per_page: 100 });
const { repository: { pullRequest: data } } = await github.graphql(`
query($owner: String!, $repo: String!, $number: Int!) {
repository(owner: $owner, name: $repo) {
pullRequest(number: $number) {
comments(last: 100) { totalCount nodes { author { __typename login } body createdAt url } }
reviews(last: 100) { totalCount nodes { author { __typename login } body state createdAt url } }
reviewThreads(last: 100) {
totalCount
nodes {
isResolved isOutdated path line originalLine
comments(first: 100) { totalCount nodes { author { __typename login } body createdAt url diffHunk } }
}
}
}
}
}`, { owner, repo, number: pull_number });
for (const [name, connection] of [['comments', data.comments], ['reviews', data.reviews], ['review threads', data.reviewThreads], ...data.reviewThreads.nodes.map(t => [`comments in a thread on ${t.path}`, t.comments])]) {
if (connection.totalCount > connection.nodes.length)
core.warning(`Read only ${connection.nodes.length} of ${connection.totalCount} ${name}`);
}

// Only people with write access count as maintainers. The agent never sees anyone else's comments.
const maintainers = new Map();
const isMaintainer = async author => {
if (author?.__typename !== 'User')
return false;
if (!maintainers.has(author.login)) {
const { data } = await github.rest.repos.getCollaboratorPermissionLevel({ owner, repo, username: author.login }).catch(() => ({ data: {} }));
maintainers.set(author.login, ['admin', 'write'].includes(data.permission));
}
return maintainers.get(author.login);
};
const comment = c => ({ author: c.author.login, createdAt: c.createdAt, url: c.url, body: c.body });

const comments = [];
for (const c of data.comments.nodes) {
if (await isMaintainer(c.author))
comments.push({ kind: 'comment', ...comment(c) });
}
for (const r of data.reviews.nodes) {
if (r.body.trim() && await isMaintainer(r.author))
comments.push({ kind: 'review', state: r.state, ...comment(r) });
}
for (const t of data.reviewThreads.nodes) {
if (t.isResolved)
continue;
const thread = [];
for (const c of t.comments.nodes) {
if (await isMaintainer(c.author))
thread.push(comment(c));
}
if (thread.length)
comments.push({ kind: 'thread', path: t.path, line: t.line ?? t.originalLine, outdated: t.isOutdated, diffHunk: t.comments.nodes[0].diffHunk, createdAt: thread[0].createdAt, comments: thread });
}
comments.sort((a, b) => a.createdAt.localeCompare(b.createdAt));
if (!comments.length)
throw new Error('There are no comments from maintainers to address, other than in resolved threads');

fs.writeFileSync(path.join(DIR, 'review.json'), JSON.stringify({
url: pr.html_url,
title: pr.title,
body: pr.body,
head: pr.head.sha,
base: base.sha,
commits: commits.map(c => ({ sha: c.sha, date: c.commit.committer.date, message: c.commit.message })),
comments,
}, null, 2));
core.setOutput('head', pr.head.sha);
core.setOutput('base', base.sha);
core.setOutput('branch', pr.head.ref);
- name: Upload the review
uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2
with:
name: ${{ inputs.artifact-name }}
path: ${{ inputs.dir }}
110 changes: 110 additions & 0 deletions .github/actions/publish-patch/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
name: 'Publish a patch'
description: >
Publishes the patch an agent left in its output directory, from a job that has write credentials
and never runs the agent's code. Applies the patch to the checkout, rejects one that changes
anything under .github/, commits it as the Playwright automation app with the title and body from
the metadata file, pushes it to the branch and, when asked, opens the branch as a draft pull
request with the body file as its description.
inputs:
token:
description: 'Token with write access to contents and pull requests, from the app'
required: true
dir:
description: 'Directory with the patch, the metadata and the body file'
required: true
patch:
description: 'File name of the patch in dir'
required: true
metadata:
description: 'File name of the JSON in dir with the commit title and, optionally, body'
required: true
branch:
description: 'Branch to push to; created when it does not exist, never force-pushed'
required: true
commit-body:
description: 'Commit body when the metadata has none, such as the Fixes: line'
required: false
default: ''
pull-request:
description: 'true opens a draft pull request from the branch; false only pushes'
required: false
default: 'false'
base:
description: 'Base branch of the pull request'
required: false
default: 'main'
body-file:
description: 'File name of the pull request description in dir'
required: false
default: ''
workflow-name:
description: 'How the pull request footer names the workflow'
required: false
default: 'triage workflow'
outputs:
commit:
description: 'The pushed commit'
value: ${{ steps.publish.outputs.commit }}
pull-request:
description: 'URL of the pull request, when one was opened'
value: ${{ steps.publish.outputs.pull-request }}
number:
description: 'Number of the pull request, when one was opened'
value: ${{ steps.publish.outputs.number }}
runs:
using: composite
steps:
- name: Validate, commit, push and open
id: publish
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
env:
DIR: ${{ inputs.dir }}
PATCH: ${{ inputs.patch }}
METADATA: ${{ inputs.metadata }}
BRANCH: ${{ inputs.branch }}
COMMIT_BODY: ${{ inputs.commit-body }}
PULL_REQUEST: ${{ inputs.pull-request }}
BASE: ${{ inputs.base }}
BODY_FILE: ${{ inputs.body-file }}
WORKFLOW_NAME: ${{ inputs.workflow-name }}
RUN_URL: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}
with:
github-token: ${{ inputs.token }}
script: |
const fs = require('fs');
const path = require('path');
const { execFileSync } = require('child_process');
const { DIR, PATCH, METADATA, BRANCH, COMMIT_BODY, PULL_REQUEST, BASE, BODY_FILE, WORKFLOW_NAME, RUN_URL } = process.env;
const git = (...args) => execFileSync('git', args, { encoding: 'utf8' }).trim();
const { owner, repo } = context.repo;
git('apply', '--index', path.join(DIR, PATCH));

// Workflow and action changes would run with this repo's secrets in PR CI.
const workflowFiles = git('diff', '--cached', '--name-only', '--no-renames', '-z').split('\0').filter(file => file.startsWith('.github/'));
if (workflowFiles.length)
throw new Error(`Patch changes workflow files, treating it as compromised: ${workflowFiles.join(', ')}`);

const { title, body } = JSON.parse(fs.readFileSync(path.join(DIR, METADATA), 'utf8'));
if (typeof title !== 'string' || !title.trim())
throw new Error(`${METADATA} needs a title`);
const commitBody = typeof body === 'string' && body.trim() ? body : COMMIT_BODY;
git('config', 'user.name', 'microsoft-playwright-automation[bot]');
git('config', 'user.email', '203992400+microsoft-playwright-automation[bot]@users.noreply.github.com');
git('commit', '-m', title, ...(commitBody ? ['-m', commitBody] : []));
// Not a force push: it fails if an existing branch moved after the agent started.
git('push', 'origin', `HEAD:refs/heads/${BRANCH}`);
const commit = git('rev-parse', 'HEAD');
core.setOutput('commit', commit);
if (PULL_REQUEST !== 'true') {
await core.summary.addRaw(`Pushed https://github.com/${owner}/${repo}/commit/${commit}\n`).write();
return;
}
const description = BODY_FILE ? fs.readFileSync(path.join(DIR, BODY_FILE), 'utf8').trim() : '';
const { data: pr } = await github.rest.pulls.create({
owner, repo, head: BRANCH, base: BASE, title,
body: `${description}\n\n<sub>Prepared by the [${WORKFLOW_NAME}](${RUN_URL}).</sub>`,
draft: true,
});
core.setOutput('pull-request', pr.html_url);
core.setOutput('number', pr.number);
await core.summary.addRaw(`Draft pull request: ${pr.html_url}\n`).write();
106 changes: 106 additions & 0 deletions .github/actions/run-agent/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
name: 'Run the agent'
description: >
Runs the Copilot CLI unattended with the prompt, the way the triage and revision workflows do: all
tools and paths allowed, no questions, the session transcript in the output directory. The agent
leaves its results as files in the output directory. The action reports whether the result file
is there, writes the agent's summary.md into the run summary and uploads the output directory as
an artifact, also when the session failed or ran out of budget. The Copilot CLI has to be
installed already.
inputs:
prompt:
description: 'The prompt'
required: true
output-dir:
description: 'Directory the agent writes its results to; uploaded as the artifact'
required: true
result-file:
description: 'Path in the output directory the agent writes last when it has a result, such as fix/proposal.json'
required: true
artifact-name:
description: 'Name of the uploaded artifact'
required: true
github-tools:
description: 'true gives the agent the GitHub MCP tools with the token; false gives it no built-in MCP servers and no URL access'
required: false
default: 'false'
exec:
description: 'Command the Copilot CLI runs through, such as a wrapper that enters a container or sets the I/O priority'
required: false
default: ''
working-directory:
description: 'Directory the agent runs in'
required: false
default: '.'
model:
description: 'Model for the Copilot CLI'
required: false
default: 'gpt-6.1-sol'
max-ai-credits:
description: 'Budget for the session'
required: false
default: '5000'
token:
description: 'Token for the Copilot CLI and its GitHub tools'
required: false
default: ${{ github.token }}
outputs:
outcome:
description: 'success or failure of the Copilot session'
value: ${{ steps.agent.outcome }}
has-result:
description: 'true when the result file exists'
value: ${{ steps.save.outputs.has-result }}
runs:
using: composite
steps:
- name: Run the Copilot CLI
id: agent
# A finished result is still useful if the session exhausts its budget.
continue-on-error: true
shell: bash
working-directory: ${{ inputs.working-directory }}
env:
COPILOT_GITHUB_TOKEN: ${{ inputs.token }}
PROMPT: ${{ inputs.prompt }}
OUTPUT_DIR: ${{ inputs.output-dir }}
WRAPPER: ${{ inputs.exec }}
TOOLS: ${{ inputs.github-tools == 'true' && '--enable-all-github-mcp-tools' || '--disable-builtin-mcps' }}
MODEL: ${{ inputs.model }}
MAX_AI_CREDITS: ${{ inputs.max-ai-credits }}
run: |
mkdir -p "$OUTPUT_DIR"
$WRAPPER copilot \
--allow-all-tools \
--allow-all-paths \
--no-ask-user \
"$TOOLS" \
--share="$OUTPUT_DIR/copilot-session.md" \
--model "$MODEL" \
--max-ai-credits "$MAX_AI_CREDITS" \
-p "$PROMPT"
- name: Save the outcome
id: save
shell: bash
env:
OUTPUT_DIR: ${{ inputs.output-dir }}
RESULT_FILE: ${{ inputs.result-file }}
AGENT_OUTCOME: ${{ steps.agent.outcome }}
run: |
mkdir -p "$OUTPUT_DIR"
if [[ -f "$OUTPUT_DIR/$RESULT_FILE" ]]; then
echo "has-result=true" >> "$GITHUB_OUTPUT"
else
echo "has-result=false" >> "$GITHUB_OUTPUT"
fi
echo "Copilot step: $AGENT_OUTCOME" >> "$GITHUB_STEP_SUMMARY"
if [[ -f "$OUTPUT_DIR/summary.md" ]]; then
cat "$OUTPUT_DIR/summary.md" >> "$GITHUB_STEP_SUMMARY"
else
echo "No summary was produced; see the session artifact." >> "$GITHUB_STEP_SUMMARY"
fi
- name: Upload the output directory
uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2
with:
name: ${{ inputs.artifact-name }}
path: ${{ inputs.output-dir }}
if-no-files-found: warn
Loading
Loading