0
\$\begingroup\$

Objective: The objective of the program is to create a JIRA ticket using a REST API. It's working perfectly fine without any issue. However I am not sure if it's up to good Scala standards.

I would be delighted if someone could let me know what I can do to improve it.

import org.apache.http.HttpResponse
import org.apache.http.auth.UsernamePasswordCredentials
import org.apache.http.client.methods.HttpPost
import org.apache.http.entity.StringEntity
import org.apache.http.impl.auth.BasicScheme
import org.apache.http.impl.client.HttpClients
import org.apache.http.util.EntityUtils

object JIRAUtil{


  private def getJiraID(response: HttpResponse): String ={
    val entity = response.getEntity
    val content = EntityUtils.toString(entity)
    val id = "DPPR-\\d{4}".r
    val jiraID = id.findFirstIn(content)
    assert(jiraID.isDefined, s"Failed to create JIRA: ${content}")
    jiraID.get
  }


  def createJIRA(project: String, summary: String, description: String, assigneeName: String,
                 assigneeEmail: String): String = {
    val data = s"""{
      "fields": {
         "project":{
            "key": "${project}"
         },
         "summary": "[Automation] - ${summary}",
         "customfield_10118": "DPPR-1109",
         "assignee": {
            "name": "${assigneeName}",
            "emailAddress": "${assigneeEmail}"
         },
         "description": "${description}",
         "issuetype": {
            "name": "Bug"
         }
     }
    }"""

    val url = "https://acosta-it.atlassian.net/rest/api/latest/issue/"
    val post = new HttpPost(url)
    val httpClient = HttpClients.createDefault()

    val credential = new UsernamePasswordCredentials("gaurang.shah@abc.com", "CoddadEhptnDfU18F")
    post.addHeader("Content-type", "application/json")
    post.addHeader(new BasicScheme().authenticate(credential, post, null))
    post.setEntity(new StringEntity(data))
    val response = httpClient.execute(post)
    val jiraId = getJiraID(response)
    jiraId
  }

}
\$\endgroup\$
  • \$\begingroup\$ The variables response and jiraId don't appear to serve any purpose other than documentation and, in the case of jiraId, that's already well served by getJiraID(). \$\endgroup\$ – jwvh Aug 30 at 4:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.