-
Notifications
You must be signed in to change notification settings - Fork 28
/
daily_open_prs.rb
executable file
·107 lines (93 loc) · 3.21 KB
/
daily_open_prs.rb
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#!/usr/bin/env ruby
# frozen_string_literal: true
# This script is to output a csv file to tally how many PRs are currently open on each day between two date ranges. It is also split into both PRs raised by community members and PRs raised by Puppet members.
require 'optparse'
require 'csv'
require 'octokit'
require_relative 'octokit_utils'
options = {}
options[:oauth] = ENV['GITHUB_COMMUNITY_TOKEN'] if ENV['GITHUB_COMMUNITY_TOKEN']
parser = OptionParser.new do |opts|
opts.banner = 'Usage: open_and_created.rb [options]'
opts.on('-t', '--oauth-token TOKEN', 'OAuth token. Required.') { |v| options[:oauth] = v }
opts.on('-o', '--overview', 'Output overview, summary totals to csv') { options[:display_overview] = true }
opts.on('-f', '--file NAME', String, 'Module file list') { |v| options[:file] = v }
end
parser.parse!
options[:file] = 'modules.json' if options[:file].nil?
missing = []
missing << '-t' if options[:oauth].nil?
unless missing.empty?
puts "Missing options: #{missing.join(', ')}"
puts parser
exit
end
util = OctokitUtils.new(options[:oauth])
parsed = util.load_module_list(options[:file])
all_pulls = []
pr_cache = []
parsed.each do |m|
# Retrieves all PRs for the repo
pr_cache = util.fetch_async("#{m['github_namespace']}/#{m['repo_name']}", { state: 'open' }, [])
pr_cache.concat(util.fetch_async("#{m['github_namespace']}/#{m['repo_name']}", { state: 'closed' }, []))
pr_cache.each do |pr|
all_pulls.push(pr[:pull])
end
end
puppet_members = {}
puppet_members = util.puppet_organisation_members(all_pulls)
# Defines the dates required
end_date = Time.now.to_date
start_date = end_date - 20
# Currently open per day
days = []
open = []
(start_date..end_date).each do |day_to_check|
puppet_prs = 0
community_prs = 0
created_puppet_prs = 0
created_community_prs = 0
all_pulls.each do |pull|
if pull[:created_at].to_date == day_to_check
if puppet_members.key?(pull.user[:login])
created_puppet_prs += 1
else
created_community_prs += 1
end
end
if pull[:state] == 'closed'
if (pull[:closed_at].to_date >= day_to_check) && (pull[:created_at].to_date <= day_to_check)
if puppet_members.key?(pull.user[:login])
puppet_prs += 1
else
community_prs += 1
end
end
elsif (pull[:state] == 'open') && (pull[:created_at].to_date <= day_to_check)
if puppet_members.key?(pull.user[:login])
puppet_prs += 1
else
community_prs += 1
end
end
end
daily_total = community_prs + puppet_prs
row = { 'date' => day_to_check.strftime('%F'), 'community' => community_prs, 'puppet' => puppet_prs, 'total' => daily_total }
open_row = { 'date' => day_to_check.strftime('%F'), 'puppet' => created_puppet_prs, 'community' => created_community_prs }
days.push(row)
open.push(open_row)
day_to_check += 1
end
# Creates the CSV files
CSV.open('daily_open_prs.csv', 'wb') do |csv|
csv << %w[date community puppet total]
days.each do |day|
csv << [day['date'], day['community'], day['puppet'], day['total']]
end
end
CSV.open('created_per_day.csv', 'wb') do |csv|
csv << %w[date puppet community]
open.each do |o|
csv << [o['date'], o['puppet'], o['community']]
end
end