Class: SpreenPr::Application

Inherits:
Object
  • Object
show all
Defined in:
lib/spreen_pr/application.rb,
sig/generated/spreen_pr/application.rbs

Overview

Derives a pull request title and labels from a topic branch name shaped {user}/{issue}/{category}/{summary}, with the hotfix special case {user}/{issue}/hotfix/{category}/{summary}.

Constant Summary collapse

EXPECTED_SHAPE =

: String

Returns:

  • (String)
'{user}/{issue}/{category}/{summary}'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(branch_name:) ⇒ Application

Returns a new instance of Application.

Parameters:

  • branch_name: (String)


19
20
21
22
23
# File 'lib/spreen_pr/application.rb', line 19

def initialize(branch_name:)
  @branch_name = branch_name
  @segments    = branch_name.split('/')
  validate!
end

Instance Attribute Details

#branch_nameString (readonly)

: String

Returns:

  • (String)


47
48
49
# File 'lib/spreen_pr/application.rb', line 47

def branch_name
  @branch_name
end

#segmentsArray[String] (readonly)

: Array

Returns:

  • (Array[String])


48
49
50
# File 'lib/spreen_pr/application.rb', line 48

def segments
  @segments
end

Class Method Details

.run(branch_name:) ⇒ String

Parameters:

  • branch_name: (String)

Returns:

  • (String)


13
14
15
# File 'lib/spreen_pr/application.rb', line 13

def self.run(branch_name:)
  new(branch_name:).title
end

Instance Method Details

#hotfix?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/spreen_pr/application.rb', line 51

def hotfix?
  segments[-3] == 'hotfix'
end

#labelsArray[String]

Returns:

  • (Array[String])


31
32
33
# File 'lib/spreen_pr/application.rb', line 31

def labels
  hotfix? ? [segments[-3].capitalize, segments[-2]] : [segments[-2]]
end

#titleString

Returns:

  • (String)


26
27
28
# File 'lib/spreen_pr/application.rb', line 26

def title
  "#{labels.map { |label| "[#{label}]" }.join} #{topic}"
end

#to_hHash[Symbol, String | Array[String]]

Returns:

  • (Hash[Symbol, String | Array[String]])


41
42
43
# File 'lib/spreen_pr/application.rb', line 41

def to_h
  { title:, labels: }
end

#topicString

Returns:

  • (String)


36
37
38
# File 'lib/spreen_pr/application.rb', line 36

def topic
  segments.last.split(/[-_]/).map(&:capitalize).join("\s")
end

#validate!void

This method returns an undefined value.



56
57
58
59
60
61
# File 'lib/spreen_pr/application.rb', line 56

def validate!
  return if segments.size >= 3 && segments.none?(&:empty?)

  raise ArgumentError,
        "Branch name `#{branch_name}` does not match the expected `#{EXPECTED_SHAPE}` shape"
end