Skip to content

Commit

Permalink
SocialSteps + tests + Logger->Log
Browse files Browse the repository at this point in the history
  • Loading branch information
amihaiemil committed Nov 4, 2017
1 parent 3023850 commit 584fcbf
Show file tree
Hide file tree
Showing 16 changed files with 257 additions and 109 deletions.
54 changes: 54 additions & 0 deletions src/main/java/co/comdor/SocialSteps.java
@@ -0,0 +1,54 @@
/**
* Copyright (c) 2017, Mihai Emil Andronache
* All rights reserved.
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* 1)Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2)Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3)Neither the name of comdor nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
package co.comdor;

import co.comdor.github.Mention;

/**
* After executing all the steps which are needed to fulfill an action,
* the bot performs a few more social, cosmetic steps, like starring the repo,
* following the user on Github, tweeting etc.
*
* These steps should be executted all the time, in the end of the Steps
* pyramid.
*
* @author Mihai Andronache (amihaiemil@gmail.com)
* @version $Id$
* @since 0.0.2
*/

public interface SocialSteps extends Step {

/**
* Notice that this overriden perform does not throw IOException.
* This is because we are not treating social steps failures, they are only
* cosmetic, not affecting the business at all.
* @param mention Mention which triggered everything.
* @param log Log of the Action.
*/
@Override
void perform(final Mention mention, final Log log);
}
9 changes: 4 additions & 5 deletions src/main/java/co/comdor/Step.java
Expand Up @@ -26,7 +26,6 @@
package co.comdor;

import co.comdor.github.Mention;
import org.slf4j.Logger;
import java.io.IOException;

/**
Expand All @@ -44,7 +43,7 @@ public interface Step {
* @throws IOException If there is anything wrong in the communication
* with Github.
*/
void perform(final Mention mention, final Logger log) throws IOException;
void perform(final Mention mention, final Log log) throws IOException;

/**
* Final step in the chain.
Expand Down Expand Up @@ -73,8 +72,8 @@ public FinalStep(final String message) {
}

@Override
public void perform(final Mention mention, final Logger log) {
log.info(this.message);
public void perform(final Mention mention, final Log log) {
log.logger().info(this.message);
}
}

Expand All @@ -97,7 +96,7 @@ public Fake(final boolean pass) {
}

@Override
public void perform(final Mention mention, final Logger log) {
public void perform(final Mention mention, final Log log) {
if(!this.pass) {
throw new IllegalStateException(
"Step should not have been executed!"
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/co/comdor/Steps.java
Expand Up @@ -25,7 +25,6 @@
*/
package co.comdor;

import org.slf4j.Logger;
import java.io.IOException;

/**
Expand All @@ -42,5 +41,6 @@ public interface Steps {
* @throws IOException If there is anything wrong in the communication
* with Github.
*/
void perform(final Logger log) throws IOException;
void perform(final Log log) throws IOException;

}
12 changes: 6 additions & 6 deletions src/main/java/co/comdor/github/FollowUser.java
Expand Up @@ -30,7 +30,7 @@
import com.jcabi.http.Request;
import java.io.IOException;
import java.net.HttpURLConnection;
import org.slf4j.Logger;
import co.comdor.Log;

/**
* Step where the bot follows the Github user.
Expand All @@ -50,25 +50,25 @@ public FollowUser(final Step next) {

@Override
public void perform(
final Mention mention, final Logger log
final Mention mention, final Log log
) throws IOException {
final String author = mention.author();
final Request follow = mention.issue().repo().github().entry()
.uri().path("/user/following/").path(author).back()
.method("PUT");
log.info("Following Github user " + author + " ...");
log.logger().info("Following Github user " + author + " ...");
try {
final int status = follow.fetch().status();
if(status != HttpURLConnection.HTTP_NO_CONTENT) {
log.error(
log.logger().error(
"User follow status response is " + status
+ " . Should have been 204 (NO CONTENT)"
);
} else {
log.info("Followed user " + author + " .");
log.logger().info("Followed user " + author + " .");
}
} catch (final IOException ex) {
log.warn("IOException while trying to follow the user.");
log.logger().warn("IOException while trying to follow the user.");
}
this.next().perform(mention, log);
}
Expand Down
20 changes: 17 additions & 3 deletions src/main/java/co/comdor/github/GithubAction.java
Expand Up @@ -28,6 +28,7 @@
import co.comdor.Action;
import co.comdor.Log;
import co.comdor.LogFile;
import co.comdor.SocialSteps;
import co.comdor.Steps;
import co.comdor.WebLog;
import com.jcabi.github.Issue;
Expand All @@ -52,6 +53,12 @@ public final class GithubAction implements Action {
*/
private Issue issue;

/**
* Social steps to be executed in the end, after the bot finishes everything
* else.
*/
private SocialSteps social;

/**
* Log of this action. Each Github action should be logged in its own file,
* since we want to let the user inspect the logs sometimes.
Expand All @@ -61,11 +68,16 @@ public final class GithubAction implements Action {
/**
* Ctor.
* @param issue Github Issue which triggered this action.
* @param social Social steps that the bot executes after the he fulfills
* the triggering Mention.
* @throws IOException If there is any IO problem (e.g. writing files,
* communicating with Github etc).
*/
public GithubAction(final Issue issue) throws IOException {
public GithubAction(
final Issue issue, final SocialSteps social
) throws IOException {
this.issue = issue;
this.social = social;
this.id = UUID.randomUUID().toString();
this.log = new WebLog(
new LogFile(
Expand All @@ -89,8 +101,10 @@ public void perform() {
new Confused()
)
);
final Steps steps = talk.start(new LastMention(this.issue));
steps.perform(this.log.logger());
final Mention mention = new LastMention(this.issue);
final Steps steps = talk.start(mention);
steps.perform(this.log);
this.social.perform(mention, this.log);
} catch (final IllegalArgumentException iae) {
this.log.logger().warn(
"No command found in the issue or the agent has already"
Expand Down
63 changes: 63 additions & 0 deletions src/main/java/co/comdor/github/GithubSocialSteps.java
@@ -0,0 +1,63 @@
/**
* Copyright (c) 2017, Mihai Emil Andronache
* All rights reserved.
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* 1)Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2)Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3)Neither the name of comdor nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
package co.comdor.github;

import co.comdor.SocialSteps;
import co.comdor.Step;
import co.comdor.Log;
import java.io.IOException;

/**
* Github social steps. Follow the user and star the repository.
* @todo #34:30min StarRepo and FollowUser are each tested separately, but some
* tests for this class would also be useful.
*/
public final class GithubSocialSteps implements SocialSteps {

/**
* Social steps.
*/
private final Step social;

/**
* Ctor.
*/
public GithubSocialSteps() {
this.social = new StarRepo(
new FollowUser(
new Step.FinalStep("Starred the repo and followed the user.")
)
);
}

@Override
public void perform(final Mention mention, final Log log) {
try {
this.social.perform(mention, log);
} catch (final IOException ioe){}
}

}
52 changes: 18 additions & 34 deletions src/main/java/co/comdor/github/GithubSteps.java
Expand Up @@ -27,7 +27,7 @@

import co.comdor.Step;
import co.comdor.Steps;
import org.slf4j.Logger;
import co.comdor.Log;

import java.io.IOException;

Expand All @@ -43,63 +43,47 @@ public final class GithubSteps implements Steps {
* Steps to be performed.
*/
private Step steps;

/**
* Initial mention. The one that triggered everything.
*/
private Mention mention;

/**
* Message to send in case some step fails.
*/
private SendReply failureMessage;

/**
* Constructor.
* @param steps Steps to perform everything.
* @param mention Initial menton.
*/
public GithubSteps(final Step steps, final Mention mention) {
this(
steps, mention,
new SendReply(
mention.language().response("step.failure.comment"),
new Step.FinalStep("[ERROR] Some step didn't execute properly.")
)
);
}

/**
* Constructor.
* @param steps Steps to perform everything.
* @param mention Initial menton.
* @param failureMessage Reply sent in case of failure.
*/
public GithubSteps(
final Step steps, final Mention mention,
final SendReply failureMessage
) {
this.steps = steps;
this.mention = mention;
this.failureMessage = failureMessage;
}

/**
* Perform all the given steps.
* @param logger Action logger.
* @param log Action logger.
* @throws IOException If something goes wrong while calling Github.
*/
@Override
public void perform(final Logger logger) throws IOException {
public void perform(final Log log) throws IOException {
try {
logger.info(
log.logger().info(
"Received command: " + this.mention.json().getString("body")
);
logger.info("Author login: " + this.mention.author());
this.steps.perform(this.mention, logger);
log.logger().info("Author login: " + this.mention.author());
this.steps.perform(this.mention, log);
} catch (final IOException ex) {
logger.error("An exception occured, sending failure comment.", ex);
this.failureMessage.perform(this.mention, logger);
log.logger().error(
"Some step did not execute properly, sending failure reply.", ex
);
this.mention.reply(
String.format(
this.mention.language().response("steps.failure.comment"),
log.location()
)
);
}

}

}
6 changes: 3 additions & 3 deletions src/main/java/co/comdor/github/SendReply.java
Expand Up @@ -26,7 +26,7 @@
package co.comdor.github;
import co.comdor.IntermediaryStep;
import co.comdor.Step;
import org.slf4j.Logger;
import co.comdor.Log;
import java.io.IOException;

/**
Expand Down Expand Up @@ -64,12 +64,12 @@ public SendReply(final String rep, final Step next) {

@Override
public void perform(
final Mention mention, final Logger log
final Mention mention, final Log log
) throws IOException {
try {
mention.reply(this.rep);
} catch (final IOException ex) {
log.error("IOException when sending a reply!", ex);
log.logger().error("IOException when sending a reply!", ex);
throw new IOException("IOException when sending a reply!", ex);
}
this.next().perform(mention, log);
Expand Down

1 comment on commit 584fcbf

@0pdd
Copy link

@0pdd 0pdd commented on 584fcbf Nov 4, 2017

Choose a reason for hiding this comment

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

Puzzle 34-9af8cff0 discovered in src/main/java/co/comdor/github/GithubSocialSteps.java and submitted as #37.

Please sign in to comment.