Skip to content

Commit dfe4f7a

Browse files
Mauricio Cassolajoulei
authored andcommitted
Schedule job for initial merge
1 parent 7713255 commit dfe4f7a

File tree

5 files changed

+107
-27
lines changed

5 files changed

+107
-27
lines changed

parser/src/command/decision.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ pub enum Reversibility {
4848
Irreversible,
4949
}
5050

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

src/db/issue_decision_state.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//! the decision process state of each issue
33
44
use anyhow::{Context as _, Result};
5-
use chrono::{DateTime, FixedOffset};
5+
use chrono::{DateTime, Utc};
66
use parser::command::decision::{Resolution, Reversibility};
77
use serde::{Deserialize, Serialize};
88
use std::collections::BTreeMap;
@@ -12,8 +12,8 @@ use tokio_postgres::Client as DbClient;
1212
pub struct IssueDecisionState {
1313
pub issue_id: i64,
1414
pub initiator: String,
15-
pub start_date: DateTime<FixedOffset>,
16-
pub end_date: DateTime<FixedOffset>,
15+
pub start_date: DateTime<Utc>,
16+
pub end_date: DateTime<Utc>,
1717
pub current: BTreeMap<String, UserStatus>,
1818
pub history: BTreeMap<String, Vec<UserStatus>>,
1919
pub reversibility: Reversibility,
@@ -32,8 +32,8 @@ pub async fn insert_issue_decision_state(
3232
db: &DbClient,
3333
issue_number: &u64,
3434
initiator: &String,
35-
start_date: &DateTime<FixedOffset>,
36-
end_date: &DateTime<FixedOffset>,
35+
start_date: &DateTime<Utc>,
36+
end_date: &DateTime<Utc>,
3737
current: &BTreeMap<String, UserStatus>,
3838
history: &BTreeMap<String, Vec<UserStatus>>,
3939
reversibility: &Reversibility,
@@ -56,7 +56,7 @@ pub async fn insert_issue_decision_state(
5656
pub async fn update_issue_decision_state(
5757
db: &DbClient,
5858
issue_number: &u64,
59-
end_date: &DateTime<FixedOffset>,
59+
end_date: &DateTime<Utc>,
6060
current: &BTreeMap<String, UserStatus>,
6161
history: &BTreeMap<String, Vec<UserStatus>>,
6262
reversibility: &Reversibility,
@@ -95,8 +95,8 @@ pub async fn get_issue_decision_state(
9595
fn deserialize_issue_decision_state(row: &tokio_postgres::row::Row) -> Result<IssueDecisionState> {
9696
let issue_id: i64 = row.try_get(0)?;
9797
let initiator: String = row.try_get(1)?;
98-
let start_date: DateTime<FixedOffset> = row.try_get(2)?;
99-
let end_date: DateTime<FixedOffset> = row.try_get(3)?;
98+
let start_date: DateTime<Utc> = row.try_get(2)?;
99+
let end_date: DateTime<Utc> = row.try_get(3)?;
100100
let current: BTreeMap<String, UserStatus> = serde_json::from_value(row.try_get(4).unwrap())?;
101101
let history: BTreeMap<String, Vec<UserStatus>> =
102102
serde_json::from_value(row.try_get(5).unwrap())?;

src/github.rs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ impl fmt::Display for IssueRepository {
402402
}
403403

404404
impl IssueRepository {
405-
fn url(&self) -> String {
405+
pub fn url(&self) -> String {
406406
format!(
407407
"https://api.github.com/repos/{}/{}",
408408
self.organization, self.repository
@@ -779,6 +779,27 @@ impl Issue {
779779
Ok(())
780780
}
781781

782+
pub async fn merge(&self, client: &GithubClient) -> anyhow::Result<()> {
783+
let merge_url = format!("{}/pulls/{}/merge", self.repository().url(), self.number);
784+
785+
// change defaults by reading from somewhere, maybe in .toml?
786+
#[derive(serde::Serialize)]
787+
struct MergeIssue<'a> {
788+
commit_title: &'a str,
789+
merge_method: &'a str
790+
}
791+
792+
client
793+
._send_req(client.put(&merge_url).json(&MergeIssue {
794+
commit_title: "Merged by the bot!",
795+
merge_method: "merge"
796+
}))
797+
.await
798+
.context("failed to merge issue")?;
799+
800+
Ok(())
801+
}
802+
782803
/// Returns the diff in this event, for Open and Synchronize events for now.
783804
pub async fn diff(&self, client: &GithubClient) -> anyhow::Result<Option<String>> {
784805
let (before, after) = if let (Some(base), Some(head)) = (&self.base, &self.head) {
@@ -1827,7 +1848,7 @@ impl GithubClient {
18271848
response.text().await.context("raw gist from url")
18281849
}
18291850

1830-
fn get(&self, url: &str) -> RequestBuilder {
1851+
pub fn get(&self, url: &str) -> RequestBuilder {
18311852
log::trace!("get {:?}", url);
18321853
self.client.get(url).configure(self)
18331854
}

src/handlers/decision.rs

Lines changed: 42 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,35 @@
1+
use crate::db::jobs::*;
12
use crate::{
23
config::DecisionConfig, db::issue_decision_state::*, github::Event, handlers::Context,
34
interactions::ErrorComment,
45
};
56
use anyhow::Context as Ctx;
6-
use chrono::{DateTime, Duration, FixedOffset};
7-
use parser::command::decision::{DecisionCommand, Resolution::*, Reversibility::*};
7+
use chrono::{DateTime, Duration, Utc};
8+
use parser::command::decision::Resolution::{Hold, Merge};
9+
use parser::command::decision::*;
10+
use serde::{Deserialize, Serialize};
811
use std::collections::BTreeMap;
912

1013
// get state for issue_id from db
11-
// if no state (first call)
12-
// initialize state
13-
// schedule job if necessary
14-
// send comment to github
15-
// save state
16-
// if state
17-
// apply logic to decide what to do
18-
// schedule job if necessary
19-
// send comment to github
20-
// save state
14+
// if no state (first call)
15+
// initialize state
16+
// schedule job if necessary
17+
// send comment to github
18+
// save state
19+
// if state
20+
// apply logic to decide what to do
21+
// schedule job if necessary
22+
// send comment to github
23+
// save state
24+
25+
pub const DECISION_PROCESS_JOB_NAME: &str = "decision_process_action";
26+
27+
#[derive(Serialize, Deserialize)]
28+
pub struct DecisionProcessActionMetadata {
29+
pub message: String,
30+
pub get_issue_url: String,
31+
pub status: Resolution,
32+
}
2133

2234
pub(super) async fn handle_command(
2335
ctx: &Context,
@@ -77,8 +89,8 @@ pub(super) async fn handle_command(
7789
match resolution {
7890
Hold => Ok(()), // change me!
7991
Merge => {
80-
let start_date: DateTime<FixedOffset> = chrono::Utc::now().into();
81-
let end_date: DateTime<FixedOffset> =
92+
let start_date: DateTime<Utc> = chrono::Utc::now().into();
93+
let end_date: DateTime<Utc> =
8294
start_date.checked_add_signed(Duration::days(10)).unwrap();
8395

8496
let mut current: BTreeMap<String, UserStatus> = BTreeMap::new();
@@ -87,7 +99,7 @@ pub(super) async fn handle_command(
8799
UserStatus {
88100
comment_id: "comment_id".to_string(),
89101
text: "something".to_string(),
90-
reversibility: Reversible,
102+
reversibility: Reversibility::Reversible,
91103
resolution: Merge,
92104
},
93105
);
@@ -106,6 +118,21 @@ pub(super) async fn handle_command(
106118
)
107119
.await?;
108120

121+
let metadata = serde_json::value::to_value(DecisionProcessActionMetadata {
122+
message: "some message".to_string(),
123+
get_issue_url: format!("{}/issues/{}", issue.repository().url(), issue.number),
124+
status: Merge,
125+
})
126+
.unwrap();
127+
128+
insert_job(
129+
&db,
130+
&DECISION_PROCESS_JOB_NAME.to_string(),
131+
&end_date,
132+
&metadata,
133+
)
134+
.await?;
135+
109136
// let team = github::get_team(&ctx.github, &"T-lang"); // change this to be configurable in toml?
110137

111138
let comment = format!(

src/handlers/jobs.rs

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,12 @@
33
// the job name and the corresponding function.
44

55
// Further info could be find in src/jobs.rs
6-
76
use super::Context;
7+
use crate::github::*;
8+
use crate::handlers::decision::{DecisionProcessActionMetadata, DECISION_PROCESS_JOB_NAME};
9+
use parser::command::decision::Resolution::Merge;
10+
use reqwest::Client;
11+
use tracing as log;
812

913
pub async fn handle_job(
1014
ctx: &Context,
@@ -17,6 +21,9 @@ pub async fn handle_job(
1721
super::rustc_commits::synchronize_commits_inner(ctx, None).await;
1822
Ok(())
1923
}
24+
matched_name if *matched_name == DECISION_PROCESS_JOB_NAME.to_string() => {
25+
decision_process_handler(&metadata).await
26+
}
2027
_ => default(&name, &metadata),
2128
}
2229
}
@@ -30,3 +37,28 @@ fn default(name: &String, metadata: &serde_json::Value) -> anyhow::Result<()> {
3037

3138
Ok(())
3239
}
40+
41+
async fn decision_process_handler(metadata: &serde_json::Value) -> anyhow::Result<()> {
42+
tracing::trace!(
43+
"handle_job fell into decision process case: (metadata={:?})",
44+
metadata
45+
);
46+
47+
let metadata: DecisionProcessActionMetadata = serde_json::from_value(metadata.clone())?;
48+
49+
match metadata.status {
50+
Merge => {
51+
let gh_client = GithubClient::new_with_default_token(Client::new().clone());
52+
53+
let request = gh_client.get(&metadata.get_issue_url);
54+
55+
match gh_client.json::<Issue>(request).await {
56+
Ok(issue) => issue.merge(&gh_client).await?,
57+
Err(e) => log::error!("Failed to get issue {}, error: {}", metadata.get_issue_url, e),
58+
}
59+
}
60+
_ => {}
61+
}
62+
63+
Ok(())
64+
}

0 commit comments

Comments
 (0)