Skip to content
This repository has been archived by the owner on Dec 5, 2020. It is now read-only.

Commit

Permalink
Step.perform() -> Step.perform(Command, Logger)
Browse files Browse the repository at this point in the history
  • Loading branch information
amihaiemil committed Jun 15, 2017
1 parent f4eabef commit 513e46a
Show file tree
Hide file tree
Showing 38 changed files with 319 additions and 529 deletions.
2 changes: 1 addition & 1 deletion src/main/java/com/amihaiemil/charles/github/Action.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public void perform() {
String commandBody = command.json().getString("body");
logger.info("Received command: " + commandBody);
Steps steps = br.understand(command);
steps.perform();
steps.perform(command, logger);
} catch (IllegalArgumentException e) {
logger.warn("No command found in the issue or the agent has already replied to the last command!");
} catch (IOException | IllegalStateException e) {
Expand Down
23 changes: 5 additions & 18 deletions src/main/java/com/amihaiemil/charles/github/AuthorOwnerCheck.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,6 @@
*/
public class AuthorOwnerCheck extends PreconditionCheckStep {

/**
* Given command;
*/
private Command com;

/**
* Logger of the action.
*/
private Logger logger;

/**
* Constructor.
* @param com Command.
Expand All @@ -57,30 +47,27 @@ public class AuthorOwnerCheck extends PreconditionCheckStep {
* @param onFalse Step that should be performed next if the check is false.
*/
public AuthorOwnerCheck(
Command com, Logger logger,
Step onTrue, Step onFalse
) {
super(onTrue, onFalse);
this.com = com;
this.logger = logger;
}

/**
* Check that the author of a command is owner of the repo.
* @return true if the check is successful, false otherwise
*/
@Override
public void perform() {
public void perform(Command command, Logger logger) {
logger.info("Checking ownership of the repo");
try {
String repoOwner = this.com.repo().json().getJsonObject("owner").getString("login");
String author = this.com.authorLogin();
String repoOwner = command.repo().json().getJsonObject("owner").getString("login");
String author = command.authorLogin();
if(repoOwner.equals(author)) {
logger.info("Commander is repo owner - OK");
this.onTrue().perform();
this.onTrue().perform(command, logger);
} else {
logger.warn("Commander is NOT repo owner");
this.onFalse().perform();
this.onFalse().perform(command, logger);
}
} catch (IOException ex) {
logger.error("IOException when fetching repo owner from Github API", ex);
Expand Down
48 changes: 13 additions & 35 deletions src/main/java/com/amihaiemil/charles/github/Brain.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,8 @@ public Steps understand(Command com) throws IOException {
case "hello":
String hello = String.format(category.language().response("hello.comment"), authorLogin);
steps = new SendReply(
new TextReply(com, hello),
this.logger,
new Step.FinalStep(this.logger)
);
new TextReply(com, hello), new Step.FinalStep()
);
break;
case "indexsite":
steps = this.withCommonChecks(
Expand All @@ -104,7 +102,6 @@ public Steps understand(Command com) throws IOException {
break;
case "indexpage":
steps = new PageHostedOnGithubCheck(
com, this.logger,
this.withCommonChecks(
com, category.language(), this.indexPageStep(com, category.language())
),
Expand All @@ -113,7 +110,6 @@ public Steps understand(Command com) throws IOException {
break;
case "indexsitemap":
steps = new PageHostedOnGithubCheck(
com, this.logger,
this.withCommonChecks(
com, category.language(),
this.indexSitemapStep(com, category.language())
Expand All @@ -127,14 +123,11 @@ public Steps understand(Command com) throws IOException {
break;
case "deleteindex":
steps = new DeleteIndexCommandCheck(
com, this.logger,
new IndexExistsCheck(
com.indexName(), this.logger,
com.indexName(),
new AuthorOwnerCheck(
com, this.logger,
this.deleteIndexStep(com, category.language()),
new OrganizationAdminCheck(
com, this.logger,
this.deleteIndexStep(com, category.language()),
this.finalCommentStep(
com, category.language(),
Expand All @@ -161,14 +154,12 @@ public Steps understand(Command com) throws IOException {
authorLogin);
steps = new SendReply(
new TextReply(com, unknown),
this.logger,
new Step.FinalStep(this.logger)
new Step.FinalStep()
);
break;
}
return new Steps(
steps,
this.logger,
new SendReply(
new TextReply(
com,
Expand All @@ -177,11 +168,7 @@ public Steps understand(Command com) throws IOException {
com.authorLogin(), this.logsLoc.address()
)
),
this.logger,
new Step.FinalStep(
this.logger,
"[ERROR] Some step didn't execute properly."
)
new Step.FinalStep("[ERROR] Some step didn't execute properly.")
)
);
}
Expand All @@ -202,11 +189,9 @@ public Step indexPageStep(Command com, Language lang) throws IOException {
com.authorLogin(),
this.logsLoc.address()
)
), this.logger,
),
new IndexPage(
com, this.logger,
new StarRepo(
com.issue().repo(), this.logger,
this.finalCommentStep(
com, lang, "index.finished.comment",
com.authorLogin(),
Expand All @@ -233,11 +218,9 @@ public Step indexSitemapStep(Command com, Language lang) throws IOException {
com.authorLogin(),
this.logsLoc.address()
)
), this.logger,
),
new IndexSitemap(
com, this.logger,
new StarRepo(
com.issue().repo(), this.logger,
this.finalCommentStep(
com, lang, "index.finished.comment",
com.authorLogin(),
Expand All @@ -264,11 +247,9 @@ public Step indexSiteStep(Command com, Language lang) throws IOException {
com.authorLogin(),
this.logsLoc.address()
)
), this.logger,
),
new IndexSite(
com, this.logger,
new StarRepo(
com.issue().repo(), this.logger,
this.finalCommentStep(
com, lang, "index.finished.comment",
com.authorLogin(),
Expand All @@ -288,7 +269,6 @@ public Step indexSiteStep(Command com, Language lang) throws IOException {
*/
public Step deleteIndexStep(Command com, Language lang) throws IOException {
return new DeleteIndex(
com, logger,
this.finalCommentStep(
com, lang, "deleteindex.finished.comment",
com.authorLogin(),
Expand Down Expand Up @@ -331,22 +311,20 @@ private CommandCategory categorizeCommand(Command com) throws IOException {
*/
private Step withCommonChecks(Command com, Language lang, Step action) throws IOException {
PreconditionCheckStep repoForkCheck = new RepoForkCheck(
com.repo().json(), this.logger, action,
com.repo().json(), action,
this.finalCommentStep(com, lang, "denied.fork.comment", com.authorLogin())
);
PreconditionCheckStep authorOwnerCheck = new AuthorOwnerCheck(
com, this.logger,
repoForkCheck,
new OrganizationAdminCheck(
com, this.logger,
repoForkCheck,
this.finalCommentStep(com, lang, "denied.commander.comment", com.authorLogin())
)
);
PreconditionCheckStep repoNameCheck = new RepoNameCheck(
com.repo().json(), this.logger, authorOwnerCheck,
com.repo().json(), authorOwnerCheck,
new GhPagesBranchCheck(
com, this.logger, authorOwnerCheck,
authorOwnerCheck,
this.finalCommentStep(com, lang, "denied.name.comment", com.authorLogin())
)
);
Expand All @@ -370,8 +348,8 @@ private SendReply finalCommentStep(
lang.response(messagekey),
(Object[]) formatParts
)
), this.logger,
new Step.FinalStep(this.logger)
),
new Step.FinalStep()
);
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/amihaiemil/charles/github/Command.java
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ public JsonObject authorOrgMembership() throws IOException {
*/
public CommandedRepo repo() throws IOException {
if(this.repo == null) {
this.repo = new CommandedRepo(this.issue.repo());;
this.repo = new CommandedRepo(this.issue.repo());
}
return this.repo;
}
Expand Down
23 changes: 6 additions & 17 deletions src/main/java/com/amihaiemil/charles/github/DeleteIndex.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,39 +40,28 @@
*/
public class DeleteIndex extends IntermediaryStep {

/**
* Command.
*/
private Command com;

/**
* Action's logger.
*/
private Logger logger;

/**
* Constructor.
* @param com Command
* @param logger The action's logger
* @param next The next step to take
*/
public DeleteIndex(Command com, Logger logger, Step next) {
public DeleteIndex(Step next) {
super(next);
this.com = com;
this.logger = logger;
}

@Override
public void perform() {
this.logger.info("Starting index deletion...");
public void perform(Command command, Logger logger) {
logger.info("Starting index deletion...");
try {
new AmazonEsRepository(com.indexName()).delete();
new AmazonEsRepository(command.indexName()).delete();
} catch (IOException e) {
logger.error("Exception while deleting the index!", e);
throw new IllegalStateException("Exception while deleting the index!" , e);
}
this.logger.info("Index successfully deleted!");
this.next().perform();
logger.info("Index successfully deleted!");
this.next().perform(command, logger);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -39,43 +39,28 @@
*/
public class DeleteIndexCommandCheck extends PreconditionCheckStep {

/**
* Given command.
*/
private Command com;

/**
* Logger.
*/
private Logger logger;

public DeleteIndexCommandCheck(
Command com, Logger logger,
Step onTrue, Step onFalse
) {
public DeleteIndexCommandCheck(Step onTrue, Step onFalse) {
super(onTrue, onFalse);
this.com = com;
this.logger = logger;
}

@Override
public void perform() {
public void perform(Command command, Logger logger) {
boolean passed = false;
String text = com.json().getString("body");
String text = command.json().getString("body");
if(text.contains("`")) {
String repoToBeDeleted = text.substring(text.indexOf('`') + 1, text.lastIndexOf('`'));
try {
String repoName = com.repo().name();
String repoName = command.repo().name();
passed = repoName.equals(repoToBeDeleted);
} catch (IOException e) {
logger.error("Exception when getting repo's name", e);
throw new IllegalStateException("Exception when getting repo's name", e);
}
}
if(passed) {
this.onTrue().perform();
this.onTrue().perform(command, logger);
} else {
this.onFalse().perform();
this.onFalse().perform(command, logger);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,46 +39,31 @@
*/
public class GhPagesBranchCheck extends PreconditionCheckStep {

/**
* Given command.
*/
private Command com;

/**
* Logger.
*/
private Logger logger;

/**
* Constructor.
* @param com Given command.
* @param logger Action logger.
* @param onTrue Step that should be performed next if the check is true.
* @param onFalse Step that should be performed next if the check is false.
*/
public GhPagesBranchCheck(
Command com, Logger logger,
Step onTrue, Step onFalse
) {
public GhPagesBranchCheck(Step onTrue, Step onFalse) {
super(onTrue, onFalse);
this.com = com;
this.logger = logger;
}

/**
* Perform this step.
*/
@Override
public void perform() {
public void perform(Command command, Logger logger) {
logger.info("Checking whether the repository has a gh-pages branch...");
try {

if (com.repo().hasGhPagesBranch()) {
if (command.repo().hasGhPagesBranch()) {
logger.info("The repo has a gh-pages branch - OK!");
this.onTrue().perform();
this.onTrue().perform(command, logger);
} else {
logger.info("The repo does NOT have a gh-pages branch");
this.onFalse().perform();
this.onFalse().perform(command, logger);
}
} catch (IOException e) {
logger.error("Exception when checking if gh-pages branch exists", e);
Expand Down
Loading

0 comments on commit 513e46a

Please sign in to comment.