Created
March 12, 2018 10:22
-
-
Save mac2000/d1dcc72623ab678e7ccd795a572394d9 to your computer and use it in GitHub Desktop.
TeamCity mass vcs rename via API
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
from getpass import getpass | |
import json | |
import re | |
import requests # pip3 install requests | |
username = input('Username: ') | |
password = getpass() | |
# username = 'AlexandrM' | |
# password = '********' | |
auth = (username, password) | |
accept = {'Accept': 'application/json'} | |
def get(uri): | |
url = 'http://teamcity.acme.com/httpAuth/app/rest' | |
return requests.get(url + uri, auth=auth, headers=accept).json() | |
def get_property_value_by_name(data, name): | |
for prop in data['properties']['property']: | |
if prop['name'] == name: | |
return prop['value'] | |
return None | |
def slugify(str): | |
return re.sub(r'[^a-z0-9]+', '_', str.lower()) | |
def get_new_id(url): | |
matches = re.search(r'(?P<vendor>bitbucket.org|github.com)[:/](?P<account>[^/]+)/(?P<repo>.+)', url.lower()) | |
vendor = matches.group('vendor').replace('.org', '').replace('.com', '') | |
account = matches.group('account') | |
repo = matches.group('repo').replace('.git', '') | |
id = '_'.join([slugify(repo), slugify(account), slugify(vendor)]) | |
return id | |
repositories = get('/vcs-roots')['vcs-root'] | |
for repository in repositories: | |
#if 'admin3' not in repository['name'].lower() and 'admin2' not in repository['name'].lower(): | |
old_id = repository['id'] | |
data = get('/vcs-roots/id:' + old_id) | |
url = get_property_value_by_name(data, 'url') | |
new_id = get_new_id(url) | |
requests.put('http://teamcity.acme.com/httpAuth/app/rest/vcs-roots/id:' + old_id + '/name', data=new_id, auth=auth, headers={'Content-Type': 'text/plain'}) | |
requests.put('http://teamcity.acme.com/httpAuth/app/rest/vcs-roots/id:' + old_id + '/id', data=new_id, auth=auth, headers={'Content-Type': 'text/plain'}) | |
print(new_id) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment