Class: Giticious::Service::Repository

Inherits:
Object
  • Object
show all
Defined in:
lib/giticious/service/repository.rb

Instance Method Summary collapse

Instance Method Details

#add_user(repo_name, username, permissions) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/giticious/service/repository.rb', line 133

def add_user(repo_name, username, permissions)
  repo = Giticious::Model::Repository.find_by_name(repo_name)

  if repo.nil?
    raise ArgumentError, "The selected repository does not exist"
  end

  user = Giticious::Model::User.find_by_username(username)

  if user.nil?
    raise ArgumentError, "The selected user does not exist"
  end

  if Giticious::Model::Permission.where(user_id: user.id, repository_id: repo.id).empty? == false
    raise ArgumentError, "This user does already have permissions"
  end

  perm = Giticious::Model::Permission.new
  perm.repository_id = repo.id
  perm.user_id = user.id
  perm.perm_read = permissions.include?("r")
  perm.perm_write = permissions.include?("w")

  perm.save!
end

#build_repo_path(name) ⇒ Object



9
10
11
12
13
14
15
# File 'lib/giticious/service/repository.rb', line 9

def build_repo_path(name)
  if name.empty?
    raise ArgumentError, "repo name may not be empty"
  end

  "#{Dir.home}/repositories/#{name}.git"
end

#build_temp_path(name) ⇒ Object



17
18
19
# File 'lib/giticious/service/repository.rb', line 17

def build_temp_path(name)
  "#{Dir.home}/.giticious/temp_#{File.basename(name)}"
end

#create(name) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/giticious/service/repository.rb', line 52

def create(name)
  path = build_repo_path(name)

  if init_repository(path) == false
    raise RuntimeError, "Could not init Git repository"
  end

  repo = Giticious::Model::Repository.new
  repo.name = name
  repo.path = path

  repo.save!
end

#delete(name) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/giticious/service/repository.rb', line 66

def delete(name)
  repo = Giticious::Model::Repository.find_by_name(name)

  if repo.nil?
    raise ArgumentError, "The selected repository does not exist"
  end

  Giticious::Model::Permission.where(repository_id: repo.id).delete_all
  FileUtils.rm_rf(build_repo_path(repo.name))

  repo.delete
end

#delete_user(repo_name, username) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/giticious/service/repository.rb', line 111

def delete_user(repo_name, username)
  repo = Giticious::Model::Repository.find_by_name(repo_name)

  if repo.nil?
    raise ArgumentError, "The selected repository does not exist"
  end

  user = Giticious::Model::User.find_by_username(username)

  if user.nil?
    raise ArgumentError, "The selected user does not exist"
  end

  perms = Giticious::Model::Permission.where(user_id: user.id, repository_id: repo.id)

  if perms.empty?
    raise ArgumentError, "This user does not have permissions"
  end

  perms.delete_all
end

#import(url) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/giticious/service/repository.rb', line 21

def import(url)
  target_repo_name = File.basename(url).gsub(".git", "")
  
  if create(target_repo_name) == false
    raise RuntimeError, "Could not create repository"
  end

  temp_name = build_temp_path(url)  

  begin
    new_url = build_repo_path(target_repo_name)

    `git clone #{url} #{temp_name}`

    if $?.success? == false
      raise RuntimeError, "Could not clone repo URL"
    end

    `cd #{temp_name} && git remote add new #{new_url} && git push --all new`

    if $?.success? == false
      raise RuntimeError, "Could not import repo"
    end
  rescue => e
    delete(target_repo_name)
    raise e
  finally
    FileUtils.rm_rf(temp_name)
  end
end

#listObject



5
6
7
# File 'lib/giticious/service/repository.rb', line 5

def list
  Giticious::Model::Repository.all
end

#permissions(repo) ⇒ Object



79
80
81
82
83
84
85
86
87
# File 'lib/giticious/service/repository.rb', line 79

def permissions(repo)
  repo = Giticious::Model::Repository.find_by_name(repo)

  if repo.nil?
    raise ArgumentError, "The selected repository does not exist"
  end

  repo.permissions
end

#permissions_for(repo_name, username) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/giticious/service/repository.rb', line 89

def permissions_for(repo_name, username)
  repo = Giticious::Model::Repository.find_by_name(repo_name)

  if repo.nil?
    raise ArgumentError, "The selected repository does not exist"
  end

  user = Giticious::Model::User.find_by_username(username)

  if user.nil?
    raise ArgumentError, "The selected user does not exist"
  end

  permissions = Giticious::Model::Permission.where(user_id: user.id, repository_id: repo.id)

  if permissions.empty?
    return false
  end

  return permissions.first!
end