Skip to content

Instantly share code, notes, and snippets.

@jechlin
Created July 22, 2013 20:23
Show Gist options
  • Save jechlin/6057318 to your computer and use it in GitHub Desktop.
Save jechlin/6057318 to your computer and use it in GitHub Desktop.
package examples
import com.atlassian.crowd.embedded.api.User
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.config.properties.APKeys
import com.atlassian.jira.web.session.currentusers.JiraUserSessionTracker
import groovy.json.JsonSlurper
import org.apache.commons.httpclient.Header
import org.apache.commons.httpclient.HttpClient
import org.apache.commons.httpclient.HttpMethod
import org.apache.commons.httpclient.HttpStatus
import org.apache.commons.httpclient.NameValuePair
import org.apache.commons.httpclient.methods.GetMethod
import org.apache.log4j.Logger
public class IssuesInSprintDiagnostics {
def log = Logger.getLogger("com.onresolve.jira.groovy.IssuesInSprintDiagnostics")
def sb = new StringBuilder()
private String getSessionId(User user) {
def jiraUserSessionTracker = ComponentAccessor.getComponent(JiraUserSessionTracker.class)
def userSessions = jiraUserSessionTracker.getSnapshot()
def sessionId = userSessions.find { it.userName == user?.name }?.id
sessionId
}
private static Header getCookieHeader(String sessionId) {
def cookieHeader = new Header("Cookie", "JSESSIONID=$sessionId")
cookieHeader
}
private Map execMethod(String url, String sessionId, NameValuePair[] query, HttpClient client) {
def baseUrl = ComponentAccessor.getApplicationProperties().getString(APKeys.JIRA_BASEURL)
HttpMethod method = new GetMethod("${baseUrl}${url}")
log.debug("Req: ${method.getURI()}")
method = addAuthenticationDetails(method, sessionId)
if (query) {
method.setQueryString(query)
}
def httpStatus = client.executeMethod(method)
if (httpStatus != HttpStatus.SC_OK) {
throw new Exception ("Function failed to execute http method - failed with code: $httpStatus")
}
def data = method.getResponseBodyAsStream().text
method.releaseConnection()
def json = new JsonSlurper().parseText(data) as Map
json
}
private static HttpMethod addAuthenticationDetails(HttpMethod method, String sessionId) {
def header = getCookieHeader(sessionId)
if (header) {
method.addRequestHeader(header)
}
else {
throw new Exception("Couldn't find user session, this function can fail if you haven't logged in recently")
}
method
}
public String main() {
def client = new HttpClient()
def baseUrl = ComponentAccessor.getApplicationProperties().getString(APKeys.JIRA_BASEURL)
sb << "Base url is ${baseUrl}\n"
def url = "${baseUrl}/login.jsp"
HttpMethod method = new GetMethod(url)
def httpStatus = client.executeMethod(method)
method.releaseConnection()
sb << "Request to ${url} returned ${httpStatus}\n"
def authenticationContext = ComponentAccessor.getJiraAuthenticationContext()
def sessionId = getSessionId(authenticationContext.getLoggedInUser())
sb << "session ID: " + sessionId << "\n"
url = "/rest/greenhopper/1.0/rapidviews/list"
log.debug(url)
def data = execMethod(url, sessionId, null, client)
sb << "Available boards: " + data.get("views").collect {it["name"]} + "\n"
return sb.toString()
}
}
new examples.IssuesInSprintDiagnostics().main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment