Commit d7fe22c7f70f870e6eccda97ea452482536126ba
1 parent
5d6b3215
Add Model to access The gitlab API.
Showing
10 changed files
with
115 additions
and
2 deletions
app/models/gitlab.rb
0 → 100644
app/models/gitlab/project.rb
0 → 100644
| 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: | ... | ... |
config/gitlab.yml
0 → 100644
| 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: | ... | ... |
test/fixtures/gitlab/projects.yml
0 → 100644
| 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 | ... | ... |
Please
register
or
login
to post a comment