Passing arguments from WebItem to WebPanel

Hi,

I am creating a new page, accessible from the project side bar.
I used a WebItem to display the link and a WebPanel to render the page.

The problem is, I need the project object (or project key) in the contextProvider of the WebPanel.
I created a custom contextProvider that implements AbstractJiraContextProvider. This provides a JiraHelper object. However the getProject() call on this helper returns null.

I suspect I will need to pass the project key as a parameter from the WebItem to the WebPanel, but it is unclear to me how I would do this. The documentation does not provide a straight answer.

Any suggestions?

The Atlassian-plugin.xml items

    <web-item key="overview-menu-item" name="Upsource Overview Link" 
              section="jira.project.sidebar.plugins.navigation"
              weigth="30" application="jira">
        <desctiption>Link to the upsource connectivity overview page for this project</desctiption>
        <label key="upsource-connectivity.overview.label"/>
        <link linkId="upsource-connectivity-overview-link">/projects/$pathEncodedProjectKey?selectedItem=com.atlassian.jira.jira-projects-plugin:upsource-overview-page</link>
        <param name="iconClass" value="aui-icon-large icon-sidebar-issues"/>
    </web-item>
  
    <web-panel key="upsource-overview-panel" location="com.atlassian.jira.jira-projects-plugin:upsource-overview-page">
        <description>Project overview of open upsource reviews</description>
        <resource name="view" type="velocity" location="templates/OverviewPanel.vsl"/>
        <context-provider class="com.nikon.developmenttools.impl.OverviewContextProvider"/>
        
    </web-panel>

The context Provider

public class OverviewContextProvider extends AbstractJiraContextProvider {

    @Inject
    private UpsourceConnectivityExtentionMethods methodContainer;
    
    public OverviewContextProvider(){}

    public UpsourceConnectivityExtentionMethods getMethodContainer() {
        return methodContainer;
    }

    public void setMethodContainer(UpsourceConnectivityExtentionMethods methodContainer) {
        this.methodContainer = methodContainer;
    }
    
    @Override
    public Map getContextMap(ApplicationUser au, JiraHelper jh) {
        Map contextMap = new HashMap();
        List<String> exceptionMessages = new ArrayList<String>();
        try{
            Project currentProject = (Project) (jh.getProject());
            if(currentProject == null){
                String message = "it's the currentproject"
                        + "\nqs: " + jh.getQueryString()
                        + "\ncontextparam: "+ jh.getContextParams().toString();
                throw new Exception(message);
            }
            UpsourceConfig uc = this.methodContainer.loadConfig(currentProject.getKey());
            if(uc == null){
                throw new Exception("its the uc");
            }
            HashMap<String, String> ucProperties = new HashMap<String, String>();
            ucProperties.put("host", uc.getHost());
            ucProperties.put("user", uc.getUsername());
            ucProperties.put("upsourceId", uc.getUpsourceProjectId());
            contextMap.put("config", ucProperties);
            ResultDTO result = this.methodContainer.getReviews(currentProject);
            if(result == null || result.getResult() == null){
                throw new Exception("its the result");
            }
            ReviewsDTO response = (ReviewsDTO) result.getResult();
            contextMap.put("reviewsDTO", response);
        }catch (UpsourceConnectionFailureException ucfe) {
            System.err.println("UPSOURCE CONNECTION EXCEPTION: " + ucfe.getMessage());
            if (ucfe.getCause() != null && ucfe.getCause().getCause() instanceof java.net.UnknownHostException) {
                exceptionMessages.add("Error resolving host, please check the Upsource Connectivity Settings for this project and your network settings.");
            } else {
                exceptionMessages.add(ucfe.toString());
            }
        } catch (UpsourceUnsetConfigException uuce) {
            System.err.println("UPSOURCE CONFIGRATION EXCEPTION: " + uuce.getMessage());
            exceptionMessages.add(uuce.getMessage());
        } catch (UpsourceConnectivityException uce) {
            System.err.println("UPSOURCE EXCEPTION: " + uce.getMessage());
            if (uce.getCause() != null) {
                exceptionMessages.add(uce.getCause().toString());
            } else {
                exceptionMessages.add(uce.toString());
            }
        } catch (Exception ex) {
            exceptionMessages.add(ex.toString());
            System.err.println("EXCEPTION: " + ex.toString());
        }
        contextMap.put("exceptionList", exceptionMessages);
        return contextMap;
    }   
}

Alternatively, it would be possible to parse the project key out of the request URL.

This seems a workable, if inelegant, way of accomplishing my goal.

1 Like

Hi,

Just have fight with the same problem and not possible find any solutions. Just how you mentioned to parse project key
from the request URI or URL. If somebody would find more user-friendly solutions then this, I will appreciate it.

Thanks

In the end, I just parsed it out of the url.

            import java.util.regex.*;

           [......]

            Project currentProject = (Project) (jh.getProject());
            String parseResult = "";
            if(currentProject == null){ 
                String target = jh.getRequest().getRequestURI();
                Pattern p = Pattern.compile("\\w+$");
                Matcher m = p.matcher(target);
                m.find();
                parseResult = m.group();
                currentProject = this.projectManager.getProjectObjByKey(parseResult);
            }

I just used regex in the end. The pattern might require some tweaking. Im no longer on that project though, so I wont be able to elaborate further.