Skip to content

Commit 52a93c2

Browse files
committed
Add votes closer + some refactoring
Signed-off-by: Sergio Castaño Arteaga <[email protected]>
1 parent 2c958c9 commit 52a93c2

File tree

12 files changed

+328
-56
lines changed

12 files changed

+328
-56
lines changed

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
templates/* -linguist-detectable

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,10 @@ serde = { version = "1.0.137", features = ["derive"] }
2424
serde_json = "1.0.81"
2525
serde_yaml = "0.8.24"
2626
sha2 = "0.10.2"
27-
tokio = {version = "1.18.2", features = ["macros", "rt-multi-thread", "signal"] }
27+
tokio = {version = "1.18.2", features = ["macros", "rt-multi-thread", "signal", "time"] }
2828
tokio-postgres = { version = "0.7.6", features = ["with-uuid-1", "with-serde_json-1", "with-time-0_3"] }
2929
tower = "0.4.12"
3030
tower-http = { version = "0.3.3", features = ["trace"] }
3131
tracing = "0.1.34"
3232
tracing-subscriber = "0.3.11"
33+
uuid = "1.1.0"

database/migrations/migrate.sh

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,3 @@ tern status --config $TERN_CONF --version-table $schemaVersionTable
88
tern migrate --config $TERN_CONF --version-table $schemaVersionTable
99
if [ $? -ne 0 ]; then exit 1; fi
1010
echo "Done"
11-

database/migrations/schema/001_initial.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ create extension if not exists pgcrypto;
22

33
create table if not exists vote (
44
vote_id uuid primary key default gen_random_uuid(),
5+
vote_comment_id bigint not null,
56
created_at timestamptz default current_timestamp not null,
67
ends_at timestamptz not null,
78
closed boolean not null default false,

docs/metadata/.gitvote.yml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,16 @@ voters:
99

1010
# Pass threshold (required)
1111
# Percentage of votes required to pass the vote
12-
passThreshold: 67
12+
pass_threshold: 67
1313

1414
# Voting duration (required)
1515
# How long the vote will be open
1616
#
17-
# Units supported (can be combined as in 5mins 30s):
17+
# Units supported (can be combined as in 1hour 30mins):
1818
#
19-
#  seconds | second | secs | sec | s
2019
# minutes | minute | mins | min | m
2120
# hours | hour | hrs | hrs | h
2221
# days | day | d
2322
# weeks | week | w
2423
#
25-
duration: 5mins 30s
24+
duration: 5m

src/events.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,12 @@ pub(crate) struct Issue {
6969
}
7070

7171
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
72-
pub struct Repository {
72+
pub(crate) struct Repository {
7373
pub full_name: String,
7474
}
75+
76+
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
77+
pub(crate) struct Reaction {
78+
pub user: User,
79+
pub content: String,
80+
}

src/main.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1-
use crate::votes::Processor;
21
use anyhow::{Context, Result};
32
use clap::Parser;
43
use config::{Config, File};
54
use deadpool_postgres::{Config as DbConfig, Runtime};
65
use openssl::ssl::{SslConnector, SslMethod, SslVerifyMode};
76
use postgres_openssl::MakeTlsConnector;
87
use std::{net::SocketAddr, path::PathBuf, sync::Arc};
9-
use tokio::{signal, sync::mpsc};
8+
use tokio::{
9+
signal,
10+
sync::{broadcast, mpsc},
11+
};
1012
use tracing::info;
1113

1214
mod events;
@@ -50,7 +52,9 @@ async fn main() -> Result<()> {
5052

5153
// Setup and launch votes processor
5254
let (cmds_tx, cmds_rx) = mpsc::channel(100);
53-
let processor_done = Processor::start(cfg.clone(), db, cmds_rx)?;
55+
let (stop_tx, _): (broadcast::Sender<()>, _) = broadcast::channel(1);
56+
let votes_processor = votes::Processor::new(cfg.clone(), db)?;
57+
let votes_processor_done = votes_processor.start(cmds_rx, stop_tx.clone());
5458

5559
// Setup and launch HTTP server
5660
let router = handlers::setup_router(cmds_tx).await?;
@@ -63,8 +67,9 @@ async fn main() -> Result<()> {
6367
.unwrap();
6468
info!("gitvote service stopped");
6569

66-
// Wait for votes processor to finish
67-
processor_done.await;
70+
// Ask votes processor to stop and wait for it to finish
71+
drop(stop_tx);
72+
votes_processor_done.await;
6873

6974
Ok(())
7075
}

src/metadata.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ pub const METADATA_FILE: &str = ".gitvote.yml";
88

99
/// GitVote metadata.
1010
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
11-
#[serde(rename_all = "camelCase")]
1211
pub(crate) struct Metadata {
1312
pub voters: Vec<String>,
1413
pub pass_threshold: f64,

src/templates.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
use crate::{
22
events::IssueCommentEvent,
33
metadata::{Metadata, METADATA_FILE},
4+
votes::VoteResults,
45
};
56
use askama::Template;
6-
use std::collections::HashMap;
77

88
/// Template for the vote created comment.
99
#[derive(Debug, Clone, Template)]
@@ -19,6 +19,7 @@ pub(crate) struct VoteCreated<'a> {
1919
}
2020

2121
impl<'a> VoteCreated<'a> {
22+
/// Create a new VoteCreated template.
2223
pub(crate) fn new(event: &'a IssueCommentEvent, md: &'a Metadata) -> Self {
2324
Self {
2425
creator: &event.comment.user.login,
@@ -39,12 +40,12 @@ impl<'a> VoteCreated<'a> {
3940
#[derive(Debug, Clone, Template)]
4041
#[template(path = "vote-closed.md")]
4142
pub(crate) struct VoteClosed<'a> {
42-
passed: bool,
43-
in_favor_percentage: f64,
44-
pass_threshold: f64,
45-
in_favor: u64,
46-
against: u64,
47-
abstain: u64,
48-
not_voted: u64,
49-
voters: HashMap<&'a str, &'a str>,
43+
results: &'a VoteResults,
44+
}
45+
46+
impl<'a> VoteClosed<'a> {
47+
/// Create a new VoteClosed template.
48+
pub(crate) fn new(results: &'a VoteResults) -> Self {
49+
Self { results }
50+
}
5051
}

0 commit comments

Comments
 (0)