Skip to content

Commit c395c28

Browse files
committed
export for given user, organization or repo id
1 parent 81ff4b6 commit c395c28

File tree

5 files changed

+146
-22
lines changed

5 files changed

+146
-22
lines changed

lib/backup.rb

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,24 @@ def connect_db
2020
ActiveRecord::Base.establish_connection(@config.database_url)
2121
end
2222

23-
def export(owner_id = nil)
23+
def export(args={})
24+
if args[:user_id]
25+
owner_id = args[:user_id]
26+
owner_type = 'User'
27+
elsif args[:org_id]
28+
owner_id = args[:org_id]
29+
owner_type = 'Organization'
30+
elsif args[:repo_id]
31+
repo_id = args[:repo_id]
32+
end
33+
2434
if owner_id
25-
Repository.where('owner_id = ?', owner_id).order(:id).each do |repository|
35+
Repository.where('owner_id = ? and owner_type = ?', owner_id, owner_type).order(:id).each do |repository|
2636
process_repo(repository)
2737
end
38+
elsif repo_id
39+
repository = Repository.find(repo_id)
40+
process_repo(repository)
2841
else
2942
Repository.order(:id).each do |repository|
3043
process_repo(repository)

lib/models/organization.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# frozen_string_literal: true
2+
3+
require 'models/model'
4+
5+
class Organization < Model
6+
self.table_name = 'organizations'
7+
end

lib/models/user.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# frozen_string_literal: true
2+
3+
require 'models/model'
4+
5+
class User < Model
6+
self.table_name = 'users'
7+
end

spec/backup_spec.rb

Lines changed: 85 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,37 +4,102 @@
44
require 'models/repository'
55
require 'models/build'
66
require 'models/job'
7+
require 'models/organization'
8+
require 'models/user'
79
require 'support/factories'
810
require 'pry'
911

1012
describe Backup do
1113
before(:all) do
12-
system("psql '#{Config.new.database_url}' -f db/schema.sql")
14+
system("psql '#{Config.new.database_url}' -f db/schema.sql > /dev/null")
1315
end
1416

15-
after(:each) do
16-
Repository.destroy_all
17-
Build.destroy_all
18-
Job.destroy_all
19-
end
20-
21-
let!(:config) { Config.new }
2217
let(:files_location) { "dump/tests" }
2318
let!(:backup) { Backup.new(files_location: files_location, limit: 2) }
24-
let(:datetime) { (config.delay + 1).months.ago.to_time.utc }
25-
let(:org_id) { rand(100000) }
26-
let(:com_id) { rand(100000) }
27-
let(:private_org_id) { rand(100000) }
28-
let(:private_com_id) { rand(100000) }
29-
let!(:repository) {
30-
FactoryBot.create(
31-
:repository_with_builds,
32-
created_at: datetime,
33-
updated_at: datetime
34-
)
35-
}
19+
20+
describe 'export' do
21+
let!(:unassigned_repositories) {
22+
(1..3).to_a.map do
23+
FactoryBot.create(:repository)
24+
end
25+
}
26+
let!(:user1) {
27+
FactoryBot.create(:user_with_repos)
28+
}
29+
let!(:user2) {
30+
FactoryBot.create(:user_with_repos)
31+
}
32+
let!(:organization1) {
33+
FactoryBot.create(:organization_with_repos)
34+
}
35+
let!(:organization2) {
36+
FactoryBot.create(:organization_with_repos)
37+
}
38+
39+
context 'when no arguments are given' do
40+
it 'processes every repository' do
41+
Repository.all.each do |repository|
42+
expect(backup).to receive(:process_repo).once.with(repository)
43+
end
44+
backup.export
45+
end
46+
end
47+
context 'when user_id is given' do
48+
it 'processes only the repositories of the given user' do
49+
processed_repos_ids = []
50+
allow(backup).to receive(:process_repo) {|repo| processed_repos_ids.push(repo.id)}
51+
user_repos_ids = Repository.where(
52+
'owner_id = ? and owner_type = ?',
53+
user1.id,
54+
'User'
55+
).map(&:id)
56+
backup.export(user_id: user1.id)
57+
expect(processed_repos_ids).to match_array(user_repos_ids)
58+
end
59+
end
60+
context 'when org_id is given' do
61+
it 'processes only the repositories of the given organization' do
62+
processed_repos_ids = []
63+
allow(backup).to receive(:process_repo) {|repo| processed_repos_ids.push(repo.id)}
64+
organization_repos_ids = Repository.where(
65+
'owner_id = ? and owner_type = ?',
66+
organization1.id,
67+
'Organization'
68+
).map(&:id)
69+
backup.export(org_id: organization1.id)
70+
expect(processed_repos_ids).to match_array(organization_repos_ids)
71+
end
72+
end
73+
context 'when repo_id is given' do
74+
it 'processes only the repository with the given id' do
75+
repo = Repository.first
76+
expect(backup).to receive(:process_repo).once.with(repo)
77+
backup.export(repo_id: repo.id)
78+
end
79+
end
80+
end
3681

3782
describe 'process_repo' do
83+
after(:each) do
84+
Repository.destroy_all
85+
Build.destroy_all
86+
Job.destroy_all
87+
end
88+
89+
let!(:config) { Config.new }
90+
let(:datetime) { (config.delay + 1).months.ago.to_time.utc }
91+
let(:org_id) { rand(100000) }
92+
let(:com_id) { rand(100000) }
93+
let(:private_org_id) { rand(100000) }
94+
let(:private_com_id) { rand(100000) }
95+
let!(:repository) {
96+
FactoryBot.create(
97+
:repository_with_builds,
98+
created_at: datetime,
99+
updated_at: datetime
100+
)
101+
}
102+
38103
let!(:exported_object) {
39104
[[
40105
{

spec/support/factories.rb

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,38 @@
33
require 'factory_bot'
44

55
FactoryBot.define do
6+
factory :organization do
7+
factory :organization_with_repos do
8+
transient do
9+
repos_count { 3 }
10+
end
11+
after(:create) do |organization, evaluator|
12+
create_list(
13+
:repository,
14+
evaluator.repos_count,
15+
owner_id: organization.id,
16+
owner_type: 'Organization'
17+
)
18+
end
19+
end
20+
end
21+
22+
factory :user do
23+
factory :user_with_repos do
24+
transient do
25+
repos_count { 3 }
26+
end
27+
after(:create) do |user, evaluator|
28+
create_list(
29+
:repository,
30+
evaluator.repos_count,
31+
owner_id: user.id,
32+
owner_type: 'User'
33+
)
34+
end
35+
end
36+
end
37+
638
factory :repository do
739
factory :repository_with_builds do
840
transient do

0 commit comments

Comments
 (0)