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?
Community moderators have prevented the ability to post new answers.
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.
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
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.