Merge pull request #28 from grahamc/comment-parser

Support ofborg comments inline with other text
This commit is contained in:
Graham Christensen 2017-12-22 09:30:56 -05:00 committed by GitHub
commit 36896492eb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 63 additions and 28 deletions

View file

@ -9,7 +9,10 @@
## Commands
1. To trigger the bot, the comment _must_ start with a case
The comment parser is line-based, so comments can be interleaved with
instructions.
1. To trigger the bot, the line _must_ start with a case
insensitive version of `@GrahamcOfBorg`.
2. To use multiple commands, insert a bit of whitespace and then your
new command.
@ -61,21 +64,25 @@ or even:
@grahamcofborg build list of attrs @grahamcofborg eval
```
This will _not_ work:
This will also work:
```
looks good to me!
@grahamcofborg build list of attrs
```
Also this is bad:
And this is fine:
```
@grahamcofborg build list of attrs
looks good to me!
```
as it'll try to build `list` `of` `attrs` `looks` `good` `to` `me!`.
This is will build `list`, `of`, `attrs`, `looks`, `good`, `to`, `me!`:
```
@grahamcofborg build list of attrs looks good to me!
```
# How does OfBorg call nix-build?

View file

@ -1,5 +1,23 @@
pub fn parse(text: &str) -> Option<Vec<Instruction>> {
let instructions: Vec<Instruction> = text.lines()
.map(|s| match parse_line(s) {
Some(instructions) => instructions,
None => vec![]
})
.fold(vec![], |mut collector, mut inst| {
collector.append(&mut inst);
collector
});
if instructions.len() == 0 {
return None;
} else {
return Some(instructions)
}
}
pub fn parse_line(text: &str) -> Option<Vec<Instruction>> {
let tokens: Vec<String> = text.split_whitespace()
.map(|s| s.to_owned()).collect();
@ -67,6 +85,14 @@ mod tests {
assert_eq!(None, parse(""));
}
#[test]
fn valid_trailing_instruction() {
assert_eq!(
Some(vec![Instruction::Eval]),
parse("/cc @grahamc for ^^
@GrahamcOfBorg eval")
);
}
#[test]
fn bogus_comment() {
@ -107,6 +133,31 @@ mod tests {
@grahamcofborg build foo"));
}
#[test]
fn complex_comment_with_paragraphs() {
assert_eq!(Some(vec![
Instruction::Build(Subset::Nixpkgs, vec![
String::from("bar"),
]),
Instruction::Eval,
Instruction::Build(Subset::Nixpkgs, vec![
String::from("foo"),
])
]),
parse("
I like where you're going with this PR, so let's try it out!
@grahamcofborg build bar
I noticed though that the target branch was broken, which should be fixed. Let's eval again.
@grahamcofborg eval
Also, just in case, let's try foo
@grahamcofborg build foo"));
}
#[test]
fn build_and_eval_comment() {
assert_eq!(Some(vec![
@ -122,8 +173,7 @@ mod tests {
fn build_comment() {
assert_eq!(Some(vec![Instruction::Build(Subset::Nixpkgs, vec![
String::from("foo"),
String::from("bar"),
String::from("baz")
String::from("bar")
])]),
parse("@GrahamCOfBorg build foo bar
@ -160,28 +210,6 @@ baz"));
parse("@grahamcofborg build foo bar baz"));
}
#[test]
fn build_whitespace_disregarded() {
assert_eq!(Some(vec![Instruction::Build(Subset::Nixpkgs, vec![
String::from("foo"),
String::from("bar"),
String::from("baz")
])]),
parse("
@grahamcofborg
build foo
bar baz
"));
}
#[test]
fn build_comment_lower_package_case_retained() {
assert_eq!(Some(vec![Instruction::Build(Subset::Nixpkgs, vec![