Skip to content

Commit

Permalink
🔨 Add ability to edit pull requests in VS Code.
Browse files Browse the repository at this point in the history
Why? So that you get full AI auto-completion.
  • Loading branch information
klondikemarlen committed Sep 25, 2024
1 parent e029843 commit 1d1dd56
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 0 deletions.
11 changes: 11 additions & 0 deletions bin/dev
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env ruby

require_relative './github_api'
require_relative './pull-request-editor'

class DevHelper
# Support dashes in command names
Expand All @@ -10,6 +11,7 @@ class DevHelper
"check-types" => :check_types,
"bash-completions" => :bash_completions,
"plantuml-to-png" => :plantuml_to_png,
"edit-pr" => :edit_pr,
}
METHOD_TO_COMMAND = COMMAND_TO_METHOD.invert

Expand Down Expand Up @@ -166,6 +168,15 @@ class DevHelper
system("git checkout -b #{branch_name}")
end

##
# Edits the description of a pull request.
# Example:
# dev edit-pr https://github.com/icefoganalytics/travel-authorization/pull/218
def edit_pr(pull_request_url, *args, **kwargs)
PullRequestEditor.edit_pull_request_description(pull_request_url, *args, **kwargs)
exit(0)
end

def ownit(*args, **kwargs)
file_or_directory = args[0]
raise ScriptError, "Must provide a file or directory path." if file_or_directory.nil?
Expand Down
73 changes: 73 additions & 0 deletions bin/pull-request-editor.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
require "tempfile"
require "open3"

##
# Supports fetching and editing PR descriptions from a full GitHub PR URL using SSH and GitHub CLI.
#
# Example usage:
# - PullRequestEditor.edit_pull_request_description('https://github.com/icefoganalytics/travel-authorization/pull/218')
class PullRequestEditor
# Edits the pull request description using GitHub CLI and VS Code
def self.edit_pull_request_description(pull_request_url)
repo, pull_request_number = extract_repo_and_pull_request_number(pull_request_url)

pull_request_body = fetch_pull_request_body(repo, pull_request_number)

tmp_dir = File.join(Dir.home, "tmp")
Dir.mkdir(tmp_dir) unless Dir.exist?(tmp_dir)

Tempfile.create(["pull_request_description_#{pull_request_number}", ".md"], tmp_dir) do |file|
file.write(pull_request_body)
file.flush

system("code --wait #{file.path}")

updated_pull_request_body = File.read(file.path)

if updated_pull_request_body.strip != pull_request_body.strip
update_pull_request_body(repo, pull_request_number, file.path)
else
puts "No changes made to the PR description."
end
end
end

private

# Extracts the repository name and PR number from a full GitHub PR URL
def self.extract_repo_and_pull_request_number(pull_request_url)
match_data = pull_request_url.match(%r{github.com/([^/]+)/([^/]+)/pull/(\d+)})
repo = "#{match_data[1]}/#{match_data[2]}"
pull_request_number = match_data[3]
[repo, pull_request_number]
end

# Fetches the PR body using the GitHub CLI
def self.fetch_pull_request_body(repo, pull_request_number)
command = "gh pr view #{pull_request_number} --repo #{repo} --json body --jq .body"
puts "running: #{command}"
stdout, stderr, status = Open3.capture3(command)

if status.success?
stdout.strip
else
puts "Error fetching PR description: #{stderr}"
exit(1)
end
end

# Updates the PR body using the GitHub CLI
def self.update_pull_request_body(repo, pull_request_number, new_body_file_path)
command =
"gh pr edit #{pull_request_number} --repo #{repo} --body-file \"#{new_body_file_path}\""
puts "running: #{command}"
stdout, stderr, status = Open3.capture3(command)

if status.success?
puts "stdout: #{stdout}"
puts "PR description updated successfully."
else
puts "Error updating PR description: #{stderr}"
end
end
end

0 comments on commit 1d1dd56

Please sign in to comment.