How to get the current project in a project-centric-view (web-panel)?

Dear community,

in my Add-On, I am developing a new project-centric-view aka webpanel. In my webpanel, I need to know, what the current project or at least project id is but I am confused.

Every time, I ask for the values of “currentproject” in my related ActionClass, I get a null. But why?

My current code is:

@Override
public Map<String, Object> getContextMap(ApplicationUser applicationUser, JiraHelper jiraHelper)
{
Map<String, Object> contextMap = new HashMap<>();
String prjName;
//Issue currentIssue = (Issue) jiraHelper.getContextParams().get(“issue”);
Project currentProject = jiraHelper.getProject();
String pSize = jiraHelper.getContextParams().toString();
if (currentProject != null)
{
prjName = currentProject.getName();
}
contextMap.put(“projectname”, “test”);
contextMap.put(“size”, pSize);
//contextMap.put(“projectname”, “test”);
return contextMap;
}

The variable pSize returns
“{project=null, request=com.atlassian.plugin.servlet.PluginHttpRequestWrapper@70d40cfe}”

Am I doing something wrong?

Kind regards
Manuel

I’ve learned something new. I still have the problem, but maybe, I am on the right way.

As far as I understand:

1. A web-panel needs a context-provider
Okay! It’s defined. My context provider is my java action class.

2. A java action class has function named "getContextMap"
Okay! My java class has such an function with 2 parameter. applicationuser and jirahelper. So I would expect, to get all nesseccary functions and parameters by using jirahelper.

3. jirahelper has 4 different constructors
Very interesting. It looks like, that every part of jira which provides a jirahelper class decides, if its providing

  • nothing
  • a HttpServletRequest
  • a HttpServletRequest and a project
  • a HttpServletRequest, a project and a additional Map.

I don’t know why, so I ask you for your help, is my jirahelper is not providing a project, even my web-panel is located as project-centric-view inside of an project.

Any ideas what the reason is? Maybe i missed some setting or code?

Kind regards

Does anyone have an idea or a hint? Maybe @sfbehnke?
Kind regards!

To summarize my topic: i just need to know, which project is selected in my project-centric-view “web-panel” and it seems that there is no official way which I can’t believe. “JiraHelper” offers me the “getCurrentProject” function, but it is always null. I’ve found a workaround, but I am not very happy with that because I think, there must be a better and of cource official way by atlassian. Every project centric view offers project related data so every person who develops a project centric view needs to know what the current project is.

My work around: in my add-on xml I provide the current project by adding “selectedProjectKey=$projectKey” to the linkId of my web-item link. Inside my action class, I read the URL parameter by

String s2 = jiraHelper.getRequest().getParameter(“selectedProjectKey”);

That’s it, but - like I said - it looks not very professional. :-/

An example pillaged from one of our existing plugins.

Web Item

    <web-item key="${project.artifactId}-web-item" name="Project Sidebar Link" section="jira.project.sidebar.plugins.navigation" weight="1000">
        <label>MyLabel</label>
        <link linkId="${project.artifactId}-sidebar-link">/projects/$pathEncodedProjectKey?selectedItem=${project.groupId}.${project.artifactId}:${project.artifactId}-project</link>
        <param name="iconClass" value="aui-icon-large aui-iconfont-group"/>
    </web-item>

Web Panel

    <web-panel key="${project.artifactId}-web-panel" name="Project-Centric Panel" location="${project.groupId}.${project.artifactId}:${project.artifactId}-project-screen">
        <context-provider class="com.spacex.jira.MyContextProvider"/>
        <resource name="view" type="velocity" location="/templates/template.vm"/>
    </web-panel>

Context Provider

@Named
public class MyContextProvider implements ContextProvider {
    private final MyCustomData customData;
    @ComponentImport
    private final AvatarService avatarService;
    @ComponentImport
    private final JiraAuthenticationContext jiraAuthenticationContext;
    @ComponentImport
    private final ProjectManager projectManager;

    @Inject
    MyContextProvider(MyCustomData customData, AvatarService avatarService, JiraAuthenticationContext jiraAuthenticationContext, ProjectManager projectManager) {
        this.customData = customData;
        this.avatarService = avatarService;
        this.jiraAuthenticationContext = jiraAuthenticationContext;
        this.projectManager = projectManager;
    }

    @Override
    public void init(Map<String, String> map) throws PluginParseException {

    }

    @Override
    public Map<String, Object> getContextMap(Map<String, Object> map) {
        Project currentProject = projectManager.getProjectByCurrentKey((String) map.get("projectKey"));

        Map<String,Object> contextMap = new HashMap<>();
        contextMap.put("customData",customData.getBeans(currentProject));
        contextMap.put("avatarService", avatarService);
        contextMap.put("avatarSize", Avatar.Size.SMALL);
        contextMap.put("loggedInUser", jiraAuthenticationContext.getLoggedInUser());
        contextMap.put("projectLead", currentProject.getProjectLead());

        return contextMap;
    }
}
2 Likes

Thanks a lot! You helped me a lot. :grinning:

1 Like

I just found this example and it helped me a lot. But it seems that there is a small error:

The location of the web panel should match the link of the web item. There is a “-screen” too much in the location element of the web panel. After I removed this it worked.

Did you find an answer to this problem about the jiraHelper? I am using this in a custom condition and I have the same problem.

Sorry! I must have broken it when I copy+pasted and tried to redact any sensitive information.

Can you raise a new question and ping me referring to the JiraHelper class?