11#!/usr/bin/env ruby
22
3- require 'optparse'
4- require 'date'
5- require 'yaml'
6- require 'octokit'
7- require 'set'
8-
3+ require "optparse"
4+ require "date"
5+ require "yaml"
6+ require "octokit"
7+ require "set"
98
109# Get GitHub token
1110def get_github_token
12- config_path = File . expand_path ( ' ~/.config/gh/hosts.yml' )
11+ config_path = File . expand_path ( " ~/.config/gh/hosts.yml" )
1312 if File . exist? ( config_path )
1413 config = YAML . load_file ( config_path )
15- return config . dig ( ' github.com' , ' oauth_token' )
14+ return config . dig ( " github.com" , " oauth_token" )
1615 end
1716 nil
1817end
@@ -33,7 +32,7 @@ def get_failed_tests_from_ci(client, options, branches)
3332 page = 1
3433
3534 loop do
36- runs = client . workflow_runs ( repo , workflow . id , branch : branch , status : ' completed' , per_page : 100 , page : page )
35+ runs = client . workflow_runs ( repo , workflow . id , branch : branch , status : " completed" , per_page : 100 , page : page )
3736 break if runs . workflow_runs . empty?
3837
3938 workflow_runs . concat ( runs . workflow_runs )
@@ -42,7 +41,7 @@ def get_failed_tests_from_ci(client, options, branches)
4241 page += 1
4342 end
4443
45- workflow_runs . sort_by! { | run | run . created_at }
44+ workflow_runs . sort_by! ( & : created_at)
4645 last_failure_date = { }
4746
4847 workflow_runs . each do |run |
@@ -55,7 +54,7 @@ def get_failed_tests_from_ci(client, options, branches)
5554 jobs . each do |job |
5655 puts " Checking job: #{ job . name } (Status: #{ job . conclusion } )" if options [ :verbose ]
5756
58- failed_tests [ branch ] [ job . name ] ||= Set . new if job . conclusion == ' failure'
57+ failed_tests [ branch ] [ job . name ] ||= Set . new if job . conclusion == " failure"
5958 last_date = last_failure_date [ job . name ]
6059
6160 if last_date
@@ -66,11 +65,11 @@ def get_failed_tests_from_ci(client, options, branches)
6665 end
6766 end
6867
69- if job . conclusion == ' failure'
68+ if job . conclusion == " failure"
7069 puts " -> First failure of #{ job . name } " if options [ :verbose ] && !last_failure_date . key? ( job . name )
7170 failed_tests [ branch ] [ job . name ] << run_date
7271 last_failure_date [ job . name ] = run_date
73- elsif job . conclusion == ' success'
72+ elsif job . conclusion == " success"
7473 last_failure_date . delete ( job . name )
7574 end
7675 rescue StandardError => e
@@ -93,35 +92,35 @@ def get_failed_tests_from_ci(client, options, branches)
9392end
9493
9594options = {
96- owner : ' chef' ,
97- repo : ' chef' ,
98- branches : ' chef-18,main' ,
95+ owner : " chef" ,
96+ repo : " chef" ,
97+ branches : " chef-18,main" ,
9998 days : 30 ,
100- verbose : false
99+ verbose : false ,
101100}
102101
103102OptionParser . new do |opts |
104- opts . banner = ' Usage: script.rb [options]'
103+ opts . banner = " Usage: script.rb [options]"
105104
106- opts . on ( ' --owner OWNER' , ' GitHub owner/org name' ) { |v | options [ :owner ] = v }
107- opts . on ( ' --repo REPO' , ' GitHub repository name' ) { |v | options [ :repo ] = v }
108- opts . on ( ' --branches BRANCHES' , ' Comma-separated list of branches' ) { |v | options [ :branches ] = v }
109- opts . on ( ' --days DAYS' , Integer , ' Number of days to analyze' ) { |v | options [ :days ] = v }
110- opts . on ( '-v' , ' --verbose' , ' Enable verbose output' ) { options [ :verbose ] = true }
105+ opts . on ( " --owner OWNER" , " GitHub owner/org name" ) { |v | options [ :owner ] = v }
106+ opts . on ( " --repo REPO" , " GitHub repository name" ) { |v | options [ :repo ] = v }
107+ opts . on ( " --branches BRANCHES" , " Comma-separated list of branches" ) { |v | options [ :branches ] = v }
108+ opts . on ( " --days DAYS" , Integer , " Number of days to analyze" ) { |v | options [ :days ] = v }
109+ opts . on ( "-v" , " --verbose" , " Enable verbose output" ) { options [ :verbose ] = true }
111110end . parse!
112111
113112github_token = get_github_token
114- raise ' GitHub token not found in ~/.config/gh/hosts.yml' unless github_token
113+ raise " GitHub token not found in ~/.config/gh/hosts.yml" unless github_token
115114
116115client = Octokit ::Client . new ( access_token : github_token )
117116
118- branches = options [ :branches ] . split ( ',' )
117+ branches = options [ :branches ] . split ( "," )
119118test_failures = get_failed_tests_from_ci ( client , options , branches )
120119puts "Days each job was broken in the last #{ options [ :days ] } days:"
121120test_failures . each do |branch , jobs |
122121 puts "\n Branch: #{ branch } "
123122 if jobs . empty?
124- puts ' No job failures found.'
123+ puts " No job failures found."
125124 else
126125 jobs . sort . each do |job , dates |
127126 puts " #{ job } : #{ dates . size } days"
0 commit comments