Module: UserFunctions

Included in:
Tefoji::Tefoji
Defined in:
lib/mixins/user_functions.rb

Instance Method Summary collapse

Instance Method Details

#_date_string_to_date(date_string) ⇒ Object

Expects date_string in the format YYYY-MM-DD



297
298
299
300
# File 'lib/mixins/user_functions.rb', line 297

def _date_string_to_date(date_string)
  year, month, day = date_string.split('-').map(&:to_i)
  Date.new(year, month, day)
end

#_date_to_date_string(date) ⇒ Object



302
303
304
# File 'lib/mixins/user_functions.rb', line 302

def _date_to_date_string(date)
  "#{date.year}-#{date.month}-#{date.day}"
end

#_fail_if_unset(function_name, arguments) ⇒ Object

Fail on unset arguments



318
319
320
321
322
# File 'lib/mixins/user_functions.rb', line 318

def _fail_if_unset(function_name, arguments)
  arguments.each do |a|
    fatal "\"#{function_name}\" called with unset variable." if a.nil?
  end
end

#_jira_key(item_name, hash, key) ⇒ Object

Jira fields helper functions



286
287
288
289
290
291
292
# File 'lib/mixins/user_functions.rb', line 286

def _jira_key(item_name, hash, key)
  key_symbol = key.upcase.to_sym
  unless hash.key? key_symbol
    fatal "Unknown #{item_name} \"#{key}\""
  end
  hash[key_symbol]
end

#_string_to_xyz(version) ⇒ Object

Release type helper functions



307
308
309
310
311
312
313
314
315
# File 'lib/mixins/user_functions.rb', line 307

def _string_to_xyz(version)
  y_value = version.split('.')[1]
  z_value = version.split('.')[2]
  return 'Z' unless z_value == '0'

  return 'Y' unless y_value == '0'

  return 'X'
end

#chooser(args) ⇒ Object

Takes at least two arguments. The first is a string to match against. The remainders are single key -> value hashes. If the string matches the key, the value is returned.

Returns ” if nothing matches.



113
114
115
116
117
118
119
120
# File 'lib/mixins/user_functions.rb', line 113

def chooser(args)
  _fail_if_unset(__method__.to_s, args)
  match_string = args[0]
  args[1..].each do |match_case|
    return match_case[match_string] if match_case.key? match_string
  end
  return ''
end

#conditional(args) ⇒ Object

Takes three arguments. If the first is truthy, return the 2nd. Else return the 3rd



123
124
125
126
127
128
129
130
# File 'lib/mixins/user_functions.rb', line 123

def conditional(args)
  case args[0]
  when nil, '', false, 'false'
    args[2]
  else
    args[1]
  end
end

#counter(args) ⇒ Object

Takes 2 optional arguments:

initial_value (default 1), increment (default)

Returns a Struct that starts with the initial value and post-increments every time it is used. The method name ‘computed_value’ is required for this form.



137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/mixins/user_functions.rb', line 137

def counter(args)
  initial_value = args[0] || 1
  increment = args[1] || 1

  Struct.new(:current_value, :increment) do
    def computed_value
      current = current_value.to_s
      self.current_value += self.increment
      current
    end
  end.new(initial_value, increment)
end

#date_after(args) ⇒ Object

Date ‘number_of_days` after `date_string`



151
152
153
154
155
156
157
158
# File 'lib/mixins/user_functions.rb', line 151

def date_after(args)
  _fail_if_unset(__method__.to_s, args)
  date_string = args[0]
  number_of_days = args[1].to_i

  date = _date_string_to_date(date_string)
  _date_to_date_string(date + number_of_days)
end

#date_before(args) ⇒ Object

Date ‘number_of_days` before `date_string`



161
162
163
164
165
166
167
168
# File 'lib/mixins/user_functions.rb', line 161

def date_before(args)
  _fail_if_unset(__method__.to_s, args)
  date_string = args[0]
  number_of_days = args[1].to_i

  date = _date_string_to_date(date_string)
  _date_to_date_string(date - number_of_days)
end

#default(args) ⇒ Object

From the argument list, returns the first that isn’t empty or unset Return an empty string if none are.



172
173
174
175
176
177
178
179
# File 'lib/mixins/user_functions.rb', line 172

def default(args)
  args.each do |a|
    next if a.nil?

    return a
  end
  return ''
end

#jira_predeclared_variablesObject



97
98
99
100
101
102
103
104
105
# File 'lib/mixins/user_functions.rb', line 97

def jira_predeclared_variables
  variables = {}

  projects = jira_projects.transform_keys { |k| "#{k.downcase}_project".to_sym }
  sprints = jira_sprints.transform_keys { |k| "#{k.downcase}_sprint".to_sym }
  teams = jira_teams.transform_keys { |k| "#{k.downcase}_team".to_sym }

  variables.merge(**projects, **sprints, **teams)
end

#jira_project(args) ⇒ Object



181
182
183
# File 'lib/mixins/user_functions.rb', line 181

def jira_project(args)
  _jira_key('project', jira_projects, args[0])
end

#jira_projectsObject

Maps of Winston-style Jira names (projects, sprints, to the actual assets



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/mixins/user_functions.rb', line 36

def jira_projects
  {
    BUILD_TOOLS: 'BUILD',
    CLIENT_TOOLS: 'CT',
    CODE_MANAGEMENT: 'CODEMGMT',
    COMMUNITY_PACKAGE_REPO: 'CPR',
    DOC: 'PUPDOC',
    DOCS: 'PUPDOC',
    EZBAKE: 'EZ',
    FACTER: 'FACT',
    FORGE_INTERNAL: 'PF',
    IMAGES: 'IMAGES',
    MODULES: 'MODULES',
    MODULES_INTERNAL: 'FM',
    OPERATIONS: 'OPS',
    PDK: 'CONT',
    PE_INTERNAL: 'PE',
    POOLER: 'POOLER',
    PROJECT_CENTRAL: 'PC',
    PUPPET: 'PUP',
    PUPPETDB: 'PDB',
    PUPPETSERVER: 'SERVER',
    PUPPET_AGENT: 'PA',
    QUALITY_ENGINEERING: 'QENG',
    RELEASE_ENGINEERING: 'RE',
    SLV: 'SLV',
    SUPPORT: 'SUP',
    VANAGON: 'VANAGON',
    WEB: 'WWM',
    WHO: 'WHO'
  }
end

#jira_sprint(args) ⇒ Object



185
186
187
# File 'lib/mixins/user_functions.rb', line 185

def jira_sprint(args)
  _jira_key('sprint', jira_sprints, args[0])
end

#jira_sprintsObject



69
70
71
72
73
74
# File 'lib/mixins/user_functions.rb', line 69

def jira_sprints
  {
    DUMPLING_READY_TO_WORK: 5515,
    RE_KANBAN: 3150
  }
end

#jira_team(args) ⇒ Object Also known as: scrum_team



189
190
191
# File 'lib/mixins/user_functions.rb', line 189

def jira_team(args)
  _jira_key('team', jira_teams, args[0])
end

#jira_teamsObject



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/mixins/user_functions.rb', line 76

def jira_teams
  {
    BOLT: 'Bolt',
    CD4PE: 'CD4PE',
    CODE_MANAGEMENT: 'Dumpling',
    DUMPLING: 'Dumpling',
    FACTER: 'Phoenix',
    INSTALLER: 'Installer and Management',
    NETWORKING: 'Network Automation',
    OPERATIONS: 'Operations',
    PE: 'Dumpling',
    PLATFORM_OS: 'Unicorn',
    PUPPETDB: 'Dumpling',
    PUPPETSERVER: 'Dumpling',
    QE: 'Quality Engineering',
    RELEASE_ENGINEERING: 'Release Engineering',
    SKELETOR: 'Skeletor',
    SLV: 'System Level Validation'
  }
end

#n_digit_version(args) ⇒ Object

Return the first n fields of ‘.’ - delimited version string



195
196
197
198
199
200
201
# File 'lib/mixins/user_functions.rb', line 195

def n_digit_version(args)
  _fail_if_unset(__method__.to_s, args)

  n = args[0].to_i - 1
  version = args[1]
  version.split('.')[0..n].join('.')
end

#prompt(args) ⇒ Object

Prompt if the value isn’t defined or in the environment Arguments:

0: The name of the user variable to prompt for

Returns:

The first found
  value of declared variable
  value from the shell environment
  the string the user types at the prompt


211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/mixins/user_functions.rb', line 211

def prompt(args)
  if args.nil?
    fatal 'function "prompt_only" requires variable name.'
  end
  _fail_if_unset(__method__.to_s, args)

  variable_name = args[0]
  variable_symbol = variable_name.to_sym

  return @declarations[variable_symbol] if @declarations.key?(variable_symbol)
  return ENV[variable_name] if ENV.key?(variable_name)

  print "Enter value for \"#{variable_name}\": "
  return $stdin.gets.chomp
end

#prompt_only(args) ⇒ Object

Explicitly prompt for a value Arguments:

0: The name of the user variable to prompt for

Returns:

The string user types from STDIN


232
233
234
235
236
237
238
239
240
# File 'lib/mixins/user_functions.rb', line 232

def prompt_only(args)
  if args.nil?
    fatal 'function "prompt_only" requires variable name.'
  end
  _fail_if_unset(__method__.to_s, args)
  variable_name = args[0]
  print "Enter value for \"#{variable_name}\": "
  $stdin.gets.chomp
end

#random(args) ⇒ Object

Randomly pick from a list of strings Arguments:

*: a list of strings from which to pick


246
247
248
249
# File 'lib/mixins/user_functions.rb', line 246

def random(args)
  _fail_if_unset(__method__.to_s, args)
  args[Random.new.rand(args.count)]
end

#release_type(args) ⇒ Object

Return a conditional value based upon a version string Arguments:

0: a semver version string.
1: a hash with keys of 'X', 'Y', or 'Z' representing the semver fields
  and a value to set for that corresponding release type.

For example:

0: "2.5.9"
1: {'X' => 'foo', 'Y' => 'bar', 'Z' => 'baz'}

will return ‘baz’



260
261
262
263
264
265
266
267
268
269
270
271
272
273
# File 'lib/mixins/user_functions.rb', line 260

def release_type(args)
  _fail_if_unset(__method__.to_s, args)
  this_type = _string_to_xyz(args[0])
  condition_hash = args[1]

  unless condition_hash.keys.sort == %w[X Y Z].sort
    fatal 'function "release_type" requires a hash with keys ("X", "Y", "Z") as 2nd argument. ' \
          "Got: #{condition_hash}"
  end

  return condition_hash[this_type] if condition_hash.key?(this_type)

  fatal "function \"release_type\" could not find a matching case for #{args[0]}\""
end

#split(args) ⇒ Object

Split a string into a packed array given a regular expression to split with Arguments:

0: a regular expression for splitting, typically '\s*,\s*'
1: string to split


279
280
281
282
283
# File 'lib/mixins/user_functions.rb', line 279

def split(args)
  _fail_if_unset(__method__.to_s, args)
  fatal('function "split" requires exactly two arguments') unless args.size == 2
  args[1].split(%r{#{args[0]}})
end

#user_function_allowlistObject

A bit of paranoia that lists the allowable function names so that arbitrary function calls aren’t permitted from the YAML files.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/mixins/user_functions.rb', line 13

def user_function_allowlist
  %w[
    chooser
    conditional
    counter
    date_after
    date_before
    default
    jira_project
    jira_sprint
    jira_team
    jira_user
    n_digit_version
    prompt
    prompt_only
    random
    release_type
    scrum_team
    split
  ]
end