Skip to content

Commit 0dad639

Browse files
authored
Some refactoring in votes processor (#557)
Signed-off-by: Sergio Castaño Arteaga <[email protected]>
1 parent 577c3d5 commit 0dad639

12 files changed

+535
-403
lines changed

Cargo.lock

+11-8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ tokio-postgres = { version = "0.7.12", features = [
4747
"with-serde_json-1",
4848
"with-time-0_3",
4949
] }
50+
tokio-util = { version = "0.7.12", features = ["rt"] }
5051
tower = { version = "0.5.1", features = ["util"] }
5152
tower-http = { version = "0.6.1", features = ["trace"] }
5253
tracing = "0.1.40"

src/cfg.rs

+15-8
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1-
use crate::github::{DynGH, File, TeamSlug, UserName};
1+
//! This module defines some types to represent the configuration.
2+
3+
use std::{collections::HashMap, time::Duration};
4+
25
use anyhow::{bail, Result};
36
use ignore::gitignore::GitignoreBuilder;
47
use serde::{Deserialize, Serialize};
5-
use std::{collections::HashMap, time::Duration};
68
use thiserror::Error;
79

10+
use crate::github::{DynGH, File, TeamSlug, UserName};
11+
812
/// Default configuration profile.
913
const DEFAULT_PROFILE: &str = "default";
1014

@@ -15,7 +19,7 @@ const ERR_TEAMS_NOT_ALLOWED: &str = "teams in allowed voters can only be used in
1519
/// Type alias to represent a profile name.
1620
type ProfileName = String;
1721

18-
/// GitVote configuration.
22+
/// `GitVote` configuration.
1923
#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq)]
2024
pub(crate) struct Cfg {
2125
#[serde(skip_serializing_if = "Option::is_none")]
@@ -24,7 +28,7 @@ pub(crate) struct Cfg {
2428
}
2529

2630
impl Cfg {
27-
/// Get the GitVote configuration for the repository provided.
31+
/// Get the `GitVote` configuration for the repository provided.
2832
pub(crate) async fn get<'a>(
2933
gh: DynGH,
3034
inst_id: u64,
@@ -165,12 +169,15 @@ pub(crate) enum CfgError {
165169

166170
#[cfg(test)]
167171
mod tests {
168-
use super::*;
169-
use crate::github::MockGH;
170-
use crate::testutil::*;
172+
use std::sync::Arc;
173+
171174
use futures::future;
172175
use mockall::predicate::eq;
173-
use std::sync::Arc;
176+
177+
use crate::github::MockGH;
178+
use crate::testutil::*;
179+
180+
use super::*;
174181

175182
#[test]
176183
fn automation_rule_matches() {

src/cmd.rs

+22-10
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
1-
use crate::{
2-
cfg::{Cfg, CfgError},
3-
github::*,
4-
};
1+
//! This module defines the commands supported and the logic to parse them from
2+
//! GitHub events.
3+
54
use anyhow::Result;
65
use lazy_static::lazy_static;
76
use regex::Regex;
87
use serde::{Deserialize, Serialize};
98
use tracing::error;
109

10+
use crate::{
11+
cfg::{Cfg, CfgError},
12+
github::{
13+
split_full_name, DynGH, Event, IssueCommentEventAction, IssueEventAction, PullRequestEventAction,
14+
},
15+
};
16+
1117
/// Available commands.
1218
const CMD_CREATE_VOTE: &str = "vote";
1319
const CMD_CANCEL_VOTE: &str = "cancel-vote";
@@ -147,7 +153,7 @@ pub(crate) struct CreateVoteInput {
147153
}
148154

149155
impl CreateVoteInput {
150-
/// Create a new CreateVoteInput instance from the profile and event
156+
/// Create a new `CreateVoteInput` instance from the profile and event
151157
/// provided.
152158
pub(crate) fn new(profile_name: Option<&str>, event: &Event) -> Self {
153159
match event {
@@ -199,7 +205,7 @@ pub(crate) struct CancelVoteInput {
199205
}
200206

201207
impl CancelVoteInput {
202-
/// Create a new CancelVoteInput instance from the event provided.
208+
/// Create a new `CancelVoteInput` instance from the event provided.
203209
pub(crate) fn new(event: &Event) -> Self {
204210
match event {
205211
Event::Issue(event) => Self {
@@ -235,7 +241,7 @@ pub(crate) struct CheckVoteInput {
235241
}
236242

237243
impl CheckVoteInput {
238-
/// Create a new CheckVoteInput instance from the event provided.
244+
/// Create a new `CheckVoteInput` instance from the event provided.
239245
pub(crate) fn new(event: &Event) -> Self {
240246
match event {
241247
Event::Issue(event) => Self {
@@ -256,11 +262,17 @@ impl CheckVoteInput {
256262

257263
#[cfg(test)]
258264
mod tests {
259-
use super::*;
260-
use crate::testutil::*;
265+
use std::{sync::Arc, vec};
266+
261267
use futures::future;
262268
use mockall::predicate::eq;
263-
use std::{sync::Arc, vec};
269+
270+
use crate::{
271+
github::{File, MockGH},
272+
testutil::*,
273+
};
274+
275+
use super::*;
264276

265277
#[test]
266278
fn manual_command_from_issue_event_unsupported_action() {

src/db.rs

+15-11
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,22 @@
1-
use crate::{
2-
cfg::CfgProfile,
3-
cmd::{CheckVoteInput, CreateVoteInput},
4-
github::{self, split_full_name, DynGH},
5-
results::{self, Vote, VoteResults},
6-
};
1+
//! This module defines an abstraction layer over the database.
2+
3+
use std::sync::Arc;
4+
75
use anyhow::Result;
86
use async_trait::async_trait;
97
use deadpool_postgres::{Pool, Transaction};
108
#[cfg(test)]
119
use mockall::automock;
12-
use std::sync::Arc;
1310
use tokio_postgres::types::Json;
1411
use uuid::Uuid;
1512

13+
use crate::{
14+
cfg::CfgProfile,
15+
cmd::{CheckVoteInput, CreateVoteInput},
16+
github::{self, split_full_name, DynGH},
17+
results::{self, Vote, VoteResults},
18+
};
19+
1620
/// Type alias to represent a DB trait object.
1721
pub(crate) type DynDB = Arc<dyn DB + Send + Sync>;
1822

@@ -56,13 +60,13 @@ pub(crate) trait DB {
5660
async fn update_vote_last_check(&self, vote_id: Uuid) -> Result<()>;
5761
}
5862

59-
/// DB implementation backed by PostgreSQL.
63+
/// DB implementation backed by `PostgreSQL`.
6064
pub(crate) struct PgDB {
6165
pool: Pool,
6266
}
6367

6468
impl PgDB {
65-
/// Create a new PgDB instance.
69+
/// Create a new `PgDB` instance.
6670
pub(crate) fn new(pool: Pool) -> Self {
6771
Self { pool }
6872
}
@@ -91,7 +95,7 @@ impl PgDB {
9195
async fn store_vote_results(
9296
tx: &Transaction<'_>,
9397
vote_id: Uuid,
94-
results: &Option<VoteResults>,
98+
results: Option<&VoteResults>,
9599
) -> Result<()> {
96100
tx.execute(
97101
"
@@ -154,7 +158,7 @@ impl DB for PgDB {
154158
};
155159

156160
// Store results in database
157-
PgDB::store_vote_results(&tx, vote.vote_id, &results).await?;
161+
PgDB::store_vote_results(&tx, vote.vote_id, results.as_ref()).await?;
158162
tx.commit().await?;
159163

160164
Ok(Some((vote, results)))

src/github.rs

+10-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
use crate::cfg::CfgProfile;
1+
//! This module defines an abstraction layer over the GitHub API.
2+
3+
use std::sync::Arc;
4+
25
use anyhow::{bail, Context, Error, Result};
36
use async_trait::async_trait;
47
use axum::http::HeaderValue;
@@ -9,9 +12,10 @@ use mockall::automock;
912
use octocrab::{models::InstallationId, Octocrab, Page};
1013
use serde::{Deserialize, Serialize};
1114
use serde_json::{json, Value};
12-
use std::sync::Arc;
1315
use thiserror::Error;
1416

17+
use crate::cfg::CfgProfile;
18+
1519
/// GitHub API base url.
1620
const GITHUB_API_URL: &str = "https://api.github.com";
1721

@@ -56,6 +60,7 @@ pub struct CreateDiscussion;
5660

5761
/// Trait that defines some operations a GH implementation must support.
5862
#[async_trait]
63+
#[allow(clippy::ref_option_ref)]
5964
#[cfg_attr(test, automock)]
6065
pub(crate) trait GH {
6166
/// Add labels to the provided issue.
@@ -96,7 +101,7 @@ pub(crate) trait GH {
96101
cfg: &CfgProfile,
97102
owner: &str,
98103
repo: &str,
99-
org: &Option<String>,
104+
org: Option<&String>,
100105
) -> Result<Vec<UserName>>;
101106

102107
/// Get all repository collaborators.
@@ -169,7 +174,7 @@ pub(crate) struct GHApi {
169174
}
170175

171176
impl GHApi {
172-
/// Create a new GHApi instance.
177+
/// Create a new `GHApi` instance.
173178
pub(crate) fn new(app_client: Octocrab) -> Self {
174179
Self { app_client }
175180
}
@@ -270,7 +275,7 @@ impl GH for GHApi {
270275
cfg: &CfgProfile,
271276
owner: &str,
272277
repo: &str,
273-
org: &Option<String>,
278+
org: Option<&String>,
274279
) -> Result<Vec<UserName>> {
275280
let mut allowed_voters: Vec<UserName> = vec![];
276281

0 commit comments

Comments
 (0)