Skip to content

Commit ec48792

Browse files
Mauricio Cassolajoulei
authored andcommitted
Add db support for issue_decision_state
1 parent c604243 commit ec48792

File tree

5 files changed

+161
-148
lines changed

5 files changed

+161
-148
lines changed

parser/src/command/decision.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ pub enum ParseError {
3838
InvalidFirstCommand
3939
}
4040

41-
#[derive(Serialize, Deserialize, Debug, ToSql, FromSql, Eq, PartialEq)]
41+
#[derive(Serialize, Deserialize, Debug, Clone, ToSql, FromSql, Eq, PartialEq)]
4242
#[postgres(name = "reversibility")]
4343
pub enum Reversibility {
4444
#[postgres(name = "reversible")]
@@ -47,7 +47,7 @@ pub enum Reversibility {
4747
Irreversible,
4848
}
4949

50-
#[derive(Serialize, Deserialize, Debug, ToSql, FromSql, Eq, PartialEq)]
50+
#[derive(Serialize, Deserialize, Debug, Clone, ToSql, FromSql, Eq, PartialEq)]
5151
#[postgres(name = "resolution")]
5252
pub enum Resolution {
5353
#[postgres(name = "merge")]

src/db.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use std::sync::{Arc, Mutex};
88
use tokio::sync::{OwnedSemaphorePermit, Semaphore};
99
use tokio_postgres::Client as DbClient;
1010

11-
pub mod decision_state;
11+
pub mod issue_decision_state;
1212
pub mod issue_data;
1313
pub mod jobs;
1414
pub mod notifications;
@@ -280,14 +280,13 @@ CREATE TYPE reversibility AS ENUM ('reversible', 'irreversible');
280280
"
281281
CREATE TYPE resolution AS ENUM ('hold', 'merge');
282282
",
283-
"CREATE TABLE decision_state (
283+
"CREATE TABLE issue_decision_state (
284284
issue_id BIGINT PRIMARY KEY,
285285
initiator TEXT NOT NULL,
286-
team_members JSONB NOT NULL,
287286
start_date TIMESTAMP WITH TIME ZONE NOT NULL,
288-
period_end_date TIMESTAMP WITH TIME ZONE NOT NULL,
289-
current_statuses JSONB NOT NULL,
290-
status_history JSONB,
287+
end_date TIMESTAMP WITH TIME ZONE NOT NULL,
288+
current JSONB NOT NULL,
289+
history JSONB,
291290
reversibility reversibility NOT NULL DEFAULT 'reversible',
292291
resolution resolution NOT NULL DEFAULT 'merge'
293292
);",

src/db/decision_state.rs

Lines changed: 0 additions & 106 deletions
This file was deleted.

src/db/issue_decision_state.rs

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
//! The issue decision state table provides a way to store
2+
//! the decision process state of each issue
3+
4+
use serde::{Deserialize, Serialize};
5+
use chrono::{DateTime, FixedOffset};
6+
use std::collections::BTreeMap;
7+
use parser::command::decision::{Resolution, Reversibility};
8+
use anyhow::{Context as _, Result};
9+
use tokio_postgres::Client as DbClient;
10+
11+
#[derive(Debug, Serialize, Deserialize)]
12+
pub struct IssueDecisionState {
13+
pub issue_id: i64,
14+
pub initiator: String,
15+
pub start_date: DateTime<FixedOffset>,
16+
pub end_date: DateTime<FixedOffset>,
17+
pub current: BTreeMap<String, UserStatus>,
18+
pub history: BTreeMap<String, Vec<UserStatus>>,
19+
pub reversibility: Reversibility,
20+
pub resolution: Resolution,
21+
}
22+
23+
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
24+
pub struct UserStatus {
25+
pub comment_id: String,
26+
pub text: String,
27+
pub reversibility: Reversibility,
28+
pub resolution: Resolution,
29+
}
30+
31+
pub async fn insert_issue_decision_state(
32+
db: &DbClient,
33+
issue_number: &u64,
34+
initiator: &String,
35+
start_date: &DateTime<FixedOffset>,
36+
end_date: &DateTime<FixedOffset>,
37+
current: &BTreeMap<String, UserStatus>,
38+
history: &BTreeMap<String, Vec<UserStatus>>,
39+
reversibility: &Reversibility,
40+
resolution: &Resolution,
41+
) -> Result<()> {
42+
tracing::trace!("insert_issue_decision_state(issue_id={})", issue_number);
43+
let issue_id = *issue_number as i64;
44+
45+
db.execute(
46+
"INSERT INTO issue_decision_state (issue_id, initiator, start_date, end_date, current, history, reversibility, resolution) VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
47+
ON CONFLICT DO NOTHING",
48+
&[&issue_id, &initiator, &start_date, &end_date, &serde_json::to_value(current).unwrap(), &serde_json::to_value(history).unwrap(), &reversibility, &resolution],
49+
)
50+
.await
51+
.context("Inserting decision state")?;
52+
53+
Ok(())
54+
}
55+
56+
pub async fn update_issue_decision_state(
57+
db: &DbClient,
58+
issue_number: &u64,
59+
end_date: &DateTime<FixedOffset>,
60+
current: &BTreeMap<String, UserStatus>,
61+
history: &BTreeMap<String, Vec<UserStatus>>,
62+
reversibility: &Reversibility,
63+
resolution: &Resolution
64+
) -> Result<()> {
65+
tracing::trace!("update_issue_decision_state(issue_id={})", issue_number);
66+
let issue_id = *issue_number as i64;
67+
68+
db.execute("UPDATE issue_decision_state SET end_date = $2, current = $3, history = $4, reversibility = $5, resolution = $6 WHERE issue_id = $1",
69+
&[&issue_id, &end_date, &serde_json::to_value(current).unwrap(), &serde_json::to_value(history).unwrap(), &reversibility, &resolution]
70+
)
71+
.await
72+
.context("Updating decision state")?;
73+
74+
Ok(())
75+
}
76+
77+
pub async fn get_issue_decision_state(db: &DbClient, issue_number: &u64) -> Result<IssueDecisionState> {
78+
tracing::trace!("get_issue_decision_state(issue_id={})", issue_number);
79+
let issue_id = *issue_number as i64;
80+
81+
let state = db
82+
.query_one(
83+
"SELECT * FROM issue_decision_state WHERE issue_id = $1",
84+
&[&issue_id]
85+
)
86+
.await
87+
.context("Getting decision state data")?;
88+
89+
deserialize_issue_decision_state(&state)
90+
}
91+
92+
fn deserialize_issue_decision_state(row: &tokio_postgres::row::Row) -> Result<IssueDecisionState> {
93+
let issue_id: i64 = row.try_get(0)?;
94+
let initiator: String = row.try_get(1)?;
95+
let start_date: DateTime<FixedOffset> = row.try_get(2)?;
96+
let end_date: DateTime<FixedOffset> = row.try_get(3)?;
97+
let current: BTreeMap<String, UserStatus> = serde_json::from_value(row.try_get(4).unwrap())?;
98+
let history: BTreeMap<String, Vec<UserStatus>> = serde_json::from_value(row.try_get(5).unwrap())?;
99+
let reversibility: Reversibility = row.try_get(6)?;
100+
let resolution: Resolution = row.try_get(7)?;
101+
102+
Ok(IssueDecisionState {
103+
issue_id,
104+
initiator,
105+
start_date,
106+
end_date,
107+
current,
108+
history,
109+
reversibility,
110+
resolution,
111+
})
112+
}

src/handlers/decision.rs

Lines changed: 42 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
use anyhow::Context as Ctx;
2-
use parser::command::decision::{DecisionCommand, ParseError};
32
use crate::{
43
config::DecisionConfig,
5-
github::{self, Event},
4+
github::{Event},
65
handlers::Context,
76
interactions::ErrorComment,
8-
db::decision_state::*
7+
db::issue_decision_state::*
98
};
10-
use std::collections::HashMap;
11-
use chrono::{DateTime, Duration, Utc};
9+
use chrono::{DateTime, Duration, FixedOffset};
10+
use parser::command::decision::{DecisionCommand, Resolution::*, Reversibility::*};
11+
use std::collections::BTreeMap;
12+
use chrono::{DateTime, FixedOffset, Duration};
1213

1314
// get state for issue_id from db
1415
// if no state (first call)
@@ -28,15 +29,18 @@ pub(super) async fn handle_command(
2829
event: &Event,
2930
cmd: DecisionCommand,
3031
) -> anyhow::Result<()> {
32+
let db = ctx.db.get().await;
33+
3134
let DecisionCommand {
3235
resolution,
3336
reversibility
3437
} = cmd;
3538

3639
let issue = event.issue().unwrap();
40+
let user = event.user();
3741

38-
let is_team_member = event
39-
.user()
42+
let is_team_member =
43+
user
4044
.is_team_member(&ctx.github)
4145
.await
4246
.unwrap_or(false);
@@ -47,8 +51,8 @@ pub(super) async fn handle_command(
4751
return Ok(());
4852
}
4953

50-
match get_decision_state(issue.id) {
51-
Some(state) => {
54+
match get_issue_decision_state(&db, &issue.number).await {
55+
Ok(_state) => {
5256
// let name = match disposition {
5357
// Hold => "hold".into(),
5458
// Custom(name) => name,
@@ -72,45 +76,49 @@ pub(super) async fn handle_command(
7276
// status_history,
7377
// ..state
7478
// })
75-
Ok();
79+
Ok(())
7680
},
77-
None => {
81+
_ => {
7882
match resolution {
79-
Hold => Err(ParseError::InvalidFirstCommand),
83+
Hold => Ok(()), // change me!
8084
Merge => {
81-
let start_date = chrono::Utc::now().into();
82-
let end_date = start_date.checked_add_signed(Duration::days(10)).unwrap();
83-
84-
let current_statuses = HashMap::new();
85-
let status_history = HashMap::new();
85+
let start_date: DateTime<FixedOffset> = chrono::Utc::now().into();
86+
let end_date: DateTime<FixedOffset> = start_date.checked_add_signed(Duration::days(10)).unwrap();
8687

87-
let team = github::get_team(&ctx.github, &"T-lang"); // change this to be configurable in toml?
88+
let mut current: BTreeMap<String, UserStatus> = BTreeMap::new();
89+
current.insert("mcass19".to_string(), UserStatus{
90+
comment_id: "comment_id".to_string(),
91+
text: "something".to_string(),
92+
reversibility: Reversible,
93+
resolution: Merge,
94+
});
95+
let history: BTreeMap<String, Vec<UserStatus>> = BTreeMap::new();
8896

89-
insert_decision_state(
90-
db,
91-
issue.id,
92-
user.login,
93-
start_date,
94-
end_date,
95-
current_statuses,
96-
status_history,
97-
reversibility,
98-
resolution,
99-
);
97+
insert_issue_decision_state(
98+
&db,
99+
&issue.number,
100+
&user.login,
101+
&start_date,
102+
&end_date,
103+
&current,
104+
&history,
105+
&reversibility,
106+
&Merge,
107+
).await?;
100108

101-
let comment = format!(
102-
"Wow, it looks like you want to merge this, {}.", event.user().login
103-
);
109+
// let team = github::get_team(&ctx.github, &"T-lang"); // change this to be configurable in toml?
104110

105111
let comment = format!(
106-
"| Team member | State |\n|-------------|-------|\n| julmontesdeoca | merge |\n| mcass19 | |");
112+
"Wow, it looks like you want to merge this, {}.\n| Team member | State |\n|-------------|-------|\n| julmontesdeoca | merge |\n| mcass19 | |",
113+
user.login
114+
);
107115

108116
issue
109117
.post_comment(&ctx.github, &comment)
110118
.await
111119
.context("merge vote comment")?;
112120

113-
Ok();
121+
Ok(())
114122
}
115123
}
116124
}

0 commit comments

Comments
 (0)