Skip to content

Commit 4559ce7

Browse files
authored
Add support for json log format + some refactoring (#264)
Signed-off-by: Sergio Castaño Arteaga <[email protected]>
1 parent b19067d commit 4559ce7

File tree

11 files changed

+67
-43
lines changed

11 files changed

+67
-43
lines changed

Cargo.lock

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

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ tokio-postgres = { version = "0.7.7", features = [
4848
tower = "0.4.13"
4949
tower-http = { version = "0.4.0", features = ["trace"] }
5050
tracing = "0.1.37"
51-
tracing-subscriber = "0.3.16"
51+
tracing-subscriber = { version = "0.3.16", features = ["env-filter", "json"] }
5252
uuid = { version = "1.3.0", features = ["serde", "v4"] }
5353

5454
[dev-dependencies]

charts/gitvote/Chart.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ apiVersion: v2
22
name: gitvote
33
description: GitVote is a GitHub application that allows holding a vote on issues and pull requests
44
type: application
5-
version: 0.2.1-0
5+
version: 0.2.1-1
66
appVersion: 0.2.0
77
kubeVersion: ">= 1.19.0-0"
88
home: https://gitvote.dev

charts/gitvote/templates/gitvote_secret.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ stringData:
1212
dbname: {{ .Values.db.dbname }}
1313
user: {{ .Values.db.user }}
1414
password: {{ .Values.db.password }}
15+
log:
16+
format: {{ .Values.log.format }}
1517
github:
1618
appID: {{ .Values.gitvote.github.appID }}
1719
appPrivateKey: {{ .Values.gitvote.github.appPrivateKey | quote }}

charts/gitvote/values.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ db:
2626
user: postgres
2727
password: postgres
2828

29+
# Log configuration
30+
log:
31+
# Output format [json|pretty]
32+
format: json
33+
2934
# Database migrator configuration
3035
dbmigrator:
3136
job:

src/cmd.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ mod tests {
267267
#[test]
268268
fn manual_command_from_issue_event_unsupported_action() {
269269
let mut event = setup_test_issue_event();
270-
event.action = IssueEventAction::Assigned;
270+
event.action = IssueEventAction::Other;
271271
event.issue.body = Some(format!("/{CMD_CREATE_VOTE}"));
272272
let event = Event::Issue(event);
273273

@@ -316,7 +316,7 @@ mod tests {
316316
#[test]
317317
fn manual_command_from_issue_comment_event_unsupported_action() {
318318
let mut event = setup_test_issue_comment_event();
319-
event.action = IssueCommentEventAction::Edited;
319+
event.action = IssueCommentEventAction::Other;
320320
event.issue.body = Some(CMD_CREATE_VOTE.to_string());
321321
let event = Event::IssueComment(event);
322322

@@ -352,7 +352,7 @@ mod tests {
352352
#[test]
353353
fn manual_command_from_pr_event_unsupported_action() {
354354
let mut event = setup_test_pr_event();
355-
event.action = PullRequestEventAction::Edited;
355+
event.action = PullRequestEventAction::Other;
356356
event.pull_request.body = Some(CMD_CREATE_VOTE.to_string());
357357
let event = Event::PullRequest(event);
358358

src/github.rs

Lines changed: 7 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ impl GH for GHApi {
167167
if let Some(conclusion) = &check_details.conclusion {
168168
body["conclusion"] = json!(conclusion);
169169
};
170-
let _: Value = client.post(url, Some(&body)).await?; // Do not remove let _: Value
170+
let _: Value = client.post(url, Some(&body)).await?;
171171
Ok(())
172172
}
173173

@@ -428,21 +428,8 @@ pub(crate) struct IssueEvent {
428428
#[serde(rename_all = "lowercase")]
429429
pub(crate) enum IssueEventAction {
430430
Opened,
431-
Edited,
432-
Deleted,
433-
Pinned,
434-
Unpinned,
435-
Closed,
436-
Reopened,
437-
Assigned,
438-
Unassigned,
439-
Labeled,
440-
Unlabeled,
441-
Locked,
442-
Unlocked,
443-
Transferred,
444-
Milestoned,
445-
Demilestoned,
431+
#[serde(other)]
432+
Other,
446433
}
447434

448435
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
@@ -460,8 +447,8 @@ pub(crate) struct IssueCommentEvent {
460447
#[serde(rename_all = "lowercase")]
461448
pub(crate) enum IssueCommentEventAction {
462449
Created,
463-
Deleted,
464-
Edited,
450+
#[serde(other)]
451+
Other,
465452
}
466453

467454
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
@@ -477,23 +464,10 @@ pub(crate) struct PullRequestEvent {
477464
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
478465
#[serde(rename_all = "snake_case")]
479466
pub(crate) enum PullRequestEventAction {
480-
Assigned,
481-
AutoMergeDisabled,
482-
AutoMergeEnabled,
483-
Closed,
484-
ConvertedToDraft,
485-
Edited,
486-
Labeled,
487-
Locked,
488467
Opened,
489-
ReadyForReview,
490-
Reopened,
491-
ReviewRequestRemoved,
492-
ReviewRequested,
493468
Synchronize,
494-
Unassigned,
495-
Unlabeled,
496-
Unlocked,
469+
#[serde(other)]
470+
Other,
497471
}
498472

499473
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]

src/handlers.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -467,7 +467,7 @@ mod tests {
467467
let db = Arc::new(MockDB::new());
468468
let gh = Arc::new(MockGH::new());
469469
let mut event = setup_test_pr_event();
470-
event.action = PullRequestEventAction::ReadyForReview;
470+
event.action = PullRequestEventAction::Other;
471471

472472
assert!(set_check_status(db, gh, &event).await.is_ok());
473473
}

src/main.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use postgres_openssl::MakeTlsConnector;
99
use std::{net::SocketAddr, path::PathBuf, sync::Arc};
1010
use tokio::{signal, sync::broadcast};
1111
use tracing::{debug, info};
12+
use tracing_subscriber::EnvFilter;
1213

1314
mod cfg;
1415
mod cmd;
@@ -35,6 +36,7 @@ async fn main() -> Result<()> {
3536

3637
// Setup configuration
3738
let cfg = Config::builder()
39+
.set_default("log.format", "pretty")?
3840
.set_default("addr", "127.0.0.1:9000")?
3941
.add_source(File::from(args.config))
4042
.build()
@@ -45,7 +47,11 @@ async fn main() -> Result<()> {
4547
if std::env::var_os("RUST_LOG").is_none() {
4648
std::env::set_var("RUST_LOG", "gitvote=debug")
4749
}
48-
tracing_subscriber::fmt::init();
50+
let s = tracing_subscriber::fmt().with_env_filter(EnvFilter::from_default_env());
51+
match cfg.get_string("log.format").as_deref() {
52+
Ok("json") => s.json().init(),
53+
_ => s.init(),
54+
};
4955

5056
// Setup database
5157
let mut builder = SslConnector::builder(SslMethod::tls())?;

src/processor.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,8 @@ impl Processor {
128128
_ = stop_rx.recv() => break,
129129
},
130130
Err(_) => {
131+
// Something went wrong closing finished vote, pause
132+
// unless we've been asked to stop
131133
tokio::select! {
132134
_ = sleep(VOTES_CLOSER_PAUSE_ON_ERROR) => {},
133135
_ = stop_rx.recv() => break,

0 commit comments

Comments
 (0)