Skip to content

Commit

Permalink
ConDec-347: Enable multiselect for webhook root elements (#55)
Browse files Browse the repository at this point in the history
* CONDEC-347: Make root element selector multi select

* CONDEC-347: Indent code correctly

* Add event listener vor root type multiselect

* CONDEC-347: Add rootTypes to velocity page

* CONDEC-347: Update test cases

* CONDEC-347: Make string comparison case insensitive

* CONDEC-347: Fix wrong method call

* CONDEC-347: Remove unused imports
  • Loading branch information
ematios authored Nov 7, 2018
1 parent 557814f commit de84392
Show file tree
Hide file tree
Showing 11 changed files with 87 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import de.uhd.ifi.se.decision.management.jira.ComponentGetter;
import de.uhd.ifi.se.decision.management.jira.model.DecisionKnowledgeProject;
import de.uhd.ifi.se.decision.management.jira.model.DecisionKnowledgeProjectImpl;
import de.uhd.ifi.se.decision.management.jira.persistence.ConfigPersistence;

/**
* Renders the administration page to change the plug-in configuration of a
Expand Down Expand Up @@ -68,6 +69,7 @@ protected Map<String, Object> getVelocityParameters(HttpServletRequest request)
velocityParameters.put("issueTypes", issueTypes);
velocityParameters.put("imageFolderUrl", ComponentGetter.getUrlOfImageFolder());
velocityParameters.put("requestUrl", request.getRequestURL());
velocityParameters.put("rootTypes", ConfigPersistence.getEnabledWebhookTypes(projectKey));

return velocityParameters;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public String getTypeAsString() {
}
IssueManager issueManager = ComponentAccessor.getIssueManager();
Issue issue = issueManager.getIssueByCurrentKey(this.getKey());
return issue.getIssueType().toString();
return issue.getIssueType().getName();
}
return type.toString();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ static AbstractPersistenceStrategy getPersistenceStrategy(String projectKey) {
*
* @return type of webhook root element.
*/
String getWebhookRootType();
boolean isWebhookTypeEnabled(String issueType);

/**
* Checks if is icon parsing enabled.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,11 @@ public String getWebhookSecret() {
}

@Override
public String getWebhookRootType() {
return ConfigPersistence.getWebhookType(projectKey);
public boolean isWebhookTypeEnabled(String issueType) {
return ConfigPersistence.isWebhookTypeEnaled(projectKey, issueType);
}


@Override
public boolean isIconParsingEnabled() {
return ConfigPersistence.isIconParsing(projectKey);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package de.uhd.ifi.se.decision.management.jira.persistence;

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.config.IssueTypeManager;
import com.atlassian.jira.issue.issuetype.IssueType;
import com.atlassian.sal.api.pluginsettings.PluginSettings;
import com.atlassian.sal.api.pluginsettings.PluginSettingsFactory;
import com.atlassian.sal.api.transaction.TransactionCallback;
Expand All @@ -8,6 +11,9 @@
import de.uhd.ifi.se.decision.management.jira.ComponentGetter;
import de.uhd.ifi.se.decision.management.jira.model.KnowledgeType;

import java.util.ArrayList;
import java.util.Collection;

/**
* Class to store and receive configuration settings
*/
Expand Down Expand Up @@ -161,13 +167,27 @@ public static boolean isWebhookEnabled(String projectKey) {
}

// TODO Testing
public static void setWebhookType(String projectKey, String webhookType) {
setValue(projectKey, "webhookType", webhookType);
public static void setWebhookType(String projectKey, String webhookType, boolean isWebhookTypeEnabled) {
setValue(projectKey, "webhookType" + "." + webhookType, Boolean.toString(isWebhookTypeEnabled));
}

// TODO Testing
public static boolean isWebhookTypeEnaled(String projectKey, String webhookType) {
String isWebhookTypeEnabled = getValue(projectKey, "webhookType" + "." + webhookType);
return "true".equals(isWebhookTypeEnabled);
}

// TODO Testing
public static String getWebhookType(String projectKey) {
return getValue(projectKey, "webhookType");
public static Collection<String> getEnabledWebhookTypes(String projectKey) {
IssueTypeManager issueTypeManager = ComponentAccessor.getComponent(IssueTypeManager.class);
Collection<IssueType> issueTypes = issueTypeManager.getIssueTypes();
Collection<String> issueTypeNames = new ArrayList<>();
for (IssueType issueType: issueTypes) {
if (isWebhookTypeEnaled(projectKey, issueType.getName())) {
issueTypeNames.add(issueType.getName());
}
}
return issueTypeNames;
}

public static void setRequestToken(String requestToken) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,8 @@ public Response setWebhookData(@Context HttpServletRequest request,
@Path("/setWebhookType")
@POST
public Response setWebhookType(@Context HttpServletRequest request,
@QueryParam("projectKey") final String projectKey, @QueryParam("webhookType") final String webhookType) {
@QueryParam("projectKey") final String projectKey, @QueryParam("webhookType") final String webhookType,
@QueryParam("isWebhookTypeEnabled") final boolean isWebhookTypeEnabled) {
Response isValidDataResponse = checkIfDataIsValid(request, projectKey);
if (isValidDataResponse.getStatus() != Status.OK.getStatusCode()) {
return isValidDataResponse;
Expand All @@ -320,7 +321,7 @@ public Response setWebhookType(@Context HttpServletRequest request,
return Response.status(Status.BAD_REQUEST).entity(ImmutableMap.of("error", "webhook Type = null")).build();
}
try {
ConfigPersistence.setWebhookType(projectKey, webhookType);
ConfigPersistence.setWebhookType(projectKey, webhookType, isWebhookTypeEnabled);
return Response.ok(Status.ACCEPTED).build();
} catch (Exception e) {
LOGGER.error(e.getMessage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.apache.commons.httpclient.HttpClient;
Expand All @@ -26,23 +27,24 @@ public class WebhookConnector {
private String secret;
private String projectKey;
private List<Long> elementIds;
private String rootType;
private Collection<String> rootTypes;

public WebhookConnector(String projectKey, String webhookUrl, String webhookSecret, String rootType) {
public WebhookConnector(String projectKey, String webhookUrl, String webhookSecret, Collection rootTypes) {
this.projectKey = projectKey;
this.url = webhookUrl;
this.secret = webhookSecret;
if (rootType == null) {
this.rootType = "Task";
this.rootTypes = new ArrayList<>();
if (rootTypes == null || rootTypes.isEmpty()) {
this.rootTypes.add("Task");
} else {
this.rootType = rootType;
this.rootTypes = rootTypes;
}
this.elementIds = new ArrayList<Long>();
}

public WebhookConnector(String projectKey) {
this(projectKey, ConfigPersistence.getWebhookUrl(projectKey), ConfigPersistence.getWebhookSecret(projectKey),
ConfigPersistence.getWebhookType(projectKey));
ConfigPersistence.getEnabledWebhookTypes(projectKey));
}

public boolean sendElementChanges(DecisionKnowledgeElement changedElement) {
Expand All @@ -62,8 +64,11 @@ public boolean deleteElement(DecisionKnowledgeElement elementToBeDeleted, Applic

List<DecisionKnowledgeElement> rootElements = getWebhookRootElements(elementToBeDeleted);
String type = elementToBeDeleted.getTypeAsString();
if (type.toUpperCase().equals(rootType.toUpperCase())) {
rootElements.remove(elementToBeDeleted);

for (String rootType: rootTypes) {
if (rootType.equalsIgnoreCase(type)) {
rootElements.remove(elementToBeDeleted);
}
}

AbstractPersistenceStrategy strategy = StrategyProvider.getPersistenceStrategy(projectKey);
Expand Down Expand Up @@ -95,8 +100,10 @@ private List<DecisionKnowledgeElement> getWebhookRootElements(DecisionKnowledgeE
}
elementIds.add(linkedElement.getId());
String type = linkedElement.getTypeAsString();
if (type.equalsIgnoreCase(rootType)) {
webhookRootElements.add(linkedElement);
for (String rootType: rootTypes) {
if (rootType.equalsIgnoreCase(type)) {
webhookRootElements.add(linkedElement);
}
}
webhookRootElements.addAll(getWebhookRootElements(linkedElement));
}
Expand Down
5 changes: 3 additions & 2 deletions src/main/resources/js/rest.client.js
Original file line number Diff line number Diff line change
Expand Up @@ -738,9 +738,10 @@
/*
external references: settingsForSingleProject.vm ..
*/
ConDecAPI.prototype.setWebhookType = function setWebhookType(webhookType, projectKey) {
ConDecAPI.prototype.setWebhookType = function setWebhookType(webhookType, projectKey, isWebhookTypeEnabled) {
postJSON(AJS.contextPath() + "/rest/decisions/latest/config/setWebhookType.json?projectKey=" + projectKey
+ "&webhookType=" + webhookType, null, function(error, response) {
+ "&webhookType=" + webhookType + "&isWebhookTypeEnabled=" + isWebhookTypeEnabled,
null, function(error, response) {
if (error === null) {
showFlag("success", "The webhook root element type was changed for this project.");
} else {
Expand Down
47 changes: 30 additions & 17 deletions src/main/resources/templates/settingsForSingleProject.vm
Original file line number Diff line number Diff line change
Expand Up @@ -136,22 +136,21 @@ $webResourceManager.requireResource("de.uhd.ifi.se.decision.management.jira:styl
The key is the key of the root element and the value is the Treant JSON string.
</div>
</div>
<div class="field-group">
<label for="webhook-type">Root Element Type:</label>
<select id="webook-type" name="webhook-type" class="select medium-field"
onchange="conDecAPI.setWebhookType(this.options[this.selectedIndex].value, '$projectKey')"></select>
#foreach ($issueType in $issueTypes)
<script>
var isSelected = "";
if ('$issueType'.localeCompare('$project.getWebhookRootType()') == 0) {
isSelected = "selected ";
}
$("select[name='webhook-type']")[0].insertAdjacentHTML("beforeend", "<option " + isSelected + "value='"
+ '$issueType' + "'>" + '$issueType' + "</option>");
</script>
#end
<div class="description">Selects the issue type of the root element (key) of the webhook data.</div>
</div>
<div class="field-group">
<label for="select-root-types">Root Element Types:</label>
<select class="multi-select" size="8" multiple="multiple" id="select-root-types" name="select-root-types">
#foreach ($issueType in $issueTypes)
<option name="$issueType"
#if ( $rootTypes.contains($issueType) )
selected
#end
>
$issueType</option>
#end
</select>
<div class="description">Selects the issue type of the root element (key) of the webhook data.</div>
<input type="button" value="Set Root Element Types" onclick="getMultipleSelectedValue()" class="aui-button aui-button-primary"/>
</div>
<div class="field-group">
<label for="webhook-url">URL</label> <input type="text"
class="text long-field" id="webhook-url" value="$project.getWebhookUrl()" />
Expand Down Expand Up @@ -275,12 +274,26 @@ $webResourceManager.requireResource("de.uhd.ifi.se.decision.management.jira:styl
conDecAPI.setKnowledgeExtractedFromGit(this.checked, this.value);
this.busy = false;
});

webhookToggle.addEventListener('change', function(error) {
this.busy = true;
conDecAPI.setWebhookEnabled(this.checked, this.value);
this.busy = false;
});

function getMultipleSelectedValue()
{
var x = document.getElementById("select-root-types");
for (var i = 0; i < x.options.length; i++) {
if(x.options[i].selected == true){
conDecAPI.setWebhookType(x.options[i].value, '$projectKey', "true");
}
else {
conDecAPI.setWebhookType(x.options[i].value, '$projectKey', "false");
}
}
}

</script>
</form>
</body>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,6 @@ public void testGetVelocityParametersNull() {
@Test
public void testGetVelocityParametersFilled() {
request.setAttribute("projectKey", "TEST");
assertEquals(5, servlet.getVelocityParameters(request).size());
assertEquals(6, servlet.getVelocityParameters(request).size());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public class ActiveObjectStrategyTestSetUp {
private ProjectManager projectManager;
private IssueManager issueManager;
private ConstantsManager constantsManager;

private IssueTypeManager issueTypeManager = new MockIssueTypeManager();

public void initialisation() {

Expand All @@ -51,6 +51,7 @@ public void initialisation() {
.addMock(VelocityParamFactory.class, new MockVelocityParamFactory())
.addMock(IssueTypeSchemeManager.class, new MockIssueTypeSchemeManager())
.addMock(PluginSettingsFactory.class, new MockPluginSettingsFactory())
.addMock(IssueTypeManager.class, issueTypeManager)
.addMock(OptionSetManager.class, new MockOptionSetManager()).addMock(CommentManager.class, new MockCommentManager());

ActiveObjects activeObjects = new TestActiveObjects(entityManager);
Expand Down

0 comments on commit de84392

Please sign in to comment.