2025-07-18 17:35:57 +01:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
2011-05-29 13:11:52 -07:00
|
|
|
#-- copyright
|
2020-01-15 11:31:26 +01:00
|
|
|
# OpenProject is an open source project management software.
|
2024-07-30 13:42:36 +02:00
|
|
|
# Copyright (C) the OpenProject GmbH
|
2011-05-30 20:52:25 +02:00
|
|
|
#
|
2011-05-29 13:11:52 -07:00
|
|
|
# This program is free software; you can redistribute it and/or
|
2013-06-05 16:27:56 +02:00
|
|
|
# modify it under the terms of the GNU General Public License version 3.
|
2011-05-30 20:52:25 +02:00
|
|
|
#
|
2013-09-16 17:59:31 +02:00
|
|
|
# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
|
2021-01-13 17:47:45 +01:00
|
|
|
# Copyright (C) 2006-2013 Jean-Philippe Lang
|
2013-09-16 17:59:31 +02:00
|
|
|
# Copyright (C) 2010-2013 the ChiliProject Team
|
|
|
|
|
#
|
|
|
|
|
# This program is free software; you can redistribute it and/or
|
|
|
|
|
# modify it under the terms of the GNU General Public License
|
|
|
|
|
# as published by the Free Software Foundation; either version 2
|
|
|
|
|
# of the License, or (at your option) any later version.
|
|
|
|
|
#
|
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
|
#
|
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
|
# along with this program; if not, write to the Free Software
|
|
|
|
|
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
#
|
2021-09-02 21:49:06 +02:00
|
|
|
# See COPYRIGHT and LICENSE files for more details.
|
2011-05-29 13:11:52 -07:00
|
|
|
#++
|
|
|
|
|
|
2020-04-09 11:54:26 +02:00
|
|
|
class Changeset < ApplicationRecord
|
2007-03-25 12:12:15 +00:00
|
|
|
belongs_to :repository
|
2023-01-19 11:32:38 +01:00
|
|
|
has_one :project, through: :repository
|
2008-11-10 18:59:06 +00:00
|
|
|
belongs_to :user
|
2015-06-15 11:14:03 +02:00
|
|
|
has_many :file_changes, class_name: "Change", dependent: :delete_all
|
2013-07-18 09:50:41 +02:00
|
|
|
has_and_belongs_to_many :work_packages
|
2007-08-29 16:52:35 +00:00
|
|
|
|
2020-12-03 12:00:19 +01:00
|
|
|
acts_as_journalized timestamp: :committed_on
|
2013-11-28 15:33:24 +01:00
|
|
|
|
2020-09-16 11:26:15 +02:00
|
|
|
acts_as_event title: Proc.new { |o|
|
2021-02-11 16:02:18 +01:00
|
|
|
"#{I18n.t(:label_revision)} #{o.format_identifier}" + (o.short_comments.blank? ? "" : (": " + o.short_comments))
|
|
|
|
|
},
|
2013-11-28 15:33:24 +01:00
|
|
|
description: :long_comments,
|
|
|
|
|
datetime: :committed_on,
|
2020-09-16 11:26:15 +02:00
|
|
|
url: Proc.new { |o|
|
|
|
|
|
{
|
|
|
|
|
controller: "/repositories",
|
|
|
|
|
action: "revision",
|
|
|
|
|
project_id: o.repository.project_id,
|
|
|
|
|
rev: o.identifier
|
|
|
|
|
}
|
|
|
|
|
},
|
2014-11-03 23:29:15 +01:00
|
|
|
author: Proc.new { |o| o.author }
|
2013-11-28 15:33:24 +01:00
|
|
|
|
2014-11-03 21:10:39 +01:00
|
|
|
acts_as_searchable columns: "comments",
|
2014-11-03 21:35:22 +01:00
|
|
|
include: { repository: :project },
|
2015-07-02 20:29:13 +02:00
|
|
|
references: [:repositories],
|
2014-11-03 21:10:39 +01:00
|
|
|
project_key: "#{Repository.table_name}.project_id",
|
|
|
|
|
date_column: "committed_on"
|
2011-05-30 20:52:25 +02:00
|
|
|
|
2007-03-25 17:11:46 +00:00
|
|
|
validates :repository_id, :revision, :committed_on, :commit_date, presence: true
|
2014-11-03 21:10:39 +01:00
|
|
|
validates :revision, uniqueness: { scope: :repository_id }
|
|
|
|
|
validates :scmid, uniqueness: { scope: :repository_id, allow_nil: true }
|
2011-05-30 20:52:25 +02:00
|
|
|
|
2021-02-11 16:02:18 +01:00
|
|
|
scope :visible, ->(*args) {
|
2015-06-28 21:55:26 +02:00
|
|
|
includes(repository: :project)
|
2016-08-24 14:10:11 +02:00
|
|
|
.references(:projects)
|
2015-07-09 11:28:16 +02:00
|
|
|
.merge(Project.allowed_to(args.first || User.current, :view_changesets))
|
2014-11-03 21:35:22 +01:00
|
|
|
}
|
2011-05-30 20:52:25 +02:00
|
|
|
|
2008-04-26 17:56:26 +00:00
|
|
|
def revision=(r)
|
|
|
|
|
write_attribute :revision, (r.nil? ? nil : r.to_s)
|
|
|
|
|
end
|
2011-01-02 09:45:05 +00:00
|
|
|
|
|
|
|
|
# Returns the identifier of this changeset; depending on repository backends
|
|
|
|
|
def identifier
|
|
|
|
|
if repository.class.respond_to? :changeset_identifier
|
|
|
|
|
repository.class.changeset_identifier self
|
|
|
|
|
else
|
|
|
|
|
revision.to_s
|
|
|
|
|
end
|
|
|
|
|
end
|
2007-08-25 14:41:55 +00:00
|
|
|
|
2007-03-25 17:11:46 +00:00
|
|
|
def committed_on=(date)
|
2012-05-09 11:09:44 +02:00
|
|
|
self.commit_date = date.to_date
|
2007-03-25 17:11:46 +00:00
|
|
|
super
|
|
|
|
|
end
|
2011-01-02 09:45:05 +00:00
|
|
|
|
|
|
|
|
# Returns the readable identifier
|
|
|
|
|
def format_identifier
|
|
|
|
|
if repository.class.respond_to? :format_changeset_identifier
|
|
|
|
|
repository.class.format_changeset_identifier self
|
|
|
|
|
else
|
|
|
|
|
identifier
|
|
|
|
|
end
|
|
|
|
|
end
|
2011-05-30 20:52:25 +02:00
|
|
|
|
2008-11-10 18:59:06 +00:00
|
|
|
def author
|
|
|
|
|
user || committer.to_s.split("<").first
|
|
|
|
|
end
|
2011-05-30 20:52:25 +02:00
|
|
|
|
2011-07-22 10:41:34 -07:00
|
|
|
# Delegate to a Repository's log encoding
|
|
|
|
|
def repository_encoding
|
|
|
|
|
if repository.present?
|
|
|
|
|
repository.repo_log_encoding
|
|
|
|
|
end
|
|
|
|
|
end
|
2011-08-27 18:26:12 +02:00
|
|
|
|
2011-07-22 10:10:23 -07:00
|
|
|
# Committer of the Changeset
|
|
|
|
|
#
|
|
|
|
|
# Attribute reader for committer that encodes the committer string to
|
|
|
|
|
# the repository log encoding (e.g. UTF-8)
|
|
|
|
|
def committer
|
|
|
|
|
self.class.to_utf8(read_attribute(:committer), repository.repo_log_encoding)
|
|
|
|
|
end
|
|
|
|
|
|
2012-08-22 16:56:14 +02:00
|
|
|
before_create :sanitize_attributes
|
2014-02-21 15:27:41 +01:00
|
|
|
before_create :assign_openproject_user_from_comitter
|
2013-07-18 09:50:41 +02:00
|
|
|
after_create :scan_comment_for_work_package_ids
|
2011-05-30 20:52:25 +02:00
|
|
|
|
2010-12-05 11:45:09 +00:00
|
|
|
TIMELOG_RE = /
|
|
|
|
|
(
|
2011-02-26 14:46:19 +00:00
|
|
|
((\d+)(h|hours?))((\d+)(m|min)?)?
|
|
|
|
|
|
|
|
|
|
|
((\d+)(h|hours?|m|min))
|
2010-12-05 11:45:09 +00:00
|
|
|
|
|
|
|
|
|
(\d+):(\d+)
|
|
|
|
|
|
|
2021-02-11 16:02:18 +01:00
|
|
|
(\d+([.,]\d+)?)h?
|
2010-12-05 11:45:09 +00:00
|
|
|
)
|
|
|
|
|
/x
|
2011-05-30 20:52:25 +02:00
|
|
|
|
2013-07-18 09:50:41 +02:00
|
|
|
def scan_comment_for_work_package_ids
|
2007-04-25 15:06:20 +00:00
|
|
|
return if comments.blank?
|
2021-02-11 16:02:18 +01:00
|
|
|
|
2013-07-18 09:50:41 +02:00
|
|
|
# keywords used to reference work packages
|
2014-11-04 11:07:27 +01:00
|
|
|
ref_keywords = Setting.commit_ref_keywords.downcase.split(",").map(&:strip)
|
2010-12-05 11:45:09 +00:00
|
|
|
ref_keywords_any = ref_keywords.delete("*")
|
2013-07-18 09:50:41 +02:00
|
|
|
# keywords used to fix work packages
|
2014-11-04 11:07:27 +01:00
|
|
|
fix_keywords = Setting.commit_fix_keywords.downcase.split(",").map(&:strip)
|
2011-05-30 20:52:25 +02:00
|
|
|
|
2014-11-04 11:07:27 +01:00
|
|
|
kw_regexp = (ref_keywords + fix_keywords).map { |kw| Regexp.escape(kw) }.join("|")
|
2011-05-30 20:52:25 +02:00
|
|
|
|
2013-07-18 09:50:41 +02:00
|
|
|
referenced_work_packages = []
|
2011-05-30 20:52:25 +02:00
|
|
|
|
2021-02-11 16:02:18 +01:00
|
|
|
comments.scan(/([\s(\[,-]|^)((#{kw_regexp})[\s:]+)?(#\d+(\s+@#{TIMELOG_RE})?([\s,;&]+#\d+(\s+@#{TIMELOG_RE})?)*)(?=[[:punct:]]|\s|<|$)/i) do |match|
|
2015-06-27 18:15:48 +02:00
|
|
|
action = match[2]
|
|
|
|
|
refs = match[3]
|
2010-12-05 11:45:09 +00:00
|
|
|
next unless action.present? || ref_keywords_any
|
2011-05-30 20:52:25 +02:00
|
|
|
|
2024-01-04 17:01:17 +01:00
|
|
|
refs.scan(/#(\d+)(\s+@#{TIMELOG_RE})?/o).each do |m|
|
2015-06-27 18:15:48 +02:00
|
|
|
work_package = find_referenced_work_package_by_id(m[0].to_i)
|
|
|
|
|
hours = m[2]
|
2013-07-18 09:50:41 +02:00
|
|
|
if work_package
|
|
|
|
|
referenced_work_packages << work_package
|
|
|
|
|
fix_work_package(work_package) if fix_keywords.include?(action.to_s.downcase)
|
|
|
|
|
log_time(work_package, hours) if hours && Setting.commit_logtime_enabled?
|
2007-04-24 13:57:27 +00:00
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
2011-05-30 20:52:25 +02:00
|
|
|
|
2013-07-18 09:50:41 +02:00
|
|
|
referenced_work_packages.uniq!
|
|
|
|
|
self.work_packages = referenced_work_packages unless referenced_work_packages.empty?
|
2007-04-24 13:57:27 +00:00
|
|
|
end
|
2011-05-30 20:52:25 +02:00
|
|
|
|
2009-02-01 15:48:56 +00:00
|
|
|
def short_comments
|
|
|
|
|
@short_comments || split_comments.first
|
|
|
|
|
end
|
2011-05-30 20:52:25 +02:00
|
|
|
|
2009-02-01 15:48:56 +00:00
|
|
|
def long_comments
|
|
|
|
|
@long_comments || split_comments.last
|
|
|
|
|
end
|
2010-12-05 11:45:09 +00:00
|
|
|
|
|
|
|
|
def text_tag
|
2010-12-11 14:20:04 +00:00
|
|
|
if scmid?
|
|
|
|
|
"commit:#{scmid}"
|
2010-12-05 11:45:09 +00:00
|
|
|
else
|
2010-12-11 14:20:04 +00:00
|
|
|
"r#{revision}"
|
2010-12-05 11:45:09 +00:00
|
|
|
end
|
|
|
|
|
end
|
2011-05-30 20:52:25 +02:00
|
|
|
|
2007-12-01 17:15:42 +00:00
|
|
|
# Returns the previous changeset
|
|
|
|
|
def previous
|
2018-11-20 16:23:33 +01:00
|
|
|
@previous ||= Changeset.where(["id < ? AND repository_id = ?", id, repository_id]).order(Arel.sql("id DESC")).first
|
2007-12-01 17:15:42 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
# Returns the next changeset
|
|
|
|
|
def next
|
2018-11-20 16:23:33 +01:00
|
|
|
@next ||= Changeset.where(["id > ? AND repository_id = ?", id, repository_id]).order(Arel.sql("id ASC")).first
|
2007-12-01 17:15:42 +00:00
|
|
|
end
|
2011-05-30 20:52:25 +02:00
|
|
|
|
2025-02-11 11:58:51 -03:00
|
|
|
# Creates a new Change from its common parameters
|
2010-02-02 17:02:32 +00:00
|
|
|
def create_change(change)
|
2014-11-03 21:10:39 +01:00
|
|
|
Change.create(changeset: self,
|
|
|
|
|
action: change[:action],
|
|
|
|
|
path: change[:path],
|
|
|
|
|
from_path: change[:from_path],
|
|
|
|
|
from_revision: change[:from_revision])
|
2010-02-02 17:02:32 +00:00
|
|
|
end
|
2011-02-28 12:09:32 +00:00
|
|
|
|
2008-08-26 12:13:15 +00:00
|
|
|
private
|
2008-11-10 18:59:06 +00:00
|
|
|
|
2013-07-18 09:50:41 +02:00
|
|
|
# Finds a work_package that can be referenced by the commit message
|
|
|
|
|
# i.e. a work_package that belong to the repository project, a subproject or a parent project
|
|
|
|
|
def find_referenced_work_package_by_id(id)
|
2010-12-05 11:45:09 +00:00
|
|
|
return nil if id.blank?
|
2019-07-24 14:07:44 +02:00
|
|
|
|
2015-06-26 11:23:13 +02:00
|
|
|
work_package = WorkPackage.includes(:project).find_by(id: id.to_i)
|
2019-07-24 14:07:44 +02:00
|
|
|
|
|
|
|
|
# Check that the work package is either in the same,
|
|
|
|
|
# a parent or child project of the given changeset
|
|
|
|
|
if in_ancestor_chain?(work_package, project)
|
|
|
|
|
work_package
|
2010-12-05 11:45:09 +00:00
|
|
|
end
|
2019-07-24 14:07:44 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def in_ancestor_chain?(work_package, project)
|
|
|
|
|
work_package&.project &&
|
|
|
|
|
(project == work_package.project ||
|
|
|
|
|
project.is_ancestor_of?(work_package.project) ||
|
|
|
|
|
project.is_descendant_of?(work_package.project))
|
2010-12-05 11:45:09 +00:00
|
|
|
end
|
2011-05-30 20:52:25 +02:00
|
|
|
|
2013-07-18 09:50:41 +02:00
|
|
|
def fix_work_package(work_package)
|
2015-06-26 11:23:13 +02:00
|
|
|
status = Status.find_by(id: Setting.commit_fix_status_id.to_i)
|
2010-12-05 11:45:09 +00:00
|
|
|
if status.nil?
|
2013-01-14 16:51:50 +01:00
|
|
|
logger.warn("No status matches commit_fix_status_id setting (#{Setting.commit_fix_status_id})") if logger
|
2013-07-18 09:50:41 +02:00
|
|
|
return work_package
|
2010-12-05 11:45:09 +00:00
|
|
|
end
|
2011-05-30 20:52:25 +02:00
|
|
|
|
2013-07-18 09:50:41 +02:00
|
|
|
# the work_package may have been updated by the closure of another one (eg. duplicate)
|
|
|
|
|
work_package.reload
|
2014-01-03 12:41:20 +01:00
|
|
|
# don't change the status if the work package is closed
|
2013-07-18 09:50:41 +02:00
|
|
|
return if work_package.status && work_package.status.is_closed?
|
2011-05-30 20:52:25 +02:00
|
|
|
|
2024-04-09 17:46:23 +02:00
|
|
|
call = WorkPackages::UpdateService
|
|
|
|
|
.new(model: work_package,
|
|
|
|
|
user: user || User.anonymous)
|
|
|
|
|
.call(status:,
|
|
|
|
|
journal_notes: I18n.t(:text_status_changed_by_changeset,
|
|
|
|
|
value: text_tag,
|
|
|
|
|
locale: Setting.default_language))
|
|
|
|
|
|
|
|
|
|
if call.errors.any? && logger.present?
|
2021-02-11 16:02:18 +01:00
|
|
|
logger.warn("Work package ##{work_package.id} could not be saved by changeset #{id}: #{work_package.errors.full_messages}")
|
2010-12-05 11:45:09 +00:00
|
|
|
end
|
2019-07-24 14:07:44 +02:00
|
|
|
|
2013-07-18 09:50:41 +02:00
|
|
|
work_package
|
2010-12-05 11:45:09 +00:00
|
|
|
end
|
2011-05-30 20:52:25 +02:00
|
|
|
|
2013-07-18 09:50:41 +02:00
|
|
|
def log_time(work_package, hours)
|
2020-03-12 11:12:46 +01:00
|
|
|
unless user.present?
|
|
|
|
|
Rails.logger.warn("TimeEntry could not be created by changeset #{id}: #{committer} does not map to user")
|
|
|
|
|
return
|
|
|
|
|
end
|
|
|
|
|
|
2017-07-27 10:03:59 +02:00
|
|
|
Changesets::LogTimeService
|
|
|
|
|
.new(user:, changeset: self)
|
|
|
|
|
.call(work_package, hours)
|
|
|
|
|
.result
|
2010-01-31 16:25:06 +00:00
|
|
|
end
|
2011-05-30 20:52:25 +02:00
|
|
|
|
2009-02-01 15:48:56 +00:00
|
|
|
def split_comments
|
2013-10-28 12:21:28 +01:00
|
|
|
comments =~ /\A(.+?)\r?\n(.*)\z/m
|
2009-02-01 15:48:56 +00:00
|
|
|
@short_comments = $1 || comments
|
|
|
|
|
@long_comments = $2.to_s.strip
|
2014-11-03 21:35:22 +01:00
|
|
|
[@short_comments, @long_comments]
|
2009-02-01 15:48:56 +00:00
|
|
|
end
|
2008-11-10 18:59:06 +00:00
|
|
|
|
2011-02-28 12:09:32 +00:00
|
|
|
# Strips and reencodes a commit log before insertion into the database
|
|
|
|
|
def self.normalize_comments(str, encoding)
|
|
|
|
|
Changeset.to_utf8(str.to_s.strip, encoding)
|
|
|
|
|
end
|
|
|
|
|
|
2012-08-22 16:56:14 +02:00
|
|
|
def sanitize_attributes
|
2014-11-03 21:35:22 +01:00
|
|
|
self.committer = self.class.to_utf8(committer, repository.repo_log_encoding)
|
|
|
|
|
self.comments = self.class.normalize_comments(comments, repository.repo_log_encoding)
|
2012-08-22 16:56:14 +02:00
|
|
|
end
|
|
|
|
|
|
2014-02-21 15:27:41 +01:00
|
|
|
def assign_openproject_user_from_comitter
|
2014-11-03 21:35:22 +01:00
|
|
|
self.user = repository.find_committer_user(committer)
|
2023-06-07 09:52:13 +02:00
|
|
|
add_journal(user: user || User.anonymous, notes: comments)
|
2012-08-22 16:56:14 +02:00
|
|
|
end
|
|
|
|
|
|
2011-07-22 10:41:34 -07:00
|
|
|
# TODO: refactor to a standard helper method
|
2011-02-28 12:09:32 +00:00
|
|
|
def self.to_utf8(str, encoding)
|
2011-04-09 05:41:12 +00:00
|
|
|
return str if str.nil?
|
2021-02-11 16:02:18 +01:00
|
|
|
|
2014-11-03 21:35:22 +01:00
|
|
|
str.force_encoding("ASCII-8BIT") if str.respond_to?(:force_encoding)
|
2011-04-09 06:34:33 +00:00
|
|
|
if str.empty?
|
2014-11-03 21:35:22 +01:00
|
|
|
str.force_encoding("UTF-8") if str.respond_to?(:force_encoding)
|
2011-04-09 06:34:33 +00:00
|
|
|
return str
|
|
|
|
|
end
|
2014-11-03 21:35:22 +01:00
|
|
|
normalized_encoding = encoding.presence || "UTF-8"
|
2011-02-22 13:39:37 +00:00
|
|
|
if str.respond_to?(:force_encoding)
|
2014-11-03 21:35:22 +01:00
|
|
|
if normalized_encoding.upcase == "UTF-8"
|
|
|
|
|
str.force_encoding("UTF-8")
|
2011-06-03 10:46:46 -07:00
|
|
|
unless str.valid_encoding?
|
2014-11-03 21:35:22 +01:00
|
|
|
str = str.encode("US-ASCII", invalid: :replace,
|
|
|
|
|
undef: :replace, replace: "?").encode("UTF-8")
|
2011-04-09 08:19:55 +00:00
|
|
|
end
|
2022-05-31 11:55:27 +02:00
|
|
|
else
|
2011-06-03 10:46:46 -07:00
|
|
|
str.force_encoding(normalized_encoding)
|
2014-11-03 21:35:22 +01:00
|
|
|
str = str.encode("UTF-8", invalid: :replace,
|
|
|
|
|
undef: :replace, replace: "?")
|
2011-02-22 13:39:37 +00:00
|
|
|
end
|
|
|
|
|
else
|
2011-08-27 18:26:12 +02:00
|
|
|
|
2014-11-03 21:35:22 +01:00
|
|
|
txtar = ""
|
2011-02-22 13:39:37 +00:00
|
|
|
begin
|
2012-10-24 14:41:49 +02:00
|
|
|
txtar += str.encode("UTF-8", normalized_encoding)
|
|
|
|
|
rescue Encoding::InvalidByteSequenceError, Encoding::UndefinedConversionError
|
2011-04-09 09:31:14 +00:00
|
|
|
txtar += $!.success
|
2014-11-03 21:35:22 +01:00
|
|
|
str = "?" + $!.failed[1, $!.failed.length]
|
2011-04-09 09:31:14 +00:00
|
|
|
retry
|
2021-02-11 16:02:18 +01:00
|
|
|
rescue StandardError
|
2011-04-09 09:31:14 +00:00
|
|
|
txtar += $!.success
|
2011-02-22 13:39:37 +00:00
|
|
|
end
|
2011-04-09 09:31:14 +00:00
|
|
|
str = txtar
|
2010-04-11 13:55:30 +00:00
|
|
|
end
|
2011-07-22 10:10:23 -07:00
|
|
|
str
|
2008-08-26 12:13:15 +00:00
|
|
|
end
|
2007-03-25 12:12:15 +00:00
|
|
|
end
|