Skip to content

Add glacier command to track ICEs #526

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
May 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 81 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ tokio-postgres = { version = "0.5", features = ["with-chrono-0_4"] }
postgres-native-tls = "0.3"
native-tls = "0.2"
serde_path_to_error = "0.1.2"
octocrab = "0.3"

[dependencies.serde]
version = "1"
Expand Down
8 changes: 8 additions & 0 deletions parser/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::error::Error;
use crate::token::{Token, Tokenizer};

pub mod assign;
pub mod glacier;
pub mod nominate;
pub mod ping;
pub mod prioritize;
Expand All @@ -21,6 +22,7 @@ pub enum Command<'a> {
Nominate(Result<nominate::NominateCommand, Error<'a>>),
Prioritize(Result<prioritize::PrioritizeCommand, Error<'a>>),
Second(Result<second::SecondCommand, Error<'a>>),
Glacier(Result<glacier::GlacierCommand, Error<'a>>),
None,
}

Expand Down Expand Up @@ -109,6 +111,11 @@ impl<'a> Input<'a> {
Command::Second,
&original_tokenizer,
));
success.extend(parse_single_command(
glacier::GlacierCommand::parse,
Command::Glacier,
&original_tokenizer,
));

if success.len() > 1 {
panic!(
Expand Down Expand Up @@ -149,6 +156,7 @@ impl<'a> Command<'a> {
Command::Nominate(r) => r.is_ok(),
Command::Prioritize(r) => r.is_ok(),
Command::Second(r) => r.is_ok(),
Command::Glacier(r) => r.is_ok(),
Command::None => true,
}
}
Expand Down
114 changes: 114 additions & 0 deletions parser/src/command/glacier.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
//! The glacier command parser.
//!
//! This adds the option to track ICEs. Do note that the gist must be from a playground link.
//! The link must also be in quotes.
//!
//! The grammar is as follows:
//!
//! ```text
//! Command: `@bot glacier <code-source>`
//!
//! <code-source>:
//! - "https://gist.github.com/.*"
//! ```

use crate::error::Error;
use crate::token::{Token, Tokenizer};
use std::fmt;

#[derive(PartialEq, Eq, Debug)]
pub struct GlacierCommand {
pub source: String,
}

#[derive(PartialEq, Eq, Debug)]
pub enum ParseError {
NoLink,
InvalidLink,
}

impl std::error::Error for ParseError {}

impl fmt::Display for ParseError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Self::NoLink => write!(f, "no link provided - did you forget the quotes around it?"),
Self::InvalidLink => write!(f, "invalid link - must be from a playground gist"),
}
}
}

impl GlacierCommand {
pub fn parse<'a>(input: &mut Tokenizer<'a>) -> Result<Option<GlacierCommand>, Error<'a>> {
let mut toks = input.clone();
if let Some(Token::Word("glacier")) = toks.peek_token()? {
toks.next_token()?;
match toks.next_token()? {
Some(Token::Quote(s)) => {
let source = s.to_owned();
if source.starts_with("https://gist.github.com/") {
return Ok(Some(GlacierCommand { source }));
} else {
return Err(toks.error(ParseError::InvalidLink));
}
}
Some(Token::Word(_)) => {
return Err(toks.error(ParseError::InvalidLink));
}
_ => {
return Err(toks.error(ParseError::NoLink));
}
}
} else {
Ok(None)
}
}
}

#[cfg(test)]
mod test {
use super::*;

fn parse<'a>(input: &'a str) -> Result<Option<GlacierCommand>, Error<'a>> {
let mut toks = Tokenizer::new(input);
Ok(GlacierCommand::parse(&mut toks)?)
}

#[test]
fn glacier_empty() {
use std::error::Error;
assert_eq!(
parse("glacier")
.unwrap_err()
.source()
.unwrap()
.downcast_ref(),
Some(&ParseError::NoLink),
);
}

#[test]
fn glacier_invalid() {
use std::error::Error;
assert_eq!(
parse("glacier hello")
.unwrap_err()
.source()
.unwrap()
.downcast_ref(),
Some(&ParseError::InvalidLink),
);
}

#[test]
fn glacier_valid() {
assert_eq!(
parse(
r#"glacier "https://gist.github.com/rust-play/89d6c8a2398dd2dd5fcb7ef3e8109c7b""#
),
Ok(Some(GlacierCommand {
source: "https://gist.github.com/rust-play/89d6c8a2398dd2dd5fcb7ef3e8109c7b".into()
}))
);
}
}
5 changes: 5 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ pub(crate) struct Config {
pub(crate) nominate: Option<NominateConfig>,
pub(crate) prioritize: Option<PrioritizeConfig>,
pub(crate) major_change: Option<MajorChangeConfig>,
pub(crate) glacier: Option<GlacierConfig>,
}

#[derive(PartialEq, Eq, Debug, serde::Deserialize)]
Expand Down Expand Up @@ -92,6 +93,9 @@ pub(crate) struct MajorChangeConfig {
pub(crate) zulip_stream: u64,
}

#[derive(PartialEq, Eq, Debug, serde::Deserialize)]
pub(crate) struct GlacierConfig {}

pub(crate) async fn get(gh: &GithubClient, repo: &str) -> Result<Arc<Config>, ConfigurationError> {
if let Some(config) = get_cached_config(repo) {
log::trace!("returning config for {} from cache", repo);
Expand Down Expand Up @@ -226,6 +230,7 @@ mod tests {
}),
prioritize: None,
major_change: None,
glacier: None,
}
);
}
Expand Down
3 changes: 3 additions & 0 deletions src/handlers.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::config::{self, ConfigurationError};
use crate::github::{Event, GithubClient};
use futures::future::BoxFuture;
use octocrab::Octocrab;
use std::fmt;
use tokio_postgres::Client as DbClient;

Expand Down Expand Up @@ -80,12 +81,14 @@ handlers! {
prioritize = prioritize::PrioritizeHandler,
major_change = major_change::MajorChangeHandler,
//tracking_issue = tracking_issue::TrackingIssueHandler,
glacier = glacier::GlacierHandler,
}

pub struct Context {
pub github: GithubClient,
pub db: DbClient,
pub username: String,
pub octocrab: Octocrab,
}

pub trait Handler: Sync + Send {
Expand Down
Loading