Skip to content

Commit 0abfd97

Browse files
authored
Merge pull request #3 from flows-network/refactor
Refactor to fetch the patch using the GitHub API
2 parents 57f547b + b8dda86 commit 0abfd97

File tree

3 files changed

+23
-35
lines changed

3 files changed

+23
-35
lines changed

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,4 @@ flowsnet-platform-sdk = "0.1.2"
1818
lazy_static = "1.4.0"
1919
regex = "1.7.1"
2020
openai-flows = "0.4"
21-
http_req_wasi = { version = "0.10.2", features = ["wasmedge_ssl"] }
2221
words-count = "0.1.4"

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# <p align="center">GPT Nitro for Github PR</p>
1+
# <p align="center">ChatGPT/4 code reviewer for Github PR</p>
22

33
<p align="center">
44
<a href="https://discord.gg/ccZn9ZMfFf">
@@ -26,6 +26,12 @@ This flow function (or 🤖) will be triggered and executed when a new PR is rai
2626

2727
The GitHub repo is connected to the flow function via the [flows.network](https://flows.network/) platform. The "trigger phrase" can also be configured in [flows.network](https://flows.network/).
2828

29+
### Deploy your own code review bot in 3 simple steps
30+
31+
1. Fork this repo. It contains the source code for the GitHub bot.
32+
2. Import the forked repo on [flows.network](https://flows.network/) as a flow function.
33+
3. Connect the flow function to the GitHub repo you want to deploy the bot on [flows.network](https://flows.network/) UI.
34+
2935
<p align="center">
3036
<a href="https://youtu.be/kvBhNBXmBaE" taregt=_blank><img src="https://img.youtube.com/vi/kvBhNBXmBaE/hqdefault.jpg"/></a><br/>
3137
<i>Click on the picture above to watch a 3-min tutorial video</i>

src/github-pr-summary.rs

Lines changed: 16 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@ use github_flows::{
55
octocrab::models::events::payload::{IssueCommentEventAction, PullRequestEventAction},
66
EventPayload,
77
};
8-
use http_req::{
9-
request::{Method, Request},
10-
uri::Uri,
11-
};
128
use openai_flows::{chat_completion_default_key, ChatModel, ChatOptions};
139
use std::env;
1410

11+
// The soft character limit of the input context size
12+
// the max token size or word count for GPT4 is 8192
13+
// the max token size or word count for GPT35Turbo is 4096
14+
static CHAR_SOFT_LIMIT : usize = 9000;
15+
1516
#[no_mangle]
1617
#[tokio::main(flavor = "current_thread")]
1718
pub async fn run() -> anyhow::Result<()> {
@@ -44,7 +45,7 @@ async fn handler(
4445
trigger_phrase: &str,
4546
payload: EventPayload,
4647
) {
47-
let (_title, pull_number, _contributor) = match payload {
48+
let (title, pull_number, _contributor) = match payload {
4849
EventPayload::PullRequestEvent(e) => {
4950
if e.action != PullRequestEventAction::Opened {
5051
write_error_log!("Not a Opened pull event");
@@ -85,25 +86,9 @@ async fn handler(
8586
};
8687

8788
let octo = get_octo(Some(String::from(login)));
88-
let issues = octo.issues(owner, repo);
89-
90-
let chat_id = format!("PR#{pull_number}");
91-
92-
// let patch_url = "https://patch-diff.githubusercontent.com/raw/WasmEdge/WasmEdge/pull/2368.patch".to_string();
93-
let patch_url = format!(
94-
"https://patch-diff.githubusercontent.com/raw/{owner}/{repo}/pull/{pull_number}.patch"
95-
);
96-
let patch_uri = Uri::try_from(patch_url.as_str()).unwrap();
97-
let mut writer = Vec::new();
98-
let _ = Request::new(&patch_uri)
99-
.method(Method::GET)
100-
.header("Accept", "application/vnd.github+json")
101-
.header("User-Agent", "Flows Network Connector")
102-
.send(&mut writer)
103-
.map_err(|_e| {})
104-
.unwrap();
105-
let patch_as_text = String::from_utf8_lossy(&writer);
106-
89+
let pulls = octo.pulls(owner, repo);
90+
91+
let patch_as_text = pulls.get_patch(pull_number).await.unwrap();
10792
let mut current_commit = String::new();
10893
let mut commits: Vec<String> = Vec::new();
10994
for line in patch_as_text.lines() {
@@ -116,10 +101,8 @@ async fn handler(
116101
// Start a new commit
117102
current_commit.clear();
118103
}
119-
// Append the line to the current commit if the current commit is less than 18000 chars
120-
// the max token size or word count for GPT4 is 8192
121-
// the max token size or word count for GPT35Turbo is 4096
122-
if current_commit.len() < 9000 {
104+
// Append the line to the current commit if the current commit is less than CHAR_SOFT_LIMIT
105+
if current_commit.len() < CHAR_SOFT_LIMIT {
123106
current_commit.push_str(line);
124107
current_commit.push('\n');
125108
}
@@ -128,17 +111,17 @@ async fn handler(
128111
// Store the last commit
129112
commits.push(current_commit.clone());
130113
}
131-
// write_error_log!(&format!("Num of commits = {}", commits.len()));
132114

133115
if commits.is_empty() {
134116
write_error_log!("Cannot parse any commit from the patch file");
135117
return;
136118
}
137119

120+
let chat_id = format!("PR#{pull_number}");
121+
let system = &format!("You are an experienced software developer. You will act as a reviewer for a GitHub Pull Request titled \"{}\".", title);
138122
let mut reviews: Vec<String> = Vec::new();
139123
let mut reviews_text = String::new();
140124
for (_i, commit) in commits.iter().enumerate() {
141-
let system = "You are an experienced software developer. You will act as a reviewer for GitHub Pull Requests.";
142125
let co = ChatOptions {
143126
// model: ChatModel::GPT4,
144127
model: ChatModel::GPT35Turbo,
@@ -148,8 +131,7 @@ async fn handler(
148131
};
149132
let question = "The following is a GitHub patch. Please summarize the key changes and identify potential problems. Start with the most important findings.\n\n".to_string() + commit;
150133
if let Some(r) = chat_completion_default_key(&chat_id, &question, &co) {
151-
write_error_log!("Got a patch summary");
152-
if reviews_text.len() < 9000 {
134+
if reviews_text.len() < CHAR_SOFT_LIMIT {
153135
reviews_text.push_str("------\n");
154136
reviews_text.push_str(&r.choice);
155137
reviews_text.push('\n');
@@ -161,7 +143,6 @@ async fn handler(
161143
let mut resp = String::new();
162144
resp.push_str("Hello, I am a [serverless review bot](https://github.com/flows-network/github-pr-summary/) on [flows.network](https://flows.network/). Here are my reviews of code commits in this PR.\n\n------\n\n");
163145
if reviews.len() > 1 {
164-
let system = "You are a helpful assistant and an experienced software developer.";
165146
let co = ChatOptions {
166147
// model: ChatModel::GPT4,
167148
model: ChatModel::GPT35Turbo,
@@ -181,6 +162,8 @@ async fn handler(
181162
resp.push_str(review);
182163
resp.push_str("\n\n");
183164
}
165+
184166
// Send the entire response to GitHub PR
167+
let issues = octo.issues(owner, repo);
185168
issues.create_comment(pull_number, resp).await.unwrap();
186169
}

0 commit comments

Comments
 (0)