summaryrefslogtreecommitdiff
path: root/lib/gitlab_stats.rb
blob: 5b412638caa0f77e649c19b3ccece56f3a387313 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
require 'gitlab'
require 'date'

Gitlab.configure do |config|
  # API endpoint URL, default
  config.endpoint       = ENV['GITLAB_ENDPOINT']

  # User's private token or OAuth2 access token
  config.private_token  = ENV['GITLAB_TOKEN']
end

module GitlabStats
  # get a list of all projects
  def self.projects
    Gitlab.group(ENV['GITLAB_GROUP_ID']).projects.map do |proj|
      { :id => proj['id'], :name => proj['name'], :path => proj['path_with_namespace'],
        :default_branch => proj['default_branch']}
    end
  end

  def self.add_pipeline_stats_to(proj)
    pipelines = Gitlab.pipelines(proj[:id], { per_page: 10 })
    #pp pipeline[0]
    pipeline = pipelines.find{|p| p.ref == proj[:default_branch]}
    if pipeline.nil?
      if pipelines.any?
        proj[:status]      = 'No builds run for default branch lately'
      else
        proj[:status]      = 'No builds configured'
      end
      return
    end
    proj[:status]      = pipeline.status
    proj[:pipeline_id] = pipeline.id
    proj[:ref]         = pipeline.ref
    date               = DateTime.parse(pipeline.updated_at).strftime("%F %T")
    proj[:date]        = date
  rescue e
    proj[:status]      = "Error: #{e}"
  end

end