From d7fe22c7f70f870e6eccda97ea452482536126ba Mon Sep 17 00:00:00 2001 From: Georg Hopp Date: Sat, 2 Apr 2016 19:54:01 +0200 Subject: [PATCH] Add Model to access The gitlab API. --- .gitignore | 3 ++ app/controllers/welcome_controller.rb | 6 ++- app/models/gitlab.rb | 3 ++ app/models/gitlab/project.rb | 63 +++++++++++++++++++++++++++ config/environments/development.rb | 3 ++ config/environments/production.rb | 3 ++ config/environments/test.rb | 3 ++ config/gitlab.yml | 15 +++++++ test/fixtures/gitlab/projects.yml | 11 +++++ test/models/gitlab/project_test.rb | 7 +++ 10 files changed, 115 insertions(+), 2 deletions(-) create mode 100644 app/models/gitlab.rb create mode 100644 app/models/gitlab/project.rb create mode 100644 config/gitlab.yml create mode 100644 test/fixtures/gitlab/projects.yml create mode 100644 test/models/gitlab/project_test.rb diff --git a/.gitignore b/.gitignore index ebcacb5..0b8d29e 100644 --- a/.gitignore +++ b/.gitignore @@ -18,3 +18,6 @@ # Ignore vim swp files .*.sw? + +# Ignore changes on the application config. +/config/gitlab.yml diff --git a/app/controllers/welcome_controller.rb b/app/controllers/welcome_controller.rb index f9b859b..d99acfb 100644 --- a/app/controllers/welcome_controller.rb +++ b/app/controllers/welcome_controller.rb @@ -1,4 +1,6 @@ class WelcomeController < ApplicationController - def index - end + def index + @projects = Gitlab::Project.all + end end +# vim: set ts=2 sw=2: diff --git a/app/models/gitlab.rb b/app/models/gitlab.rb new file mode 100644 index 0000000..02eb59f --- /dev/null +++ b/app/models/gitlab.rb @@ -0,0 +1,3 @@ +module Gitlab +end +# vim: set ts=2 sw=2: diff --git a/app/models/gitlab/project.rb b/app/models/gitlab/project.rb new file mode 100644 index 0000000..bde8e84 --- /dev/null +++ b/app/models/gitlab/project.rb @@ -0,0 +1,63 @@ +class Gitlab::Project + include ActiveModel::Model + + attr_accessor :id, :description, :default_branch, :tag_list, :public, + :archived, :visibility_level, :ssh_url_to_repo, :http_url_to_repo, + :web_url, :name, :name_with_namespace, :path, :path_with_namespace, + :issues_enabled, :merge_requests_enabled, :wiki_enabled, :builds_enabled, + :snippets_enabled, :created_at, :last_activity_at, + :shared_runners_enabled, :creator_id, :namespace, :owner, :avatar_url, + :star_count, :forks_count, :open_issues_count, :public_builds, + :permissions + + def self.all + all = Array.new; + + api_base_uri = Rails.configuration.x.gitlab['api_base_uri'] + auth_token = Rails.configuration.x.gitlab['auth_token'] + + links = { + 'next' => api_base_uri + 'projects?visibility=public', + 'first' => nil, + 'last' => nil + } + + uri = URI.parse(links['next']) + http = Net::HTTP.new(uri.host, uri.port) + http.use_ssl = true # TODO make this aware of http/https + http.verify_mode = OpenSSL::SSL::VERIFY_NONE + + while true + request = Net::HTTP::Get.new(uri.request_uri) + request['PRIVATE-TOKEN'] = auth_token + + response = http.request(request) + + JSON.parse(response.body).each do |project| + all.push(Gitlab::Project.new(project)) + end + + links = response['link'].split(',').map! { |a| + _tmp = a.strip.split(';').map! { |a| + a.strip + } + {_tmp[1][5...-1] => _tmp[0][1...-1]} + }.reduce({}, :merge) + + break unless links['next'] + uri = URI.parse(links['next']) + end + + return all + end + + def self.find + end + + def deliver + if valid? + # deliver email + end + end +end +# vim: set ts=2 sw=2: diff --git a/config/environments/development.rb b/config/environments/development.rb index b55e214..8d5d333 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -38,4 +38,7 @@ Rails.application.configure do # Raises error for missing translations # config.action_view.raise_on_missing_translations = true + + # Load Gitlab configuration + config.x.gitlab = config_for(:gitlab) end diff --git a/config/environments/production.rb b/config/environments/production.rb index 5c1b32e..17c3d40 100644 --- a/config/environments/production.rb +++ b/config/environments/production.rb @@ -76,4 +76,7 @@ Rails.application.configure do # Do not dump schema after migrations. config.active_record.dump_schema_after_migration = false + + # Load Gitlab configuration + config.x.gitlab = config_for(:gitlab) end diff --git a/config/environments/test.rb b/config/environments/test.rb index 1c19f08..973fede 100644 --- a/config/environments/test.rb +++ b/config/environments/test.rb @@ -39,4 +39,7 @@ Rails.application.configure do # Raises error for missing translations # config.action_view.raise_on_missing_translations = true + + # Load Gitlab configuration + config.x.gitlab = config_for(:gitlab) end diff --git a/config/gitlab.yml b/config/gitlab.yml new file mode 100644 index 0000000..12b7f03 --- /dev/null +++ b/config/gitlab.yml @@ -0,0 +1,15 @@ +--- +default: &default + api_base_uri: https://your.gitlab.com/api/v3/ + # get the auth_token from your gitlab preferences + auth_token: xxxxxxxxxxxxxxx + +development: + <<: *default + +test: + <<: *default + +production: + <<: *default +# vim: set ts=2 sw=2: diff --git a/test/fixtures/gitlab/projects.yml b/test/fixtures/gitlab/projects.yml new file mode 100644 index 0000000..937a0c0 --- /dev/null +++ b/test/fixtures/gitlab/projects.yml @@ -0,0 +1,11 @@ +# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +# This model initially had no columns defined. If you add columns to the +# model remove the '{}' from the fixture names and add the columns immediately +# below each fixture, per the syntax in the comments below +# +one: {} +# column: value +# +two: {} +# column: value diff --git a/test/models/gitlab/project_test.rb b/test/models/gitlab/project_test.rb new file mode 100644 index 0000000..75178a7 --- /dev/null +++ b/test/models/gitlab/project_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class Gitlab::ProjectTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end