Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Next challenges

Recent achievements

  • Global
  • Personal

Recognition

  • Give kudos
  • Received
  • Given

Leaderboard

  • Global

Trophy case

Kudos (beta program)

Kudos logo

You've been invited into the Kudos (beta program) private group. Chat with others in the program, or give feedback to Atlassian.

View group

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Script runner script doesn't work as a service. Edited

Hello,

I created a script to create new support zip with REST.

The script works in the script runner console  but  not as a service.

i do use  setLoggedInUser.

what im doing wrong?

Thanks.

 

 

-------------------------------------------Script------------------------------------------

 

/*
* Created by: Michael Shechter.
* Date:9/6/2020.
* Description:
*/




import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import org.apache.commons.codec.binary.Base64;
import org.apache.log4j.Level
import org.apache.log4j.Logger
import com.atlassian.jira.component.ComponentAccessor

def userManager = ComponentAccessor.getUserManager()
def user = userManager.getUserByName("JiraAutomation")
ComponentAccessor.getJiraAuthenticationContext().setLoggedInUser(user)


//Set logging level
def log = Logger.getLogger(scriptrunner.SetSupportZip")
log.setLevel(Level.DEBUG)
log.error "### Create support zip is running"




final String baseUrl = ComponentAccessor.getApplicationProperties().getString("jira.baseurl")
String restUrl = "${baseUrl}/rest/troubleshooting/latest/support-zip/local"
sendPostRequest(restUrl)

def sendPostRequest(String restUrl) {
try {
String userName = 'xxxxx'
String userPass = 'xxx'
String authString = userName + ":" + userPass
byte[] authEncBytes = Base64.encodeBase64(authString.getBytes());
String authStringEnc = new String(authEncBytes);
def body_req ='{"items":["APPLICATION_LOGS"], "limitFileSizes": false}'
def connection = new URL(restUrl).openConnection() as HttpURLConnection
connection.setRequestMethod("POST")
connection.setRequestProperty('User-Agent', 'jira')
connection.setRequestProperty("Content-Type", "application/json;charset=UTF-8")
connection.setRequestProperty('Accept', 'application/json;charset=UTF-8')
connection.setRequestProperty("Authorization", "Basic " + authStringEnc);
connection.setDoOutput(true)
connection.getOutputStream().write(body_req.getBytes("UTF-8"))
connection.connect()
InputStream is = connection.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
int numCharsRead;
char[] charArray = new char[1024];
StringBuffer sb = new StringBuffer();
while ((numCharsRead = isr.read(charArray)) > 0) {
sb.append(charArray, 0, numCharsRead);
}
String result = sb.toString();
log.warn("*** BEGIN ***");
log.warn(result);
log.warn("*** END ***");
}catch(MalformedURLException e){
log.error e.printStackTrace();
}catch(IOException e){
log.error e.printStackTrace();
}
}

 

 

 

0 answers

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
SERVER
VERSION
7.13.1
TAGS
Community showcase
Published in Marketplace Apps & Integrations

Do Not Lose your Customer’s Trust

Missing deadlines is one of the biggest problems every team lead wants to avoid when dealing with managed services. When the customer contracts your company to help with IT services it is expected th...

24 views 0 0
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you