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

Slava Gefen
Contributor
April 4, 2022

Hi Folks, and especially @Robert Giddings _Adaptavist_ !

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.


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

1 answer

1 accepted

0 votes
Answer accepted
Robert Giddings _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 5, 2022

Hi @Slava Gefen ,

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

Slava Gefen
Contributor
April 5, 2022

Thanks a lot @Robert Giddings _Adaptavist_ , I raised it.

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

Robert Giddings _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 5, 2022

Hi @Slava Gefen ,

Sounds good.

Kind regards,

Robert Giddings,

Product Manager, ScriptRunner for Bitbucket

Slava Gefen
Contributor
February 5, 2023

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.ComponentLocator import com.atlassian.bitbucket.repository.RepositoryService import com.atlassian.bitbucket.content.ContentService import com.atlassian.bitbucket.util.PageRequestImpl import com.atlassian.bitbucket.content.AbstractContentTreeCallback import com.atlassian.bitbucket.content.ContentTreeNode def 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) )

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events