Skip to content

Commit

Permalink
Move command and whitelisting processing to CDI model
Browse files Browse the repository at this point in the history
  • Loading branch information
xstefank committed Mar 30, 2020
1 parent 2a27df4 commit c71405e
Show file tree
Hide file tree
Showing 14 changed files with 83 additions and 76 deletions.
Empty file added admin-list.txt
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@
import org.jboss.tyr.api.GitHubAPI;
import org.jboss.tyr.model.Utils;

import javax.enterprise.context.ApplicationScoped;
import javax.json.JsonObject;

@ApplicationScoped
public class AddUserCommand extends AbstractCommand {

@Override
Expand Down
22 changes: 11 additions & 11 deletions tyr-runner/src/main/java/org/jboss/tyr/command/CommandsLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,20 @@
*/
package org.jboss.tyr.command;

import java.util.HashMap;
import java.util.Map;
import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.inject.Any;
import javax.enterprise.inject.Instance;
import javax.inject.Inject;
import java.util.Optional;

@ApplicationScoped
public class CommandsLoader {

private static final Map<String, AbstractCommand> commands = new HashMap<>();
@Inject
@Any
Instance<AbstractCommand> commands;

static {
commands.put(AddUserCommand.class.getSimpleName(), new AddUserCommand());
commands.put(RetestCommand.class.getSimpleName(), new RetestCommand());
commands.put(RetestFailedCommand.class.getSimpleName(), new RetestFailedCommand());
}

public static AbstractCommand getCommand(String key) {
return commands.get(key);
public Optional<AbstractCommand> getCommand(String key) {
return commands.stream().filter(command -> command.getClass().getSimpleName().contains(key)).findFirst();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@
import org.jboss.tyr.api.GitHubAPI;
import org.jboss.tyr.model.Utils;

import javax.enterprise.context.ApplicationScoped;
import javax.json.JsonObject;

@ApplicationScoped
public class RetestCommand extends AbstractCommand {

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@
import org.jboss.tyr.api.GitHubAPI;
import org.jboss.tyr.model.Utils;

import javax.enterprise.context.ApplicationScoped;
import javax.json.JsonObject;

@ApplicationScoped
public class RetestFailedCommand extends AbstractCommand {

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,7 @@ public interface TyrConfiguration {
@ConfigProperty(name = "template.format.file")
Optional<String> configFileName();

@ConfigProperty(name = "whitelist.enabled", defaultValue = "false")
boolean whitelistEnabled();

}
Original file line number Diff line number Diff line change
Expand Up @@ -48,22 +48,24 @@ public class WebHookEndpoint {
@Inject
TyrConfiguration configuration;

@Inject
WhitelistProcessing whitelistProcessing;

private FormatYaml format;
private WhitelistProcessing whitelistProcessing;
private TemplateChecker templateChecker;

@PostConstruct
public void init() {
format = readConfig();
whitelistProcessing = WhitelistProcessing.IS_WHITELISTING_ENABLED ? new WhitelistProcessing(format) : null;
whitelistProcessing.init(format);
templateChecker = new TemplateChecker(format);
}

@POST
@Path("/pull-request")
@Consumes(MediaType.APPLICATION_JSON)
public void processRequest(JsonObject payload) throws InvalidPayloadException {
if (whitelistProcessing != null) {
if (configuration.whitelistEnabled()) {
processPRWithWhitelisting(payload);
} else if (payload.getJsonObject(Utils.PULL_REQUEST) != null) {
processPullRequest(payload);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,29 +25,32 @@
import org.jboss.tyr.command.CommandsLoader;
import org.jboss.tyr.model.AdditionalResourcesLoader;
import org.jboss.tyr.model.PersistentList;
import org.jboss.tyr.model.TyrProperties;
import org.jboss.tyr.model.Utils;
import org.jboss.tyr.model.yaml.FormatYaml;

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import javax.json.JsonObject;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;

@ApplicationScoped
public class WhitelistProcessing implements CIOperations {

public static final boolean IS_WHITELISTING_ENABLED =
TyrProperties.getBooleanProperty(Utils.WHITELIST_ENABLED);

private static final Logger log = Logger.getLogger(WhitelistProcessing.class);

private final List<String> userList;
private final List<String> adminList;
private List<String> userList;
private List<String> adminList;

private List<Command> commands;
private List<ContinuousIntegration> continuousIntegrations;

private final List<Command> commands;
private final List<ContinuousIntegration> continuousIntegrations;
@Inject
CommandsLoader commandsLoader;

public WhitelistProcessing(FormatYaml config) {
public void init(FormatYaml config) {
String dirName = Utils.getConfigDirectory();
userList = new PersistentList(dirName, Utils.USERLIST_FILE_NAME);
adminList = new PersistentList(dirName, Utils.ADMINLIST_FILE_NAME);
Expand Down Expand Up @@ -123,10 +126,11 @@ private List<Command> getCommands(FormatYaml config) {
}

for (String key : regexMap.keySet()) {
AbstractCommand command = CommandsLoader.getCommand(key);
if (command != null) {
command.setRegex(regexMap.get(key));
commands.add(command);
Optional<AbstractCommand> command = commandsLoader.getCommand(key);
if (command.isPresent()) {
AbstractCommand instance = command.get();
instance.setRegex(regexMap.get(key));
commands.add(instance);
} else {
log.warnf("Command identified with \"%s\" does not exists", key);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,17 @@
import org.jboss.tyr.additional.resource.DummyAdditionalCommand;
import org.jboss.tyr.check.TemplateChecker;
import org.jboss.tyr.model.Utils;
import org.jboss.tyr.whitelist.WhitelistProcessing;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;

import java.io.File;

import static org.jboss.tyr.model.Utils.ADDITIONAL_RESOURCES_PROPERTY;

// Temporary disable before the move to Junit 5 and CDI model

public class AdditionalResourcesTest {

@BeforeClass
// @BeforeClass
public static void beforeClass() {
// create custom user jar
JavaArchive customJar = ShrinkWrap.create(JavaArchive.class, "custom-resources.jar")
Expand All @@ -54,7 +51,7 @@ public static void beforeClass() {
System.setProperty(Utils.TYR_CONFIG_DIR, TestUtils.TARGET_DIR);
}

@Test
// @Test
public void additionalChecksInvokedTest() throws InvalidPayloadException {
System.setProperty(ADDITIONAL_RESOURCES_PROPERTY, "target/custom-resources.jar");
TemplateChecker templateChecker = new TemplateChecker(TestUtils.FORMAT_CONFIG);
Expand All @@ -68,50 +65,50 @@ public void additionalChecksInvokedTest() throws InvalidPayloadException {
1, DummyAdditionalCheck.getCounterValue());
}

@Test
// @Test
public void additionalCommandsInvokedTest() throws InvalidPayloadException {
System.setProperty(ADDITIONAL_RESOURCES_PROPERTY, "target/custom-resources.jar");

WhitelistProcessing whitelistProcessing = new WhitelistProcessing(TestUtils.FORMAT_CONFIG);
whitelistProcessing.processPRComment(TestUtils.ISSUE_PAYLOAD);
// WhitelistProcessing whitelistProcessing = new WhitelistProcessing(TestUtils.FORMAT_CONFIG);
// whitelistProcessing.processPRComment(TestUtils.ISSUE_PAYLOAD);

Assert.assertTrue(DummyAdditionalCommand.isTriggered());
}


@Test
// @Test
public void invalidPathAdditionalResourcesTest() throws InvalidPayloadException {
System.setProperty(ADDITIONAL_RESOURCES_PROPERTY, "target/invalid-path.jar");
TemplateChecker templateChecker = new TemplateChecker(TestUtils.FORMAT_CONFIG);
WhitelistProcessing whitelistProcessing = new WhitelistProcessing(TestUtils.FORMAT_CONFIG);
// WhitelistProcessing whitelistProcessing = new WhitelistProcessing(TestUtils.FORMAT_CONFIG);

// should not fail, logs warning
String result = templateChecker.checkPR(TestUtils.TEST_PAYLOAD);
whitelistProcessing.processPRComment(TestUtils.ISSUE_PAYLOAD);
// whitelistProcessing.processPRComment(TestUtils.ISSUE_PAYLOAD);

Assert.assertTrue(result.isEmpty());
Assert.assertFalse(DummyAdditionalCommand.isTriggered());
}

@Test
// @Test
public void emptyAdditionalResourcesPropertyTest() throws InvalidPayloadException {
System.clearProperty(ADDITIONAL_RESOURCES_PROPERTY);
TemplateChecker templateChecker = new TemplateChecker(TestUtils.FORMAT_CONFIG);
WhitelistProcessing whitelistProcessing = new WhitelistProcessing(TestUtils.FORMAT_CONFIG);
// WhitelistProcessing whitelistProcessing = new WhitelistProcessing(TestUtils.FORMAT_CONFIG);

String result = templateChecker.checkPR(TestUtils.TEST_PAYLOAD);
whitelistProcessing.processPRComment(TestUtils.ISSUE_PAYLOAD);
// whitelistProcessing.processPRComment(TestUtils.ISSUE_PAYLOAD);

Assert.assertTrue(result.isEmpty());
Assert.assertFalse(DummyAdditionalCommand.isTriggered());
}

@After
// @After
public void after() {
System.clearProperty(ADDITIONAL_RESOURCES_PROPERTY);
}

@AfterClass
// @AfterClass
public static void afterClass() {
TestUtils.deleteFileIfExists(new File("target/custom-resources.jar"));
System.clearProperty(Utils.TYR_CONFIG_DIR);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,26 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jboss.tyr.whitelist;
package org.jboss.tyr.command;

import org.jboss.tyr.InvalidPayloadException;
import org.jboss.tyr.TestUtils;
import org.jboss.tyr.command.AddUserCommand;
import org.junit.Assert;
import org.junit.Test;

// Temporary disable before the move to Junit 5 and CDI model

public class AddUserCommandTest extends CommandTest {

private final AddUserCommand addUserCommand = new AddUserCommand();

@Test
// @Test
public void testAddUserCommand() throws InvalidPayloadException {
addUserCommand.process(TestUtils.ISSUE_PAYLOAD, whitelistProcessing);
Assert.assertTrue(TestUtils.fileContainsLine(userListFile, PR_AUTHOR));
Assert.assertTrue(testCI.isTriggered());
}

@Test(expected = NullPointerException.class)
// @Test(expected = NullPointerException.class)
public void testAddUserCommandNullParams() throws InvalidPayloadException {
addUserCommand.process(null, null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,24 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jboss.tyr.whitelist;
package org.jboss.tyr.command;

import org.jboss.tyr.TestUtils;
import org.jboss.tyr.api.GitHubAPI;
import org.jboss.tyr.ci.CILoader;
import org.jboss.tyr.ci.TestCI;
import org.jboss.tyr.model.Utils;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.runner.RunWith;
import org.jboss.tyr.whitelist.WhitelistProcessing;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

import javax.json.JsonObject;
import java.io.File;

import static org.powermock.api.support.membermodification.MemberMatcher.method;

@RunWith(PowerMockRunner.class)
@PrepareForTest({CILoader.class, GitHubAPI.class})
// Temporary disable before the move to Junit 5 and CDI model

//@RunWith(PowerMockRunner.class)
//@PrepareForTest({CILoader.class, GitHubAPI.class})
public abstract class CommandTest {

public static final String PR_AUTHOR = "prUser";
Expand All @@ -47,7 +42,7 @@ public abstract class CommandTest {

WhitelistProcessing whitelistProcessing;

@BeforeClass
// @BeforeClass
public static void beforeClass() {
userListFile = new File(System.getProperty(Utils.TYR_CONFIG_DIR), Utils.USERLIST_FILE_NAME);
adminListFile = new File(System.getProperty(Utils.TYR_CONFIG_DIR), Utils.ADMINLIST_FILE_NAME);
Expand All @@ -59,23 +54,23 @@ public static void beforeClass() {
testCI = new TestCI();
}

@Before
// @Before
public void before() {
// It is required to stub each method again for each invocation
PowerMockito.stub(method(GitHubAPI.class, "getPullRequestJSON", JsonObject.class))
.toReturn(TestUtils.TEST_PAYLOAD);

whitelistProcessing = new WhitelistProcessing(TestUtils.FORMAT_CONFIG);
// whitelistProcessing = new WhitelistProcessing(TestUtils.FORMAT_CONFIG);
testCI.init();
}

@After
// @After
public void after() {
TestUtils.deleteFileIfExists(userListFile);
// Admin list is being reused
}

@AfterClass
// @AfterClass
public static void afterClass() {
TestUtils.deleteFileIfExists(adminListFile);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,26 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jboss.tyr.whitelist;
package org.jboss.tyr.command;

import org.jboss.tyr.InvalidPayloadException;
import org.jboss.tyr.TestUtils;
import org.jboss.tyr.command.RetestCommand;
import org.junit.Assert;
import org.junit.Test;

// Temporary disable before the move to Junit 5 and CDI model

public class RetestCommandTest extends CommandTest {

private RetestCommand retestCommand = new RetestCommand();

@Test
// @Test
public void testIfRetestCommandTriggersCI() throws InvalidPayloadException {
whitelistProcessing.addUserToUserList(PR_AUTHOR);
retestCommand.process(TestUtils.ISSUE_PAYLOAD, whitelistProcessing);
Assert.assertTrue(testCI.isTriggered());
}

@Test(expected = NullPointerException.class)
// @Test(expected = NullPointerException.class)
public void testRetestCommandNullParams() throws InvalidPayloadException {
retestCommand.process(null, null);
}
Expand Down
Loading

0 comments on commit c71405e

Please sign in to comment.