How to get the current project's ID in Jira 7.2

Hasnae E July 31, 2017

Hello,

It's been a while since i tried to figure out how to get current jira project id.

I've searched everywhere and i've tried all these methods :

Issue.GetProjectObject().getKey();

projectModelSelectionPanel.getSelectedProjectId();

ActionContext.getParameters().get("pid");

ParameterUtils.getLongParam(params, "selectedProjectId");

but nothing worked for me. Can anyone give me an appropriate method to use in jira 7.2?

Thanks.

3 answers

1 accepted

1 vote
Answer accepted
Hasnae E August 17, 2017

I've found a solution.

I get the current issue id in the context provider of my web panel by this method:

public Map getContextMap(ApplicationUser applicationUser, JiraHelper jiraHelper) {
Map contextMap = new HashMap();
Issue currentIssue = (Issue) jiraHelper.getContextParams().get("issue");
Long issueId = currentIssue.getId();
int Id = issueId.intValue();
contextMap.put("Ids", Id);
return contextMap;
}

 and then i send this Id to my servlet, which is the web panel's resource, in atlassian-plugin.xml like that:

<resource name="view" type="velocity"><![CDATA[<iframe src="/plugins/servlet/issuecrud?issueId=${Ids}"></iframe>]]></resource>

0 votes
Hasnae E July 31, 2017

Thanks.

Yes I've tried it before with .getId() but it gives me the same error.

I am trying to run my code in my servlet doGet method.. here is a bit of code:

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out=response.getWriter();
Long projectId = issue.getProjectObject().getId();
int project = projectId.intValue(); 
List<Env> list=EmpDao.getEnvironment(project);
Map<String, Object> context = Maps.newHashMap();
context.put("list", list);
response.setContentType("text/html;charset=utf-8");
templateRenderer.render("/templates/reference-gh-issue-details-panel-1.vm", context, response.getWriter());
}

I want to send the project Id to getEnvironment's query:

public static List<Env> getEnvironment(int project){
List<Env> list=new ArrayList<Env>();

try{
Connection con=EmpDao.getConnection();
PreparedStatement ps=con.prepareStatement("select distinct name, version, vname from environment, projectversion where version=vname and project =?");
ps.setInt(1,project);

ResultSet rs=ps.executeQuery();
while(rs.next()){
Env en=new Env();
en.setName(rs.getString(2));
list.add(en);
}
con.close();
}catch(Exception e){e.printStackTrace();}

return list;
}

and the result is a list i send to .vm file: 

#foreach($en in $list)
<blockquote>
$en.getName()
</blockquote>
#end

 

this is gonna display my environments list in a web panel i've created.

Everything works correctly if i enter manually the project Id, i'm just stick with how can I get the current project id.. i don't know what i am missing

0 votes
Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
July 31, 2017

issue.getProjectObject().getKey()

is working for me - returns the key of the project as a string

Hasnae E July 31, 2017

Thank you for your answer sir but actually i don't want the project's key, in fact it's the Id that i am looking for.

I've tried your method and it gives me the following error :  cannot find symbol. I guess that i should initialize the issue variable or something but it's not the moment it seems to take longer to get to a project key and then the Id.. I mean why not getting to the id directly?

I wonder why these lines give me a 500 error after building and activating my plugin in the jira server

String[] ids = (String[]) ActionContext.getParameters().get("pid");
String pid = ids[0];

Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
July 31, 2017

getId() instead of getKey() will work.

But it sounds like you have a context problem too - where exactly are you trying to run this code?

Hasnae E July 31, 2017

Thanks.

Yes I've tried it before with .getId() but it gives me the same error.

I am trying to run my code in my servlet doGet method.. here is a bit of code:

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out=response.getWriter();
Long projectId = issue.getProjectObject().getId();
int project = projectId.intValue(); 
List<Env> list=EmpDao.getEnvironment(project);
Map<String, Object> context = Maps.newHashMap();
context.put("list", list);
response.setContentType("text/html;charset=utf-8");
templateRenderer.render("/templates/reference-gh-issue-details-panel-1.vm", context, response.getWriter());
}

I want to send the project Id to getEnvironment's query:

public static List<Env> getEnvironment(int project){
List<Env> list=new ArrayList<Env>();

try{
Connection con=EmpDao.getConnection();
PreparedStatement ps=con.prepareStatement("select distinct name, version, vname from environment, projectversion where version=vname and project =?");
ps.setInt(1,project);

ResultSet rs=ps.executeQuery();
while(rs.next()){
Env en=new Env();
en.setName(rs.getString(2));
list.add(en);
}
con.close();
}catch(Exception e){e.printStackTrace();}

return list;
}

and the result is a list i send to .vm file: 

#foreach($en in $list)
<blockquote>
$en.getName()
</blockquote>
#end

 

this is gonna display my environments list in a web panel i've created.

Everything works correctly if i enter manually the project Id, i'm just stick with how can get the current project id.. i don't know what i am missing..

Hasnae E July 31, 2017

Thanks.

Yes I've tried it before with .getId() but it gives me the same error.

I am trying to run my code in my servlet doGet method.. here is a bit of code:

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out=response.getWriter();
Long projectId = issue.getProjectObject().getId();
int project = projectId.intValue(); 
List<Env> list=EmpDao.getEnvironment(project);
Map<String, Object> context = Maps.newHashMap();
context.put("list", list);
response.setContentType("text/html;charset=utf-8");
templateRenderer.render("/templates/reference-gh-issue-details-panel-1.vm", context, response.getWriter());
}

I want to send the project Id to getEnvironment's query:

public static List<Env> getEnvironment(int project){
List<Env> list=new ArrayList<Env>();

try{
Connection con=EmpDao.getConnection();
PreparedStatement ps=con.prepareStatement("select distinct name, version, vname from environment, projectversion where version=vname and project =?");
ps.setInt(1,project);

ResultSet rs=ps.executeQuery();
while(rs.next()){
Env en=new Env();
en.setName(rs.getString(2));
list.add(en);
}
con.close();
}catch(Exception e){e.printStackTrace();}

return list;
}

and the result is a list i send to .vm file: 

#foreach($en in $list)
<blockquote>
$en.getName()
</blockquote>
#end

 

this is gonna display my environments list in a web panel i've created.

Everything works correctly if i enter manually the project Id, i'm just stick with how can I get the current project id.. i don't know what i am missing..

Hasnae E July 31, 2017

Thanks.

Yes I've tried it before with .getId() but it gives me the same error.

I am trying to run my code in my servlet doGet method.. here is a bit of code:

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out=response.getWriter();
Long projectId = issue.getProjectObject().getId();
int project = projectId.intValue(); 
List<Env> list=EmpDao.getEnvironment(project);
Map<String, Object> context = Maps.newHashMap();
context.put("list", list);
response.setContentType("text/html;charset=utf-8");
templateRenderer.render("/templates/reference-gh-issue-details-panel-1.vm", context, response.getWriter());
}

I want to send the project Id to getEnvironment's query:

public static List<Env> getEnvironment(int project){
List<Env> list=new ArrayList<Env>();

try{
Connection con=EmpDao.getConnection();
PreparedStatement ps=con.prepareStatement("select distinct name, version, vname from environment, projectversion where version=vname and project =?");
ps.setInt(1,project);

ResultSet rs=ps.executeQuery();
while(rs.next()){
Env en=new Env();
en.setName(rs.getString(2));
list.add(en);
}
con.close();
}catch(Exception e){e.printStackTrace();}

return list;
}

and the result is a list i send to .vm file: 

#foreach($en in $list)
<blockquote>
$en.getName()
</blockquote>
#end

 

this is gonna display my environments list in a web panel i've created.

Everything works correctly if i enter manually the project Id, i'm just stick with how can I get the current project id.. i don't know what i am missing..

Hasnae E July 31, 2017

Thanks.

Yes I've tried it before with .getId() but it gives me the same error.

I am trying to run my code in my servlet doGet method.. here is a bit of code:

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out=response.getWriter();
Long projectId = issue.getProjectObject().getId();
int project = projectId.intValue(); 
List<Env> list=EmpDao.getEnvironment(project);
Map<String, Object> context = Maps.newHashMap();
context.put("list", list);
response.setContentType("text/html;charset=utf-8");
templateRenderer.render("/templates/reference-gh-issue-details-panel-1.vm", context, response.getWriter());
}

I want to send the project Id to getEnvironment's query:

public static List<Env> getEnvironment(int project){
List<Env> list=new ArrayList<Env>();

try{
Connection con=EmpDao.getConnection();
PreparedStatement ps=con.prepareStatement("select distinct name, version, vname from environment, projectversion where version=vname and project =?");
ps.setInt(1,project);

ResultSet rs=ps.executeQuery();
while(rs.next()){
Env en=new Env();
en.setName(rs.getString(2));
list.add(en);
}
con.close();
}catch(Exception e){e.printStackTrace();}

return list;
}

and the result is a list i send to .vm file: 

#foreach($en in $list)
<blockquote>
$en.getName()
</blockquote>
#end

 

this is gonna display my environments list in a web panel i've created.

Everything works correctly if i enter manually the project Id, i'm just stick with how can I get the current project id.. i don't know what i am missing..

Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
July 31, 2017

Ok, so this is an add-on.  The question is about the context of where the code runs. 

If, for example, it's a post-function, you are given the "issue" object for the issue being transitioned. 

So, where is the code running?  What is the context?

Hasnae E July 31, 2017

I don't really understand what you mean about the "context" but I will tell what i am about:

When clicking to the issue Item in Jira server it'd display (normally) an environment zone showing the list of environments of the current project. So I've guess that maybe i can get the project Id from the HttpServletRequest in the doGet method in my servlet. 

I've never initialized that "issue" and i don't know it's gonna stand for what? Should I declare it as an IssueService or Issue or what? 

Can you show an example for your code working? how did you declare your "issue"?

Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
July 31, 2017

The context is the contents of the environment in which your code is running.

If you are running a post-function, then the environment contains "issue" (the current issue you are transitioning), so you can just use that. 

If you are running it on a project screen, you won't have an issue, but the project will be available.

If it is part of the global functions, out of a project context, such as the issue navigator, then you'll need to do some work in your code to calculate a guess at what project you might need.

And so-on.

The context of the code determines what you need to do to get the data you want.The code I gave you (or rather connfirmed that it works) is the entire code you need.  When it's run in certain contexts, the ones that contain the issue object already.So, you need to look at where your code is running.

Hasnae E August 1, 2017

Thanks. But it's still not that clear to me to determine which context my code is running.. 

Would I need to determine the context even when trying to get the issue id as well?

Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
August 1, 2017

Yes, you need that context.  You've said "web panel" a couple of times, but never explained where this will be located.  It could be on the side of a fridge for all we know (context - no JIRA context, so code will need to fetch data), but I suspect it's being added into the JIRA UI - question is "where?", as that's going to determine the context.

Hasnae E August 1, 2017

It is located in the issue screen.. just like that : 

 

webpanel.png

 

Hasnae E August 1, 2017

here is the code of my webpanel :

<web-panel name="Environments" i18n-name-key="environments.name" key="environments" location="atl.jira.view.issue.right.context" weight="100">
<description key="environments.description">The Environments Plugin</description>
<context-provider class="com.atlassian.tutorial.myPlugin.Servlet.MyContextProvider">
<param name="itemCount" value="4"/>
</context-provider>
<resource name="view" type="velocity"><![CDATA[<iframe src="/plugins/servlet/issuecrud"></iframe>]]></resource>
<tooltip key="gh.issue.panel.reference.tooltip"/>
</web-panel>

 

and the servlet indexed by "issuecrud" is where i want to retrieve the issue id.

Hasnae E August 2, 2017

Any suggestion?

Suggest an answer

Log in or Sign up to answer