This repository was archived by the owner on Mar 29, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 142
Handle GitHub errors #412
Closed
Closed
Handle GitHub errors #412
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
5febf67
Add GithubErrorHandler
jhaff 9b6dcf3
Use GithubErrorHandler in ImportPrMetadataService
jhaff b5b66d0
Use GithubErrorHandler in ImportReposMetadataService
jhaff b6e4602
Add user to UserNotFoundOnGithubError
jhaff c538aae
Add last_error to User
jhaff 533a412
Pass user into GithubErrorHadler
jhaff 8c6c4e4
lint
jhaff e78ac41
Add inactive state to user
jhaff 8ca2f64
Deactivate user when processing user missing error
jhaff 7c6fa4b
Fix validations for User#deactivate
jhaff 48662dc
Fix ImportReposMetadataSerivce to call with_error_handling correctly
jhaff 119e5e6
Add tests for GithubErrorHandler
jhaff 8b6be09
Switch case when statement on error rather than error.class.to_s
jhaff dacbaa9
Add state_before_inactive to User
jhaff a884d9b
Add case for #deacitvate to UserStateTransitionSegmentService
jhaff 8fb6769
Save state_before_inactive on User before transition to inactive
jhaff 09f0424
Linting
jhaff ea0e178
Fix failing specs
jhaff a02f06a
Merge branch 'master' into handle-github-errors
mkcode 6f516f1
Use GithubErrorHandler in TryUserTransitionService
jhaff 2917b55
Use transition.from to set state_before_inactive on User
jhaff bb360de
Validate presence of state_before_inactive for inactive state
jhaff b1dc096
Merge branch 'handle-github-errors' of github.com:raise-dev/hacktober…
jhaff File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# frozen_string_literal: true | ||
|
||
module GithubErrorHandler | ||
module_function | ||
|
||
HANDLED_ERRORS = [ | ||
Octokit::AccountSuspended, | ||
GithubPullRequestService::UserNotFoundOnGithubError | ||
].freeze | ||
|
||
def with_error_handling(user) | ||
yield | ||
rescue *HANDLED_ERRORS => e | ||
process_github_error(e, user) | ||
end | ||
|
||
def process_github_error(error, user) | ||
# rubocop:disable Rails/SkipsModelValidations | ||
user.update_attribute(:last_error, error.class) | ||
# rubocop:enable Rails/SkipsModelValidations | ||
case error | ||
when Octokit::AccountSuspended | ||
process_user_missing_error(error, user) | ||
when GithubPullRequestService::UserNotFoundOnGithubError | ||
process_user_missing_error(error, user) | ||
else | ||
raise error | ||
end | ||
end | ||
|
||
def process_user_missing_error(_error, user) | ||
user.deactivate | ||
|
||
return unless (user_stat = UserStat.where(user_id: user.id).first) | ||
|
||
user_stat.destroy | ||
end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# frozen_string_literal: true | ||
|
||
class AddLastErrorToUser < ActiveRecord::Migration[5.2] | ||
def change | ||
add_column :users, :last_error, :string | ||
end | ||
end |
7 changes: 7 additions & 0 deletions
7
db/migrate/20191111195936_add_state_before_inactive_to_user.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# frozen_string_literal: true | ||
|
||
class AddStateBeforeInactiveToUser < ActiveRecord::Migration[5.2] | ||
def change | ||
add_column :users, :state_before_inactive, :string | ||
end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'rails_helper' | ||
|
||
RSpec.describe GithubErrorHandler do | ||
let(:user) { FactoryBot.create(:user) } | ||
|
||
# stub API call that will happen due to User#deactivate | ||
before do | ||
allow(UserStateTransitionSegmentService) | ||
.to receive(:call).and_return(true) | ||
end | ||
|
||
describe '.process_github_error' do | ||
context 'The error is UserNotFoundOnGithubError' do | ||
let(:error) { GithubPullRequestService::UserNotFoundOnGithubError.new } | ||
|
||
it 'calls process_user_missing_error' do | ||
expect(GithubErrorHandler) | ||
.to receive(:process_user_missing_error).with(error, user) | ||
|
||
GithubErrorHandler.process_github_error(error, user) | ||
end | ||
|
||
it 'correctly updates the last_error column on the user' do | ||
GithubErrorHandler.process_github_error(error, user) | ||
|
||
expect(user.last_error).to eq(error.class.to_s) | ||
end | ||
end | ||
|
||
context 'The error is Octokit::AccountSuspended' do | ||
let(:error) { Octokit::AccountSuspended.new } | ||
|
||
it 'calls process_user_missing_error' do | ||
expect(GithubErrorHandler) | ||
.to receive(:process_user_missing_error).with(error, user) | ||
|
||
GithubErrorHandler.process_github_error(error, user) | ||
end | ||
|
||
it 'correctly updates the last_error column on the user' do | ||
GithubErrorHandler.process_github_error(error, user) | ||
|
||
expect(user.last_error).to eq(error.class.to_s) | ||
end | ||
end | ||
|
||
context 'The error is an unhandled error' do | ||
let(:error) { StandardError.new } | ||
|
||
it 'raises the error' do | ||
expect { GithubErrorHandler.process_github_error(error, user) } | ||
.to raise_error(StandardError) | ||
end | ||
end | ||
end | ||
|
||
describe '.process_user_missing_error' do | ||
let(:error) { GithubPullRequestService::UserNotFoundOnGithubError.new } | ||
it 'destroys the UserStat' do | ||
UserStat.create(user_id: user.id, data: user) | ||
|
||
GithubErrorHandler.process_user_missing_error(error, user) | ||
|
||
expect(UserStat.count).to eq(0) | ||
end | ||
|
||
it 'deactivates the user' do | ||
# to pass user#deactivate validations | ||
allow(user) | ||
.to receive(:last_error).and_return('Octokit::AccountSuspended') | ||
|
||
GithubErrorHandler.process_user_missing_error(error, user) | ||
|
||
expect(user.state).to eq('inactive') | ||
end | ||
end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.