Created
August 1, 2024 14:44
-
-
Save vikrantyadav11/ed7124ad5e65873d7fe30e428b9bd0a6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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