Class: CommitGpt::CommitAi

Inherits:
Object
  • Object
show all
Includes:
DiffHelpers
Defined in:
lib/commitgpt/commit_ai.rb

Overview

Commit AI roboter based on GPT-3

Constant Summary collapse

COMMIT_FORMATS =

Commit format templates

{
  'simple' => '<commit message>',
  'conventional' => '<type>[optional (<scope>)]: <commit message>',
  'gitmoji' => ':emoji: <commit message>'
}.freeze
CONVENTIONAL_TYPES =

Conventional commit types based on aicommits implementation

{
  'docs' => 'Documentation only changes',
  'style' => 'Changes that do not affect the meaning of the code (white-space, formatting, missing semi-colons, etc)',
  'refactor' => 'A code change that improves code structure without changing functionality (renaming, restructuring classes/methods, extracting functions, etc)',
  'perf' => 'A code change that improves performance',
  'test' => 'Adding missing tests or correcting existing tests',
  'build' => 'Changes that affect the build system or external dependencies',
  'ci' => 'Changes to our CI configuration files and scripts',
  'chore' => "Other changes that don't modify src or test files",
  'revert' => 'Reverts a previous commit',
  'feat' => 'A new feature',
  'fix' => 'A bug fix'
}.freeze
GITMOJI_TYPES =

Gitmoji mappings based on gitmoji.dev

{
  '๐ŸŽจ' => 'Improve structure / format of the code',
  'โšก' => 'Improve performance',
  '๐Ÿ”ฅ' => 'Remove code or files',
  '๐Ÿ›' => 'Fix a bug',
  '๐Ÿš‘' => 'Critical hotfix',
  'โœจ' => 'Introduce new features',
  '๐Ÿ“' => 'Add or update documentation',
  '๐Ÿš€' => 'Deploy stuff',
  '๐Ÿ’„' => 'Add or update the UI and style files',
  '๐ŸŽ‰' => 'Begin a project',
  'โœ…' => 'Add, update, or pass tests',
  '๐Ÿ”’' => 'Fix security or privacy issues',
  '๐Ÿ”' => 'Add or update secrets',
  '๐Ÿ”–' => 'Release / Version tags',
  '๐Ÿšจ' => 'Fix compiler / linter warnings',
  '๐Ÿšง' => 'Work in progress',
  '๐Ÿ’š' => 'Fix CI Build',
  'โฌ‡๏ธ' => 'Downgrade dependencies',
  'โฌ†๏ธ' => 'Upgrade dependencies',
  '๐Ÿ“Œ' => 'Pin dependencies to specific versions',
  '๐Ÿ‘ท' => 'Add or update CI build system',
  '๐Ÿ“ˆ' => 'Add or update analytics or track code',
  'โ™ป๏ธ' => 'Refactor code',
  'โž•' => 'Add a dependency',
  'โž–' => 'Remove a dependency',
  '๐Ÿ”ง' => 'Add or update configuration files',
  '๐Ÿ”จ' => 'Add or update development scripts',
  '๐ŸŒ' => 'Internationalization and localization',
  'โœ๏ธ' => 'Fix typos',
  '๐Ÿ’ฉ' => 'Write bad code that needs to be improved',
  'โช' => 'Revert changes',
  '๐Ÿ”€' => 'Merge branches',
  '๐Ÿ“ฆ' => 'Add or update compiled files or packages',
  '๐Ÿ‘ฝ' => 'Update code due to external API changes',
  '๐Ÿšš' => 'Move or rename resources (e.g.: files, paths, routes)',
  '๐Ÿ“„' => 'Add or update license',
  '๐Ÿ’ฅ' => 'Introduce breaking changes',
  '๐Ÿฑ' => 'Add or update assets',
  'โ™ฟ' => 'Improve accessibility',
  '๐Ÿ’ก' => 'Add or update comments in source code',
  '๐Ÿป' => 'Write code drunkenly',
  '๐Ÿ’ฌ' => 'Add or update text and literals',
  '๐Ÿ—ƒ' => 'Perform database related changes',
  '๐Ÿ”Š' => 'Add or update logs',
  '๐Ÿ”‡' => 'Remove logs',
  '๐Ÿ‘ฅ' => 'Add or update contributor(s)',
  '๐Ÿšธ' => 'Improve user experience / usability',
  '๐Ÿ—' => 'Make architectural changes',
  '๐Ÿ“ฑ' => 'Work on responsive design',
  '๐Ÿคก' => 'Mock things',
  '๐Ÿฅš' => 'Add or update an easter egg',
  '๐Ÿ™ˆ' => 'Add or update a .gitignore file',
  '๐Ÿ“ธ' => 'Add or update snapshots',
  'โš—' => 'Perform experiments',
  '๐Ÿ”' => 'Improve SEO',
  '๐Ÿท' => 'Add or update types',
  '๐ŸŒฑ' => 'Add or update seed files',
  '๐Ÿšฉ' => 'Add, update, or remove feature flags',
  '๐Ÿฅ…' => 'Catch errors',
  '๐Ÿ’ซ' => 'Add or update animations and transitions',
  '๐Ÿ—‘' => 'Deprecate code that needs to be cleaned up',
  '๐Ÿ›‚' => 'Work on code related to authorization, roles and permissions',
  '๐Ÿฉน' => 'Simple fix for a non-critical issue',
  '๐Ÿง' => 'Data exploration/inspection',
  'โšฐ' => 'Remove dead code',
  '๐Ÿงช' => 'Add a failing test',
  '๐Ÿ‘”' => 'Add or update business logic',
  '๐Ÿฉบ' => 'Add or update healthcheck',
  '๐Ÿงฑ' => 'Infrastructure related changes',
  '๐Ÿง‘โ€๐Ÿ’ป' => 'Improve developer experience',
  '๐Ÿ’ธ' => 'Add sponsorships or money related infrastructure',
  '๐Ÿงต' => 'Add or update code related to multithreading or concurrency',
  '๐Ÿฆบ' => 'Add or update code related to validation'
}.freeze

Constants included from DiffHelpers

DiffHelpers::LOCK_FILES

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from DiffHelpers

#detect_lock_file_changes, #git_diff, #prompt_diff_handling, #prompt_no_staged_changes, #split_diff_by_length

Constructor Details

#initializeCommitAi



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/commitgpt/commit_ai.rb', line 120

def initialize
  provider_config = ConfigManager.get_active_provider_config

  if provider_config
    @api_key = provider_config['api_key']
    @base_url = provider_config['base_url']
    @model = provider_config['model']
    @diff_len = provider_config['diff_len'] || 32_768
  else
    @api_key = nil
    @base_url = nil
    @model = nil
    @diff_len = 32_768
  end

  @commit_format = ConfigManager.get_commit_format
end

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key.



19
20
21
# File 'lib/commitgpt/commit_ai.rb', line 19

def api_key
  @api_key
end

#base_urlObject (readonly)

Returns the value of attribute base_url.



19
20
21
# File 'lib/commitgpt/commit_ai.rb', line 19

def base_url
  @base_url
end

#commit_formatObject (readonly)

Returns the value of attribute commit_format.



19
20
21
# File 'lib/commitgpt/commit_ai.rb', line 19

def commit_format
  @commit_format
end

#diff_lenObject (readonly)

Returns the value of attribute diff_len.



19
20
21
# File 'lib/commitgpt/commit_ai.rb', line 19

def diff_len
  @diff_len
end

#modelObject (readonly)

Returns the value of attribute model.



19
20
21
# File 'lib/commitgpt/commit_ai.rb', line 19

def model
  @model
end

Instance Method Details

#aicm(verbose: false) ⇒ Object



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/commitgpt/commit_ai.rb', line 138

def aicm(verbose: false)
  exit(1) unless welcome
  diff = git_diff || exit(1)
  if verbose
    puts "โ†’ Git diff (#{diff.length} chars):".cyan
    puts diff
    puts "\n"
  end

  loop do
    ai_commit_message = message(diff) || exit(1)
    action = confirm_commit(ai_commit_message)

    case action
    when :commit
      commit_command = "git commit -m \"#{ai_commit_message}\""
      puts "\nโ†’ Executing: #{commit_command}".yellow
      system(commit_command)
      puts "\n\n"
      puts `git log -1`
      break
    when :regenerate
      puts "\n"
      next
    when :edit
      prompt = TTY::Prompt.new
      new_message = prompt.ask('Enter your commit message:')
      if new_message && !new_message.strip.empty?
         commit_command = "git commit -m \"#{new_message}\""
         system(commit_command)
         puts "\n"
         puts `git log -1`
      else
         puts 'โœ– Commit aborted (empty message).'.red
      end
      break
    when :exit
      puts 'โš  Exit without commit.'.yellow
      break
    end
  end
end

#list_modelsObject



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/commitgpt/commit_ai.rb', line 181

def list_models
  headers = {
    'Content-Type' => 'application/json',
    'User-Agent' => "Ruby/#{RUBY_VERSION}"
  }
  headers['Authorization'] = "Bearer #{@api_key}" if @api_key

  begin
    response = HTTParty.get("#{@base_url}/models", headers: headers)
    models = response['data'] || []
    models.each { |m| puts m['id'] }
  rescue StandardError => e
    puts "โœ– Failed to list models: #{e.message}".red
  end
end