Skip to content

Instantly share code, notes, and snippets.

@strviola
Created October 25, 2019 03:15
Show Gist options
  • Save strviola/4b3ca7bf8d2018d94e6a6ee92432ab56 to your computer and use it in GitHub Desktop.
Save strviola/4b3ca7bf8d2018d94e6a6ee92432ab56 to your computer and use it in GitHub Desktop.
require 'gitlab' # install by Gemfile
require 'pry' # install by Gemfile
require 'csv'
Gitlab.configure do |config|
config.endpoint = 'YOUR_GITLAB_URL'
config.private_token = 'YOUR_GITLAB_PRIVATE_TOKEN'
end
Gitlab.projects.auto_paginate do |project|
@project = project and break if project.name == 'YOUR_PROJECT_NAME'
end
# per_pageの最大値は100件。それ以上の数値を指定すると勝手に100にされる
pipelines = Gitlab.pipelines(@project.id, per_page: 100)
stats = []
100.times do |i|
count_success = pipelines.select { |pipeline| pipeline.status == 'success' }.size
time_latest = Gitlab.pipeline(@project.id, pipelines[0].id).created_at
time_oldest = Gitlab.pipeline(@project.id, pipelines[-1].id).created_at
stats << OpenStruct.new(count: count_success, time_oldest: DateTime.parse(time_oldest), time_latest: DateTime.parse(time_latest))
pipelines = pipelines.next_page
STDERR.print "Progress: #{format('%3d', i + 1)}/100\r"
sleep 1
end
STDERR.puts ''
# 500件ずつにまとめる
stats_bind = stats.each_slice(5).map do |stat|
OpenStruct.new(count: stat.map(&:count).sum, time_oldest: stat[-1].time_oldest, time_latest: stat[0].time_latest)
end
CSV.open('gitlab_pipeline_stat.csv', 'w') do |csv|
stats_bind.each do |stat|
csv << [stat.count, stat.time_oldest.strftime('%Y-%m-%d'), stat.time_latest.strftime('%Y-%m-%d')]
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment