Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save vikrantyadav11/ed7124ad5e65873d7fe30e428b9bd0a6 to your computer and use it in GitHub Desktop.
Save vikrantyadav11/ed7124ad5e65873d7fe30e428b9bd0a6 to your computer and use it in GitHub Desktop.
import java.util.Base64
// Replace these with your Jira username and API token (or password)
def username = "YOUR_USERNAME"
def token = "YOUR_API_TOKEN"
// Encode the username and token in Base64
def credentials = "${username}:${token}".bytes.encodeBase64().toString()
// Define headers with Basic Authentication
def headers = [
Authorization: "Basic ${credentials}",
Accept : "application/json"
]
def search = get('/rest/api/3/field')
.header("Authorization", headers.Authorization)
.header("Accept", headers.Accept)
.asObject(List)
.body
.collect { it.name }
// Initialize a map to store field counts
def fieldCounts = [:]
search.each { field ->
try {
// Escape special characters and handle reserved keywords
def escapedField = field.replaceAll("[+]", "\\\\u002b")
.replaceAll("[']", "\\\\u0027")
.replaceAll('[\"]', '\\\\u0022')
def jql = "\"${escapedField}\" is not EMPTY"
def searchResponse = get("/rest/api/2/search?jql=${URLEncoder.encode(jql, "UTF-8")}&maxResults=0")
.header("Authorization", headers.Authorization)
.header("Accept", headers.Accept)
.asObject(Map)
if (searchResponse.status == 200) {
def issueCount = searchResponse.body.total
fieldCounts[field] = issueCount
} else {
def errorMessage = searchResponse.body?.errorMessages?.join(", ") ?: "Unknown error"
fieldCounts[field] = "Error fetching count: ${errorMessage}"
}
} catch (Exception e) {
fieldCounts[field] = "Error fetching count: ${e.message}"
}
}
// Create a markdown table for display
def table = "| Field Name | Issue Count |\n|------------|-------------|\n"
fieldCounts.each { fieldName, count ->
table += "| ${fieldName} | ${count} |\n"
}
return table
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment