Module: PrTemplateGenerator

Defined in:
lib/pr_template_generator.rb

Class Method Summary collapse

Class Method Details

.branch_nameObject



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

def self.branch_name
  `git rev-parse --abbrev-ref HEAD`.strip
end

.commits(base_branch:) ⇒ Object



30
31
32
# File 'lib/pr_template_generator.rb', line 30

def self.commits(base_branch:)
  `git log --oneline #{base_branch}..HEAD`.strip
end

.generate_filled_template(base_branch:) ⇒ Object



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
# File 'lib/pr_template_generator.rb', line 38

def self.generate_filled_template(base_branch:)
  output = OpenAIWrapper.call_api(
    messages: [
      {
        role: 'system',
        content: 'You are a helpful assistant that generates filled pull request templates based on provided templates and context.'
      },
      {
        role: 'user',
        content: <<~PROMPT
          Please fill out the following pull request template as closely as possible using the branch name '#{branch_name}' and the following commits:
          #{commits(base_branch: base_branch)}

          Template enclosed in ``` below:
          ```
          #{template_content}
          ```

          Ensure the output closely resembles the provided template.
          Be succinct in the content.
          Return only the filled template without any additional comments.
        PROMPT
      }
    ],
    is_json_output: false
  )
  output.strip.gsub(/^```|```$/, '')
end

.main(base_branch:) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/pr_template_generator.rb', line 67

def self.main(base_branch:)
  if template_path_where_file_is_found.nil?
    puts 'Pull request template not found.'
    puts 'Template should be found at:'
    template_paths.each { |path| puts " - #{path}" }
    return
  end

  filled_template = generate_filled_template(base_branch: base_branch)

  File.write(output_path, filled_template)
  puts "Filled pull request template created at #{output_path}."
end

.output_pathObject



22
23
24
# File 'lib/pr_template_generator.rb', line 22

def self.output_path
  File.join(Dir.pwd, "pull_request_template_#{Time.now.strftime('%Y-%m-%d-%H-%M-%S')}.md")
end

.template_contentObject



34
35
36
# File 'lib/pr_template_generator.rb', line 34

def self.template_content
  File.read(template_path_where_file_is_found)
end

.template_path_where_file_is_foundObject



15
16
17
18
19
20
# File 'lib/pr_template_generator.rb', line 15

def self.template_path_where_file_is_found
  template_paths.each do |path|
    return path if File.exist?(path)
  end
  nil
end

.template_pathsObject



6
7
8
9
10
11
12
13
# File 'lib/pr_template_generator.rb', line 6

def self.template_paths
  filename = 'pull_request_template.md'
  [
    File.join(Dir.pwd, filename),
    File.join(Dir.pwd, 'docs', filename),
    File.join(Dir.pwd, '.github', filename)
  ]
end