-
Notifications
You must be signed in to change notification settings - Fork 1
/
TeamcityWebGui.py
111 lines (92 loc) · 3.38 KB
/
TeamcityWebGui.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
import requests
class TeamcityWebGui(object):
def __init__(self, teamcityURL, username, password):
"""
Teamcity WEB GUI object
:param teamcityURL: e.g. https://teamcity.example.com
:param username: user name with admin permission
:param password:
"""
self.teamcityURL = teamcityURL
self.username = username
self.password = password
self.cookie = ''
self.__login()
def __login(self):
"""
Get cookies
"""
driver = webdriver.Firefox()
try:
print("DEBUG - try login to Teamcity")
driver.get("{}/login.html".format(self.teamcityURL))
username = driver.find_element_by_id("username")
username.send_keys(username)
password = driver.find_element_by_id("password")
password.send_keys(password)
password.send_keys(Keys.RETURN)
time.sleep(3)
allCookies = driver.get_cookies()
self.cookie = dict(map(lambda x: (x['name'], x['value']), allCookies))
r = requests.get('{}'.format(self.teamcityURL), cookies=self.cookie)
r.raise_for_status()
print("DEBUG - logged OK")
finally:
driver.quit()
def moveBuildType(self, buildConfigurationId, toProjectId):
"""
Move build configuration to other project
:param buildConfigurationId: Visible ID
:param toProjectId: ID project
"""
internalTCId = self.__configuration_getValue(buildConfigurationId, 'internalId', '')
print('INFO - move {} [{}] to {}'.format(buildConfigurationId, internalTCId, toProjectId))
moveData = {
'-ufd-teamcity-ui-projectId': '',
'projectId': toProjectId,
'moveBuildType': 'Move',
'buildTypeId': internalTCId,
'sourceProjectId': ''
}
r = requests.post('{}/admin/moveBuildType.html'.format(self.teamcityURL),
cookies=self.cookie,
data=moveData,
headers={'Content-Type': 'application/x-www-form-urlencoded'}, )
r.raise_for_status()
print("INFO - moved")
def __configuration_getValue(self, conf_id, type_str, var_name):
"""
Get teamcity parameter
"""
print(
'configuration [{}] GET [{}] = '.format(conf_id, var_name),
end='',
)
time.sleep(3)
r = requests.get(
self.__add_auth_to_url(
self.teamcityURL + '/httpAuth/app/rest/buildTypes/id:{}/{}/{}'.format(conf_id, type_str, var_name),
self.username, self.password),
headers={'Content-Type': 'text/plain'},
verify=False,
)
r.raise_for_status()
value = r.text
print('{!r}'.format(value))
return value
def __add_auth_to_url(self, url_str, login_str, pass_str):
"""
Add username:password to URL
"""
i = url_str.find('//')
if i == -1:
raise Exception("Can't detect http(s):// part of url: {}".format(url_str))
return '{}//{}:{}@{}'.format(
url_str[: i],
login_str,
pass_str,
url_str[i + 2:],
)