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

Commit

Permalink
GeneralPreconditionsCheck; tests still needed
Browse files Browse the repository at this point in the history
  • Loading branch information
amihaiemil committed Aug 8, 2017
1 parent 90fd3e5 commit 88943fb
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 29 deletions.
27 changes: 27 additions & 0 deletions src/main/java/com/amihaiemil/charles/github/CachedRepo.java
Expand Up @@ -25,11 +25,13 @@
*/
package com.amihaiemil.charles.github;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.net.HttpURLConnection;

import javax.json.JsonObject;

import com.jcabi.github.Content;
import org.hamcrest.Matchers;

import com.jcabi.github.Repo;
Expand Down Expand Up @@ -62,6 +64,11 @@ public class CachedRepo {
*/
private Boolean ghPagesBranch;

/**
* Cached .charles.yml file
*/
private CharlesYml yml;

/**
* Ctor.
* @param repo Github repository.
Expand Down Expand Up @@ -143,4 +150,24 @@ public boolean isOwnedByOrganization() throws IOException {
);
}

/**
* The charles.yml file contained in the repo.
* @return {@link CharlesYml}
* @throws IOException
*/
public CharlesYml charlesYml() throws IOException {
if(this.yml == null) {
this.yml = new CharlesYmlInput(
new ByteArrayInputStream(
new Content.Smart(
this.repo
.contents()
.get(".charles.yml")
).decoded()
)
);
}
return this.yml;
}

}
Expand Up @@ -26,7 +26,6 @@
package com.amihaiemil.charles.github;

import org.slf4j.Logger;

import java.io.IOException;

/**
Expand All @@ -43,45 +42,87 @@
*/
public final class GeneralPreconditionsCheck extends PreconditionCheckStep {

/**
* .charles.yml config file.
*/
private CharlesYml charlesYml;

/**
* Ctor.
* @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.
* @param charlesYml This is the .charles.yml config file.
*/
public GeneralPreconditionsCheck(
final Step onTrue,
final Step onFalse,
final CharlesYml charlesYml
final Step onTrue, final Step onFalse
) {
super(onTrue, onFalse);
this.charlesYml = charlesYml;
}

@Override
public void perform(Command command, Logger logger) throws IOException {
// PreconditionCheckStep repoForkCheck = new RepoForkCheck(
// command.repo().json(), action,
// this.finalCommentStep(command, lang, "denied.fork.comment", command.authorLogin())
// );
// PreconditionCheckStep authorOwnerCheck = new AuthorOwnerCheck(
// repoForkCheck,
// new OrganizationAdminCheck(
// repoForkCheck,
// this.finalCommentStep(command, lang, "denied.commander.comment", command.authorLogin())
// )
// );
// PreconditionCheckStep repoNameCheck = new RepoNameCheck(
// command.repo().json(), authorOwnerCheck,
// new GhPagesBranchCheck(
// authorOwnerCheck,
// this.finalCommentStep(command, lang, "denied.name.comment", command.authorLogin())
// )
// );
final PreconditionCheckStep all;

final PreconditionCheckStep repoForkCheck = new RepoForkCheck(
command.repo().json(), this.onTrue(),
this.finalCommentStep(command, "denied.fork.comment", command.authorLogin())
);

if(!this.isCommanderAllowed(command)) {
final PreconditionCheckStep authorOwnerCheck = new AuthorOwnerCheck(
repoForkCheck,
new OrganizationAdminCheck(
repoForkCheck,
this.finalCommentStep(command, "denied.commander.comment", command.authorLogin())
)
);
all = new RepoNameCheck(
command.repo().json(), authorOwnerCheck,
new GhPagesBranchCheck(
authorOwnerCheck,
this.finalCommentStep(command, "denied.name.comment", command.authorLogin())
)
);
} else {
all = new RepoNameCheck(
command.repo().json(), repoForkCheck,
new GhPagesBranchCheck(
repoForkCheck,
this.finalCommentStep(command, "denied.name.comment", command.authorLogin())
)
);
}
all.perform(command, logger);
}

/**
* Builds the final comment to be sent to the issue.
* <b>This should be the last in the steps' chain</b>.
* @param com Command.
* @param formatParts Parts to format the response %s elements with.
* @return SendReply step.
*/
private SendReply finalCommentStep(
Command com, String messagekey, String ... formatParts
) {
return new SendReply(
new TextReply(
com,
String.format(
com.language().response(messagekey),
(Object[]) formatParts
)
),
new Step.FinalStep()
);
}

/**
* Is the command's author specified in .charles.yml as a commander?
* @param com Initial command.
* @throws IOException if the commanders' list cannot be read from Github.
* @return True of False
*/
private boolean isCommanderAllowed(Command com) throws IOException {
for(String commander : com.repo().charlesYml().commanders()) {
if(commander.equalsIgnoreCase(com.authorLogin())) {
return true;
}
}
return false;
}
}

0 comments on commit 88943fb

Please sign in to comment.