-
Notifications
You must be signed in to change notification settings - Fork 707
Add detection for stacked PRs across forks error in GitHub API responses #10654
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
The latest updates on your projects. Learn more about Vercel for GitHub.
|
…edentials Co-authored-by: Byron <[email protected]>
8386d65
to
9371137
Compare
@estib-vega I took a good look at the code and it seems sound, while solving the stated problem at least according to the tests. I don't know though if this is the right approach at all. Is this salvageable? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking pretty good!
test('should not detect stacked PR error for other validation errors', async () => { | ||
const mockError = { | ||
message: 'Validation Failed', | ||
response: { | ||
data: { | ||
message: 'Validation Failed', | ||
errors: [ | ||
{ | ||
resource: 'PullRequest', | ||
field: 'title', | ||
code: 'missing' | ||
} | ||
] | ||
} | ||
} | ||
}; | ||
|
||
vi.spyOn(octokit.pulls, 'create').mockRejectedValue(mockError); | ||
|
||
try { | ||
await service?.createPr({ | ||
title: 'Test PR', | ||
body: 'Test body', | ||
draft: false, | ||
baseBranchName: 'feature-branch', | ||
upstreamName: 'my-branch' | ||
}); | ||
expect.fail('Should have thrown an error'); | ||
} catch (err: any) { | ||
expect(err.code).not.toBe(Code.GitHubStackedPrFork); | ||
} | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps we could manually remove this test case, it feels unnecessary.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, done.
const message = isErrorlike(err) ? err.message : String(err); | ||
const code = message.startsWith('Not Found -') ? Code.GitHubTokenExpired : undefined; | ||
|
||
// Check for stacked PR across forks error (base field invalid) | ||
let code: string | undefined; | ||
if (isErrorlike(err) && 'response' in err) { | ||
const response = (err as any).response; | ||
if (response?.data?.errors instanceof Array) { | ||
const hasInvalidBaseError = response.data.errors.some( | ||
(error: any) => | ||
error.resource === 'PullRequest' && error.field === 'base' && error.code === 'invalid' | ||
); | ||
if (hasInvalidBaseError) { | ||
code = Code.GitHubStackedPrFork; | ||
} | ||
} | ||
} | ||
|
||
// Check for expired token | ||
if (!code && message.startsWith('Not Found -')) { | ||
code = Code.GitHubTokenExpired; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This block could be improved extracting a type guard called isGitHubError
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot Extract the type guard as isGitHubError
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Extracted the type guard as isGitHubError
with a proper GitHubErrorResponse
interface. This improves type safety and makes the error checking logic more readable. Commit: Extract isGitHubError type guard as suggested
Thanks for the review @mtsgrd. I did what I could and hope the copilot comes back with the type guard extraction. Can I ask you to take it from there to get it merged if good? PS: The first time I see how this works, but looks like Copilot just runs as part of an action :): https://github.com/gitbutlerapp/gitbutler/actions/runs/18523828069/job/52789809376 (this particular one is also failing with 500 server error, maybe due to a vibe-coded server ;)). |
Co-authored-by: Byron <[email protected]>
isGitHubError
type guard to improve code clarityOriginal prompt
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.