Skip to content

Commit

Permalink
JENKINS-33055 add feature to check field ids, update doc
Browse files Browse the repository at this point in the history
  • Loading branch information
Dan Alvizu committed Mar 1, 2016
1 parent 78d2644 commit 0e6cb06
Show file tree
Hide file tree
Showing 7 changed files with 206 additions and 6 deletions.
6 changes: 6 additions & 0 deletions src/main/java/org/jenkinsci/plugins/jiraext/Config.java
Expand Up @@ -158,6 +158,12 @@ public int getTimeout()
{
return timeout;
}

public boolean isJiraConfigComplete()
{
return StringUtils.isNotEmpty(jiraBaseUrl) && StringUtils.isNotEmpty(username)
&& StringUtils.isNotEmpty(password);
}
}

}
11 changes: 11 additions & 0 deletions src/main/java/org/jenkinsci/plugins/jiraext/svc/JiraClientSvc.java
Expand Up @@ -20,6 +20,8 @@

import net.rcarz.jiraclient.JiraException;

import java.util.Map;

/**
* @author dalvizu
*/
Expand Down Expand Up @@ -61,4 +63,13 @@ public interface JiraClientSvc
*/
void addFixVersion(String jiraIssueKey, String newFixVersion)
throws JiraException;

/**
* Get a map of fieldIds to fieldName for the given issue key
*
* @param issueKey
* @return a map of fieldIds to their fieldNames
*/
Map<String, String> getJiraFields(String issueKey) throws JiraException;

}
Expand Up @@ -25,13 +25,20 @@
import net.rcarz.jiraclient.JiraClient;
import net.rcarz.jiraclient.JiraException;
import net.rcarz.jiraclient.Project;
import net.rcarz.jiraclient.RestClient;
import net.rcarz.jiraclient.Version;
import net.sf.json.JSON;
import net.sf.json.JSONObject;
import net.sf.json.JSONString;
import org.apache.commons.lang.Validate;
import org.jenkinsci.plugins.jiraext.svc.JiraClientFactory;
import org.jenkinsci.plugins.jiraext.svc.JiraClientSvc;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;


Expand Down Expand Up @@ -143,6 +150,39 @@ public void addFixVersion(String jiraIssueKey, String newFixVersion)
issue.update().field(Field.FIX_VERSIONS, existingVersions).execute();
}

@Override
public Map<String, String> getJiraFields(String issueKey)
throws JiraException
{
logger.fine("Get JIRA field ids for key: " + issueKey);
Map<String, String> result = new HashMap<>();
JiraClient client = newJiraClient();
RestClient restClient = client.getRestClient();
try {
Map<String, String> params = new HashMap<>();
params.put("expand", "renderedFields");
JSONObject json = (JSONObject)restClient.get("/rest/api/latest/issue/" + issueKey, params);
JSONObject fields = (JSONObject) json.get("renderedFields");

for (Object key : fields.keySet())
{
String fieldName = (String)key;
String fieldValue = "null";
if (fields.get(fieldName) != null)
{
fieldValue = fields.get(fieldName).toString();
}
result.put(fieldName, fieldValue);

}

return result;
}
catch (Throwable t) {
throw new JiraException("Exception getting fields for JIRA issue", t);
}
}

/**
*
* @param projectVersions - list of Versions to look for a name
Expand Down
52 changes: 48 additions & 4 deletions src/main/java/org/jenkinsci/plugins/jiraext/view/UpdateField.java
Expand Up @@ -23,13 +23,20 @@
import hudson.Launcher;
import hudson.model.AbstractBuild;
import hudson.model.BuildListener;
import hudson.util.FormValidation;
import org.apache.commons.lang.StringUtils;
import org.jenkinsci.plugins.jiraext.Config;
import org.jenkinsci.plugins.jiraext.GuiceSingleton;
import org.jenkinsci.plugins.jiraext.domain.JiraCommit;
import org.jenkinsci.plugins.jiraext.svc.JiraClientSvc;
import org.kohsuke.stapler.DataBoundConstructor;

import org.kohsuke.stapler.ForwardToView;
import org.kohsuke.stapler.HttpResponse;
import org.kohsuke.stapler.QueryParameter;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
* Update a Field on an Issue
Expand All @@ -39,12 +46,11 @@
public class UpdateField
extends JiraOperationExtension
{
private JiraClientSvc jiraClientSvc;

public String fieldName;

public String fieldValue;

private static final Logger logger = Logger.getLogger(UpdateField.class.getSimpleName());

@DataBoundConstructor
public UpdateField(String fieldName, String fieldValue)
Expand Down Expand Up @@ -91,13 +97,51 @@ public String toString()

@Extension
public static class DescriptorImpl
extends JiraOperationExtensionDescriptor
extends JiraOperationExtensionDescriptor
{

private transient JiraClientSvc jiraClientSvc;

@Inject
public synchronized final void setJiraClientSvc(JiraClientSvc jiraClientSvc)
{
this.jiraClientSvc = jiraClientSvc;
}

public synchronized final JiraClientSvc getJiraClientSvc()
{
if (jiraClientSvc == null)
{
jiraClientSvc = new GuiceSingleton().getInjector().getInstance(JiraClientSvc.class);
}
return jiraClientSvc;
}

public HttpResponse doQueryJiraFields(@QueryParameter String issueKey)
{
try
{
if (!Config.getGlobalConfig().isJiraConfigComplete())
{
return FormValidation.error("JIRA settings are not set in global config");
}
final Map<String, String> jiraFields = getJiraClientSvc().getJiraFields(issueKey);
return new ForwardToView(this, "/org/jenkinsci/plugins/jiraext/view/UpdateField/jiraFields.jelly")
.with("jiraFieldMap", jiraFields);
}
catch (Throwable t)
{
String message = "Error finding FieldIds for issueKey: " + issueKey;
logger.log(Level.WARNING, message, t);
return FormValidation.error(t, message);
}
}

@Override
public String getDisplayName()
{
return "Update a Field";
}
}

}
Expand Up @@ -5,14 +5,78 @@
xmlns:t="/lib/hudson"
xmlns:f="/lib/form">

<f:entry title="JIRA Field Name" field="fieldName">
<f:entry title="JIRA Field ID" field="fieldName">
<f:textbox />
</f:entry>

<f:entry title="Value" field="fieldValue">
<f:textarea />
</f:entry>

<f:advanced title="Find Your Field ID">
<f:description>
<p>
You must identify the Field by its ID - for custom fields these
are not very friendly, for example 'customfield_123'. These are only exposed over API,
and are required over the API, but there does not appear to be any way where these are ever
correlated to their friendly names. If you know, please open a ticket! As far as I know, this
is a JIRA limitation.</p>

<p>Use the button below to see all of the field IDs of a particular issue to find the one you
want to use. </p>
</f:description>
<f:entry>
<script type="text/javascript"><![CDATA[
function populateJiraFields(checkUrl,paramList,button) {
button = button._button;
var parameters = {};
paramList.split(',').each(function(name) {
var p = findPreviousFormItem(button,name);
if(p!=null) {
if(p.type=="checkbox") parameters[name] = p.checked;
else parameters[name] = p.value;
}
});
var spinner = $(button).up("DIV").next();
var target = spinner.next();
spinner.style.display="block";
new Ajax.Request(checkUrl, {
parameters: parameters,
onComplete: function(rsp) {
spinner.style.display="none";
var i;
target.innerHTML = rsp.status==200 ? rsp.responseText
: '<a href="" onclick="document.getElementById(\'valerr' + (i=iota++)
+ '\').style.display=\'block\';return false">ERROR</a><div id="valerr'
+ i + '" style="display:none">' + rsp.responseText + '</div>';
Behaviour.applySubtree(target);
layoutUpdateCallback.call();
var s = rsp.getResponseHeader("script");
try {
geval(s);
} catch(e) {
window.alert("failed to evaluate "+s+"\n"+e.message);
}
}
});
}
]]></script>
<div style="float:right">
Issue Key: <input type="text" name="issueKey"/><br/>
<input type="button" value="Find Field IDs" class="yui-button validate-button"
onclick="populateJiraFields('${descriptor.descriptorFullUrl}/queryJiraFields', 'issueKey',this)" />
</div>
<div style="display:none;">
<img src="${imagesURL}/spinner.gif" /> ${attrs.progress}
</div>
<div><!-- this is where the error message goes --></div>
</f:entry>
</f:advanced>

</j:jelly>

@@ -1,3 +1,3 @@
<div>
The name of the field. For custom fields this will have something like customField_12349
The name of the field. For custom fields this will have something like customfield_12349
</div>
@@ -0,0 +1,35 @@
<j:jelly xmlns:j="jelly:core"
xmlns:st="jelly:stapler"
xmlns:d="jelly:define"
xmlns:l="/lib/layout"
xmlns:t="/lib/hudson"
xmlns:f="/lib/form">

<div>
Hello world!
</div>

<table>
<tr>
<th>
Field ID
</th>
<th>
Field Value
</th>
</tr>

<j:forEach items="${jiraFieldMap}" var="entry" indexVar="i">
<tr>
<td>
${entry.key}
</td>
<td>
${entry.value}
</td>
</tr>
</j:forEach>
</table>

</j:jelly>

0 comments on commit 0e6cb06

Please sign in to comment.