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

Bitbucket - get commited file content

Miś Ursowicz August 25, 2020

Hello 

Is there a way to have a conditional merge check script which would check if variable X defined in the committed application.properties file is equal to its counterpart variable X' in the master application.properties file. 

1 answer

1 accepted

Suggest an answer

Log in or Sign up to answer
1 vote
Answer accepted
Reece Lander _ScriptRunner - The Adaptavist Group_
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
August 27, 2020

Hello,

I'm Reece, an engineer working on ScriptRunner for Bitbucket.

It should be possible for you to achieve what you describe by comparing the content of the file on the source branch vs the target branch (master in your case).

Here is some example code that you could use in a conditional merge check to do this:

import com.atlassian.bitbucket.content.ContentService
import com.atlassian.sal.api.component.ComponentLocator
import com.atlassian.bitbucket.hook.repository.PullRequestMergeHookRequest

def contentService = ComponentLocator.getComponent(ContentService)
def pullRequest = (hookRequest as PullRequestMergeHookRequest).pullRequest

def file = "application.properties"

def sourceBranchFileStream = new ByteArrayOutputStream()
contentService.streamFile(repository, pullRequest.fromRef.id, file, { sourceBranchFileStream })

def targetBranchFileStream = new ByteArrayOutputStream()
contentService.streamFile(repository, pullRequest.toRef.id, file, { targetBranchFileStream })

def sourceBranchProperties = new Properties()
sourceBranchProperties.load(new ByteArrayInputStream(sourceBranchFileStream.toByteArray()))

def targetBranchProperties = new Properties()
targetBranchProperties.load(new ByteArrayInputStream(targetBranchFileStream.toByteArray()))

def nonMatchingProperty = sourceBranchProperties.findResult {
if (targetBranchProperties.get(it.key) != it.value) {
return it
}

null
}

if (nonMatchingProperty) {
return true
}

false

 

The above snippet takes the content of the file application.properties from the source branch, and compares it to the contents of the target branch, this is done by constructing a Properties instance and then comparing the entries.

The above snippet can only be configured by a global administrator, as it uses ContentService and this is not available in hooks configured at the repository level by a repository administrator.

If you have any further questions please let me know.

Kind regards,

Reece

Miś Ursowicz August 28, 2020

Hello Reece

Thx for the quick response! Actually, I have a difficulty testing your solution since I do not have global administrator rights... Is there any workaround for mere mortals repo admins? :) 

Maybe to add some details - we want to have automatic version incrementation in a gradle.properties file on merge.  E.g. version = 5.3.11 -> version = 5.3.12. In some ideal world we would also like to label 5.3.12 to the related jira afterwords, but this...

Thx in advance for help!

Reece Lander _ScriptRunner - The Adaptavist Group_
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
August 28, 2020

Hello again,

Unfortunately there is not a workaround, ContentService is not permitted for usage by repository administrators as it can be used to access content in files from multiple repositories. To allow its usage, we would need to thoroughly check that the API is safe for a repository administrator to use.

It is possible we could add something to the product to facilitate this use case in a secure manner, I'd be happy to add an item to our backlog for that.

Kind regards,

Reece

Reece Lander _ScriptRunner - The Adaptavist Group_
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
August 28, 2020

We have an existing feature request for the above: https://productsupport.adaptavist.com/browse/SRBITB-616

If you could leave a vote/comment on the above ticket, that would help when the team comes to prioritise this :)

Miś Ursowicz August 31, 2020

Hello Reece

Ok, I see... 

Just the last question - what it the best documentation source for adaptavist scripts? Cause https://scriptrunner.adaptavist.com/latest/bitbucket/quickstart.html looks really messy (sorry :( ) 

Reece Lander _ScriptRunner - The Adaptavist Group_
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
August 31, 2020

Hello,

The main documentation for the product is the link you refer to, we are currently working on updating the documentation with additional content and reworking the existing content onto a new platform, if there is anything you can't find an answer for please let me know and I can point you in the correct direction.

In addition to the product documentation, we also have the Adaptavist Library, which is a collection of code examples for various use cases, at the moment we only have a couple of examples for Bitbucket specifically, but we are working on adding more of these in the near future: Adaptavist Library 

You can also reach out to our customer support team who should be able to provide some guidance, they cannot write custom code for you, but they can provide tips and link you to relevant API documentation etc.

Kind regards,

Reece

TAGS
AUG Leaders

Atlassian Community Events