Skip to content

Nuke unwanted reactions #210

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

Closed
wants to merge 11 commits into from
Prev Previous commit
Next Next commit
introduce a throw! macro and use it.
  • Loading branch information
Centril committed May 16, 2018
commit 48ed8fd3d8fe641ddf5a879362b800ea28d6483f
8 changes: 3 additions & 5 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ pub fn init() -> Result<Config, Vec<&'static str>> {
let db_pool_size = vars.remove(DB_POOL_SIZE).unwrap();
let db_pool_size = match db_pool_size.parse::<u32>() {
Ok(size) => size,
Err(_) => return Err(vec![DB_POOL_SIZE]),
Err(_) => throw!(vec![DB_POOL_SIZE]),
};

let gh_token = vars.remove(GITHUB_TOKEN).unwrap();
Expand All @@ -83,13 +83,13 @@ pub fn init() -> Result<Config, Vec<&'static str>> {
let gh_interval = vars.remove(GITHUB_INTERVAL).unwrap();
let gh_interval = match gh_interval.parse::<u64>() {
Ok(interval) => interval,
Err(_) => return Err(vec![GITHUB_INTERVAL]),
Err(_) => throw!(vec![GITHUB_INTERVAL]),
};

let post_comments = vars.remove(POST_COMMENTS).unwrap();
let post_comments = match post_comments.parse::<bool>() {
Ok(pc) => pc,
Err(_) => return Err(vec![POST_COMMENTS]),
Err(_) => throw!(vec![POST_COMMENTS]),
};

let webhook_secrets = vars.remove(GITHUB_WEBHOOK_SECRETS).unwrap();
Expand All @@ -106,11 +106,9 @@ pub fn init() -> Result<Config, Vec<&'static str>> {
})

} else {

Err(vars.iter()
.filter(|&(_, v)| v.is_err())
.map(|(&k, _)| k)
.collect())

}
}
42 changes: 21 additions & 21 deletions src/github/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ pub struct Client {
rate_limit_timeout: DateTime<Utc>,
}

fn read_to_string<R: Read>(reader: &mut R) -> DashResult<String> {
let mut string = String::new();
reader.read_to_string(&mut string)?;
Ok(string)
}

impl Client {
pub fn new() -> Self {
let tls_connector = HttpsConnector::new(NativeTlsClient::new().unwrap());
Expand Down Expand Up @@ -72,7 +78,7 @@ impl Client {
}
}
}
return Err(DashError::Misc(None));
throw!(DashError::Misc(None))

}
Ok(repos)
Expand Down Expand Up @@ -129,7 +135,7 @@ impl Client {
let mut res = try!(self.get(url, None));
self.deserialize(&mut res)
} else {
Err(DashError::Misc(None))
throw!(DashError::Misc(None))
}
}

Expand Down Expand Up @@ -165,13 +171,11 @@ impl Client {

let mut res = self.patch(&url, &payload)?;

if let StatusCode::Ok = res.status {
Ok(())
} else {
let mut body = String::new();
res.read_to_string(&mut body)?;
Err(DashError::Misc(Some(body)))
if StatusCode::Ok != res.status {
throw!(DashError::Misc(Some(read_to_string(&mut res)?)))
}

Ok(())
}

pub fn add_label(&self, repo: &str, issue_num: i32, label: &str) -> DashResult<()> {
Expand All @@ -180,13 +184,11 @@ impl Client {

let mut res = self.post(&url, &payload)?;

if let StatusCode::Ok = res.status {
Ok(())
} else {
let mut body = String::new();
res.read_to_string(&mut body)?;
Err(DashError::Misc(Some(body)))
if StatusCode::Ok != res.status {
throw!(DashError::Misc(Some(read_to_string(&mut res)?)))
}

Ok(())
}

pub fn remove_label(&self, repo: &str, issue_num: i32, label: &str) -> DashResult<()> {
Expand All @@ -197,13 +199,11 @@ impl Client {
label);
let mut res = self.delete(&url)?;

if let StatusCode::NoContent = res.status {
Ok(())
} else {
let mut body = String::new();
res.read_to_string(&mut body)?;
Err(DashError::Misc(Some(body)))
if StatusCode::NoContent != res.status {
throw!(DashError::Misc(Some(read_to_string(&mut res)?)))
}

Ok(())
}

pub fn new_comment(&self,
Expand Down Expand Up @@ -287,7 +287,7 @@ impl Client {
Ok(m) => Ok(m),
Err(why) => {
error!("Unable to parse from JSON ({:?}): {}", why, buf);
Err(why.into())
throw!(why)
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/github/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ pub fn handle_comment(conn: &PgConnection, comment: CommentFromJson, repo: &str)

if let Err(why) = nag::update_nags(&comment) {
error!("Problem updating FCPs: {:?}", &why);
return Err(why);
throw!(why);
}
}

Expand Down
24 changes: 12 additions & 12 deletions src/github/nag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ fn evaluate_nags() -> DashResult<()> {
Ok(p) => p,
Err(why) => {
error!("Unable to retrieve list of pending proposals: {:?}", why);
return Err(why.into());
throw!(why);
}
};

Expand Down Expand Up @@ -371,7 +371,7 @@ fn evaluate_nags() -> DashResult<()> {
Err(why) => {
error!("Unable to retrieve FCPs that need to be marked as finished: {:?}",
why);
return Err(why.into());
throw!(why);
}
};

Expand Down Expand Up @@ -634,12 +634,12 @@ impl FcpDisposition {
}

pub fn from_str(string: &str) -> DashResult<Self> {
match string {
FCP_REPR_MERGE => Ok(FcpDisposition::Merge),
FCP_REPR_CLOSE => Ok(FcpDisposition::Close),
FCP_REPR_POSTPONE => Ok(FcpDisposition::Postpone),
_ => Err(DashError::Misc(None)),
}
Ok(match string {
FCP_REPR_MERGE => FcpDisposition::Merge,
FCP_REPR_CLOSE => FcpDisposition::Close,
FCP_REPR_POSTPONE => FcpDisposition::Postpone,
_ => throw!(DashError::Misc(None)),
})
}

pub fn label(self) -> Label {
Expand Down Expand Up @@ -701,7 +701,7 @@ fn parse_fcp_subcommand<'a>(
},

_ => {
Err(DashError::Misc(if fcp_context {
throw!(DashError::Misc(if fcp_context {
error!("unrecognized subcommand for fcp: {}", subcommand);
Some(format!("found bad subcommand: {}", subcommand))
} else {
Expand Down Expand Up @@ -987,7 +987,7 @@ impl<'a> RfcBotCommand<'a> {
.ok_or_else(|| DashError::Misc(Some("no user specified".to_string())))?;

if user.is_empty() {
return Err(DashError::Misc(Some("no user specified".to_string())));
throw!(DashError::Misc(Some("no user specified".to_string())));
}

Ok(RfcBotCommand::FeedbackRequest(&user[1..]))
Expand Down Expand Up @@ -1191,14 +1191,14 @@ impl<'a> RfcBotComment<'a> {
self.issue.repository,
self.issue.number);

Err(DashError::Misc(None))
throw!(DashError::Misc(None))
}

} else {
info!("Skipping comment to {}#{}, comment posts are disabled.",
self.issue.repository,
self.issue.number);
Err(DashError::Misc(None))
throw!(DashError::Misc(None))
}
}
}
Expand Down
5 changes: 5 additions & 0 deletions src/macros.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
macro_rules! throw {
($err: expr) => {
Err::<!, _>($err)?
};
}
3 changes: 3 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ extern crate toml;
extern crate url;
extern crate urlencoded;

#[macro_use]
mod macros;

mod config;
mod domain;
mod error;
Expand Down
2 changes: 1 addition & 1 deletion src/teams.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ impl Team {
.first::<GitHubUser>(conn)
{
error!("unable to find {} in database: {:?}", member_login, why);
return Err(why.into());
throw!(why);
}
}

Expand Down