Skip to content
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

#864 Correctly handle situation when report is created for stopped command. #1046

Merged
merged 5 commits into from
Mar 10, 2016
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
14 changes: 10 additions & 4 deletions src/main/java/com/rultor/agents/github/Reports.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,18 +95,24 @@ public Iterable<Directive> process(final XML xml) throws IOException {
} else {
pattern = "Reports.failure";
}
final StringBuilder msg = new StringBuilder(
final int number = Integer.parseInt(req.xpath("@id").get(0));
final Comment.Smart comment = Reports.origin(issue, number);
final StringBuilder message = new StringBuilder();
if (comment.body().contains("stop")) {
message.append(Reports.PHRASES.getString("Reports.stop-fails"))
.append(' ');
}
message.append(
Logger.format(
Reports.PHRASES.getString(pattern),
home.toASCIIString(),
Long.parseLong(req.xpath("msec/text()").get(0))
)
).append(Reports.highlights(req));
if (!success) {
msg.append(Reports.tail(req));
message.append(Reports.tail(req));
}
final int number = Integer.parseInt(req.xpath("@id").get(0));
new Answer(Reports.origin(issue, number)).post(success, msg.toString());
new Answer(comment).post(success, message.toString());
Logger.info(this, "issue #%d reported: %B", issue.number(), success);
return new Directives()
.xpath("/talk/request[success]")
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/phrases_en_US.properties
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ QnVersion.intro=My current version is %s, \

Reports.success=Done! FYI, the full log is [here](%s) (took me %[ms]s)
Reports.failure=Oops, I failed. You can see the full log [here](%s) (spent %[ms]s)
Reports.stop-fails=Sorry, I failed to stop the previous command, however it has the following result:

CommentsTag.duplicate=Release `%s` already exists! I can't duplicate it, \
but I posted a comment there. In the future, try to avoid duplicate releases
Expand Down
92 changes: 81 additions & 11 deletions src/test/java/com/rultor/agents/github/ReportsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,12 @@
import com.jcabi.github.Issue;
import com.jcabi.github.Repo;
import com.jcabi.github.mock.MkGithub;
import com.jcabi.log.Logger;
import com.jcabi.matchers.XhtmlMatchers;
import com.rultor.spi.Agent;
import com.rultor.spi.Talk;
import java.io.IOException;
import java.util.ResourceBundle;
import org.hamcrest.MatcherAssert;
import org.junit.Test;
import org.xembly.Directives;
Expand All @@ -47,34 +50,101 @@
* @since 1.3
*/
public final class ReportsTest {
/**
* Message bundle.
*/
private static final ResourceBundle PHRASES = ResourceBundle.getBundle(
"phrases"
);

/**
* Duration of command execution.
*/
private static final long DURATION = 1234567;

/**
* Xpath to check that talk was executed correctly.
*/
private static final String XPATH = "/talk[not(request)]";

/**
* Reports can report a result of a request.
* @throws Exception In case of error.
* @throws Exception In case of error
*/
@Test
public void reportsRequestResult() throws Exception {
final Repo repo = new MkGithub().randomRepo();
final Issue issue = repo.issues().create("", "");
final Talk talk = ReportsTest.example(
repo, repo.issues().create("", "")
);
final Agent agent = new Reports(repo.github());
final Talk talk = new Talk.InFile();
talk.modify(
agent.execute(talk);
MatcherAssert.assertThat(
talk.read(),
XhtmlMatchers.hasXPath(XPATH)
);
}

/**
* Reports can report a result of a request, when stop command fails.
* @throws Exception In case of error
*/
@Test
public void reportsRequestResultWhenStopFails() throws Exception {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@xupyprmv where in this test we simulate failure?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mkordas In line:

final Issue issue = repo.issues().create("Bug", "stop it please");

I added comment with stop command. So Reports creates report on command that was tried to stop.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@xupyprmv now I see, thanks

final String user = "john";
final String stop = "stop it please";
final Repo repo = new MkGithub(user).randomRepo();
final Talk talk = ReportsTest.example(
repo, repo.issues().create("Bug", stop)
);
final Agent agent = new Reports(repo.github());
agent.execute(talk);
MatcherAssert.assertThat(
talk.read(),
XhtmlMatchers.hasXPath(XPATH)
);
MatcherAssert.assertThat(
"Comment contains warning about stop request",
repo.issues().get(1).comments().get(1).json().getString(
"body"
).equals(
String.format(
"> %s\n\n@%s %s %s",
stop,
user,
ReportsTest.PHRASES.getString("Reports.stop-fails"),
Logger.format(
ReportsTest.PHRASES.getString("Reports.success"),
"http://www.rultor.com/t/1-1",
DURATION
)
)
)
);
}

/**
* Create Talk, that will be used to test Reports.
* @param repo Repository
* @param issue Issue
* @return Example of Talk
* @throws IOException In case of error
*/
private static Talk example(final Repo repo, final Issue issue)
throws IOException {
final Talk result = new Talk.InFile();
result.modify(
new Directives().xpath("/talk").add("wire")
.add("href").set("http://test").up()
.add("github-repo").set(repo.coordinates().toString()).up()
.add("github-issue").set(Integer.toString(issue.number())).up()
.up()
.add("request").attr("id", "1")
.add("msec").set("1234567").up()
.add("msec").set(DURATION).up()
.add("success").set("true").up()
.add("type").set("something").up()
.add("args")
);
agent.execute(talk);
MatcherAssert.assertThat(
talk.read(),
XhtmlMatchers.hasXPath("/talk[not(request)]")
);
return result;
}

}