Class: Gitlab::ContributionsCalendar

Inherits:
Object
  • Object
show all
Includes:
TimeZoneHelper
Defined in:
lib/gitlab/contributions_calendar.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from TimeZoneHelper

#local_time, #local_timezone_instance, #timezone_data

Constructor Details

#initialize(contributor, current_user = nil) ⇒ ContributionsCalendar

Returns a new instance of ContributionsCalendar.


11
12
13
14
15
16
17
18
19
20
# File 'lib/gitlab/contributions_calendar.rb', line 11

def initialize(contributor, current_user = nil)
  @contributor = contributor
  @contributor_time_instance = local_timezone_instance(contributor.timezone).now
  @current_user = current_user
  @projects = if @contributor.include_private_contributions?
                ContributedProjectsFinder.new(@contributor).execute(@contributor)
              else
                ContributedProjectsFinder.new(contributor).execute(current_user)
              end
end

Instance Attribute Details

#contributorObject (readonly)

Returns the value of attribute contributor.


7
8
9
# File 'lib/gitlab/contributions_calendar.rb', line 7

def contributor
  @contributor
end

#current_userObject (readonly)

Returns the value of attribute current_user.


8
9
10
# File 'lib/gitlab/contributions_calendar.rb', line 8

def current_user
  @current_user
end

#projectsObject (readonly)

Returns the value of attribute projects.


9
10
11
# File 'lib/gitlab/contributions_calendar.rb', line 9

def projects
  @projects
end

Instance Method Details

#activity_datesObject

rubocop: disable CodeReuse/ActiveRecord


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
# File 'lib/gitlab/contributions_calendar.rb', line 23

def activity_dates
  return {} if @projects.empty?
  return @activity_dates if @activity_dates.present?

  start_time = @contributor_time_instance.years_ago(1).beginning_of_day
  end_time = @contributor_time_instance.end_of_day

  date_interval = "INTERVAL '#{@contributor_time_instance.utc_offset} seconds'"

  # Can't use Event.contributions here because we need to check 3 different
  # project_features for the (currently) 3 different contribution types
  repo_events = events_created_between(start_time, end_time, :repository)
    .where(action: :pushed)
  issue_events = events_created_between(start_time, end_time, :issues)
    .where(action: [:created, :closed], target_type: "Issue")
  mr_events = events_created_between(start_time, end_time, :merge_requests)
    .where(action: [:merged, :created, :closed], target_type: "MergeRequest")
  note_events = events_created_between(start_time, end_time, :merge_requests)
    .where(action: :commented)

  events = Event
    .select("date(created_at + #{date_interval}) AS date", 'COUNT(*) AS num_events')
    .from_union([repo_events, issue_events, mr_events, note_events], remove_duplicates: false)
    .group(:date)
    .map(&:attributes)

  @activity_dates = events.each_with_object(Hash.new {|h, k| h[k] = 0 }) do |event, activities|
    activities[event["date"]] += event["num_events"]
  end
end

#events_by_date(date) ⇒ Object

rubocop: disable CodeReuse/ActiveRecord


56
57
58
59
60
61
62
63
64
65
# File 'lib/gitlab/contributions_calendar.rb', line 56

def events_by_date(date)
  return Event.none unless can_read_cross_project?

  date_in_time_zone = date.in_time_zone(@contributor_time_instance.time_zone)

  Event.contributions.where(author_id: contributor.id)
    .where(created_at: date_in_time_zone.beginning_of_day..date_in_time_zone.end_of_day)
    .where(project_id: projects)
    .with_associations
end

#starting_monthObject


72
73
74
# File 'lib/gitlab/contributions_calendar.rb', line 72

def starting_month
  @contributor_time_instance.month
end

#starting_yearObject

rubocop: enable CodeReuse/ActiveRecord


68
69
70
# File 'lib/gitlab/contributions_calendar.rb', line 68

def starting_year
  @contributor_time_instance.years_ago(1).year
end