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

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

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 vote

issue.getProjectObject().getKey()

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

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];

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?

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..

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..

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..

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..

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?

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"?

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.

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?

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.

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

 

webpanel.png

 

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.

Any suggestion?

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

Suggest an answer

Log in or Sign up to answer
Atlassian Community Anniversary

Happy Anniversary, Atlassian Community!

This community is celebrating its one-year anniversary and Atlassian co-founder Mike Cannon-Brookes has all the feels.

Read more
Community showcase
Julia Dillon
Posted Tuesday in Jira

Tell us how your team runs on Jira!

Hey Atlassian Community! Today we are launching a bunch of customer stories about the amazing work teams, like Dropbox and Twilio, are doing with Jira. You can check out the stories here. The thi...

758 views 2 19
Join discussion

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you