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

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

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!

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

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 :( ) 

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
Community showcase
Published in Opsgenie

Investigate deployment-related incidents with Bamboo and Opsgenie together

Back in April of last year one of the major product announcements from Opsgenie was the launch of the Incident investigation view which created a deep connection between Bitbucket and Opsgenie, empow...

151 views 1 4
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