Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
languagegroovy
import flexagon.ff.common.core.exceptions.FlexCheckedException;

//Carry out any custom action you wish for this event. Check out the snippets for reference or ideas.
def listenerName = "AddUpload logsLogs toTo Jira";
LOG.info("Running listener: ${listenerName}");

//Prior to 5.4.0.1 this function only returned a list of input streams. Update accordingly
//The second argument specifies we only want the error streams returned.
def streams = FLEXDEPLOY.getPluginLogInputStreams(EVENT.payload.workflowExecutionId, true);

//upfront validation on streams
if(!streams || streams.size() == 0) {
  LOG.setMessage("Received failure event but couldn't find any plugin log streams");
  return;
}

//We should only ever have one errored plugin execution, so grab first entry.
def failedPlugin = streams.entrySet().iterator().next();

//create our form data body
def propertiesbody = getJiraProperties('JIRA');

LOG.info("${EVENT.payload.issueNumbers}REST.getClient().createFormDataWithInputStream(['file': failedPlugin.getValue()], "${failedPlugin.getKey()}.txt");

for(def issue: EVENT.payload.issueNumbers) {
properties = getJiraProperties('JIRA');

LOG.info("Upload logs to issue: ${issue}");
  
  //create our form data body
  failedPlugin.getValue().reset();
  def body = REST.getClient().createFormDataWithInputStream(['file': failedPlugin.getValue()], "${failedPlugin.getKey()}.txtEVENT.payload.issueNumbers}");

for(def issue: EVENT.payload.issueNumbers) {
  LOG.info("Upload logs to issue: ${issue}");
  
  //send request
  def client = REST.getClient().url(properties.url).addHeader('X-Atlassian-Token', 'no-check').basicauth(properties.user,properties.password);
  def response = client.path("/rest/api/2/issue/${issue}/attachments").post(body);
  LOG.setMessage("Uploaded logs to ${issue}. Response: ${response.getResponseCode()}");
}

//helper function to get jira properties from integration instance
def getJiraProperties(String code) {
  def instance = FLEXDEPLOY.findIntegrationInstance(code,'ITS');
  def properties = instance.getProperties();
  def url = properties.find { it.getPropertyName() == 'JIRA_URL' }.getPropertyValue();
  def user = properties.find { it.getPropertyName() == 'JIRA_USER_NAME' }.getPropertyValue();
  def password = properties.find { it.getPropertyName() == 'JIRA_PASSWORD' }.getPropertyValue();
  return ['url':url, 'user': user, 'password': password];
}

...