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

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,556,712
Community Members
 
Community Events
184
Community Groups

Bitbucket - get commited file content

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

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.
Aug 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

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.
Aug 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.
Aug 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 :)

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.
Aug 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

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events