debian-mirror-gitlab/app/models/concerns/taskable.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

120 lines
3.7 KiB
Ruby
Raw Normal View History

2018-11-20 20:47:30 +05:30
# frozen_string_literal: true
2015-09-11 14:41:01 +05:30
require 'task_list'
require 'task_list/filter'
2015-04-26 12:48:37 +05:30
# Contains functionality for objects that can have task lists in their
# descriptions. Task list items can be added with Markdown like "* [x] Fix
# bugs".
#
# Used by MergeRequest and Issue
module Taskable
2019-12-04 20:38:33 +05:30
COMPLETED = 'completed'
INCOMPLETE = 'incomplete'
2022-06-21 17:19:12 +05:30
COMPLETE_PATTERN = /\[[xX]\]/.freeze
INCOMPLETE_PATTERN = /\[[[:space:]]\]/.freeze
2019-03-02 22:35:43 +05:30
ITEM_PATTERN = %r{
2015-12-23 02:04:40 +05:30
^
2022-04-04 11:22:00 +05:30
(?:(?:>\s{0,4})*) # optional blockquote characters
((?:\s*(?:[-+*]|(?:\d+\.)))+) # list prefix (one or more) required - task item has to be always in a list
\s+ # whitespace prefix has to be always presented for a list item
2022-06-21 17:19:12 +05:30
( # checkbox
#{COMPLETE_PATTERN}|#{INCOMPLETE_PATTERN}
)
2022-04-04 11:22:00 +05:30
(\s.+) # followed by whitespace and some text.
2019-07-31 22:56:46 +05:30
}x.freeze
2015-12-23 02:04:40 +05:30
2023-05-08 21:46:49 +05:30
ITEM_PATTERN_UNTRUSTED =
'^' \
'(?:(?:>\s{0,4})*)' \
'(?P<prefix>(?:\s*(?:[-+*]|(?:\d+\.)))+)' \
'\s+' \
'(?P<checkbox>' \
"#{COMPLETE_PATTERN.source}|#{INCOMPLETE_PATTERN.source}" \
')' \
'(?P<label>\s.+)'.freeze
2023-04-23 21:23:45 +05:30
# ignore tasks in code or html comment blocks. HTML blocks
# are ok as we allow tasks inside <detail> blocks
2023-05-08 21:46:49 +05:30
REGEX =
"#{::Gitlab::Regex.markdown_code_or_html_comments_untrusted}" \
"|" \
"(?P<task_item>" \
"#{ITEM_PATTERN_UNTRUSTED}" \
")".freeze
2023-04-23 21:23:45 +05:30
2015-12-23 02:04:40 +05:30
def self.get_tasks(content)
2023-04-23 21:23:45 +05:30
items = []
2023-05-08 21:46:49 +05:30
regex = Gitlab::UntrustedRegexp.new(REGEX, multiline: true)
regex.scan(content.to_s).each do |match|
next unless regex.extract_named_group(:task_item, match)
prefix = regex.extract_named_group(:prefix, match)
checkbox = regex.extract_named_group(:checkbox, match)
label = regex.extract_named_group(:label, match)
2023-04-23 21:23:45 +05:30
2023-05-08 21:46:49 +05:30
items << TaskList::Item.new("#{prefix.strip} #{checkbox}", label.strip)
2015-12-23 02:04:40 +05:30
end
2023-04-23 21:23:45 +05:30
items
2015-12-23 02:04:40 +05:30
end
def self.get_updated_tasks(old_content:, new_content:)
2021-04-29 21:17:54 +05:30
old_tasks = get_tasks(old_content)
new_tasks = get_tasks(new_content)
2015-12-23 02:04:40 +05:30
new_tasks.select.with_index do |new_task, i|
old_task = old_tasks[i]
next unless old_task
new_task.source == old_task.source && new_task.complete? != old_task.complete?
end
end
2015-09-11 14:41:01 +05:30
# Called by `TaskList::Summary`
def task_list_items
return [] if description.blank?
2015-04-26 12:48:37 +05:30
2018-03-17 18:26:18 +05:30
@task_list_items ||= Taskable.get_tasks(description) # rubocop:disable Gitlab/ModuleWithInstanceVariables
2015-09-11 14:41:01 +05:30
end
2015-04-26 12:48:37 +05:30
2015-09-11 14:41:01 +05:30
def tasks
@tasks ||= TaskList.new(self)
2015-04-26 12:48:37 +05:30
end
# Return true if this object's description has any task list items.
def tasks?
2015-09-11 14:41:01 +05:30
tasks.summary.items?
2015-04-26 12:48:37 +05:30
end
# Return a string that describes the current state of this Taskable's task
2022-08-27 11:52:29 +05:30
# list items, e.g. "12 of 20 checklist items completed"
2017-08-17 22:00:37 +05:30
def task_status(short: false)
2015-09-11 14:41:01 +05:30
return '' if description.blank?
2015-04-26 12:48:37 +05:30
2015-09-11 14:41:01 +05:30
sum = tasks.summary
2023-03-04 22:38:38 +05:30
checklist_item_noun = n_('checklist item', 'checklist items', sum.item_count)
if short
format(s_('Tasks|%{complete_count}/%{total_count} %{checklist_item_noun}'),
2023-04-23 21:23:45 +05:30
checklist_item_noun: checklist_item_noun, complete_count: sum.complete_count, total_count: sum.item_count)
2023-03-04 22:38:38 +05:30
else
format(s_('Tasks|%{complete_count} of %{total_count} %{checklist_item_noun} completed'),
2023-04-23 21:23:45 +05:30
checklist_item_noun: checklist_item_noun, complete_count: sum.complete_count, total_count: sum.item_count)
2023-03-04 22:38:38 +05:30
end
2017-08-17 22:00:37 +05:30
end
# Return a short string that describes the current state of this Taskable's
# task list items -- for small screens
def task_status_short
task_status(short: true)
2015-04-26 12:48:37 +05:30
end
2019-09-04 21:01:54 +05:30
def task_completion_status
@task_completion_status ||= {
count: tasks.summary.item_count,
completed_count: tasks.summary.complete_count
}
end
2015-04-26 12:48:37 +05:30
end