Commit d7fe22c7f70f870e6eccda97ea452482536126ba

Authored by Georg Hopp
1 parent 5d6b3215

Add Model to access The gitlab API.

@@ -18,3 +18,6 @@ @@ -18,3 +18,6 @@
18 18
19 # Ignore vim swp files 19 # Ignore vim swp files
20 .*.sw? 20 .*.sw?
  21 +
  22 +# Ignore changes on the application config.
  23 +/config/gitlab.yml
1 class WelcomeController < ApplicationController 1 class WelcomeController < ApplicationController
2 - def index  
3 - end 2 + def index
  3 + @projects = Gitlab::Project.all
  4 + end
4 end 5 end
  6 +# vim: set ts=2 sw=2:
  1 +module Gitlab
  2 +end
  3 +# vim: set ts=2 sw=2:
  1 +class Gitlab::Project
  2 + include ActiveModel::Model
  3 +
  4 + attr_accessor :id, :description, :default_branch, :tag_list, :public,
  5 + :archived, :visibility_level, :ssh_url_to_repo, :http_url_to_repo,
  6 + :web_url, :name, :name_with_namespace, :path, :path_with_namespace,
  7 + :issues_enabled, :merge_requests_enabled, :wiki_enabled, :builds_enabled,
  8 + :snippets_enabled, :created_at, :last_activity_at,
  9 + :shared_runners_enabled, :creator_id, :namespace, :owner, :avatar_url,
  10 + :star_count, :forks_count, :open_issues_count, :public_builds,
  11 + :permissions
  12 +
  13 + def self.all
  14 + all = Array.new;
  15 +
  16 + api_base_uri = Rails.configuration.x.gitlab['api_base_uri']
  17 + auth_token = Rails.configuration.x.gitlab['auth_token']
  18 +
  19 + links = {
  20 + 'next' => api_base_uri + 'projects?visibility=public',
  21 + 'first' => nil,
  22 + 'last' => nil
  23 + }
  24 +
  25 + uri = URI.parse(links['next'])
  26 + http = Net::HTTP.new(uri.host, uri.port)
  27 + http.use_ssl = true # TODO make this aware of http/https
  28 + http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  29 +
  30 + while true
  31 + request = Net::HTTP::Get.new(uri.request_uri)
  32 + request['PRIVATE-TOKEN'] = auth_token
  33 +
  34 + response = http.request(request)
  35 +
  36 + JSON.parse(response.body).each do |project|
  37 + all.push(Gitlab::Project.new(project))
  38 + end
  39 +
  40 + links = response['link'].split(',').map! { |a|
  41 + _tmp = a.strip.split(';').map! { |a|
  42 + a.strip
  43 + }
  44 + {_tmp[1][5...-1] => _tmp[0][1...-1]}
  45 + }.reduce({}, :merge)
  46 +
  47 + break unless links['next']
  48 + uri = URI.parse(links['next'])
  49 + end
  50 +
  51 + return all
  52 + end
  53 +
  54 + def self.find
  55 + end
  56 +
  57 + def deliver
  58 + if valid?
  59 + # deliver email
  60 + end
  61 + end
  62 +end
  63 +# vim: set ts=2 sw=2:
@@ -38,4 +38,7 @@ Rails.application.configure do @@ -38,4 +38,7 @@ Rails.application.configure do
38 38
39 # Raises error for missing translations 39 # Raises error for missing translations
40 # config.action_view.raise_on_missing_translations = true 40 # config.action_view.raise_on_missing_translations = true
  41 +
  42 + # Load Gitlab configuration
  43 + config.x.gitlab = config_for(:gitlab)
41 end 44 end
@@ -76,4 +76,7 @@ Rails.application.configure do @@ -76,4 +76,7 @@ Rails.application.configure do
76 76
77 # Do not dump schema after migrations. 77 # Do not dump schema after migrations.
78 config.active_record.dump_schema_after_migration = false 78 config.active_record.dump_schema_after_migration = false
  79 +
  80 + # Load Gitlab configuration
  81 + config.x.gitlab = config_for(:gitlab)
79 end 82 end
@@ -39,4 +39,7 @@ Rails.application.configure do @@ -39,4 +39,7 @@ Rails.application.configure do
39 39
40 # Raises error for missing translations 40 # Raises error for missing translations
41 # config.action_view.raise_on_missing_translations = true 41 # config.action_view.raise_on_missing_translations = true
  42 +
  43 + # Load Gitlab configuration
  44 + config.x.gitlab = config_for(:gitlab)
42 end 45 end
  1 +---
  2 +default: &default
  3 + api_base_uri: https://your.gitlab.com/api/v3/
  4 + # get the auth_token from your gitlab preferences
  5 + auth_token: xxxxxxxxxxxxxxx
  6 +
  7 +development:
  8 + <<: *default
  9 +
  10 +test:
  11 + <<: *default
  12 +
  13 +production:
  14 + <<: *default
  15 +# vim: set ts=2 sw=2:
  1 +# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
  2 +
  3 +# This model initially had no columns defined. If you add columns to the
  4 +# model remove the '{}' from the fixture names and add the columns immediately
  5 +# below each fixture, per the syntax in the comments below
  6 +#
  7 +one: {}
  8 +# column: value
  9 +#
  10 +two: {}
  11 +# column: value
  1 +require 'test_helper'
  2 +
  3 +class Gitlab::ProjectTest < ActiveSupport::TestCase
  4 + # test "the truth" do
  5 + # assert true
  6 + # end
  7 +end
Please register or login to post a comment