Skip to content

Commit c7a0755

Browse files
committed
Adds logic to Repo#update_from_github handle more sittuations (like removeal of the repo and change of properties)
1 parent 86bac12 commit c7a0755

File tree

2 files changed

+85
-9
lines changed

2 files changed

+85
-9
lines changed

app/models/repo.rb

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -192,12 +192,26 @@ def self.exists_with_name?(name)
192192
end
193193

194194
def update_from_github
195-
json = fetcher.as_json
196-
self.update(
197-
language: json.fetch('language', language),
198-
description: json.fetch('description', description)&.first(255),
199-
stars_count: json.fetch('stargazers_count', stars_count)
200-
)
195+
if fetcher.not_found?
196+
self.update!(removed_from_github: true)
197+
elsif fetcher.success?
198+
repo_full_name = fetcher_json.fetch('full_name', full_name)
199+
200+
if repo_full_name != full_name && self.class.exists_with_name?(repo_full_name)
201+
# TODO: Add deduplication step
202+
return
203+
end
204+
205+
repo_user_name, repo_name = repo_full_name.split("/")
206+
self.update!(
207+
name: repo_name,
208+
user_name: repo_user_name,
209+
language: fetcher_json.fetch('language', language),
210+
description: fetcher_json.fetch('description', description)&.first(255),
211+
full_name: repo_full_name,
212+
removed_from_github: false
213+
)
214+
end
201215
end
202216

203217
def repo_path

test/jobs/update_repo_info_job_test.rb

Lines changed: 65 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,70 @@
22

33
require 'test_helper'
44

5+
# frozen_string_literal: true
6+
7+
require 'test_helper'
58
class UpdateRepoInfoJobTest < ActiveJob::TestCase
6-
# test "the truth" do
7-
# assert true
8-
# end
9+
test 'repo deleted or made private' do
10+
GithubFetcher::Resource.any_instance.stubs(:status).returns(404)
11+
@repo = repos(:node)
12+
assert_changes -> {
13+
[
14+
@repo.reload.removed_from_github,
15+
]
16+
} do
17+
UpdateRepoInfoJob.perform_now(@repo)
18+
end
19+
assert @repo.reload.removed_from_github
20+
end
21+
22+
test 'repo with information updated' do
23+
GithubFetcher::Resource.any_instance.stubs(:status).returns(200)
24+
GithubFetcher::Resource.any_instance.stubs(:as_json).returns(
25+
{
26+
'full_name' => 'test_owner/test_repo',
27+
'language' => 'test_language',
28+
'description' => 'test_description'
29+
}
30+
)
31+
@repo = repos(:node)
32+
assert_changes -> {
33+
[
34+
@repo.reload.full_name,
35+
@repo.reload.name,
36+
@repo.reload.user_name,
37+
@repo.reload.language,
38+
@repo.reload.description,
39+
]
40+
} do
41+
UpdateRepoInfoJob.perform_now(@repo)
42+
end
43+
assert_equal false, @repo.reload.removed_from_github
44+
assert_equal 'test_owner/test_repo', @repo.reload.full_name
45+
assert_equal 'test_repo', @repo.reload.name
46+
assert_equal 'test_owner', @repo.reload.user_name
47+
assert_equal 'test_language', @repo.reload.language
48+
assert_equal 'test_description', @repo.reload.description
49+
end
50+
51+
test 'repo rename conflict' do
52+
GithubFetcher::Resource.any_instance.stubs(:status).returns(200)
53+
GithubFetcher::Resource.any_instance.stubs(:as_json).returns(
54+
{
55+
'full_name' => 'sinatra/sinatra',
56+
}
57+
)
58+
@repo = repos(:node)
59+
assert_no_changes -> {
60+
[
61+
@repo.reload.full_name,
62+
@repo.reload.name,
63+
@repo.reload.user_name,
64+
@repo.reload.language,
65+
@repo.reload.description,
66+
]
67+
} do
68+
UpdateRepoInfoJob.perform_now(@repo)
69+
end
70+
end
971
end

0 commit comments

Comments
 (0)