'How to bulk grant access to many bitbucket repos?

I have 100+ private git repos in Bitbucket and want to allow access to read them for new private user. It is terrible to set this access to each separate repo. Is it possible to select several repos and allow access to them by one operation? May be it is possible to do this by loop and curl in bash using REST api of the Bitbucket?

Thanks for the answer in advance!



Solution 1:[1]

This code uses stashy - a python client for Bitbucket Server.
It may needs small modifications per the project's structure of your server.

#!/usr/bin/python
import stashy
import requests
from requests.auth import HTTPBasicAuth

# User to be granted access to
user = ""

bitbucket_url = "https://SERVER_URL"
bitbucket_username= "<bitbucket_username>"
bitbucket_passwd = "<pass>"
header = {'content-type': 'application/json'}

"""
        Promote or demote a user's permission level.
        Depending on context, you may use one of the following set of permissions:
        project permissions:
            * PROJECT_READ
            * PROJECT_WRITE
            * PROJECT_ADMIN
"""
permission = "PROJECT_READ"
stash = stashy.connect(bitbucket_url, bitbucket_username, bitbucket_passwd)
for project in stash.projects.list():
    print("granting "+bitbucket_username+" access "+permission+" access to "+project["name"])
    print(stash.projects[project["key"]].permissions.users.grant(user,permission))

https://gist.github.com/ibidani/9ae06c690fb32ee09aa6bb5480c18325

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 TheVillageIdiot