Skip to content

Commit 923c072

Browse files
committed
update to github integration 0.4
1 parent eaa79a2 commit 923c072

File tree

2 files changed

+38
-12
lines changed

2 files changed

+38
-12
lines changed

Cargo.toml

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ crate-type = ["cdylib"]
1111
[dependencies]
1212
dotenv = "0.15.0"
1313
flowsnet-platform-sdk = "0.1.1"
14-
github-flows = "0.3.2"
14+
github-flows = "0.4"
1515
openai-flows = "0.2"
1616
serde_json = "1.0.93"
17+
store-flows = "0.2.1"
1718
tokio_wasi = "1.25.1"

src/lib.rs

+36-11
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,19 @@ use github_flows::{
44
get_octo, listen_to_event,
55
octocrab::{models::events::payload::EventPayload, models::events::payload::IssuesEventAction},
66
};
7-
use openai_flows::{chat_completion, ChatOptions, ChatModel};
7+
use openai_flows::{chat_completion, ChatModel, ChatOptions};
88
use std::env;
99

1010
#[no_mangle]
1111
#[tokio::main(flavor = "current_thread")]
1212
pub async fn run() {
1313
dotenv().ok();
1414

15+
let login: String = match env::var("login") {
16+
Err(_) => "alabulei1".to_string(),
17+
Ok(name) => name,
18+
};
19+
1520
let owner: String = match env::var("owner") {
1621
Err(_) => "second-state".to_string(),
1722
Ok(name) => name,
@@ -27,29 +32,41 @@ pub async fn run() {
2732
Ok(name) => name,
2833
};
2934

30-
listen_to_event(&owner, &repo, vec!["issue_comment", "issues"], |payload| {
31-
handler(&owner, &repo, &openai_key_name, payload)
32-
})
35+
listen_to_event(
36+
&login,
37+
&owner,
38+
&repo,
39+
vec!["issue_comment", "issues"],
40+
|payload| handler(&owner, &repo, &openai_key_name, payload),
41+
)
3342
.await;
3443
}
3544

3645
async fn handler(owner: &str, repo: &str, openai_key_name: &str, payload: EventPayload) {
3746
let octo = get_octo(Some(String::from(owner)));
3847
let issues = octo.issues(owner, repo);
3948

40-
4149
match payload {
4250
EventPayload::IssueCommentEvent(e) => {
43-
if e.comment.user.r#type != "Bot" {
51+
let last_comment_id = store_flows::get("last_created_comment").unwrap_or_default();
52+
if e.comment.id.into_inner() != last_comment_id.as_u64().unwrap_or_default() {
4453
if let Some(b) = e.comment.body {
4554
if let Some(r) = chat_completion(
4655
openai_key_name,
4756
&format!("issue#{}", e.issue.number),
4857
&b,
4958
&ChatOptions::default(),
5059
) {
51-
if let Err(e) = issues.create_comment(e.issue.number, r.choice).await {
52-
write_error_log!(e.to_string());
60+
match issues.create_comment(e.issue.number, r.choice).await {
61+
Ok(comment) => {
62+
store_flows::set(
63+
"last_created_comment",
64+
serde_json::to_value(comment.id.into_inner()).unwrap(),
65+
);
66+
}
67+
Err(e) => {
68+
write_error_log!(e.to_string());
69+
}
5370
}
5471
}
5572
}
@@ -69,7 +86,7 @@ async fn handler(owner: &str, repo: &str, openai_key_name: &str, payload: EventP
6986
let co = ChatOptions {
7087
model: ChatModel::GPT4,
7188
restart: true,
72-
restarted_sentence: Some(&prompt)
89+
restarted_sentence: Some(&prompt),
7390
};
7491

7592
if let Some(r) = chat_completion(
@@ -78,8 +95,16 @@ async fn handler(owner: &str, repo: &str, openai_key_name: &str, payload: EventP
7895
&prompt,
7996
&co,
8097
) {
81-
if let Err(e) = issues.create_comment(e.issue.number, r.choice).await {
82-
write_error_log!(e.to_string());
98+
match issues.create_comment(e.issue.number, r.choice).await {
99+
Ok(comment) => {
100+
store_flows::set(
101+
"last_created_comment",
102+
serde_json::to_value(comment.id.into_inner()).unwrap(),
103+
);
104+
}
105+
Err(e) => {
106+
write_error_log!(e.to_string());
107+
}
83108
}
84109
}
85110
}

0 commit comments

Comments
 (0)