Class: Bugsage::FixApplicator
- Inherits:
-
Object
- Object
- Bugsage::FixApplicator
- Extended by:
- JsonEndpoint
- Defined in:
- lib/bugsage/fix_applicator.rb
Constant Summary collapse
- ENDPOINT =
"/bugsage/apply-fix"
Class Method Summary collapse
- .apply(location:, fix: "", code_patch: nil, code_fix: "", mode: "comment") ⇒ Object
- .apply_allowed? ⇒ Boolean
- .apply_comment!(lines, line_number, fix) ⇒ Object
- .apply_patch!(lines, patch_data, _error_line) ⇒ Object
- .forbidden ⇒ Object
- .handle_request(env) ⇒ Object
Methods included from JsonEndpoint
error_response, json_response, not_found, parse_request_body
Class Method Details
.apply(location:, fix: "", code_patch: nil, code_fix: "", mode: "comment") ⇒ Object
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 68 69 70 71 72 73 74 |
# File 'lib/bugsage/fix_applicator.rb', line 37 def self.apply(location:, fix: "", code_patch: nil, code_fix: "", mode: "comment") file_path, line_number = CodeContext.extract_location(location) return error_response(Bugsage.t("errors.could_not_parse_location")) unless file_path && line_number unless File.exist?(file_path) return error_response(Bugsage.t("errors.source_file_not_found", file_path: file_path)) end lines = File.readlines(file_path, chomp: true) if line_number < 1 || line_number > lines.length return error_response(Bugsage.t("errors.line_out_of_range", line_number: line_number)) end if code_patch apply_patch!(lines, code_patch, line_number) elsif !code_fix.empty? && mode != "comment" patch = CodePatch.from_legacy(code_fix, line_number) apply_patch!(lines, patch, line_number) else apply_comment!(lines, line_number, fix) end File.write(file_path, "#{lines.join("\n")}\n") editor = EditorLinks.for_location("#{file_path}:#{line_number}") { ok: true, message: Bugsage.t("errors.fix_applied", file_path: file_path, line_number: line_number), file_path: editor[:file_path], line_number: editor[:line_number], editor_links: editor } rescue Bugsage::Error => e error_response(e.) rescue StandardError => e error_response("#{e.class}: #{e.}") end |
.apply_allowed? ⇒ Boolean
98 99 100 101 |
# File 'lib/bugsage/fix_applicator.rb', line 98 def self.apply_allowed? env = Bugsage.configuration.current_environment %w[development test].include?(env.to_s) end |
.apply_comment!(lines, line_number, fix) ⇒ Object
90 91 92 93 94 95 96 |
# File 'lib/bugsage/fix_applicator.rb', line 90 def self.apply_comment!(lines, line_number, fix) marker = "# BUGSAGE:" comment = "#{marker} #{fix}" return if line_number > 1 && lines[line_number - 2].to_s.include?(marker) lines.insert(line_number - 1, comment) end |
.apply_patch!(lines, patch_data, _error_line) ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/bugsage/fix_applicator.rb', line 76 def self.apply_patch!(lines, patch_data, _error_line) patch = case patch_data when CodePatch then patch_data when Hash then CodePatch.from_hash(patch_data) end raise Bugsage::Error, Bugsage.t("errors.ai_code_patch_missing") unless patch raise Bugsage::Error, Bugsage.t("errors.no_code_change_required") if patch.action == "no_change" raise Bugsage::Error, Bugsage.t("errors.suggested_code_already_exists") if patch.duplicates_existing?(lines) patch.apply!(lines) end |
.forbidden ⇒ Object
103 104 105 |
# File 'lib/bugsage/fix_applicator.rb', line 103 def self.forbidden json_response(error_response(Bugsage.t("errors.fix_only_in_dev_test")), status: 403) end |
.handle_request(env) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/bugsage/fix_applicator.rb', line 11 def self.handle_request(env) return not_found unless Bugsage.configuration.enabled? return forbidden unless apply_allowed? payload = parse_request_body(env) location = payload["location"].to_s fix = payload["fix"].to_s.strip code_patch = payload["code_patch"] code_fix = payload["code_fix"].to_s.strip mode = payload["mode"].to_s.strip return json_response(error_response(Bugsage.t("errors.location_required"))) if location.empty? if code_patch.nil? && code_fix.empty? && fix.empty? return json_response(error_response(Bugsage.t("errors.fix_text_or_patch_required"))) end result = apply( location: location, fix: fix, code_patch: code_patch, code_fix: code_fix, mode: mode ) json_response(result) end |