Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions app/services/repo_host/github_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def repository_exists?

case response.status.code
when 200 then true
when 404 then github_repo_scope?(response) ? false : nil
when 404 then false
else
Rails.logger.warn "[#{self.class.name}] Could not verify #{owner}/#{repo}: #{response.status}"
nil
Expand Down Expand Up @@ -59,10 +59,6 @@ def fetch_repo_metadata

private

def github_repo_scope?(response)
response.headers["X-OAuth-Scopes"].to_s.split(",").map(&:strip).include?("repo")
end

def api_headers
self.class.api_headers_for(user.github_access_token)
end
Expand Down
28 changes: 4 additions & 24 deletions test/models/project_repo_mapping_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,39 +36,19 @@ class ProjectRepoMappingTest < ActiveSupport::TestCase
assert_predicate mapping, :valid?
end

test "inaccessible GitHub repository URLs are invalid" do
test "nonexistent GitHub repository URLs are invalid" do
user = User.create!(github_access_token: "github-token")
stub_request(:get, "https://api.github.com/repos/example/missing")
.to_return(
status: 404,
body: '{"message":"Not Found"}',
headers: { "X-OAuth-Scopes" => "repo, user:email" }
)
stub_request(:get, "https://api.github.com/repos/hackcl/hackatime")
.to_return(status: 404, body: '{"message":"Not Found"}')
mapping = user.project_repo_mappings.build(
project_name: "missing",
repo_url: "https://github.com/example/missing"
repo_url: "https://github.com/hackcl/hackatime"
)

assert_not mapping.valid?
assert_includes mapping.errors[:repo_url], "does not exist or is not accessible"
end

test "a private repository hidden from a limited token is not treated as nonexistent" do
user = User.create!(github_access_token: "github-token")
stub_request(:get, "https://api.github.com/repos/example/private")
.to_return(
status: 404,
body: '{"message":"Not Found"}',
headers: { "X-OAuth-Scopes" => "user:email" }
)
mapping = user.project_repo_mappings.build(
project_name: "private",
repo_url: "https://github.com/example/private"
)

assert_predicate mapping, :valid?
end

test "temporary GitHub failures do not mark repository URLs as nonexistent" do
user = User.create!(github_access_token: "github-token")
stub_request(:get, "https://api.github.com/repos/example/repository")
Expand Down