Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Attaching evidence to a test execution API POST

Scott Maitland
November 28, 2019

Hi,

Scenario:

I am using Java + TM4J REST API to do a GET request on a test cycle, and test all the test cases that live within that cycle, before then doing a test execution POST to return the results of the tests when they are complete. As part of this, my program captures a screenshot of the browser at point of test failure and stores this locally within the project as a PNG image. All works so far..

Issue:

If you run a test execution through the GUI, you can copy and paste an image in or use the upload file to attach the image.

I cannot seem to work out however, or find any resource on how to use Java to attach an image or to paste in the image as part of the test steps or comments of that test execution. There is no API endpoints (checked with Adaptivist themselves on that) but there must be some way of getting around this problem by passing the image along with the api POST as part of the JSON body. 

 

Thankful for any help regarding this.

2 answers

Suggest an answer

Log in or Sign up to answer
0 votes
Fidelidade JIRA Admin
March 14, 2022

Hi Martin,

The script did not create any projects.
As you can see in the image, it didn't generate any logs either.
Am I doing something wrong?
Thank you
Emanuel

scrip_log.JPG

rocket chou
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
October 8, 2024

without Thread, it works. This is my code

import com.atlassian.jira.component.ComponentAccessor
import com.onresolve.scriptrunner.canned.jira.admin.CopyProject
import org.apache.log4j.Logger
import org.apache.log4j.Level

// Create a logger instance
def log = Logger.getLogger(getClass())
// Set the log level to DEBUG
log.setLevel(Level.DEBUG)

// Get instances of the Custom Field Manager and Project Manager
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def projectManager = ComponentAccessor.getProjectManager()

// Specify the key of the source project (adjust according to your situation)
def srcProjectKey = 'TEST01' // Key of the source project
// Get the instance of the source project by its key
def srcProjectObject = projectManager.getProjectByCurrentKey(srcProjectKey)

// Specify the key and name of the target project (adjust according to your needs)
def targetKey = "HR9" // Key of the target project
def targetProjectName = "Business Core HR9" // Name of the target project

// Check if the source project object, target key, and target project name are all defined
if (srcProjectObject && targetKey && targetProjectName) {
// Create a CopyProject instance
def copyProject = new CopyProject()
// Define parameters for copying the project
def inputs = [
(CopyProject.FIELD_SOURCE_PROJECT): srcProjectKey, // Key of the source project
(CopyProject.FIELD_TARGET_PROJECT): targetKey, // Key of the target project
(CopyProject.FIELD_TARGET_PROJECT_NAME): targetProjectName, // Name of the target project
(CopyProject.FIELD_COPY_VERSIONS): false, // Whether to copy versions
(CopyProject.FIELD_COPY_COMPONENTS): false, // Whether to copy components
(CopyProject.FIELD_COPY_ISSUES): false, // Whether to copy issues
(CopyProject.FIELD_COPY_DASH_AND_FILTERS): false // Whether to copy dashboards and filters
// (CopyProject.FIELD_ORDER_BY): "Rank", // No longer a valid option, commented out
]

// Validate the input parameters
def errorCollection = copyProject.doValidate(inputs, false)
if (errorCollection.hasAnyErrors()) {
// If there are errors, log a warning message
log.warn("Couldn't create project: " + errorCollection)
} else {
// If there are no errors, perform the copy operation
def util = ComponentAccessor.getUserUtil()
def adminsGroup = ComponentAccessor.getGroupManager().getGroup("jira-administrators")
// Ensure the jira-administrators group is defined
assert adminsGroup
def admins = util.getAllUsersInGroups([adminsGroup])
// Ensure there is at least one admin
assert admins

// Set the current logged-in user to the first user in the jira-administrators group
ComponentAccessor.getJiraAuthenticationContext().setLoggedInUser(util.getUserByName(admins.first().name))
// Execute the script to copy the project
copyProject.doScript(inputs)
}
} else {
// If the source project, target key, or target project name are not found or specified, log an error message
log.error("Cannot clone project: source project, target key, or target project name not found or specified")
}

0 votes
Martin Bayer _MoroSystems_ s_r_o__
Community Champion
November 27, 2020

Hi @Emanuel Paquetelima I guess I have the script you need:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.util.thread.JiraThreadLocalUtils
import com.onresolve.scriptrunner.canned.jira.admin.CopyProject
import org.apache.log4j.Logger
import org.apache.log4j.Level
def log = Logger.getLogger(getClass())
log.setLevel(Level.DEBUG)

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def projectManager = ComponentAccessor.getProjectManager()


//change this to your template source project
def srcProjectKey = 'SPK' // source project key
def getSrcProjectObject = projectManager.getProjectByCurrentKey(srcProjectKey)

def targetKey = "SP" // you must adjust for your purpose
def targetProjectName = "Some project" // you must adjust for your purpose

if(getSrcProjectObject && targetKey && targetProjectName) {


//You have to wrap the thread in JiraThreadLocalUtils in Jira 8 or no issues will be cloned
Thread executorThread = new Thread(JiraThreadLocalUtils.wrap {
def copyProject = new CopyProject()
def inputs = [
(CopyProject.FIELD_SOURCE_PROJECT) : srcProjectKey,
(CopyProject.FIELD_TARGET_PROJECT) : targetKey,
(CopyProject.FIELD_TARGET_PROJECT_NAME) : targetProjectName,
(CopyProject.FIELD_COPY_VERSIONS) : true,
(CopyProject.FIELD_COPY_COMPONENTS) : true,
(CopyProject.FIELD_COPY_ISSUES) : true,
(CopyProject.FIELD_COPY_DASH_AND_FILTERS): false,
//(CopyProject.FIELD_ORDER_BY) : "Rank", <-- no longer a valid option so I am commenting this out
]

def errorCollection = copyProject.doValidate(inputs, false)
if (errorCollection.hasAnyErrors()) {
log.warn("Couldn't create project: $errorCollection")
} else {
def util = ComponentAccessor.getUserUtil()
def adminsGroup = ComponentAccessor.getGroupManager().getGroup("jira-administrators")
assert adminsGroup // must have jira-administrators group defined
def admins = util.getAllUsersInGroups([adminsGroup])
assert admins // must have at least one admin

ComponentAccessor.getJiraAuthenticationContext().setLoggedInUser(util.getUserByName(admins.first().name))
copyProject.doScript(inputs)
}

})

//must call start Not run or the Post functions will throw an error because it tries to access a dead thread
executorThread.start()

}else{
log.error("Cannot Clone Project as source Project, target key or target project names were Not found or Not specified")
}

this script copies also issues, versions and components from source project

Fidelidade JIRA Admin
March 10, 2022

Good Day Martin Bayer,
Thank you very much for your feedback, and I apologize for the late response.
I adapted your code according to my needs, but when I run it, it returns null.
Am I doing something wrong?

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.util.thread.JiraThreadLocalUtils
import com.onresolve.scriptrunner.canned.jira.admin.CopyProject
import org.apache.log4j.Logger
import org.apache.log4j.Level
def log = Logger.getLogger(getClass())
log.setLevel(Level.DEBUG)

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def projectManager = ComponentAccessor.getProjectManager()


//change this to your template source project
def srcProjectKey = 'BCKUATOUT' // source project key
def getSrcProjectObject = projectManager.getProjectByCurrentKey(srcProjectKey)

def targetKey = "CPYBCK" // you must adjust for your purpose
def targetProjectName = "CPYBCKUATOUT" // you must adjust for your purpose

if(getSrcProjectObject && targetKey && targetProjectName) {


//You have to wrap the thread in JiraThreadLocalUtils in Jira 8 or no issues will be cloned
Thread executorThread = new Thread(JiraThreadLocalUtils.wrap {
def copyProject = new CopyProject()
def inputs = [
(CopyProject.FIELD_SOURCE_PROJECT) : srcProjectKey,
(CopyProject.FIELD_TARGET_PROJECT) : targetKey,
(CopyProject.FIELD_TARGET_PROJECT_NAME) : targetProjectName,
(CopyProject.FIELD_COPY_VERSIONS) : true,
(CopyProject.FIELD_COPY_COMPONENTS) : true,
(CopyProject.FIELD_COPY_ISSUES) : true,
(CopyProject.FIELD_COPY_DASH_AND_FILTERS): false,
//(CopyProject.FIELD_ORDER_BY) : "Rank", <-- no longer a valid option so I am commenting this out
]

def errorCollection = copyProject.doValidate(inputs, false)
if (errorCollection.hasAnyErrors()) {
log.warn("Couldn't create project: $errorCollection")
} else {
def util = ComponentAccessor.getUserUtil()
def adminsGroup = ComponentAccessor.getGroupManager().getGroup("jira-administrators")
assert adminsGroup // must have jira-administrators group defined
def admins = util.getAllUsersInGroups([adminsGroup])
assert admins // must have at least one admin

ComponentAccessor.getJiraAuthenticationContext().setLoggedInUser(util.getUserByName(admins.first().name))
copyProject.doScript(inputs)
}

})

//must call start Not run or the Post functions will throw an error because it tries to access a dead thread
executorThread.start()

}else{
log.error("Cannot Clone Project as source Project, target key or target project names were Not found or Not specified")
}

Martin Bayer _MoroSystems_ s_r_o__
Community Champion
March 13, 2022

Hi @Fidelidade JIRA Admin it will return null because you are starting a new thread. But is the project created? Can you see any log messages in your atlassian-jira.log file?

Fidelidade JIRA Admin
March 14, 2022

Hi Martin,

The script did not create any projects.
As you can see in the image, it didn't generate any logs either.
Am I doing something wrong?
Thank you
Emanuel

scrip_log.JPG

Fidelidade JIRA Admin
July 5, 2022

Hi Martin,

Any news regarding this post?

Thank you

BR

Martin Bayer _MoroSystems_ s_r_o__
Community Champion
July 5, 2022

Hi @Fidelidade JIRA Admin you need to check your server log, not only the script runner console log

Fidelidade JIRA Admin
July 7, 2022

Hi Martin,

How are you?

I got these logs, but I can't find anything that helps me understand why the script doesn't create project. 

Can you help?

Thank you

Fidelidade JIRA Admin
July 7, 2022

Hi Martin,

As I have several types of projects in my instance, I created several templates.
So, I created a project in Jira, with a Kanban board and a workflow with three states (Backlog, Rifinement, Done).

From this project, in the transition from the "Rifinement" to "Done" state, I intend to run the script to clone one of the templates.
and I have this script that in Jira says it ran, but it doesn't generate any clones. And in the log I don't see anything that helps me understand why.
Can anyone help me?
It is very important for me because I create many projects manually.

 

Emanuel Lima

Martin Bayer _MoroSystems_ s_r_o__
Community Champion
July 8, 2022

Hi @Fidelidade JIRA Admin it is difficult for me to help you without access to your environment but I always suggest (and I use it in this way) to log as much log.error messages as necessary. If you put your log entries and check your server log it is helpful because you can find out what part of code is OK and what part of it is wrong. For example you can change

//(CopyProject.FIELD_ORDER_BY) : "Rank", <-- no longer a valid option so I am commenting this out
]

def errorCollection = copyProject.doValidate(inputs, false)

to

//(CopyProject.FIELD_ORDER_BY) : "Rank", <-- no longer a valid option so I am commenting this out
]
log.error "going to get an error collection"
def errorCollection = copyProject.doValidate(inputs, false)


Do you know what I mean?
Fidelidade JIRA Admin
July 18, 2022

nop...trz_sml.jfif

TAGS
AUG Leaders

Atlassian Community Events