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'
DEFAULT_FILE_PATH =

The default 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.



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

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, path = nil) ⇒ Object



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

def self.from_git(project, user = nil, path = nil)
  yaml = project.repository.changelog_config('HEAD', path.presence || DEFAULT_FILE_PATH)
  if yaml.present?
    from_hash(project, YAML.safe_load(yaml), user)
  else
    new(project)
  end
end

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



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
85
# File 'lib/gitlab/changelog/config.rb', line 48

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)


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

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

#category(name) ⇒ Object



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

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

#contributor?(user) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#format_date(date) ⇒ Object



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

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