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.
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.ContentServiceimport com.atlassian.sal.api.component.ComponentLocatorimport com.atlassian.bitbucket.hook.repository.PullRequestMergeHookRequestdef contentService = ComponentLocator.getComponent(ContentService)def pullRequest = (hookRequest as PullRequestMergeHookRequest).pullRequestdef 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.
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
It looks like you're new here. Sign in or register to get started.