-
Notifications
You must be signed in to change notification settings - Fork 150
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
fix: handle error when branch already exists #48
Conversation
src/utils/errors.js
Outdated
@@ -21,8 +21,16 @@ class UserNotFoundError extends AllContributorBotError { | |||
} | |||
} | |||
|
|||
class ReferenceExistsError extends AllContributorBotError { | |||
constructor() { | |||
super(`A branch or Pull Request is already open.`) |
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.
error message any suggestions?
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 Branch ${branch} already exists
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.
^ so at least it indicates which branch.
Codecov Report
@@ Coverage Diff @@
## master #48 +/- ##
===========================================
+ Coverage 30.35% 59.09% +28.73%
===========================================
Files 3 4 +1
Lines 56 110 +54
Branches 8 11 +3
===========================================
+ Hits 17 65 +48
- Misses 37 42 +5
- Partials 2 3 +1
Continue to review full report at Codecov.
|
test/Repository/index.test.js
Outdated
.reply(422) | ||
|
||
const error = await rejectionOf( | ||
repository.createPullRequestFromFiles({ |
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.
When we are creating the pull request and the branch exists can we recover somehow?
E.g. delete the branch then create. Or Maybe have a unique I'd for the branch we are creating (using UUID) to avoid collisions?
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.
I prefer the second suggestion.
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.
Yeah I think so to.
Alternatively if there was a way to differentiate between 'you already have a branch open' and 'you already have a pull request open'. The pull request being checked first tho.
What are your thoughts on the best User Experience here?
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.
If the branch exists, whether or not the pr is opened, we update the file on the existing branch.
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.
Create a unique branch so that it is the easiest and the smallest to change
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.
If the branch is already open, I think that the bot should be able to (re)-use it.
If the PR is already open then it shouldn't be doing anything unless it's changing/adding/removing something else.
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.
To re-use the branch, we need to re get the original sha's / generate off the branch (not master/the default branch).
😅
Yes that may make the most sense
src/Repository/index.js
Outdated
}) | ||
} catch (error) { | ||
if (error.code === 422) { | ||
throw new ReferenceExistsError() |
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.
👍
some cases:
still, need to add some tests. |
@sinchang those cases look good. This one is hard, keep at it! |
Here's one more case (although casi unlikely to occur): I think that's all possible cases unless we're missing a variable/factor? |
src/Repository/index.js
Outdated
|
||
return false | ||
} catch (error) { | ||
// branch already exists |
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.
What would be the advantage and reason for not handling situations where this.github.git.createRef
fails and throws an error?
src/Repository/index.js
Outdated
branchName, | ||
}) | ||
return pullRequestURL | ||
} catch (error) { |
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 catch and error thrown should be done in the createPullRequest
method
src/Repository/index.js
Outdated
return pullRequestURL | ||
} catch (error) { | ||
if (error.status === 422) | ||
throw new AllContributorBotError('Pull request is already open') |
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.
Can we add a link?
I've updated the [pull request](INSERT_LINK_HERE) you have open.
I don't think this should error too, could we handle this as a reply case here: (https://github.com/all-contributors/all-contributors-bot/blob/master/src/processIssueComment.js#L65-L67) e.g. can throw the error, but catch it here
src/Repository/index.js
Outdated
@@ -61,17 +71,17 @@ class Repository { | |||
return multipleFilesByPath | |||
} | |||
|
|||
async getHeadRef(defaultBranch) { | |||
async getHeadRef(branchName) { | |||
const result = await this.github.git.getRef({ |
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.
Instead of checking 404 above when called.
Can we throw an error here? (BranchAlreadyExists) and catch that
src/processIssueComment.js
Outdated
await repository.getHeadRef(branchName) | ||
repository.setBasedBranch(branchName) | ||
} catch (error) { | ||
if (error.status !== 404) throw error |
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.
See other comment and consider using here:
if (error instance of BranchAlreadyExists)
src/processIssueComment.js
Outdated
}) | ||
|
||
try { |
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 should happen after fetching config IMO
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 should happen inside the processAddContributor
block. (For future support of other actions)
test/index-e2e.test.js
Outdated
@@ -273,4 +304,148 @@ describe('All Contributors app - End to end', () => { | |||
) | |||
expect(error instanceof Error).toBeTruthy() | |||
}) | |||
|
|||
test('Happy path, add correct new contributor, but branch exists', async () => { |
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.
[nb] and pr closed
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 is a really good solution @sinchang! Thanks for doing this.
Overall really happy with the approach!
Just some comments/feedback to make it easier for future developers to understand (please reply to my comments if you think they don't make sense).
src/Repository/index.js
Outdated
setBasedBranch(branchName) { | ||
this.basedBranch = branchName | ||
} | ||
|
||
async getFile(filePath) { |
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.
I wonder how we can take the latest master file and overwrite the current branch :think: like a rebase
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.
We probably shouldn't address this here. This is probably a whole ticket in itself. #62.
I'm interested in hearing how you would solve this problem tho.
@sinchang are you ok if I get this over the line? |
@jakebolam ok |
@sinchang THIS IS AWESOME, THANK YOU |
@all-contributors please add @sinchang for ideas |
I've put up a pull request to add @sinchang! 🎉 |
🎉 This PR is included in version 1.0.1 🎉 The release is available on GitHub release Your semantic-release bot 📦🚀 |
fixes #16