Skip to content

Commit

Permalink
aprey10#17 Add condition to web-panel; refactor code; Styling
Browse files Browse the repository at this point in the history
  • Loading branch information
mfrank-decadis committed Jan 17, 2023
1 parent 0ca5e7a commit 9d53dd9
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 55 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.aprey.jira.plugin.openpoker.api.config;

import java.util.List;

import com.atlassian.jira.plugin.webfragment.conditions.AbstractWebCondition;
import com.atlassian.jira.plugin.webfragment.model.JiraHelper;
import com.atlassian.jira.user.ApplicationUser;

/**
* Class that defines a condition for the web fragment visibility
*/
public class AllowedProjectsCondition extends AbstractWebCondition {

/**
* Method to determine whether the web fragment should be displayed or not
* @param applicationUser The user currently viewing the page
* @param jiraHelper The JiraHelper that provides access to various Jira-related objects
* @return boolean value indicating whether the web fragment should be displayed or not
*/
@Override
public boolean shouldDisplay(ApplicationUser applicationUser, JiraHelper jiraHelper) {
List<String> allowedProjects = ConfigurationManager.getStoredAllowedProjects();
if ( allowedProjects.isEmpty() ) {
return true;
}
if ( jiraHelper.getProject() != null ) {
return allowedProjects.contains(jiraHelper.getProject().getKey());
}
return true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.aprey.jira.plugin.openpoker.api.config;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.stream.Collectors;

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.sal.api.pluginsettings.PluginSettings;
import com.atlassian.sal.api.pluginsettings.PluginSettingsFactory;

public abstract class ConfigurationManager {
private static PluginSettingsFactory pluginSettingsFactory = ComponentAccessor.getOSGiComponentInstanceOfType(PluginSettingsFactory.class);

//Key for plugin settings
private static String OPEN_POKER_KEY = "com.aprey.jira.plugin.openpoker";

/**
* Method to get the plugin settings
*
* @return pluginSettings instance
*/
public static PluginSettings getPluginSettings() {
PluginSettingsFactory pluginSettingsFactory = ComponentAccessor.getOSGiComponentInstanceOfType(PluginSettingsFactory.class);
PluginSettings pluginSettings = pluginSettingsFactory.createSettingsForKey(OPEN_POKER_KEY);
return pluginSettings;
}

/**
* Method to get the stored allowed projects from plugin settings
*
* @return ArrayList of allowed projects
*/
public static ArrayList<String> getStoredAllowedProjects() {
PluginSettings pluginSettings = getPluginSettings();
String storedProjects = ((String) pluginSettings.get("allowedProjects"));
if ( storedProjects != null && !storedProjects.isEmpty() ) {
return Arrays.stream(storedProjects.split(",")).collect(Collectors.toCollection(ArrayList::new));
}
return new ArrayList<>();
}

/**
* Method to store the updated allowed projects in plugin settings
*
* @param updatedAllowedProjects String of allowed projects
*/
public static void storeAllowedProjects(String updatedAllowedProjects) {
PluginSettings pluginSettings = getPluginSettings();
pluginSettings.put("allowedProjects", updatedAllowedProjects);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,27 @@

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.project.Project;
import com.atlassian.jira.security.request.RequestMethod;
import com.atlassian.jira.security.request.SupportedMethods;
import com.atlassian.jira.security.xsrf.DoesNotRequireXsrfCheck;
import com.atlassian.jira.web.action.JiraWebActionSupport;
import com.atlassian.sal.api.pluginsettings.PluginSettings;
import com.atlassian.sal.api.pluginsettings.PluginSettingsFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import webwork.action.Action;
import webwork.action.ServletActionContext;

import static com.aprey.jira.plugin.openpoker.api.config.ConfigurationManager.getStoredAllowedProjects;
import static com.aprey.jira.plugin.openpoker.api.config.ConfigurationManager.storeAllowedProjects;

@SupportedMethods({ RequestMethod.GET })
public class PokerConfigPage extends JiraWebActionSupport {

//Logger instance for the class
private static Logger log = LoggerFactory.getLogger(PokerConfigPage.class);
//Key for plugin settings
private static String OPEN_POKER_KEY = "com.aprey.jira.plugin.openpoker";
//List of allowed projects
private List<String> allowedProjects = new ArrayList<>();

Expand All @@ -38,19 +35,6 @@ public String doDefault() throws Exception {
return Action.INPUT;
}

/**
* Method to get the stored allowed projects from plugin settings
* @return ArrayList of allowed projects
*/
private static ArrayList<String> getStoredAllowedProjects() {
PluginSettings pluginSettings = getPluginSettings();
String storedProjects = ((String) pluginSettings.get("allowedProjects"));
if ( storedProjects != null ) {
return Arrays.stream(storedProjects.split(",")).collect(Collectors.toCollection(ArrayList::new));
}
return new ArrayList<>();
}

/**
* Method is called when the form is submitted
*/
Expand All @@ -69,35 +53,15 @@ protected String doExecute() throws Exception {

/**
* Method to get the updated allowed projects from the form
*
* @return String of allowed projects
*/
private static String getUpdatedAllowedProjects() {
HttpServletRequest request
= ServletActionContext.getRequest();
HttpServletRequest request = ServletActionContext.getRequest();
String updatedAllowedProjects = request.getParameter("allowedProjects");
return updatedAllowedProjects;
}

/**
* Method to store the updated allowed projects in plugin settings
* @param updatedAllowedProjects String of allowed projects
*/
private static void storeAllowedProjects(String updatedAllowedProjects) {
PluginSettings pluginSettings = getPluginSettings();
pluginSettings.put("allowedProjects", updatedAllowedProjects);
}

/**
* Method to get the plugin settings
* @return pluginSettings instance
*/
private static PluginSettings getPluginSettings() {
PluginSettingsFactory pluginSettingsFactory = ComponentAccessor.getOSGiComponentInstanceOfType(PluginSettingsFactory.class);
PluginSettings pluginSettings = pluginSettingsFactory.createSettingsForKey(OPEN_POKER_KEY);
return pluginSettings;
}


public List<String> getAllowedProjects() {
return allowedProjects;
}
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/atlassian-plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
<context-provider class="com.aprey.jira.plugin.openpoker.api.PlanningPokerWebPanelProvider"/>
<resource name="view" type="velocity" location="templates/planning-poker.vm"/>
<label key="planning-poker.title"/>
<condition class="com.aprey.jira.plugin.openpoker.api.config.AllowedProjectsCondition"/>
</web-panel>

<ao key="ao-module">
Expand Down
14 changes: 9 additions & 5 deletions src/main/resources/js/config/open-poker-config.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
AJS.$(document).ready(() => {
const initialize = () => {
let $allowedProjectsSelect = AJS.$("#allowedProjectsSelect");
$allowedProjectsSelect.auiSelect2();
$allowedProjectsSelect.auiSelect2(
{
placeholder: "Open poker will be available for each project",
allowClear: true
});

$allowedProjectsSelect.on("change", () => {
AJS.$("#allowedProjects").val(AJS.$("#allowedProjectsSelect").val())
})
}
AJS.$("#allowedProjects").val(AJS.$("#allowedProjectsSelect").val());
});
};

initialize();
})
});
16 changes: 7 additions & 9 deletions src/main/resources/templates/config/open-poker-config.vm
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,13 @@ $webResourceManager.requireResource("com.aprey.jira.plugin.openpoker.open-poker:
<form action="$action.getURL()" class="aui" method="POST">
<input type="hidden" id="allowedProjects" name="allowedProjects" value="$action.getAllowedProjectsValue()"/>
<fieldset>
<div class="field-group">
<label for="allowedProjects" class="paramName">Used projects:</label>
<select id="allowedProjectsSelect" name="allowedProjectsSelect" style="padding: 5px 0 0 0" multiple>
#foreach ($project in $projects)
<option #if(${allowedProjects.contains(${project.getKey()})}) selected #end value="${project.getKey()}">${project.getName()} (${project.getKey()})</option>
#end
</select>
<div class="description">Define the projects, where open poker should be rendered on the issue view. If no project is selected, it will be enabled for all projects.</div>
</div>
<label for="allowedProjects" class="paramName">Projects:</label>
<select id="allowedProjectsSelect" name="allowedProjectsSelect" style="padding: 5px 0 0 0" multiple>
#foreach ($project in $projects)
<option #if(${allowedProjects.contains(${project.getKey()})}) selected #end value="${project.getKey()}">${project.getName()} (${project.getKey()})</option>
#end
</select>
<div class="description">Define the projects, where open poker should be rendered on the issue view. If no project is selected, it will be enabled for all projects.</div>
</fieldset>

<div class="buttons-container" style="padding: 10px 0 4px 0;">
Expand Down

0 comments on commit 9d53dd9

Please sign in to comment.