Java Rally Rest Api:如何将新用户添加到项目中

时间:2013-07-14 15:02:39

标签: java api rest rally

我要求将用户添加到Project 用户abcd@web.com具有workSpace1的用户权限 Project1存在于workSpace1中 现在通过Java代码 - 我需要将用户的编辑器权限授予Project1

我有以下代码 - 没有错误 - 但没有更新

//fetch project as JSonObject
JsonObject prjObj = new Example1().fetchPrjDetails("../project/12399939580.js", restApi);   

//get Editors as JSonArray           
JsonArray editorsList = prjObj.get("Editors").getAsJsonArray();

//Create new JSonObject with new user information            
JsonObject newUser = new JsonObject();
newUser.addProperty("_rallyAPIMajor", "1");
newUser.addProperty("_rallyAPIMinor", "37");
newUser.addProperty("_ref", "../user/12418060172.js");
newUser.addProperty("_refObjectName", "venu");
newUser.addProperty("_type", "User");

//add this new JSonObject to Editors Array          
editorsList.add(newUser);

//remove the existing editors  
prjObj.remove("Editors");
//i dont know whether i need the above line 

//add editors to 
prjObj.add("Editors", editorsList);


UpdateRequest uRequest = new UpdateRequest("..project/12399939580.js", prjObj);
UpdateResponse uResponse = restApi.update(uRequest);
    if (uResponse.wasSuccessful()) {
    System.out.println("update success");
    }       

1 个答案:

答案 0 :(得分:1)

项目上的编辑器集合是只读的。在Rally的Web服务API中,拉力用户的用户权限在用户对象的UserPermissions集合下进行管理。通过为用户创建新的ProjectPermission,可以将用户作为编辑器添加到项目中。以下示例说明如何为现有Rally用户创建编辑级ProjectPermission,以及如何更新User的TeamMemberships集合,以便他或她也是该项目中的团队成员。

public class RestExample_AddExistingUserToProject {

    public static void main(String[] args) throws URISyntaxException, IOException {
        // Create and configure a new instance of RallyRestApi
        // Connection parameters
        String rallyURL = "https://rally1.rallydev.com";
        String wsapiVersion = "1.43";
        String applicationName = "RestExample_AddExistingUserToProject";

        // Integration Credentials
        // Must be a Rally Subscription Administrator, or a Workspace Administrator
        // in a Rally Workspace whose Workspace Admins have been granted user administration
        // privileges

        String myUserName = "subadmin@company.com";
        String myUserPassword = "topsecret";

        RallyRestApi restApi = new RallyRestApi(
                new URI(rallyURL),
                myUserName,
                myUserPassword);
        restApi.setWsapiVersion(wsapiVersion);
        restApi.setApplicationName(applicationName);       

        // Workspace and Project Settings
        String myWorkspace = "Middle Earth";
        String myProject = "Mirkwood";

        // Rally UserID and meta-data for whom we wish to add a new Project Permission
        String rallyUserId = "tauriel@midearth.com";

        // Workspace Permission Strings
        final String WORKSPACE_ADMIN = "Admin";
        final String WORKSPACE_USER = "User";
        final String PROJECT_EDITOR = "Editor";
        final String PROJECT_VIEWER = "Viewer";

        //Read User
        QueryRequest userRequest = new QueryRequest("User");
        userRequest.setFetch(new Fetch(
                "UserName", 
                "FirstName",
                "LastName",
                "DisplayName",
                "UserPermissions",
                "Name", 
                "Role", 
                "Workspace", 
                "ObjectID", 
                "Project", 
                "ObjectID", 
                "TeamMemberships")
        );
        userRequest.setQueryFilter(new QueryFilter("UserName", "=", rallyUserId));
        QueryResponse userQueryResponse = restApi.query(userRequest);
        JsonObject userObject = userQueryResponse.getResults().get(0).getAsJsonObject();      
        String userRef = userObject.get("_ref").toString();
        System.out.println("Found User with Ref: " + userRef);

        // Now add an additional Project Permission to the found User   

        // Get reference to Workspace in which Project for Permission Grant Resides
        QueryRequest workspaceRequest = new QueryRequest("Workspace");
        workspaceRequest.setFetch(new Fetch("Name", "Owner", "Projects"));
        workspaceRequest.setQueryFilter(new QueryFilter("Name", "=", myWorkspace));
        QueryResponse workspaceQueryResponse = restApi.query(workspaceRequest);
        String workspaceRef = workspaceQueryResponse.getResults().get(0).getAsJsonObject().get("_ref").toString();

        // Get reference to Project for this Permission Grant
        QueryRequest projectRequest = new QueryRequest("Project");
        projectRequest.setFetch(new Fetch("Name", "Owner", "Projects"));
        projectRequest.setQueryFilter(new QueryFilter("Name", "=", myProject));
        QueryResponse projectQueryResponse = restApi.query(projectRequest);
        JsonObject projectObj = projectQueryResponse.getResults().get(0).getAsJsonObject();
        String projectRef = projectObj.get("_ref").toString();

        // Create the new ProjectPermission for the User
        JsonObject newProjectPermission = new JsonObject();
        newProjectPermission.addProperty("Workspace", workspaceRef);      
        newProjectPermission.addProperty("Project", projectRef);
        newProjectPermission.addProperty("User", userRef);
        newProjectPermission.addProperty("Role", PROJECT_EDITOR);

        CreateRequest createProjectPermissionRequest = new CreateRequest("projectpermission", newProjectPermission);
        System.out.println(createProjectPermissionRequest.getBody());
        CreateResponse createProjectPermissionResponse = restApi.create(createProjectPermissionRequest);

        if (createProjectPermissionResponse.wasSuccessful()) {          
            System.out.println(String.format("Create Project Permission Successful? %s", createProjectPermissionResponse.wasSuccessful()));
            System.out.println(String.format("Added %s Access to Project %s", PROJECT_EDITOR, myProject));
            System.out.println(String.format("For user %s", rallyUserId));                     
        } else {
            String[] createErrors;
            createErrors = createProjectPermissionResponse.getErrors();
            System.out.println("Error occurred creating User Project Permissions: ");
            for (int i=0; i<createErrors.length;i++) {
                System.out.println(createErrors[i]);
            }
        }

        // Also Make this User a Team Member for Project of interest.
        // Get this User's Existing Team Memberships
        JsonArray existTeamMemberships = (JsonArray) userQueryResponse.getResults().get(0).getAsJsonObject().get("TeamMemberships");

        // Add this Project to User's Team Membership collection
        existTeamMemberships.add(projectObj);

        // Setup update fields/values for Team Membership
        JsonObject updateUserTeamMembershipObj = new JsonObject();
        updateUserTeamMembershipObj.add("TeamMemberships", existTeamMemberships);

        // Issue UpdateRequest for Team Membership
        System.out.println("\nUpdating User's Team Membership...");
        UpdateRequest updateTeamMembershipsRequest = new UpdateRequest(userRef, updateUserTeamMembershipObj);
        UpdateResponse updateTeamMembershipResponse = restApi.update(updateTeamMembershipsRequest);

        if (updateTeamMembershipResponse.wasSuccessful()) {
            System.out.println("Updated User to be Team Member in Project: " + myProject);
        } else {
            String[] updateTeamMembershipErrors;
            updateTeamMembershipErrors = updateTeamMembershipResponse.getErrors();
                System.out.println("Error occurred updating Team Membership: ");
            for (int i=0; i<updateTeamMembershipErrors.length;i++) {
                System.out.println(updateTeamMembershipErrors[i]);
            }                   
        }                
        restApi.close();
    }
}