Module: Commiti::TextGenerationStyle

Defined in:
lib/services/text_generation_style.rb

Constant Summary collapse

DEFAULT_PR_SECTIONS =
[
  { name: 'Summary', guidance: 'Summarize the change in one concise paragraph.' },
  { name: 'Motivation', guidance: 'Explain why this change is needed.' },
  { name: 'Changes Made', guidance: 'List the concrete changes made in the diff.' },
  { name: 'Testing Notes', guidance: 'Describe the tests, checks, or verification performed.' }
].freeze
DEFAULT_CONFIG =
{
  commit: {
    subject_case: 'preserve'
  },
  pr: {
    sections: DEFAULT_PR_SECTIONS
  }
}.freeze
ALLOWED_COMMIT_SUBJECT_CASES =
%w[preserve uppercase lowercase].freeze

Class Method Summary collapse

Class Method Details

.apply_commit_subject_case(subject, style_config) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/services/text_generation_style.rb', line 52

def self.apply_commit_subject_case(subject, style_config)
  normalized_subject = subject.to_s

  case commit_subject_case(style_config)
  when 'uppercase'
    normalized_subject.sub(/\A([[:alpha:]])/) { Regexp.last_match(1).upcase }
  when 'lowercase'
    normalized_subject.sub(/\A([[:alpha:]])/) { Regexp.last_match(1).downcase }
  else
    normalized_subject
  end
end

.commit_subject_case(style_config) ⇒ Object



37
38
39
# File 'lib/services/text_generation_style.rb', line 37

def self.commit_subject_case(style_config)
  lookup(lookup(style_config, :commit), :subject_case) || DEFAULT_CONFIG[:commit][:subject_case]
end

.commit_subject_case_instruction(style_config) ⇒ Object



41
42
43
44
45
46
47
48
49
50
# File 'lib/services/text_generation_style.rb', line 41

def self.commit_subject_case_instruction(style_config)
  case commit_subject_case(style_config)
  when 'uppercase'
    'Capitalize the first alphabetic character in the subject line.'
  when 'lowercase'
    'Keep the first alphabetic character in the subject line lowercase.'
  else
    'Preserve the natural casing chosen by the content of the change.'
  end
end

.first_pr_section_header(style_config) ⇒ Object



82
83
84
# File 'lib/services/text_generation_style.rb', line 82

def self.first_pr_section_header(style_config)
  pr_section_headers(style_config).first
end

.normalize(raw_config) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/services/text_generation_style.rb', line 23

def self.normalize(raw_config)
  raw_hash = raw_config.is_a?(Hash) ? raw_config : {}
  style_hash = lookup(raw_hash, :text_generation) || lookup(raw_hash, :generation) || raw_hash

  {
    commit: {
      subject_case: normalize_subject_case(lookup(lookup(style_hash, :commit), :subject_case))
    },
    pr: {
      sections: normalize_pr_sections(lookup(lookup(style_hash, :pr), :sections))
    }
  }
end

.pr_section_headers(style_config) ⇒ Object



71
72
73
# File 'lib/services/text_generation_style.rb', line 71

def self.pr_section_headers(style_config)
  pr_sections(style_config).map { |section| "## #{section[:name]}" }
end

.pr_section_prompt_lines(style_config) ⇒ Object



75
76
77
78
79
80
# File 'lib/services/text_generation_style.rb', line 75

def self.pr_section_prompt_lines(style_config)
  pr_sections(style_config).each_with_index.map do |section, index|
    guidance = section[:guidance] || default_pr_section_guidance(index, section[:name])
    "   - ## #{section[:name]}\n     #{guidance}"
  end.join("\n")
end

.pr_sections(style_config) ⇒ Object



65
66
67
68
69
# File 'lib/services/text_generation_style.rb', line 65

def self.pr_sections(style_config)
  sections = lookup(lookup(style_config, :pr), :sections)
  sections = DEFAULT_PR_SECTIONS if sections.nil? || sections.empty?
  sections
end