Last active
February 26, 2018 19:47
-
-
Save labkey-tchad/68f88c2d9f66e284f9035972323965a2 to your computer and use it in GitHub Desktop.
Convert TeamCity GitHub VCS roots to use password authentication
This file contains 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
#!/usr/bin/env python3 | |
# | |
from simple_rest_client.api import API | |
from simple_rest_client.resource import Resource | |
import xml.etree.ElementTree as ET | |
class VcsRootResource(Resource): | |
actions = { | |
'listRoots': {'method': 'GET', 'url': '/httpAuth/app/rest/vcs-roots'}, | |
'getUrl': {'method': 'GET', 'url': '/httpAuth/app/rest/vcs-roots/{}/properties/url'}, | |
'setUrl': {'method': 'PUT', 'url': '/httpAuth/app/rest/vcs-roots/{}/properties/url'}, | |
'setAuthMethod': {'method': 'PUT', 'url': '/httpAuth/app/rest/vcs-roots/{}/properties/authMethod'}, | |
'setUsername': {'method': 'PUT', 'url': '/httpAuth/app/rest/vcs-roots/{}/properties/username'}, | |
'setPassword': {'method': 'PUT', 'url': '/httpAuth/app/rest/vcs-roots/{}/properties/secure:password'}, | |
} | |
def updateVcsRoot(rootId): | |
ssh_root_prefix = 'git@github.com:' | |
oldUrl = tc_api.vcsRoots.getUrl(rootId).body | |
if oldUrl.startswith(ssh_root_prefix): | |
print("Updating VCS root for: " + oldUrl + " -- " + rootId) | |
tc_api.vcsRoots.setUrl(rootId, body = oldUrl.replace(ssh_root_prefix, 'https://github.com/')) | |
tc_api.vcsRoots.setAuthMethod(rootId, body = 'PASSWORD') | |
tc_api.vcsRoots.setUsername(rootId, body = '<username>') | |
tc_api.vcsRoots.setPassword(rootId, body = "<password>") | |
else: | |
print("Skipping VCS root for: " + oldUrl + " -- " + rootId) | |
tc_api = API( | |
api_root_url='https://<teamcity>/', # TeamCity server URL | |
headers={'Authorization': 'Basic <auth_token>'}, # 64-bit encoding of "username:password" | |
timeout=10 # It can take a while to download the list of VCS roots | |
) | |
tc_api.add_resource(resource_name='vcsRoots', resource_class=VcsRootResource) | |
vcsRootsXml = tc_api.vcsRoots.listRoots().body | |
vcsRoots = ET.fromstring(vcsRootsXml) | |
for vcsRoot in vcsRoots: | |
updateVcsRoot(vcsRoot.attrib['id']) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment