summaryrefslogtreecommitdiff
path: root/jobs/gitlab_build_status.rb
blob: addfaede60c15a4b40e2ef28b6621bb0e1d9be89 (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
43
44
require 'gitlab'
require 'date'
require 'pp'

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

SCHEDULER.every '300s', :first_in => 0 do
  broken_builds = []

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

  projects.each do |proj|

    begin
      pipeline = Gitlab.pipelines(proj[:id], { per_page: 1 })
      #pp pipeline[0]
      proj[:status]      = pipeline[0].status
      proj[:pipeline_id] = pipeline[0].id
      proj[:ref]         = pipeline[0].ref
      date               = DateTime.parse(pipeline[0].updated_at).strftime("%F %T")
      proj[:date]        = date
    rescue
      proj[:status]      = 'No builds configured'
    end

    unless proj[:status] =~ /^success|running|No builds configured$/
      broken_builds << proj
    end
    puts proj
  end

  failed = broken_builds.size > 0

  send_event('gitlab-builds', { failed: failed, header: "Gitlab builds", broken_builds: broken_builds })
end