Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Is it already possible to edit issue-fields via JRJC?

TW April 13, 2015

Hi,

are there already methods to edit issue-fields (custom text-fields) via JRJC?

 

If not - how to handle that use case? Are there examples via REST and JRJC?

3 answers

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

3 votes
Answer accepted
Arun_Thundyill_Saseendran
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 13, 2015

Hi 

I am sure whether it is possible using JRJC. As per my faint remembrance, YES it is possible. Even with JIRA 5.2.9, I have updated custom  fields using JRJC.

However, I no more use JRJC. Stopped using it at least a year back. JIRA REST is simple and straight forward. You can easily update any field of an issue with the REST. The URL is https://docs.atlassian.com/jira/REST/latest/#d2e5296

 

You can set, add or remove values to any issue.

If you are using Java, the steps are as follows.

  1. Create a HTTP Client (I use Apache HTTP Client)
  2. Create a HTTP PUT request with the above rest handle
  3. Set two headers
    1. Content-Type  -> application/json
    2. Authorization -> Basic [username]:[password]
      Note: Encode the '[username]:[password]' string into Base64 and then add in the header
  4. You can use Simple JSON to create the JSON that is required to update the issue.
  5. Set the Input Entity as the formed JSON string (Step 4)
  6. Fire the PUT request.
  7. Read the response. The response status code should be 200

You can find a small snippet of the above algorithm below 

Note: I am providing the snippets to update one custom field.

Snippet to create JSON string

private String createJsonExternalIssueIdString(String valueToUpdateString,String customFieldIdString)
	{
		JSONObject masterJsonObject = new JSONObject();
		JSONObject fieldsObject = new JSONObject();
		fieldsObject.put(customFieldIdString, valueToUpdateString);
		masterJsonObject.put("fields", fieldsObject);
		return masterJsonObject.toJSONString();
	}

Base 64 Encoding

public String encodeToBase64(String value)
	{
		String retString = "";
		byte[] encodedBytes = Base64.encodeBase64(value.getBytes());
		try
		{
			retString = new String(encodedBytes);
		}
		catch(Exception exception)
		{
			retString = "NA";
		}
		return retString;
	}

Create a HTTP Client

private CloseableHttpClient createClient()
{
	CloseableHttpClient httpClient;
	HttpHost proxy;
	PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
	cm.setMaxTotal(306);
	cm.setDefaultMaxPerRoute(108);
	RequestConfig requestConfig = RequestConfig.custom()
		.setConnectTimeout(15000)
		.setSocketTimeout(15000).build();
	//Proxy required only if the connection is through a proxy server
	proxy = new HttpHost("<proxy IP>",<Proxy Port>);
	httpClient= 				HttpClients.custom().setConnectionManager(cm).setProxy(proxy).setDefaultRequestConfig(requestConfig).build();
	return httpClient;
}

 

Fire REST PUT Query To Update Issue

private boolean updateExternalIssueId(String customFieldId, String valueToUpdate,String issueKey)
	{
		boolean flag = true;
		CloseableHttpClient httpClient = createClient();
		String jiraUrl = "<JIRA base URL>";
		String username = "<Your Username>";
		String password = "<Your Password>";
		String passwordEncoded = encodeToBase64(username+":"+password);
		try
		{
				HttpPut put = new HttpPut(jiraUrl
						+ "/rest/api/latest/issue/"+issueKey.trim());
				String jsonString = createJsonExternalIssueIdString(StringEscapeUtils.escapeHtml(valueToUpdate),customFieldId);
				StringEntity input;
				input = new StringEntity(jsonString);
				input.setContentType("application/json");
				put.setEntity(input);
				put.setHeader("Authorization", "Basic "+passwordEncoded);
				CloseableHttpResponse response;
				try {
					response = httpClient.execute(put);
					if((response.getStatusLine().getStatusCode()/100)==2)
					{
						System.out.println("SUCCESS");
					}
					else
					{
						BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
						String line= "";
						String responseContent = "";
						while((line=bufferedReader.readLine())!=null)
						{
							responseContent+=line;
						}
						JSONParser parser = new JSONParser();
						JSONObject jsonObject = (JSONObject) parser.parse(responseContent);
						String errorMessageString = "";
						if(jsonObject.containsKey("errorMessages"))
						{
							errorMessageString = jsonObject.get("errorMessages").toString();
							
						}
						System.out.println("FAILURE: Error message returned from JIRA is \n'"+errorMessageString+"'");
						
					}
					response.close();
				} 
				catch (ClientProtocolException e) 
				{
					flag = false;
					e.printStackTrace();
				} 
				catch (IOException e) 
				{
					flag =false;
					e.printStackTrace();
				}
			}
		}
		catch(Exception e)
		{
			flag = false;
			e.printStackTrace();
		}
		return flag;
	}

 

Hope it helps. Do let me know if you face any issues.

 

Thanks

Arun

 

0 votes
TW April 13, 2015

well, i think i found a solution:

 

{"update" : {"customfield_10737": [{"set" : {"name": "V12.9"}}]}}

0 votes
TW April 13, 2015

Hi,

thanks for your reply. Currently i found a solution, using Jersey.


At the moment i only can change custom fields with text-boxes. But I also want to set a value value of a custom field (version picker):

{"fields":{"customfield_10737": { "name": "R14" }}

 

I got an error with code 400.

 

TAGS
AUG Leaders

Atlassian Community Events