Class: Danger::MilestoneChecker

Inherits:
Plugin
  • Object
show all
Defined in:
lib/dangermattic/plugins/milestone_checker.rb

Overview

This plugin checks whether a pull request is assigned to a milestone and whether the milestone's due date is approaching.

Examples:

Check if a milestone is set


# Check if PR is assigned to a milestone
checker.check_milestone_set

# Check if PR is assigned to a milestone, reporting an error if that's not the case
checker.check_milestone_set(report_type: :error)

Run a milestone check


# Check if milestone due date is approaching, reporting a warning if the milestone is in less than 5 days
checker.check_milestone_due_date(days_before_due: 5)

Run a milestone check with custom parameters


# Check if milestone due date is within 3 days, reporting an error if the due date has passed and in case there's no milestone set
checker.check_milestone_due_date(days_before_due: 3, report_type: :error, report_type_if_no_milestone: :error)

Run a milestone check with a custom milestone behaviour parameter


# Check if milestone due date is approaching and don't report anything if no milestone is assigned
checker.check_milestone_due_date(report_type_if_no_milestone: :none)

See Also:

  • Automattic/dangermattic

Instance Method Summary collapse

Instance Method Details

#check_milestone_due_date(days_before_due:, report_type: :warning, report_type_if_no_milestone: :warning) ⇒ void

This method returns an undefined value.

Checks if the pull request's milestone is due to finish within a certain number of days.

Parameters:

  • days_before_due (Integer)

    Number of days before the milestone due date for the check to apply.

  • report_type (Symbol) (defaults to: :warning)

    (optional) The type of message for when the PR is has passed over the days_before_due threshold. Types: :error, :warning (default), :message.

  • report_type_if_no_milestone (Symbol) (defaults to: :warning)

    The type of message for when the PR is not assigned to a milestone. Types: :error, :warning (default), :message. You can also pass :none to not leave a message when there is no milestone.



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
# File 'lib/dangermattic/plugins/milestone_checker.rb', line 50

def check_milestone_due_date(days_before_due:, report_type: :warning, report_type_if_no_milestone: :warning)
  if milestone.nil?
    check_milestone_set(report_type: report_type_if_no_milestone)
    return
  end

  return unless pr_state != 'closed' && milestone_due_date

  today = DateTime.now

  seconds_threshold = days_before_due * 24 * 60 * 60
  time_before_due_date = milestone_due_date.to_time.to_i - today.to_time.to_i
  return unless time_before_due_date <= seconds_threshold

  message_text = "This PR is assigned to the milestone [#{milestone_title}](#{milestone_url}). "
  message_text += if time_before_due_date.positive?
                    "This milestone is due in less than #{days_before_due} days.\n" \
                      'Please make sure to get it merged by then or assign it to a milestone with a later deadline.'
                  else
                    "The due date for this milestone has already passed.\n" \
                      'Please assign it to a milestone with a later deadline or check whether the release for this milestone has already been finished.'
                  end

  reporter.report(message: message_text, type: report_type)
end

#check_milestone_set(report_type: :warning) ⇒ void

This method returns an undefined value.

Checks if the pull request is assigned to a milestone.



36
37
38
39
40
41
# File 'lib/dangermattic/plugins/milestone_checker.rb', line 36

def check_milestone_set(report_type: :warning)
  return unless milestone.nil?

  message = 'PR is not assigned to a milestone.'
  reporter.report(message: message, type: report_type)
end