Browse Source

Add Model to access The gitlab API.

master
Georg Hopp 10 years ago
parent
commit
d7fe22c7f7
  1. 3
      .gitignore
  2. 6
      app/controllers/welcome_controller.rb
  3. 3
      app/models/gitlab.rb
  4. 63
      app/models/gitlab/project.rb
  5. 3
      config/environments/development.rb
  6. 3
      config/environments/production.rb
  7. 3
      config/environments/test.rb
  8. 15
      config/gitlab.yml
  9. 11
      test/fixtures/gitlab/projects.yml
  10. 7
      test/models/gitlab/project_test.rb

3
.gitignore

@ -18,3 +18,6 @@
# Ignore vim swp files
.*.sw?
# Ignore changes on the application config.
/config/gitlab.yml

6
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:

3
app/models/gitlab.rb

@ -0,0 +1,3 @@
module Gitlab
end
# vim: set ts=2 sw=2:

63
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:

3
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

3
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

3
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

15
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:

11
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

7
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
Loading…
Cancel
Save