使用API​​在Rally中重新获取子项目并更新所选项目的父项

时间:2017-02-10 11:22:03

标签: python python-3.x rally code-rally

为了清理Rally,我想更改标记为"Closed"的项目的父级

--MyWorkspace
  - Project 1
  - Project 2
    - Child 1 (Status = Closed)
    - Child 2 (Status = Closed)
    - Child 3 (Status = Open)
  - Project 3

我想更新"Child 1" and "Child 2" to "Project 1"的父级(想要将这些子项目移到不同的父级。

import sys
from pyral import Rally, rallyWorkset
options = [arg for arg in sys.argv[1:] if arg.startswith('--')]
args    = [arg for arg in sys.argv[1:] if arg not in options]
server = <server>
apikey = <api_key>
workspace = <workspace>
project = <project_name>
rally = Rally(server,apikey=apikey, workspace=workspace, project=project)
rally.enableLogging('mypyral.log')

我使用以下方法检查所需工作区下的所有项目

projects = rally.getProjects(workspace=workspace)
for proj in projects:
    print ("    %12.12s  %s  %s" % (proj.oid, proj.Name, proj.State))

这只给我父级项目

<id> <Name>  <status>

这个逻辑不会返回所有子项目。

此外,一旦获得要关闭的项目列表,我正在寻找post方法更新父母。

1 个答案:

答案 0 :(得分:2)

这是正确的 - projects = rally.getProjects(workspace=workspace)返回所有Parents项目。

为了获得所有子项目,您需要询问它的父项:

for proj in projects:
children = proj.Children
for child in children:
    print ("    %12.12s  %s  %s" % (child.oid, child.Name, child.State))

我将通过以下方式解决整个任务:

#get object for 'New Parent':
target_project = rally.getProject('NewParentForClosedProjects')

projects = rally.getProjects(workspace=workspace)
for proj in projects:

    #get children of project
    children = proj.Children

    for child in children:

        #if project closed:
        if child.State == 'Closed':
            #Then update Parent to new one:
            project_fields = {
                "ObjectID": child.oid,
                "Parent": target_project.ref
            }
            try:
                result = rally.update('Project', project_fields)
                print "Project %s has been successfully updated with new %s parent" % (str(child.Name), str(child.Parent))
            except RallyRESTException, ex:
                print "Update failure for Project %s" % (str(child.Name))
                print ex