Añade el plugin Redmine Git Hosting 5.0.0

This commit is contained in:
Manuel Cillero 2021-03-20 13:29:16 +01:00
parent cfa0d58b18
commit a3bddad233
458 changed files with 30396 additions and 1 deletions

View file

@ -0,0 +1,11 @@
class ReportBase
include Redmine::I18n
include ReportHelper
include ReportQuery
attr_reader :repository
def initialize(repository)
@repository = repository
end
end

View file

@ -0,0 +1,44 @@
module ReportHelper
def date_to
User.current.today
end
def week_day_hash
{ day_name(1) => 0,
day_name(2) => 0,
day_name(3) => 0,
day_name(4) => 0,
day_name(5) => 0,
day_name(6) => 0,
day_name(0) => 0 }
end
def hours
(0..23).step(1).map { |h| "#{h}h" }
end
def months
(1..12).map { |m| l('date.month_names')[m].capitalize }
end
def get_hour_from_date(date)
return nil unless date
time = date.to_time
zone = User.current.time_zone
local = zone ? time.in_time_zone(zone) : (time.utc? ? time.localtime : time)
local.hour
end
def total_by_month_for(method)
total = [0] * 12
send(method).each { |c| total[(date_to.month - c.first.to_date.month) % 12] += c.last }
total
end
def total_by_hour_for(method)
total = [0] * 24
send(method).each { |c| total[get_hour_from_date(c)] += 1 }
total
end
end

View file

@ -0,0 +1,47 @@
module ReportQuery
private
def all_changesets
@all_changesets ||= Changeset.where(repository_id: repository.id)
end
def all_changes
@all_changes ||= Change.joins(:changeset).where("#{Changeset.table_name}.repository_id = ?", repository.id)
end
def all_commits_by_day
@all_commits_by_day ||= all_changesets.group(:commit_date)
end
def all_changes_by_day
@all_changes_by_day ||= all_changes.group(:commit_date)
end
def redmine_committers
@redmine_committers ||= all_changesets.where.not(user_id: nil).distinct.count(:user_id)
end
def external_committers
@external_committers ||= all_changesets.where(user_id: nil).distinct.count(:committer)
end
def commits_by_day
@commits_by_day ||= all_commits_by_day.count
end
def changes_by_day
@changes_by_day ||= all_changes_by_day.count
end
def commits_by_hour
@commits_by_hour ||= all_changesets.map(&:committed_on)
end
def commits_by_author
@commits_by_author ||= all_changesets.group(:committer).count
end
def changes_by_author
@changes_by_author ||= all_changes.group(:committer).count
end
end

View file

@ -0,0 +1,91 @@
class RepositoryCommitsStats < ReportBase
def commits_per_month
data = {}
data[:categories] = months.reverse
data[:series] = []
data[:series] << { name: l(:label_commit_plural), data: total_commits_by_month[0..11].reverse }
data[:series] << { name: l(:label_change_plural), data: total_changes_by_month[0..11].reverse }
data
end
def commits_per_day
data = {}
data[:categories] = total_commits_by_day.keys
data[:series] = []
data[:series] << { name: l(:label_commit_plural), data: total_commits_by_day.values }
data[:series] << { name: l(:label_change_plural), data: total_changes_by_day.values }
data
end
def commits_per_hours
data = {}
data[:categories] = hours
data[:series] = []
data[:series] << { name: l(:label_commit_plural), data: total_commits_by_hour }
data
end
def commits_per_weekday
data = {}
data[:name] = l(:label_commit_plural)
data[:data] = []
total_commits_by_weekday.each do |key, value|
data[:data] << [key, value]
end
[data]
end
private
def total_commits_by_month
total_by_month_for(:commits_by_day)
end
def total_changes_by_month
total_by_month_for(:changes_by_day)
end
def total_commits_by_day
@total_commits_by_day ||= all_commits_by_day.order(:commit_date).count
end
def total_changes_by_day
return @total_changes_by_day unless @total_changes_by_day.nil?
@total_changes_by_day = nil
changes = {}
Changeset.where('repository_id = ?', repository.id).includes(:filechanges).order(:commit_date).each do |changeset|
changes[changeset.commit_date] = 0 unless changes.key?(changeset.commit_date)
changes[changeset.commit_date] += changeset.filechanges.size
end
@total_changes_by_day = changes
@total_changes_by_day
end
def total_commits_by_hour
total_by_hour_for(:commits_by_hour)
end
def total_commits_by_weekday
week_day = week_day_hash
commits_by_day.each do |commit_date, commit_count|
case commit_date.to_date.wday
when 0
week_day[day_name(0)] += commit_count
when 1
week_day[day_name(1)] += commit_count
when 2
week_day[day_name(2)] += commit_count
when 3
week_day[day_name(3)] += commit_count
when 4
week_day[day_name(4)] += commit_count
when 5
week_day[day_name(5)] += commit_count
when 6
week_day[day_name(6)] += commit_count
end
end
week_day
end
end

View file

@ -0,0 +1,105 @@
class RepositoryContributorsStats < ReportBase
def initialize(repository)
super
@changes_for_committer = {}
end
def commits_per_author
data = []
sorted_commits_per_author_with_aliases.each do |committer_hash|
commits = {}
committer_hash[:committers].each do |committer|
commits = commits.merge(count_changes_for_committer(committer)) { |key, oldval, newval| newval + oldval }
end
commits = Hash[commits.sort]
commits_data = {}
commits_data[:author_name] = committer_hash[:name]
commits_data[:author_mail] = committer_hash[:mail]
commits_data[:total_commits] = committer_hash[:commits]
commits_data[:categories] = commits.keys
commits_data[:series] = []
commits_data[:series] << { name: l(:label_commit_plural), data: commits.values }
data.push(commits_data)
end
data
end
def commits_per_author_global
merged = commits_per_author_with_aliases
data = {}
data[:categories] = merged.map { |x| x[:name] }
data[:series] = []
data[:series] << { name: l(:label_commit_plural), data: merged.map { |x| x[:commits] } }
data[:series] << { name: l(:label_change_plural), data: merged.map { |x| x[:changes] } }
data
end
private
# Generate mappings from the registered users to the comitters
# user_committer_mapping = { name => [comitter, ...] }
# registered_committers = [ committer,... ]
#
def commits_per_author_with_aliases
return @commits_per_author_with_aliases if !@commits_per_author_with_aliases.nil?
@commits_per_author_with_aliases = nil
registered_committers = []
user_committer_mapping = {}
Changeset.select('changesets.committer, changesets.user_id')
.where(repository_id: repository.id)
.where.not(user_id: nil)
.group(:committer, :user_id)
.includes(:user).each do |x|
name = "#{x.user.firstname} #{x.user.lastname}"
registered_committers << x.committer
user_committer_mapping[[name, x.user.mail]] ||= []
user_committer_mapping[[name, x.user.mail]] << x.committer
end
merged = []
commits_by_author.each do |committer, count|
# skip all registered users
next if registered_committers.include?(committer)
name = committer.gsub(%r{<.+@.+>}, '').strip
mail = committer[/<(.+@.+)>/, 1]
merged << { name: name, mail: mail, commits: count, changes: changes_by_author[committer] || 0, committers: [committer] }
end
user_committer_mapping.each do |identity, committers|
count = 0
changes = 0
committers.each do |c|
count += commits_by_author[c] || 0
changes += changes_by_author[c] || 0
end
merged << { name: identity[0], mail: identity[1], commits: count, changes: changes, committers: committers }
end
# sort by name
merged.sort! { |x, y| x[:name] <=> y[:name] }
# merged = merged + [{name:"",commits:0,changes:0}]*(10 - merged.length) if merged.length < 10
@commits_per_author_with_aliases = merged
@commits_per_author_with_aliases
end
def sorted_commits_per_author_with_aliases
@committers ||= commits_per_author_with_aliases.sort! { |x, y| y[:commits] <=> x[:commits] }
end
def count_changes_for_committer(committer)
return @changes_for_committer[committer] unless @changes_for_committer[committer].nil?
@changes_for_committer[committer] ||= Changeset.where(repository_id: repository.id, committer: committer)
.group(:commit_date)
.order(:commit_date)
.count
@changes_for_committer[committer]
end
end

View file

@ -0,0 +1,43 @@
class RepositoryGlobalStats < ReportBase
def build
data = {}
data[l(:label_total_commits)] = total_commits
data[l(:label_total_contributors)] = committers
data[l(:label_first_commit_date)] = format_date(first_commit.commit_date)
data[l(:label_latest_commit_date)] = format_date(last_commit.commit_date)
data[l(:label_active_for)] = "#{active_for} #{l(:days, active_for)}"
data[l(:label_average_commit_per_day)] = average_commit_per_day
data[l(:label_average_contributor_commits)] = average_contributor_commits
data
end
private
def total_commits
@total_commits ||= all_changesets.count
end
def committers
@committers ||= redmine_committers + external_committers
end
def first_commit
@first_commit ||= all_changesets.order(commit_date: :asc).first
end
def last_commit
@last_commit ||= all_changesets.order(commit_date: :asc).last
end
def active_for
@active_for ||= (last_commit.commit_date - first_commit.commit_date).to_i
end
def average_commit_per_day
@average_commit_per_day ||= total_commits.fdiv(active_for).round(2)
end
def average_contributor_commits
@average_contributor_commits ||= total_commits.fdiv(committers).round(2)
end
end