How to read or write into a file using scriptrunner, e.g. the Script Console?

Günter Halmans February 3, 2019

In our company we are using Jira Server and Scriptrunner. Currently, I'm trying to read a file using a script in the script console. The file should be stored in a working directory on e.g. "C:/Users/<myname>/Desktop".

Up to now I didn't get access to a file which is stored in the given directory. I'm able to list the content of the current working directory (which is located at the server) but I'm not able to change the path to "C:/Users/<myname>/Desktop". The system always throws an java.io.FileNotFoundException (no such file or directory). Here a snapshot of the script:

def a1 = new File("C:/Users/Name/Desktop").path
log.debug "path a1: "+a1

def a2 = new File(a1,"hello.txt")
log.debug"The file ${a2.absolutePath} has ${a2.length()} bytes"

a2.eachLine {  
         line -> log.debug "line : $line";
      }

I tried the script on my own local version of Jira and Scriptrunner and there it is no problem to read a file of my desktop.

Any idea?

1 answer

1 accepted

0 votes
Answer accepted
Tom Lister
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
February 3, 2019

Hi @Günter Halmans

just a quick suggestion, without me checking ,that might work

replace / with // in your path string

or try \

Java handles paths in a machine independent way

Günter Halmans February 3, 2019

Hi Tom,

thank you for your feedback!

I tried different combinations:

1. "\\"

Script (part)

def a1 = new File("C:\\Users\\name\\Desktop\\").absolutePath
log.debug "path a1: "+a1
def a2 = new File(a1,"hello.txt")
log.debug"The file ${a2.absolutePath} has ${a2.length()} bytes"

Result:

2019-02-04 08:23:21,967 DEBUG : path a1: /opt/atlassian/jira/bin/C:\Users\name\Desktop\ 2019-02-04 08:23:21,967 DEBUG: The file /opt/atlassian/jira/bin/C:\Users\name\Desktop\/hello.txt has 0 bytes

2. "\"

--> error: startup failed: Script2305.groovy: 9: unexpected char: '\' @ line 9, column 22. def a1 = new File("C:\Users\name\Desktop\").absolutePath ^ 1 error 

3. "/"

Script (part):

def a1 = new File("C:/Users/name/Desktop/").absolutePath
log.debug "path a1: "+a1
def a2 = new File(a1,"hello.txt")
log.debug"The file ${a2.absolutePath} has ${a2.length()} bytes"

Result:

2019-02-04 08:31:54,123 DEBUG [NN]: path a1: /opt/atlassian/jira/bin/C:/Users/name/Desktop 2019-02-04 08:31:54,124 DEBUG [NN]: The file /opt/atlassian/jira/bin/C:/Users/name/Desktop/hello.txt has 0 bytes

4. "//"

Script (part):

def a1 = new File("C://Users//name//Desktop//").absolutePath
log.debug "path a1: "+a1
def a2 = new File(a1,"hello.txt")
log.debug"The file ${a2.absolutePath} has ${a2.length()} bytes"

Result:

same as in 3.

4. "/" and ".path"

Script (part)

def a1 = new File("C:/Users/name//Desktop/").path
log.debug "path a1: "+a1
def a2 = new File(a1,"hello.txt")
log.debug"The file ${a2} has ${a2.length()} bytes"
a2.eachLine {  
         line -> log.debug "line : $line";
      }

Result:

2019-02-04 08:36:47,796 DEBUG [NN]: path a1: C:/Users/name/Desktop 2019-02-04 08:36:47,796 DEBUG [NN]: The file C:/Users/name/Desktop/hello.txt has 0 bytes 2019-02-04 08:36:47,806 WARN [common.UserScriptEndpoint]: Script console script failed: java.io.FileNotFoundException: C:/Users/name/Desktop/hello.txt (No such file or directory) at Script2323.run(Script2323.groovy:15)

---------------------------

Thus, the alternative 4 seems to be the pormising one...but it also doesn't work. I firstly thought that it might be a permission problem, but the error message points to "no such file or directory"...

Thanks in advance for your support!

BR, Günter

Tom Lister
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
February 4, 2019

I see that you are running Jira on a unix flavour

/opt etc path

its not possible for a server side script to see your desktop

you will need to put the file on a share visible to both

Günter Halmans February 4, 2019

Ok, thanks!

BR, Günter

Harish_Kumar April 17, 2020

@Günter Halmans  , Have you overcome this? Can you help me on this with the code.

Thanks

Günter Halmans April 17, 2020

Hi Harish,

yes, in the meantime we could solve the problem. It was manly caused by setting the shenv. parameter correctly. For whatever reason we have two "scripts" folders (.../scripts/scripts/... - see example below) and I thought we have only one. Therefore we searched a long time at a wrong place... ;-)

I can give you a short example of code which I have used to get all project leaders of our Jira projects in the ScriptRunner console. The leaders are identified and documented with project key and project name into a file. Reading from a file is straight forward.

Currently I often use writing into a file for debugging code, especially when only the log.file can be used to get information about intermediate results...

I hope, the code snippet may help you.

Regards,

Günter

--------

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.project.ProjectManager
import com.atlassian.jira.project.Project
import java.util.List
import java.io.File
import groovy.xml.MarkupBuilder




def issueManager = ComponentAccessor.getIssueManager()
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def userManager = ComponentAccessor.getUserManager()
ProjectManager projectManager = ComponentAccessor.getComponent(ProjectManager);
List<Project> projects = projectManager.getProjects();

def list1 = []
def list2 = []
def list3 = []
def Counter = 0


// iteration about all projects
projects.each{project ->

list1.add(project.getKey())
list2.add(project.getName())
def leadName = userManager.getUserByKey(project.getLeadUserKey())

if ( leadName != null){
list3.add(leadName.getDisplayName())
}else {
list3.add("no lead")
}
Counter++
}

// preparation of the output-string
def writer = new StringWriter()
def outputString = ""

// header string
outputString = outputString + "Project-Key;Project-Name;Lead\r\n"

for(int i = 0;i<Counter-1;i++) {
outputString = outputString +
list1[i].toString()+";"+
list2[i].toString()+";"+
list3[i].toString()+"\r\n"

}

// writing outputstring into a file
def issueFile = new File('/var/atlassian/application-data/jira/scripts/scripts/data','Example.txt')
issueFile.write outputString.toString()

return Counter
Harish_Kumar April 17, 2020

Hi @Günter Halmans , thanks.

 Can you help me on : whenever i close a sprint (SprintClosedEvenet) a custom field should be set to "xyz" should be set to some string "ABC".

I was trying with Listeners not able to make it

 

THanks

Harish

Günter Halmans April 30, 2020

Hi Harish,

sorry for answering lately. I'm not really familiar with the context "closing sprints". I would propose to open a new request, especially because it is a new topic. I assume that  Jira experts will identify the question and will give you support.

Kind regards,

Günter

Suggest an answer

Log in or Sign up to answer