Conditional merge check: How to get content of .yml file and compare it with a specific group
Asked by Slava Gefen:

Hi Folks, and especially [@Robert Giddings \[Adaptavist\]](/t5/user/viewprofilepage/user-id/1196283) !

I'll split the global task in three smaler.  

1. We need to read a content of .yml file that is located in the root. There we need to find a row with "*verConfigRefs:* *- release/13.01.00.x* " and after that get only the "*13.01.00.x*" value.

2. After that we need to check if a current user is a member of any group with pattern "*13.01.00.x*".

3. If Yes, we allow them to Merge.

<br />

I've found a solution or solved particularly for all small tasks and I need your precious help to combine them and get the final result.

With **part 1** I found "*Load content of JSON file from Pull Request* " example. But I need to test it and have it worked. How can I use the similar code inside of **Script Console** to get Content of file and maybe to try to get the required parameter?  

With **part 2** I've checked these two scripts and them works. Could you check everything is ok?  

This script to compare group name and prefix:

```
import java.util.regex.Matcher
import java.util.regex.Pattern
def text = "group_prefix"

Matcher m = text =~ /prefix/
if (m) {
true
}
```

And this one to get list of user groups:

```
import com.atlassian.sal.api.component.ComponentLocator
import com.atlassian.bitbucket.user.UserService
import com.atlassian.bitbucket.auth.AuthenticationContext

import com.atlassian.crowd.manager.directory.DirectoryManager
import com.atlassian.crowd.search.query.entity.GroupQuery
import com.atlassian.crowd.model.group.GroupType
import com.atlassian.crowd.search.query.entity.restriction.TermRestriction
import com.atlassian.crowd.search.query.entity.restriction.PropertyImpl
import com.atlassian.crowd.search.query.entity.restriction.MatchMode
import com.atlassian.crowd.search.query.membership.MembershipQuery
import com.atlassian.crowd.search.builder.QueryBuilder
import com.atlassian.crowd.search.query.entity.UserQuery
import com.atlassian.crowd.search.EntityDescriptor
import com.atlassian.crowd.model.user.Users
import com.atlassian.crowd.model.group.Membership
import com.atlassian.bitbucket.user.UserSearchRequest
import com.atlassian.bitbucket.util.PageRequest
import com.atlassian.bitbucket.util.PageRequestImpl
import static com.onresolve.scriptrunner.canned.bitbucket.util.BitbucketCannedScriptUtils.unlimitedPager

def dm = ComponentLocator.getComponent(DirectoryManager)
def dir = dm.findAllDirectories().find {it.name == "Bitbucket Internal Directory"}?.id
def us = ComponentLocator.getComponent(UserService)

us.findGroupsByUser("slava", getUnlimitedPager()).values
```

One question here: Why *findGroupsWithUser()* function is not working for us? It allows to filter groups of user by specified name. And maybe with this function I could check if a user already a member of a group without matching strings. But it is not a blocker in final solution.

And with **part 3** I already set the conditional merge check if user is a member of specific group then allow them to click on the Merge button.

To summarize: Could you help with a final script and to combine all the things above?  

Thank you in advance!  
With kind regards  
Slava


Answers:
author: Robert Giddings [Adaptavist]
body: Hi [@Slava Gefen](/t5/user/viewprofilepage/user-id/4704947) ,

Thank you for your post.

There are quite a few things in there, so I'm going to suggest that you raise a Support Request with us at Adaptavist, in the first instance.

You can do this via our Support Portal for ScriptRunner for Bitbucket here: <https://productsupport.adaptavist.com/servicedesk/customer/portal/3>

Our Support team might be able to help you answer some of your questions. And if not, they will be able to advise you on what to do next.

Hope that helps?

Kind regards,

Robert Giddings,

Product Manager, ScriptRunner for Bitbucket

comments: author: Slava Gefen
body: Thanks a lot [@Robert Giddings \[Adaptavist\]](/t5/user/viewprofilepage/user-id/1196283) , I raised it.  

So, I'll make this question as answered after the support team help me, ok?


author: Robert Giddings [Adaptavist]
body: Hi [@Slava Gefen](/t5/user/viewprofilepage/user-id/4704947) ,

Sounds good.

Kind regards,

Robert Giddings,

Product Manager, ScriptRunner for Bitbucket


author: Slava Gefen
body: For a history and maybe it'll help someone too, the most important part of code from the support team:  

```
You have to implement the ContentTreeCallback yourself.
As an example, you can run the following snippet 
in ScriptRunner > Console to list the directory of a repo 
in master branch:


import com.atlassian.sal.api.component.ComponentLocatorimport com.atlassian.bitbucket.repository.RepositoryServiceimport com.atlassian.bitbucket.content.ContentServiceimport com.atlassian.bitbucket.util.PageRequestImplimport com.atlassian.bitbucket.content.AbstractContentTreeCallbackimport com.atlassian.bitbucket.content.ContentTreeNodedef repo = ComponentLocator.getComponent(RepositoryService).getById(1)ComponentLocator.getComponent(ContentService).streamDirectory(    repo, "master", "", true, new AbstractContentTreeCallback() {        @Override        boolean onTreeNode(ContentTreeNode node) {            log.warn node.getPath()            return true        }    }, new PageRequestImpl(0, PageRequestImpl.MAX_PAGE_LIMIT))
```


Tags: bitbucket-server

merge

scriptrunner

server