Skip to content

Commit

Permalink
TMP
Browse files Browse the repository at this point in the history
  • Loading branch information
Morriar committed Aug 27, 2024
1 parent 5ac1ce9 commit 6f1ddd6
Show file tree
Hide file tree
Showing 4 changed files with 11,816 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@ group :development do
gem "rubocop-sorbet", require: false
gem "tapioca", require: false
end

gem "octokit", "~> 9.1"
19 changes: 19 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -10,33 +10,47 @@ PATH
GEM
remote: https://rubygems.org/
specs:
addressable (2.8.7)
public_suffix (>= 2.0.2, < 7.0)
ansi (1.5.0)
ast (2.4.2)
builder (3.3.0)
debug (1.9.2)
irb (~> 1.10)
reline (>= 0.3.8)
erubi (1.13.0)
faraday (2.10.1)
faraday-net_http (>= 2.0, < 3.2)
logger
faraday-net_http (3.1.1)
net-http
io-console (0.7.2)
irb (1.12.0)
rdoc
reline (>= 0.4.2)
json (2.7.2)
language_server-protocol (3.17.0.3)
logger (1.6.0)
minitest (5.24.1)
minitest-reporters (1.7.1)
ansi
builder
minitest (>= 5.0)
ruby-progressbar
net-http (0.4.1)
uri
netrc (0.11.0)
octokit (9.1.0)
faraday (>= 1, < 3)
sawyer (~> 0.9)
parallel (1.25.1)
parser (3.3.4.0)
ast (~> 2.4.1)
racc
prism (0.30.0)
psych (5.1.2)
stringio
public_suffix (6.0.1)
racc (1.8.0)
rainbow (3.1.1)
rake (13.2.1)
Expand Down Expand Up @@ -68,6 +82,9 @@ GEM
rubocop-sorbet (0.8.5)
rubocop (>= 1)
ruby-progressbar (1.13.0)
sawyer (0.9.2)
addressable (>= 2.3.5)
faraday (>= 0.17.3, < 3)
sorbet (0.5.11506)
sorbet-static (= 0.5.11506)
sorbet-runtime (0.5.11506)
Expand All @@ -89,6 +106,7 @@ GEM
yard-sorbet
thor (1.3.1)
unicode-display_width (2.5.0)
uri (0.13.0)
yard (0.9.36)
yard-sorbet (0.9.0)
sorbet-runtime
Expand All @@ -105,6 +123,7 @@ DEPENDENCIES
debug
minitest (~> 5.0)
minitest-reporters
octokit (~> 9.1)
rake (~> 13.2.1)
rubocop-shopify
rubocop-sorbet
Expand Down
240 changes: 240 additions & 0 deletions pr_review
Original file line number Diff line number Diff line change
@@ -0,0 +1,240 @@
#!/usr/bin/env ruby
# typed: strict
# frozen_string_literal: true

require "spoom"

require "open-uri"
require "octokit"

require "net/http"
require "json"
require "uri"

require "erb"

# https://openai-proxy.shopify.io/
module OpenAI
class << self
extend T::Sig

sig { returns(Client) }
def client
Client.new
end
end

class Client
extend T::Sig

sig { void }
def initialize
@token = T.let(File.read("TOKEN_OPENAI"), String)
end

sig { params(content: String).returns(String) }
def message(content)
uri = URI("https://proxy.shopify.ai/v3/v1/chat/completions")
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true

request = Net::HTTP::Post.new(uri)
request["Content-Type"] = "application/json"
request["Authorization"] = @token

payload = {
model: "anthropic:claude-3-5-sonnet",
messages: [
{
role: "user",
content: content,
},
],
}

request.body = payload.to_json
response = http.request(request)

JSON.parse(response.body)["choices"][0]["message"]["content"]
end
end
end

module Github
class << self
extend T::Sig

sig { returns(Octokit::Client) }
def client
Octokit::Client.new(
user_agent: "Shopify - CodeDB",
bearer_token: File.read("TOKEN_GITHUB"),
auto_paginate: false,
per_page: 100,
)
end
end
end

class Reviewer
extend T::Sig

sig { params(repo: String, pr_number: String).void }
def initialize(repo, pr_number)
@repo = repo
@pr_number = pr_number
end

sig { returns(T::Hash[String, T.untyped]) }
def make_review
github = Github.client
pr = github.pull_request(@repo, @pr_number)
commit_sha = pr.head.sha
diff = github.pull_request_files(@repo, @pr_number)

template = ERB.new(<<~ERB).result(binding)
Can you do code review for this PR?
Here are some rules to follow:
* Act as a skilled, patient yet amicable senior developer.
* Be as succinct and to the point as possible.
* If the PR is a new feature, explain what would be the best way to implement the feature.
* If the PR is a refactor, explain what would be the best way to refactor the code.
* If the PR is a bug fix, explain what would be the best way to fix the bug.
* When suggesting changes, explain why the change is necessary and what problem it solves.
* When suggesting changes, explain how the change would improve the code.
* When suggesting changes, five a code snippet that shows the change.
* When talking about corner cases, always provide a test case that demonstrates the corner case.
* Format your answer as a valid JSON object and only return the JSON object:
{
commit_id: commit_sha,
body: review_content,
event: "COMMENT", # and nothing else
comments: [
{
path: "path/to/file",
position: 1,
body: "This is a comment",
},
],
}
Title: <%= pr.title %>
Description:
```md
<%= pr.body %>
```
Diff at commit #{commit_sha}:
<% diff.each do |file| %>
```diff
<%= file.filename %> (<%= file.status %>, +<%= file.additions %> -<%= file.deletions %>)
<%= file.patch %>
```
<% end %>
ERB

openai = OpenAI.client
review_json = openai.message(template)
JSON.parse(review_json)
end

# sig { params(review_json: String).void }
# def post_review!(review_json)
# object = JSON.parse(review_json)
# review = github.create_pull_request_review(REPO, PR_NUMBER, object)

# puts "Review created successfully. Review ID: #{review.id}"
# end
end

unless ARGV.length == 2
puts "Usage: #{$PROGRAM_NAME} <repo> <pr_number>"
exit 1
end

repo, pr_number = ARGV

puts repo
repos_root = "/Users/at/src/github.com/"
repo_path = File.join(repos_root, repo)

context = Spoom::Context.new(repo_path)

github = Github.client
pr = github.pull_request(repo, pr_number)
commit_sha = pr.head.sha
diff = github.pull_request_files(repo, pr_number)

# diff.each do |file|
# puts JSON.generate(context.read(file.filename))
# end
# exit(1)

prompt = ERB.new(<<~ERB).result(binding)
Can you do code review for this PR?
Here are some rules to follow:
* Act as a skilled, patient yet amicable senior developer.
* Be as succinct and to the point as possible.
* If the PR is a new feature, explain what would be the best way to implement the feature.
* If the PR is a refactor, explain what would be the best way to refactor the code.
* If the PR is a bug fix, explain what would be the best way to fix the bug.
* When suggesting changes, explain why the change is necessary and what problem it solves.
* When suggesting changes, explain how the change would improve the code.
* When suggesting changes, five a code snippet that shows the change.
* When talking about corner cases, always provide a test case that demonstrates the corner case.
* Format your answer as a valid JSON object and only return the JSON object:
{
commit_id: #{commit_sha},
body: review_content,
event: "COMMENT", # and nothing else
comments: [
{
path: "path/to/file",
position: 1,
body: "This is a comment",
},
],
}
* Do not make comments on code that is not impacted by the PR.
* Do not create comments if they are only positive.
Title: <%= pr.title %>
Description:
```md
<%= pr.body %>
```
Diff:
<% diff.each do |file| %>
```diff
<%= file.filename %> (<%= file.status %>, +<%= file.additions %> -<%= file.deletions %>)
<%= file.patch %>
```
<% end %>
Here are the files impacted by this PR:
<% diff.each do |file| %>
~~~rb
<%= JSON.generate(context.read(file.filename)) %>
~~~
<% end %>
ERB

openai = OpenAI.client
review_json = openai.message(prompt)
review_obj = JSON.parse(review_json)
puts JSON.pretty_generate(review_obj)


# TODO: move to CodeDB
# TODO: better engineer the prompts (files? previous reviews? docs? style guide?)
Loading

0 comments on commit 6f1ddd6

Please sign in to comment.