Skip to content
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

Merged
merged 12 commits into from
Jan 26, 2019

Conversation

sinchang
Copy link
Contributor

fixes #16

@@ -21,8 +21,16 @@ class UserNotFoundError extends AllContributorBotError {
}
}

class ReferenceExistsError extends AllContributorBotError {
constructor() {
super(`A branch or Pull Request is already open.`)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

error message any suggestions?

Copy link
Contributor

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

Copy link
Member

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
Copy link

codecov bot commented Jan 16, 2019

Codecov Report

Merging #48 into master will increase coverage by 28.73%.
The diff coverage is 100%.

Impacted file tree graph

@@             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
Impacted Files Coverage Δ
src/processIssueComment.js 92.3% <100%> (ø)
src/serverless-webhook.js 0% <0%> (-65.39%) ⬇️
src/serverless-health-check.js
src/index.js 100% <0%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update f72ee4a...2a6bfc6. Read the comment docs.

.reply(422)

const error = await rejectionOf(
repository.createPullRequestFromFiles({
Copy link
Contributor

@jakebolam jakebolam Jan 16, 2019

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?

Copy link
Contributor Author

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.

Copy link
Contributor

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?

Copy link
Contributor Author

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.

Copy link
Contributor Author

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

Copy link
Member

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.

Copy link
Contributor

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

})
} catch (error) {
if (error.code === 422) {
throw new ReferenceExistsError()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@sinchang
Copy link
Contributor Author

sinchang commented Jan 17, 2019

some cases:

  1. branch not exists and pr not open - create branch and open pr

  2. branch exists and pr not open - update branch and open pr

  3. branch exists and pr open - update branch

  4. branch exists and pr closed - update branch and create a new pr

still, need to add some tests.

@jakebolam
Copy link
Contributor

jakebolam commented Jan 17, 2019

@sinchang those cases look good. This one is hard, keep at it!

@Berkmann18
Copy link
Member

Here's one more case (although casi unlikely to occur):
5. branch doesn't exist but there's a pr open - move the pr to the correct branch

I think that's all possible cases unless we're missing a variable/factor?


return false
} catch (error) {
// branch already exists
Copy link
Member

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?

branchName,
})
return pullRequestURL
} catch (error) {
Copy link
Contributor

@jakebolam jakebolam Jan 19, 2019

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

return pullRequestURL
} catch (error) {
if (error.status === 422)
throw new AllContributorBotError('Pull request is already open')
Copy link
Contributor

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

@@ -61,17 +71,17 @@ class Repository {
return multipleFilesByPath
}

async getHeadRef(defaultBranch) {
async getHeadRef(branchName) {
const result = await this.github.git.getRef({
Copy link
Contributor

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

await repository.getHeadRef(branchName)
repository.setBasedBranch(branchName)
} catch (error) {
if (error.status !== 404) throw error
Copy link
Contributor

@jakebolam jakebolam Jan 19, 2019

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)

})

try {
Copy link
Contributor

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

Copy link
Contributor

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)

@@ -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 () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nb] and pr closed

src/Repository/index.js Outdated Show resolved Hide resolved
src/Repository/index.js Outdated Show resolved Hide resolved
Copy link
Contributor

@jakebolam jakebolam left a 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).

setBasedBranch(branchName) {
this.basedBranch = branchName
}

async getFile(filePath) {
Copy link
Contributor

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

Copy link
Contributor

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.

@jakebolam jakebolam added this to Reviewer approved in All Contributors Kanban Jan 21, 2019
@jakebolam
Copy link
Contributor

@sinchang are you ok if I get this over the line?

@sinchang
Copy link
Contributor Author

@jakebolam ok

@jakebolam
Copy link
Contributor

@sinchang THIS IS AWESOME, THANK YOU

@jakebolam jakebolam merged commit ab0857b into all-contributors:master Jan 26, 2019
All Contributors Kanban automation moved this from Reviewer approved to Done Jan 26, 2019
@jakebolam
Copy link
Contributor

@all-contributors please add @sinchang for ideas

@allcontributors
Copy link
Contributor

@jakebolam

I've put up a pull request to add @sinchang! 🎉

@all-contributors-release-bot
Copy link
Member

🎉 This PR is included in version 1.0.1 🎉

The release is available on GitHub release

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
No open projects
Development

Successfully merging this pull request may close these issues.

Handle case where branch already exists
4 participants