diff --git a/app/services/github/errors.rb b/app/services/github/errors.rb index 51ac71268..74e620875 100644 --- a/app/services/github/errors.rb +++ b/app/services/github/errors.rb @@ -17,5 +17,11 @@ module Github class InvalidRepository < ArgumentError end + + class AccountSuspended < ClientError + end + + class RepositoryUnavailable < ClientError + end end end diff --git a/app/workers/github_repos/repo_sync_worker.rb b/app/workers/github_repos/repo_sync_worker.rb index ab5791fb3..7a5e7bb25 100644 --- a/app/workers/github_repos/repo_sync_worker.rb +++ b/app/workers/github_repos/repo_sync_worker.rb @@ -27,12 +27,10 @@ module GithubRepos info_hash: fetched_repo.to_hash, ) repo.user&.touch(:github_repos_updated_at) - rescue Github::Errors::NotFound, Github::Errors::Unauthorized - repo.destroy - rescue Github::Errors::ClientError => e - msg = e.message - raise e unless msg.include?("Your account was suspended") || msg.include?("Repository access blocked") - + rescue Github::Errors::NotFound, + Github::Errors::Unauthorized, + Github::Errors::AccountSuspended, + Github::Errors::RepositoryUnavailable repo.destroy end end diff --git a/spec/services/github/oauth_client_spec.rb b/spec/services/github/oauth_client_spec.rb index 22f307230..3cbf7f31e 100644 --- a/spec/services/github/oauth_client_spec.rb +++ b/spec/services/github/oauth_client_spec.rb @@ -26,6 +26,28 @@ RSpec.describe Github::OauthClient, type: :service, vcr: true do end end + describe ".repository" do + subject(:client) { described_class.new(client_id: "value", client_secret: "value") } + + let(:repo_id) { 100 } + + context "when the Github account to which the repo belongs to is suspended" do + it "returns a Github::Errors::AccountSuspended error" do + stub_request(:get, %r{repositories/#{repo_id}}).to_return(body: "account was suspended", status: 403) + + expect { client.repository(repo_id) }.to raise_error(Github::Errors::AccountSuspended) + end + end + + context "when the repo is unavailable" do + it "returns a Github::Errors::RepositoryUnavailable error" do + stub_request(:get, %r{repositories/#{repo_id}}).to_return(body: "repository access blocked", status: 403) + + expect { client.repository(repo_id) }.to raise_error(Github::Errors::RepositoryUnavailable) + end + end + end + describe ".issue" do it "returns a an issue using the client_id/client_secret" do VCR.use_cassette("github_client_issue") do diff --git a/spec/workers/github_repos/repo_sync_worker_spec.rb b/spec/workers/github_repos/repo_sync_worker_spec.rb index 4b804889a..cb64624a1 100644 --- a/spec/workers/github_repos/repo_sync_worker_spec.rb +++ b/spec/workers/github_repos/repo_sync_worker_spec.rb @@ -52,7 +52,9 @@ RSpec.describe GithubRepos::RepoSyncWorker, type: :worker do it "destroys suspended account repos" do repo_id = repo.id - client_error = Github::Errors::ClientError.new(message: "GET https:// 403 - Sorry. Your account was suspended.") + client_error = Github::Errors::AccountSuspended.new( + message: "GET https:// 403 - Sorry. Your account was suspended.", + ) allow(github_client).to receive(:repository).and_raise(client_error) worker.perform(repo.id) @@ -61,11 +63,20 @@ RSpec.describe GithubRepos::RepoSyncWorker, type: :worker do it "destroys blocked access repos" do repo_id = repo.id - client_error = Github::Errors::ClientError.new(message: "GET https:// 451 - Repository access blocked.") + client_error = Github::Errors::RepositoryUnavailable.new(message: "GET https:// 451 - Repository access blocked.") allow(github_client).to receive(:repository).and_raise(client_error) worker.perform(repo.id) expect(GithubRepo.find_by(id: repo_id)).to be_nil end + + it "retains the repo on an unexpected Github client error" do + repo_id = repo.id + client_error = Github::Errors::ServerError.new(message: "GET https:// 500 - Internal Server Error") + allow(github_client).to receive(:repository).and_raise(client_error) + + expect { worker.perform(repo.id) }.to raise_error(client_error) + expect(GithubRepo.find_by(id: repo_id)).to be_present + end end end