我有以下代码将xml发布到teamcity以创建新的VCS根目录:
def addVcsRoot(vcsRootId, vcsRootName, projectId, projectName, buildName, repoId, teamAdminUser, teamAdminPass):
headers = {'Content-type': 'application/xml'}
data = ("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>" +
"<vcs-root id=\"" + vcsRootId + "\" " + "name=\"" + vcsRootName + "\" " +
"vcsName=\"jetbrains.git\" href=\"/app/rest/vcs-roots/id:" + vcsRootId + "\">" +
"<project id=\"" + projectId + "\" " "name=\"" + projectName + "\" " "parentProjectId=\"_Root\" " +
"description=\"Single repository for all components\" href=\"/app/rest/projects/id:" + projectId +
"\" " + "webUrl=\"http://teamcity.company.com/project.html?projectId=" + projectId + "\"/>" +
"<properties count=\"11\">" + "<property name=\"agentCleanFilesPolicy\" value=\"ALL_UNTRACKED\"/>" +
"<property name=\"agentCleanPolicy\" value=\"ON_BRANCH_CHANGE\"/>" +
"<property name=\"authMethod\" value=\"PASSWORD\"/>" +
"<property name=\"branch\" value=\"refs/heads/master\"/>" +
"<property name=\"ignoreKnownHosts\" value=\"true\"/>" +
"<property name=\"submoduleCheckout\" value=\"CHECKOUT\"/>" +
"<property name=\"url\" value=\"https://source.company.com/scm/" +repoId + "/" + buildName +
".git\"" + "/>" + "<property name=\"username\" value=\"" + teamAdminUser + "\"/>" +
"<property name=\"usernameStyle\" value=\"USERID\"/>" +
"<property name=\"secure:password\" value=\"" + teamAdminPass + "\"/>" +
"<property name=\"useAlternates\" value=\"true\"/>" + "</properties>" +
"<vcsRootInstances href=\"/app/rest/vcs-root-instances?locator=vcsRoot:(id:" + vcsRootId + ")" +
"\"" + "/>" + "</vcs-root>")
url = path + 'vcs-roots'
return requests.post(url, auth=auth, headers=headers, data=data)
我做了一个看看xml文件应该是什么样子并且做了它以便我可以为不同的构建输入不同的参数,并且脚本工作正常。我的问题是:有更优雅的方式来做到这一点吗?使用连接发布这个长字符串似乎很丑陋且效率低下。使用请求发布xml有哪些其他方法?
答案 0 :(得分:1)
我不打算重写它,除了使用str.format,kwargsa和triple引用一个字符串将使代码更加混乱:
addVcsRoot(vcsRootId=1234, ......)
然后:
def addVcsRoot(vcsRootId, vcsRootName, projectId, projectName, buildName, repoId, teamAdminUser, teamAdminPass)
headers = {'Content-type': 'application/xml'}
data = """<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<vcs-root id="{vcsRootId}" name="{vcsRootName}"
"vcsName="jetbrains.git" href="/app/rest/vcs-roots/id:"{vcsRootId}"\>"""\
.format(vcsRootI=vcsRootId, vcsRootName=vcsRootName.....)
或者如果你想保持命名的args:
$(document).ready(function(){
$("#days option:selected").each(function(){
alert ($(this).val());
})
})