summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAzul <azul@riseup.net>2017-07-18 12:57:44 +0200
committerVarac <varac@leap.se>2017-10-16 13:20:43 +0200
commitb207ea5f24cb52d5818d04d2b0c872cc8598628e (patch)
treea17da07e15e2029b4892438be434c29d3c3e7a11
parent60cecf2f1815dfa51ea64091cc3afcda47438623 (diff)
refactor: GitlabStats to collect stats from gitlab
Want to add stats about merge requests in the future while still fetching each project once.
-rw-r--r--jobs/gitlab_build_status.rb34
-rw-r--r--lib/gitlab_stats.rb42
2 files changed, 46 insertions, 30 deletions
diff --git a/jobs/gitlab_build_status.rb b/jobs/gitlab_build_status.rb
index 317118d..7febcae 100644
--- a/jobs/gitlab_build_status.rb
+++ b/jobs/gitlab_build_status.rb
@@ -1,39 +1,13 @@
-require 'gitlab'
-require 'date'
+require 'gitlab_stats'
-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 '3000s', :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'],
- :default_branch => proj['default_branch']}
- end
-
- projects.each do |proj|
- begin
- pipelines = Gitlab.pipelines(proj[:id], { per_page: 10 })
- #pp pipeline[0]
- pipeline = pipelines.find{|p| p.ref == proj[:default_branch]}
- next if pipeline.nil?
- 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
- proj[:status] = 'No builds configured'
- end
+ GitlabStats.projects.each do |proj|
+ GitlabStats.add_pipeline_stats_to proj
- unless proj[:status] =~ /^success|running|No builds configured$/
+ unless proj[:status] =~ /^success|running|No builds configured/
broken_builds << proj
end
puts proj
diff --git a/lib/gitlab_stats.rb b/lib/gitlab_stats.rb
new file mode 100644
index 0000000..5b41263
--- /dev/null
+++ b/lib/gitlab_stats.rb
@@ -0,0 +1,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