Created
June 23, 2015 16:38
-
-
Save rtluckie/6c6365e274fddf99b673 to your computer and use it in GitHub Desktop.
bulk set stash branch permssions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
import requests | |
import logging as log | |
perms = [ | |
{ | |
"matcher": { | |
"id": "refs/heads/**", | |
"displayId": "refs/heads/**", | |
"type": { | |
"id": "PATTERN" | |
} | |
}, | |
"type": "read-only", | |
"users": [ | |
], | |
"groups": [ | |
"git-lead" | |
] | |
}, | |
{ | |
"matcher": { | |
"id": "refs/heads/develop", | |
"displayId": "refs/heads/develop", | |
"type": { | |
"id": "PATTERN" | |
} | |
}, | |
"type": "read-only", | |
"users": [ | |
], | |
"groups": [ | |
"developers", | |
"git-lead" | |
] | |
}, | |
{ | |
"matcher": { | |
"id": "refs/heads/develop", | |
"displayId": "refs/heads/develop", | |
"type": { | |
"id": "PATTERN" | |
} | |
}, | |
"type": "fast-forward-only", | |
"users": [ | |
], | |
"groups": [ | |
] | |
}, | |
{ | |
"matcher": { | |
"id": "refs/heads/develop", | |
"displayId": "refs/heads/develop", | |
"type": { | |
"id": "PATTERN" | |
} | |
}, | |
"type": "pull-request-only", | |
"users": [ | |
], | |
"groups": [ | |
] | |
}, | |
{ | |
"matcher": { | |
"id": "refs/heads/develop", | |
"displayId": "refs/heads/develop", | |
"type": { | |
"id": "PATTERN" | |
} | |
}, | |
"type": "no-deletes", | |
"users": [ | |
], | |
"groups": [ | |
] | |
}, | |
{ | |
"matcher": { | |
"id": "refs/heads/master", | |
"displayId": "refs/heads/master", | |
"type": { | |
"id": "PATTERN" | |
} | |
}, | |
"type": "read-only", | |
"users": [ | |
], | |
"groups": [ | |
"git-lead" | |
] | |
}, | |
{ | |
"matcher": { | |
"id": "refs/heads/master", | |
"displayId": "refs/heads/master", | |
"type": { | |
"id": "PATTERN" | |
} | |
}, | |
"type": "fast-forward-only", | |
"users": [ | |
], | |
"groups": [ | |
] | |
}, | |
{ | |
"matcher": { | |
"id": "refs/heads/master", | |
"displayId": "refs/heads/master", | |
"type": { | |
"id": "PATTERN" | |
} | |
}, | |
"type": "no-deletes", | |
"users": [ | |
], | |
"groups": [ | |
] | |
}, | |
{ | |
"matcher": { | |
"id": "refs/tags/**", | |
"displayId": "refs/tags/**", | |
"type": { | |
"id": "PATTERN" | |
} | |
}, | |
"type": "read-only", | |
"users": [ | |
], | |
"groups": [ | |
"git-lead" | |
] | |
}, | |
{ | |
"matcher": { | |
"id": "refs/heads/feature/*", | |
"displayId": "refs/heads/feature/*", | |
"type": { | |
"id": "PATTERN" | |
} | |
}, | |
"type": "read-only", | |
"users": [ | |
], | |
"groups": [ | |
"developers", | |
"git-lead" | |
] | |
}, | |
{ | |
"matcher": { | |
"id": "refs/heads/bugfix/*", | |
"displayId": "refs/heads/bugfix/*", | |
"type": { | |
"id": "PATTERN" | |
} | |
}, | |
"type": "read-only", | |
"users": [ | |
], | |
"groups": [ | |
"developers", | |
"git-lead" | |
] | |
}, | |
{ | |
"matcher": { | |
"id": "refs/heads/hotfix/*", | |
"displayId": "refs/heads/hotfix/*", | |
"type": { | |
"id": "PATTERN" | |
} | |
}, | |
"type": "read-only", | |
"users": [ | |
], | |
"groups": [ | |
"git-lead" | |
] | |
} | |
] | |
base_url = 'https://stash.company.com' | |
auth = ('me', 'secrets') | |
def add_json_headers(kw=None): | |
if not kw: | |
kw = {} | |
if 'headers' not in kw: | |
kw['headers'] = {} | |
headers = {'Content-type': 'application/json', 'Accept': 'application/json'} | |
for header, value in headers.items(): | |
kw['headers'][header] = value | |
return kw | |
def get_repos(project): | |
repos = [] | |
url = "{}/rest/api/1.0/projects/{}/repos".format(base_url, project) | |
r = requests.get(url, verify=False, auth=auth) | |
if str(r.status_code)[0] != '2': | |
log.error('error: %s', url) | |
for i in r.json()['values']: | |
repos.append(i['slug']) | |
return repos | |
def reset_branch_perms(project, repo): | |
url = "{}/rest/branch-permissions/2.0/projects/{}/repos/{}/restrictions".format(base_url, project, repo) | |
r = requests.get(url, verify=False, auth=auth) | |
if str(r.status_code)[0] != '2': | |
log.error('error: %s', url) | |
for i in r.json()['values']: | |
r = requests.delete("{}/{}".format(url, i['id']), verify=False, auth=auth) | |
if str(r.status_code)[0] != '2': | |
log.error('error: %s', url) | |
def set_branch_perms(project, repo): | |
kw = add_json_headers() | |
url = "{}/rest/branch-permissions/2.0/projects/{}/repos/{}/restrictions".format(base_url, project, repo) | |
for p in perms: | |
r = requests.post(url, verify=False, auth=auth, data=json.dumps(p), **kw) | |
if str(r.status_code)[0] != '2': | |
log.error('error: %s', url) | |
def batch_set_branch_perms(project): | |
repos = get_repos(project) | |
for r in repos: | |
reset_branch_perms(project, r) | |
set_branch_perms(project, r) | |
for project in ['projectA', 'projectB']: | |
batch_set_branch_perms(project) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment