import groovy.json.JsonSlurper;
import groovy.json.StreamingJsonBuilder;
import com.atlassian.jira.issue.customfields.CustomFieldManager;CustomFieldType
import com.atlassian.jira.bc.issue.fieldssearch.CustomFieldSearchService;
import com.atlassian.jira.issuecomponent.IssueManagerComponentAccessor;
import com.atlassian.jira.jql.componentparser.ComponentAccessorJqlQueryParser;
import com.atlassian.jira.issue.IssueCustomFieldManager;
import com.atlassian.jira.issue.fields.MutableIssueCustomField;
import com.atlassian.jira.user.util.UserManager;
import com.atlassian.jira.bcweb.issuebean.search.SearchServicePagerFilter;
import comgroovy.atlassian.jira.jql.parser.JqlQueryParserjson.StreamingJsonBuilder;
import com.atlassian.jira.webissue.bean.PagerFilterIssue;
import orggroovy.apache.commons.codec.binary.Base64json.JsonSlurper;
import groovy.json.*;
////////////////////////////////////////////
// ***** CHANGE VARIABLES HERE *****
////////////////////////////////////////////
// Authorization
String authorizationStringServer = "Basic ==="
String authorizationStringCloud = "Basic ==="
String authorizationStringCloudAccountID = """Basic ==="""
//Jira GridBASE IDs URL
String gridCustomFieldIdServerjiraBaseURLServer = 1
String gridIdCloud = "-"
//Jira BASE URL
String jiraBaseURLServer = "http://---"https://==="
String cloudInstanceName = "==="
String jqlQuery = '------'"==="
////////////////////////////////////////////
// ***** CHANGE VARIABLES HERE *****
////////////////////////////////////////////
String baseURL = "https://databuckets.net/rest/tgc/api/v1/IssueManager issueManagergrids"
CustomFieldManager customFieldManager = ComponentAccessor.getOSGiComponentInstanceOfType(IssueManagerCustomFieldManager.class);
//Collection<Long>def idstgngFieldType = issueManagercustomFieldManager.getIssueIdsForProject(projectId);getCustomFieldType("tge.cloud:idalko-table-grid-custom-field-key")
def tgngFieldList = customFieldManager.getCustomFieldObjects().findAll {it.getCustomFieldType() == tgngFieldType}
// Cloud URL
def jsonSlurperAccount = new JsonSlurper()
StringBuilder responses = new StringBuilder()
// Get all Cloud Grids
issueURL IDsgetGridsURL;
bygetGridsURL the= Project ID
//Collection<Issue> issues = issueManager.getIssueObjects(ids);
StringBuilder responses = new StringBuilder()new URL(baseURL);
URLConnection getGridsConnection = getGridsURL.openConnection();
getGridsConnection.setRequestMethod("GET")
getGridsConnection.doOutput = true
getGridsConnection.setRequestProperty("Authorization", authorizationStringCloud)
getGridsConnection.connect();
def getGridsResponseCode = getGridsConnection.getResponseCode()
def gridsList
if(getGridsResponseCode != 200){
responses.append("Error while getting the list of Grids in Cloud")
}else{
gridsList = jsonSlurperAccount.parseText(getGridsConnection.getInputStream().getText());
}
Map<Long,String> gridCorrespondance = [:]
for(grid in tgngFieldList){
if(gridsList.find { it.name == grid.toString() } != null) {
gridCorrespondance.putAll([(grid): (gridsList.find { it.name == grid.toString() }.id)])
}
}
def searchService = ComponentAccessor.getComponent(SearchService)
def loggedInUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def queryParser = ComponentAccessor.getComponent(JqlQueryParser)
def sb = new StringBuffer()
//your query goes here
def query = queryParser.parseQuery(jqlQuery)
//gets results of query
def search = searchService.search(loggedInUser, query, PagerFilter.getUnlimitedFilter())
//iterate over each returned issue
Collection<Issue> issues = search.results.key
for (String issue : issues){ = search.results.key
for (String issue : issues){
for (tgngCustomField : gridCorrespondance) {
String issueKey = issue
// Grid IDs
String issueKeygridCustomFieldIdServer = issue
tgngCustomField.getKey().getIdAsLong()
//String issueKeygridIdCloud = issuetgngCustomField.getKeygetValue()
def baseURLgridDataDCURL = jiraBaseURLServer + "/rest/idalko-grid/1.0/api/grid/" + gridCustomFieldIdServer + "/issue/" + issueKey;
def baseURL2gridDataCloudURL = "https://databuckets.net/rest/tgc/api/v1/grids/" + gridIdCloud + "/issue/" + issueKey;
def
instanceName = "----" //Getting the Grid information from the Data Center Jira instance
URL url;
url = new URL(baseURLgridDataDCURL);
URLConnection connection = url.openConnection();
connection.setRequestMethod("GET")
connection.setRequestProperty("Authorization", authorizationStringServer)
connection.doOutput = true
connection.setRequestProperty("Content-TypeAuthorization", "application/json;charset=UTF-8"authorizationStringServer)
connection.connect();
String serverData = connection.getInputStream().getText()
def jsonSlurper = new JsonSlurper()
def object = jsonSlurper.parseText(serverData)
Long gridRowsinCloud = -1; //create variable with invalid value just to signal exception and skip condition if failed
try {
// Geting Grid info from Cloud to see if it was already uploaded (avoid duplicates)
URL url3;
url3 = new URL(baseURL2gridDataCloudURL);
URLConnection connection3 = url3.openConnection();
connection3.setRequestMethod("GET")
connection3.doOutput = true
connection3.setRequestProperty("Authorization", authorizationStringCloud)
connection3.connect();
responseCode = connection3.getResponseCode()
if(responseCode != 200){ 200){
responseMessage = connection3.getErrorStream().text;
}else{
String cloudData = connection3.getInputStream().getText();
def jsonSlurperAccount = new JsonSlurper()
def cloudjson = jsonSlurperAccount.parseText(cloudData)
//Long gridRowsinServer = object.rows.size();
gridRowsinCloud = cloudjson.rows.size(); //if it works, change value of variable
}
} catch(java.io.IOException ex) {
responses.append("!! exception while getting Grid data !!"); }
}
// check if data was already uploaded
if(gridRowsinCloud == 0){
//if(gridRowsinServer > 0
&&
gridRowsinCloud == 0){ // Formatting the output so that it fits the POST request body formatting requirements
requirements object.remove("customFieldId") //Removing Grid ID since does not need to be passed in the request body
object.rows.each { e -> //Going through all of the rows from the response
e.remove("order") // Removing order index
e.remove("rowId") // Removing Row ID since it will be automatically assigned
e.columns.each{ key, value -> // Unpacking the "columns" section into the previous level of JSON structure
if(value != null || !value.toString().equals("{}") || value != ""){
if(value instanceof String){
e.put(key, value)
}else if(value instanceof org.apache.groovy.json.internal.LazyMap){
if(value.keySet().contains("username")){
// Get user email to match with user in Cloud
def userManager = ComponentAccessor.getUserManager()
def user = userManager.getUserByName(value.username)
if(user != null){
String email = user.getEmailAddress()
// Getting new user info from Cloud based on email
URL urlAccountID
urlAccountID = new URL("https://${instanceName}.atlassian.net/rest/api/3/user/search?query=" + email.replace(" ","%20"));
URLConnection connection4 = urlAccountID.openConnection();
connection4.setRequestMethod("GET")
connection4.setRequestProperty("Authorization", authorizationStringCloudAccountID)
connection4.doOutput = true
connection4.connect();
String cloudData1 = connection4.getInputStream().getText()
def jsonSlurperAccount1 = new JsonSlurper()
def objectAccount1 = jsonSlurperAccount1.parseText(cloudData1)
if(objectAccount1[0] != null){
e.put(key, jsonSlurperAccount1.parseText('{"accountId":"' + objectAccount1[0].accountId+'"}'))
}
}
}
if(value.keySet().contains("label")){
e.put(key, value.label)
}
}else if(value.toString().length() != 2){
e.put(key, value.value)
}
}
}
e.remove("columns") // Removing the packed "columns" section as it is redundant and breaks the formatting
e.remove("Item Number") // Remove SEQUENCE type column from the data load
}
try {
// Uploading the grid data to the Cloud
URL url2;;
url2 = new URL(baseURL2gridDataCloudURL);
URLConnection connection2 = url2.openConnection();
connection2.setRequestMethod("POST")
connection2.doOutput = true
connection2.setRequestProperty("Authorization", authorizationStringCloud)
connection2.setRequestProperty("Content-Type", "application/json;charset=UTF-8")")
connection2.outputStream.withWriter("UTF-8") { new StreamingJsonBuilder(it, object) }
connection2.connect();
responseCode = connection2.outputStream.withWriter("UTF-8"getResponseCode()
{
new StreamingJsonBuilder(it, object) } connection2.connect();if(responseCode != 201){
responseCoderesponseMessage = connection2.getResponseCode().getErrorStream().text + JsonOutput.toJson(object);
if(responseCode != 201){}else{
responseMessage = connection2.getErrorStream().text + JsonOutput.toJson(object);getResponseMessage();
}
}else{responses.append( " || IssueKey: " + issueKey +
responseMessage = connection2.getResponseMessage(); } responses.append( " Grid ||Field IssueKeyName: " + issueKeytgngCustomField.getKey() +
" ResponseCode: " + responseCode +
" ResponseMessage: " + responseMessage)
} catch(java.io.IOException ex) {
responses.append("!! exception !!");
}
responses.append("\n")
}
}
}
return responses |