I am writing a groovy script to be run through Adaptavist Scriptrunner that needs to retrieve a raw file housed on the host Bitbucket server and then (eventually) parse the data. When I go to make the http call using Apache HttpClient, I am getting a 401 unauthorized returned.
How can I pass the current user's credentials to the http request? I'm only able to retrieve the file by changing the repository permissions to "Public", which is not something that I am able to do outside of testing. We also do not want to pass a "dummy" user's credentials (i.e. username: admin; password: admin). I've looked at several answers to similar questions, but all pertained to JIRA, not Bitbucket-Server or used a dummy admin user for authentication.
Below is my code so far:
import org.apache.commons.io.IOUtils import org.apache.http.client.methods.CloseableHttpResponse import org.apache.http.client.methods.HttpGet import org.apache.http.impl.client.CloseableHttpClient import org.apache.http.impl.client.HttpClientBuilder def url = "http://${base_url}/bitbucket/projects/TEST/repos/test/browse/someFile.txt?raw" CloseableHttpClient client = HttpClientBuilder.create().build() HttpGet request = new HttpGet(url) CloseableHttpResponse response = client.execute(request) String response = IOUtils.toString(response.getEntity().getContent()) println(response) client.close()
Pretty new to scripting and web development. Any help would be appreciated!
-Ian
Community moderators have prevented the ability to post new answers.
Sorry for the delay in responding. You can easily do this in ScriptRunner for Bitbucket by using the ContentService.
There is some fiddly bits with getting the output supplier setup, but once thats done its fairly straightforward.
The code below will get you started which you can run from the Script Console. Just replace the variables I have commented on in the code. This will work well for UTF-8 encoded text files, for anything else the byte array will need to be decoded differently:
import com.atlassian.bitbucket.content.ContentService import com.atlassian.bitbucket.io.TypeAwareOutputSupplier import com.atlassian.bitbucket.repository.RepositoryService import com.atlassian.sal.api.component.ComponentLocator import javax.annotation.Nonnull import java.nio.charset.StandardCharsets def contentService = ComponentLocator.getComponent(ContentService) def repositoryService = ComponentLocator.getComponent(RepositoryService) // replace with your project key def projectKey = "PROJECT_1" //replace with your repository slug def slug = "rep_1" def repo = repositoryService.getBySlug(projectKey, slug) def supplier = new ByteArrayStreamOutputSupplier() // identifies point in repository (can be commit id, branch or tag) def objectId = "master" // this is the path to the file from root of repository def pathToFile = "modification/mod_file.txt" contentService.streamFile(repo, objectId, pathToFile, supplier) supplier.stream.close() // convert byte array to UTF-8 encoded string which has the file contents def content = new String(supplier.baos.toByteArray(), StandardCharsets.UTF_8) // process each line in the content content.readLines().each { line -> log.warn line } public class ByteArrayStreamOutputSupplier implements TypeAwareOutputSupplier { ByteArrayOutputStream baos BufferedOutputStream stream ByteArrayStreamOutputSupplier() { this.baos = new ByteArrayOutputStream() this.stream = new BufferedOutputStream(baos) } @Override OutputStream getStream(@Nonnull String contentType) throws IOException { stream } }
I had been looking for a java/groovy way to get content from Bitbucket for quite a while. This definitely solved my problem! Thanks!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
It's for Bitbucket. I'm using Scriptrunner for Bitbucket to access Bitbucket resources. Though it should be noted that this is NOT a REST API call.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Ian, is this about accessing Bitbucket resources using ScriptRunner for JIRA or ScriptRunner for Bitbucket?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.