Class: Gitlab::Changelog::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/gitlab/changelog/config.rb

Overview

Configuration settings used when generating changelogs.

Constant Summary collapse

AUTHORS_NONE =

When rendering changelog entries, authors are not included.

'none'
FILE_PATH =

The path to the configuration file as stored in the project's Git repository.

'.gitlab/changelog_config.yml'
DEFAULT_DATE_FORMAT =

The default date format to use for formatting release dates.

'%Y-%m-%d'
DEFAULT_TEMPLATE =

The default template to use for generating release sections.

File.read(File.join(__dir__, 'template.tpl'))
DEFAULT_TAG_REGEX =

The regex to use for extracting the version from a Git tag.

This regex is based on the official semantic versioning regex (as found on semver.org/), with the addition of allowing a ā€œvā€ at the start of a tag name.

We default to a strict regex as we simply don't know what kind of data users put in their tags. As such, using simpler patterns (e.g. just `d+` for the major version) could lead to unexpected results.

We use a String here as `Gitlab::UntrustedRegexp` is a mutable object.

'^v?(?P<major>0|[1-9]\d*)' \
'\.(?P<minor>0|[1-9]\d*)' \
'\.(?P<patch>0|[1-9]\d*)' \
'(?:-(?P<pre>(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))' \
'?(?:\+(?P<meta>[0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(project) ⇒ Config

Returns a new instance of Config.


86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/gitlab/changelog/config.rb', line 86

def initialize(project)
  @project = project
  @date_format = DEFAULT_DATE_FORMAT
  @template =
    begin
      TemplateParser::Parser.new.parse_and_transform(DEFAULT_TEMPLATE)
    rescue TemplateParser::Error => e
      raise Error, e.message
    end
  @categories = {}
  @tag_regex = DEFAULT_TAG_REGEX
end

Instance Attribute Details

#always_credit_user_idsObject

Returns the value of attribute always_credit_user_ids.


37
38
39
# File 'lib/gitlab/changelog/config.rb', line 37

def always_credit_user_ids
  @always_credit_user_ids
end

#categoriesObject

Returns the value of attribute categories.


37
38
39
# File 'lib/gitlab/changelog/config.rb', line 37

def categories
  @categories
end

#date_formatObject

Returns the value of attribute date_format.


37
38
39
# File 'lib/gitlab/changelog/config.rb', line 37

def date_format
  @date_format
end

#tag_regexObject

Returns the value of attribute tag_regex.


37
38
39
# File 'lib/gitlab/changelog/config.rb', line 37

def tag_regex
  @tag_regex
end

#templateObject

Returns the value of attribute template.


37
38
39
# File 'lib/gitlab/changelog/config.rb', line 37

def template
  @template
end

Class Method Details

.from_git(project, user = nil) ⇒ Object


39
40
41
42
43
44
45
# File 'lib/gitlab/changelog/config.rb', line 39

def self.from_git(project, user = nil)
  if (yaml = project.repository.changelog_config.presence)
    from_hash(project, YAML.safe_load(yaml), user)
  else
    new(project)
  end
end

.from_hash(project, hash, user = nil) ⇒ Object


47
48
49
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
75
76
77
78
79
80
81
82
83
84
# File 'lib/gitlab/changelog/config.rb', line 47

def self.from_hash(project, hash, user = nil)
  config = new(project)

  if (date = hash['date_format'])
    config.date_format = date
  end

  if (template = hash['template'])
    config.template =
      begin
        TemplateParser::Parser.new.parse_and_transform(template)
      rescue TemplateParser::Error => e
        raise Error, e.message
      end
  end

  if (categories = hash['categories'])
    if categories.is_a?(Hash)
      config.categories = categories
    else
      raise Error, 'The "categories" configuration key must be a Hash'
    end
  end

  if (regex = hash['tag_regex'])
    config.tag_regex = regex
  end

  config.always_credit_user_ids = Set.new
  if (group_paths = Array(hash['include_groups']))
    group_paths.each do |group_path|
      group = Group.find_by_full_path(group_path)
      config.always_credit_user_ids.merge(group&.users_ids_of_direct_members&.compact) if user&.can?(:read_group, group)
    end
  end

  config
end

Instance Method Details

#always_credit_author?(user) ⇒ Boolean

Returns:

  • (Boolean)

103
104
105
# File 'lib/gitlab/changelog/config.rb', line 103

def always_credit_author?(user)
  always_credit_user_ids&.include?(user&.id) || false
end

#category(name) ⇒ Object


107
108
109
# File 'lib/gitlab/changelog/config.rb', line 107

def category(name)
  @categories[name] || name
end

#contributor?(user) ⇒ Boolean

Returns:

  • (Boolean)

99
100
101
# File 'lib/gitlab/changelog/config.rb', line 99

def contributor?(user)
  @project.team.contributor?(user&.id)
end

#format_date(date) ⇒ Object


111
112
113
# File 'lib/gitlab/changelog/config.rb', line 111

def format_date(date)
  date.strftime(@date_format)
end