Skip to content

Commit 99b5183

Browse files
committed
Add import all repos feature
If you put an org or username in the new projects page, it'll look up all repos for that user and import them all.
1 parent 6ed7eb5 commit 99b5183

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

app/controllers/projects_controller.rb

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
limitations under the License.
1515
=end
1616

17+
require_relative "#{Rails.root}/lib/github_api"
18+
1719
class ProjectsController < ApplicationController
1820
def index
1921
@projects = Projects
@@ -24,9 +26,14 @@ def edit
2426
end
2527

2628
def create
27-
@project = Projects.new(create_project_params)
29+
params = create_project_params
2830
begin
29-
@project.save
31+
if params[:name].include?('/')
32+
@project = import_project(params[:name], params[:rule_sets])
33+
else
34+
@project = import_user(params[:name], params[:rule_sets])
35+
end
36+
3037
redirect_to action: 'index'
3138
rescue Sequel::ValidationFailed
3239
render 'new'
@@ -56,6 +63,21 @@ def destroy
5663

5764
private
5865

66+
def import_user(username, rule_sets)
67+
github_token = Configurations.first.github_token
68+
gh = GitHubAPI.new(github_token)
69+
repo_names = gh.get_repo_names(username)
70+
repo_names.each do |repo_name|
71+
import_project("#{username}/#{repo_name}", rule_sets)
72+
end
73+
end
74+
75+
def import_project(name, rule_sets)
76+
project = Projects.new({ name: name, rule_sets: rule_sets })
77+
project.save
78+
project
79+
end
80+
5981
def create_project_params
6082
params.require(:project).permit(:name, :rule_sets)
6183
end

app/views/projects/new.html.erb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ limitations under the License.
3434
</div>
3535
<% end %>
3636

37+
<p>
38+
Using a group or username will cause all repositories of that name to be imported from GitHub.
39+
</p>
40+
3741
<p>
3842
<%= f.label :name %><br>
3943
<%= f.text_field :name %>

0 commit comments

Comments
 (0)