Skip to content

PR Creation

PR Creation

Fixwright can automatically create GitHub pull requests for validated fixes. This page covers PR configuration and customization.

Configuration

import { FixWright } from '@korvol/fixwright';
const fixwright = new FixWright({
ai: { apiKey: process.env.ANTHROPIC_API_KEY! },
github: {
token: process.env.GITHUB_TOKEN!,
owner: 'your-org',
repo: 'your-repo',
},
git: {
baseBranch: 'main',
fixBranchPrefix: 'fix/stepwright-',
},
});

GitHub Configuration Options

token

GitHub personal access token with repo permissions:

github: {
token: process.env.GITHUB_TOKEN!,
}

Required scopes:

  • repo (full control of private repos)
  • Or public_repo (for public repos only)

owner

GitHub organization or username:

github: {
owner: 'your-org', // or 'your-username'
}

repo

Repository name:

github: {
repo: 'your-repo',
}

PR Events

pr:created

Emitted when a PR is successfully created:

fixwright.on('pr:created', (prUrl) => {
console.log('Pull request created:', prUrl);
// https://github.com/org/repo/pull/123
});

Accessing PR Details

Get full PR information from the fix completion event:

fixwright.on('failure:completed', (summary) => {
if (summary.status === 'fixed') {
console.log('PR Number:', summary.prNumber);
console.log('PR URL:', summary.prUrl);
}
});

PR Content

Title Format

fix(stepwright): <script-name> - <step-name>

Example:

fix(stepwright): Login Flow - Click login button

Body Structure

The PR body includes:

## Automated Fix by FixWright
### Failure Details
- **Script**: Login Flow
- **Failed Step**: Click login button
- **Error Type**: TimeoutError
- **Error Message**: Timeout 30000ms exceeded waiting for selector "#login-btn"
### Analysis
**Cause**: selector_changed
The button ID changed from #login-btn to #submit-login.
The original selector no longer matches any element on the page.
### Fix Applied
Updated selector from #login-btn to #submit-login.
```diff
- await page.click('#login-btn');
+ await page.click('#submit-login');

Validation

  • Failing step now passes
  • Full script runs successfully

This PR was automatically generated by FixWright. Please review before merging.

Fixes: fc-2024-01-15-abc123

## PRCreator API
For advanced usage, use PRCreator directly:
```typescript
import { PRCreator } from '@korvol/fixwright';
const prCreator = new PRCreator({
token: process.env.GITHUB_TOKEN!,
owner: 'your-org',
repo: 'your-repo',
});
// Create a PR
const result = await prCreator.createPR(
failureCase,
fixAttempt,
'fix/stepwright-fc-abc123',
'main'
);
if (result.success) {
console.log('PR #', result.prNumber);
console.log('URL:', result.prUrl);
}

PRCreator Methods

MethodDescription
createPR(failure, fix, branch, base)Create a new PR
addComment(prNumber, comment)Add comment to PR
findExistingPR(branchName)Check if PR exists

Adding Comments

Add comments to existing PRs:

await prCreator.addComment(123, `
## Additional Information
The fix was validated on commit \`abc123\`.
Test results:
- Login step: ✅ Passed
- Dashboard step: ✅ Passed
`);

Finding Existing PRs

Check if a PR already exists before creating:

const existingPR = await prCreator.findExistingPR('fix/stepwright-fc-abc123');
if (existingPR) {
console.log('PR already exists:', existingPR);
await prCreator.addComment(existingPR, 'Updated with new fix attempt');
} else {
await prCreator.createPR(...);
}

Customizing PR Content

Custom Labels

Add labels via GitHub API after creation:

fixwright.on('pr:created', async (prUrl) => {
const prNumber = parseInt(prUrl.split('/').pop()!);
await fetch(`https://api.github.com/repos/${owner}/${repo}/issues/${prNumber}/labels`, {
method: 'POST',
headers: {
Authorization: `Bearer ${token}`,
Accept: 'application/vnd.github.v3+json',
},
body: JSON.stringify({
labels: ['automated', 'fixwright', 'needs-review'],
}),
});
});

Requesting Reviewers

fixwright.on('pr:created', async (prUrl) => {
const prNumber = parseInt(prUrl.split('/').pop()!);
await fetch(`https://api.github.com/repos/${owner}/${repo}/pulls/${prNumber}/requested_reviewers`, {
method: 'POST',
headers: {
Authorization: `Bearer ${token}`,
Accept: 'application/vnd.github.v3+json',
},
body: JSON.stringify({
reviewers: ['team-lead', 'qa-engineer'],
}),
});
});

Skipping PR Creation

No GitHub Config

If github is not configured, PRs are not created:

const fixwright = new FixWright({
ai: { apiKey: process.env.ANTHROPIC_API_KEY! },
// No github config - fixes are applied but no PR created
});

Conditional PR Creation

Create PRs only for certain failures:

fixwright.on('attempt:success', async (attempt) => {
const shouldCreatePR = attempt.failureCase.script.name.includes('critical');
if (!shouldCreatePR) {
console.log('Skipping PR creation for non-critical script');
return;
}
// Use PRCreator directly
const prCreator = new PRCreator(config);
await prCreator.createPR(...);
});

Error Handling

Common PR Errors

fixwright.on('error', (error) => {
if (error.message.includes('401')) {
console.error('Invalid GitHub token');
}
if (error.message.includes('403')) {
console.error('Insufficient permissions');
}
if (error.message.includes('422')) {
console.error('PR already exists or branch not found');
}
});

Token Permissions

Ensure your token has:

repo (Full control of private repositories)
├── repo:status
├── repo_deployment
├── public_repo
├── repo:invite
└── security_events

Or for public repos only:

public_repo

GitHub Actions Integration

Use the built-in GITHUB_TOKEN:

- name: Fix Failures
run: npx tsx scripts/fix-failures.ts
env:
ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

In your script:

const fixwright = new FixWright({
ai: { apiKey: process.env.ANTHROPIC_API_KEY! },
github: {
token: process.env.GITHUB_TOKEN!,
owner: context.repo.owner,
repo: context.repo.repo,
},
});

Best Practices

1. Use Bot Accounts

Create a dedicated bot account for Fixwright PRs:

  • Clear attribution
  • Separate from developer tokens
  • Easy to audit

2. Review Before Merging

Always review automated PRs:

  • Verify the fix makes sense
  • Check for unintended changes
  • Ensure tests pass

3. Add Context

Include relevant information in PR comments:

  • Link to CI failure
  • Include artifact screenshots
  • Reference related issues

4. Use Protected Branches

Require review for fix branches:

  • Prevent auto-merge of bad fixes
  • Ensure human oversight

Next Steps